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