4d328f8c3b4d51ddff4250953dca0f9f98a898eb
[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.HashMap;\r
34 import java.util.Iterator;\r
35 import java.util.List;\r
36 import java.util.Map;\r
37 import java.util.Vector;\r
38 \r
39 import mir.config.MirPropertiesConfiguration;\r
40 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
41 import mir.entity.adapter.EntityAdapter;\r
42 import mir.log.LoggerWrapper;\r
43 import mir.misc.ConfigException;\r
44 import mircoders.accesscontrol.AccessControl;\r
45 import mircoders.entity.EntityComment;\r
46 import mircoders.entity.EntityContent;\r
47 import mircoders.entity.EntityUsers;\r
48 import mircoders.localizer.MirAdminInterfaceLocalizer;\r
49 import mircoders.localizer.MirCachingLocalizerDecorator;\r
50 import mircoders.localizer.MirLocalizer;\r
51 \r
52 public class MirGlobal {\r
53   static private MirLocalizer localizer;\r
54   static private ProducerEngine producerEngine;\r
55   static private Abuse abuse;\r
56   static private MRUCache mruCache;\r
57   static private AccessControl accessControl;\r
58   static private Map articleOperations;\r
59   static private Map commentOperations;\r
60   static private Map loggedInUsers = new HashMap();\r
61   static private Map loggedInUserIds = new HashMap();\r
62   static private LoggerWrapper logger = new LoggerWrapper("Global");\r
63   static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");\r
64 \r
65   public synchronized static MirLocalizer localizer() {\r
66     String localizerClassName;\r
67     Class localizerClass;\r
68 \r
69     if (localizer == null ) {\r
70       localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");\r
71 \r
72       try {\r
73         localizerClass = Class.forName(localizerClassName);\r
74       }\r
75       catch (Throwable t) {\r
76         throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());\r
77       }\r
78 \r
79       if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))\r
80         throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");\r
81 \r
82       try {\r
83         localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());\r
84       }\r
85       catch (Throwable t) {\r
86         throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());\r
87       }\r
88     }\r
89 \r
90     return localizer;\r
91   }\r
92 \r
93   /**\r
94    * Returns a string that provides some global status information\r
95    */\r
96   public static String getStatus() {\r
97     StringBuffer result = new StringBuffer();\r
98 \r
99     result.append((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/(1024*1024));\r
100     result.append("M in use, ");\r
101     result.append(Thread.currentThread().activeCount()).append(" active threads");\r
102 \r
103     return result.toString();\r
104   }\r
105 \r
106   public synchronized static Abuse abuse() {\r
107     if (abuse==null)\r
108       abuse = new Abuse();\r
109 \r
110     return abuse;\r
111   }\r
112 \r
113   public static MirPropertiesConfiguration config() {\r
114     try {\r
115       return MirPropertiesConfiguration.instance();\r
116     }\r
117     catch (PropertiesConfigExc e) {\r
118       throw new RuntimeException(e.getMessage());\r
119     }\r
120   }\r
121 \r
122   public static ProducerEngine producerEngine() {\r
123     if (producerEngine == null) {\r
124       producerEngine = new ProducerEngine();\r
125     }\r
126 \r
127     return producerEngine;\r
128   }\r
129 \r
130   public static MRUCache mruCache() {\r
131     synchronized(MirGlobal.class) {\r
132       if (mruCache == null) {\r
133         mruCache = new MRUCache();\r
134       }\r
135       return mruCache;\r
136     }\r
137   }\r
138 \r
139   public static synchronized AccessControl accessControl() {\r
140     if (accessControl == null) {\r
141       accessControl=new AccessControl();\r
142     }\r
143 \r
144     return accessControl;\r
145   }\r
146 \r
147   public static void performArticleOperation(EntityUsers aUser, EntityContent  anArticle, String anOperation) {\r
148     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);\r
149 \r
150     try {\r
151       EntityAdapter user = null;\r
152       if (aUser!=null)\r
153           user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);\r
154 \r
155       if (operation!=null)\r
156         operation.perform(\r
157             user,\r
158             localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));\r
159     }\r
160     catch (Throwable t) {\r
161       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
162 \r
163       throw new RuntimeException(t.toString());\r
164     }\r
165   }\r
166 \r
167   public static void performCommentOperation(EntityUsers aUser, EntityComment  aComment, String anOperation) {\r
168     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);\r
169 \r
170     try {\r
171       EntityAdapter user = null;\r
172       if (aUser!=null)\r
173           user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);\r
174 \r
175       if (operation!=null)\r
176         operation.perform(\r
177             user,\r
178             localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));\r
179     }\r
180     catch (Throwable t) {\r
181       throw new RuntimeException(t.toString());\r
182     }\r
183   }\r
184 \r
185   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {\r
186     try {\r
187       if (articleOperations == null) {\r
188         articleOperations = new HashMap();\r
189         Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();\r
190         while (i.hasNext()) {\r
191           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
192           articleOperations.put(operation.getName(), operation);\r
193         }\r
194       }\r
195 \r
196       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);\r
197     }\r
198     catch (Throwable t) {\r
199       throw new RuntimeException(t.toString());\r
200     }\r
201   }\r
202 \r
203   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {\r
204     try {\r
205       if (commentOperations == null) {\r
206         commentOperations = new HashMap();\r
207         Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();\r
208         while (i.hasNext()) {\r
209           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
210           commentOperations.put(operation.getName(), operation);\r
211         }\r
212       }\r
213 \r
214       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);\r
215     }\r
216     catch (Throwable t) {\r
217       throw new RuntimeException(t.toString());\r
218     }\r
219   }\r
220 \r
221   public static boolean isUserLoggedIn(String anId) {\r
222     synchronized (loggedInUserIds) {\r
223       return loggedInUserIds.containsKey(anId);\r
224     }\r
225   }\r
226 \r
227   public static List getLoggedInUsers() {\r
228     List result = new Vector();\r
229 \r
230     synchronized (loggedInUsers) {\r
231       Iterator i = loggedInUsers.entrySet().iterator();\r
232 \r
233       while (i.hasNext()) {\r
234         Map.Entry entry = (Map.Entry) i.next();\r
235 \r
236         Map item = new HashMap();\r
237         item.put("name", entry.getKey());\r
238         item.put("count", entry.getValue());\r
239         result.add(item);\r
240       }\r
241     }\r
242 \r
243     return result;\r
244   }\r
245 \r
246   public static void registerLogin(String aName, String anId) {\r
247     modifyLoggedInCount(aName, anId, 1);\r
248   }\r
249 \r
250   public static void registerLogout(String aName, String anId) {\r
251     modifyLoggedInCount(aName, anId, -1);\r
252   }\r
253 \r
254   private static void modifyLoggedInCount(String aName, String anId, int aModifier) {\r
255     synchronized (loggedInUsers) {\r
256       Integer count = (Integer) loggedInUsers.get(aName);\r
257       if (count==null)\r
258         count = new Integer(0);\r
259 \r
260       if (count.intValue()+aModifier<=0) {\r
261         loggedInUsers.remove(aName);\r
262       }\r
263       else {\r
264         loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));\r
265       }\r
266     }\r
267 \r
268     synchronized (loggedInUserIds) {\r
269       Integer count = (Integer) loggedInUserIds.get(anId);\r
270       if (count==null)\r
271         count = new Integer(0);\r
272 \r
273       if (count.intValue()+aModifier<=0) {\r
274         loggedInUserIds.remove(anId);\r
275       }\r
276       else {\r
277         loggedInUserIds.put(anId, new Integer(count.intValue() + aModifier));\r
278       }\r
279     }\r
280   }\r
281 \r
282   public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {\r
283     try {\r
284       if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {\r
285         String user = "unknown (" + aUser.toString() + ")";\r
286         if (aUser != null)\r
287           user = aUser.getFieldValue("login");\r
288         adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);\r
289       }\r
290     }\r
291     catch (Throwable t) {\r
292       logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());\r
293     }\r
294   }\r
295 }\r
296 \r
297 \r