anti-abuse filter upgrade
[mir.git] / source / mircoders / global / MirGlobal.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 \r
31 package mircoders.global;\r
32 \r
33 import java.util.*;\r
34 \r
35 import mir.config.MirPropertiesConfiguration;\r
36 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
37 import mir.misc.ConfigException;\r
38 import mircoders.localizer.MirCachingLocalizerDecorator;\r
39 import mircoders.localizer.*;\r
40 import mircoders.accesscontrol.*;\r
41 import mircoders.entity.*;\r
42 import mir.entity.adapter.*;\r
43 \r
44 public class MirGlobal {\r
45   static private MirPropertiesConfiguration configuration;\r
46   static private MirLocalizer localizer;\r
47   static private ProducerEngine producerEngine;\r
48   static private Abuse abuse;\r
49   static private MRUCache mruCache;\r
50   static private AccessControl accessControl;\r
51   static private Map articleOperations;\r
52   static private Map commentOperations;\r
53 \r
54   public synchronized static MirLocalizer localizer() {\r
55     String localizerClassName;\r
56     Class localizerClass;\r
57 \r
58     if (localizer == null ) {\r
59       localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");\r
60 \r
61       try {\r
62         localizerClass = Class.forName(localizerClassName);\r
63       }\r
64       catch (Throwable t) {\r
65         throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());\r
66       }\r
67 \r
68       if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))\r
69         throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");\r
70 \r
71       try {\r
72         localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());\r
73       }\r
74       catch (Throwable t) {\r
75         throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());\r
76       }\r
77     }\r
78 \r
79     return localizer;\r
80   }\r
81 \r
82   public static Abuse abuse() {\r
83     if (abuse==null) {\r
84       synchronized(MirGlobal.class) {\r
85         if (abuse==null)\r
86           abuse = new Abuse();\r
87       }\r
88     }\r
89 \r
90     return abuse;\r
91   }\r
92 \r
93   public static MirPropertiesConfiguration config() {\r
94     try {\r
95       return MirPropertiesConfiguration.instance();\r
96     }\r
97     catch (PropertiesConfigExc e) {\r
98       throw new RuntimeException(e.getMessage());\r
99     }\r
100   }\r
101 \r
102   public static ProducerEngine producerEngine() {\r
103     if (producerEngine == null) {\r
104       producerEngine = new ProducerEngine();\r
105     }\r
106 \r
107     return producerEngine;\r
108   }\r
109 \r
110   public static MRUCache mruCache() {\r
111     synchronized(MirGlobal.class) {\r
112       if (mruCache == null) {\r
113         mruCache = new MRUCache();\r
114       }\r
115       return mruCache;\r
116     }\r
117   }\r
118 \r
119   public static synchronized AccessControl accessControl() {\r
120     if (accessControl == null) {\r
121       accessControl=new AccessControl();\r
122     }\r
123 \r
124     return accessControl;\r
125   }\r
126 \r
127   public static void performArticleOperation(EntityUsers aUser, EntityContent  anArticle, String anOperation) {\r
128     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);\r
129 \r
130     try {\r
131       if (operation!=null)\r
132         operation.perform(\r
133             localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),\r
134             localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));\r
135     }\r
136     catch (Throwable t) {\r
137       throw new RuntimeException(t.toString());\r
138     }\r
139   }\r
140 \r
141   public static void performCommentOperation(EntityUsers aUser, EntityComment  aComment, String anOperation) {\r
142     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);\r
143 \r
144     try {\r
145       if (operation!=null)\r
146         operation.perform(\r
147             localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),\r
148             localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));\r
149     }\r
150     catch (Throwable t) {\r
151       throw new RuntimeException(t.toString());\r
152     }\r
153   }\r
154 \r
155   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {\r
156     try {\r
157       if (articleOperations == null) {\r
158         articleOperations = new HashMap();\r
159         Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();\r
160         while (i.hasNext()) {\r
161           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
162           articleOperations.put(operation.getName(), operation);\r
163         }\r
164       }\r
165 \r
166       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);\r
167     }\r
168     catch (Throwable t) {\r
169       throw new RuntimeException(t.toString());\r
170     }\r
171   }\r
172 \r
173   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {\r
174     try {\r
175       if (commentOperations == null) {\r
176         commentOperations = new HashMap();\r
177         Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();\r
178         while (i.hasNext()) {\r
179           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
180           commentOperations.put(operation.getName(), operation);\r
181         }\r
182       }\r
183 \r
184       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);\r
185     }\r
186     catch (Throwable t) {\r
187       throw new RuntimeException(t.toString());\r
188     }\r
189   }\r
190 \r
191 }\r
192 \r
193 \r