optionally show a list of logged in admin users
[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   static private Map loggedInUsers = new HashMap();\r
54 \r
55   public synchronized static MirLocalizer localizer() {\r
56     String localizerClassName;\r
57     Class localizerClass;\r
58 \r
59     if (localizer == null ) {\r
60       localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");\r
61 \r
62       try {\r
63         localizerClass = Class.forName(localizerClassName);\r
64       }\r
65       catch (Throwable t) {\r
66         throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());\r
67       }\r
68 \r
69       if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))\r
70         throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");\r
71 \r
72       try {\r
73         localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());\r
74       }\r
75       catch (Throwable t) {\r
76         throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());\r
77       }\r
78     }\r
79 \r
80     return localizer;\r
81   }\r
82 \r
83   public static Abuse abuse() {\r
84     if (abuse==null) {\r
85       synchronized(MirGlobal.class) {\r
86         if (abuse==null)\r
87           abuse = new Abuse();\r
88       }\r
89     }\r
90 \r
91     return abuse;\r
92   }\r
93 \r
94   public static MirPropertiesConfiguration config() {\r
95     try {\r
96       return MirPropertiesConfiguration.instance();\r
97     }\r
98     catch (PropertiesConfigExc e) {\r
99       throw new RuntimeException(e.getMessage());\r
100     }\r
101   }\r
102 \r
103   public static ProducerEngine producerEngine() {\r
104     if (producerEngine == null) {\r
105       producerEngine = new ProducerEngine();\r
106     }\r
107 \r
108     return producerEngine;\r
109   }\r
110 \r
111   public static MRUCache mruCache() {\r
112     synchronized(MirGlobal.class) {\r
113       if (mruCache == null) {\r
114         mruCache = new MRUCache();\r
115       }\r
116       return mruCache;\r
117     }\r
118   }\r
119 \r
120   public static synchronized AccessControl accessControl() {\r
121     if (accessControl == null) {\r
122       accessControl=new AccessControl();\r
123     }\r
124 \r
125     return accessControl;\r
126   }\r
127 \r
128   public static void performArticleOperation(EntityUsers aUser, EntityContent  anArticle, String anOperation) {\r
129     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);\r
130 \r
131     try {\r
132       if (operation!=null)\r
133         operation.perform(\r
134             localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),\r
135             localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));\r
136     }\r
137     catch (Throwable t) {\r
138       throw new RuntimeException(t.toString());\r
139     }\r
140   }\r
141 \r
142   public static void performCommentOperation(EntityUsers aUser, EntityComment  aComment, String anOperation) {\r
143     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);\r
144 \r
145     try {\r
146       if (operation!=null)\r
147         operation.perform(\r
148             localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),\r
149             localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));\r
150     }\r
151     catch (Throwable t) {\r
152       throw new RuntimeException(t.toString());\r
153     }\r
154   }\r
155 \r
156   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {\r
157     try {\r
158       if (articleOperations == null) {\r
159         articleOperations = new HashMap();\r
160         Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();\r
161         while (i.hasNext()) {\r
162           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
163           articleOperations.put(operation.getName(), operation);\r
164         }\r
165       }\r
166 \r
167       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);\r
168     }\r
169     catch (Throwable t) {\r
170       throw new RuntimeException(t.toString());\r
171     }\r
172   }\r
173 \r
174   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {\r
175     try {\r
176       if (commentOperations == null) {\r
177         commentOperations = new HashMap();\r
178         Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();\r
179         while (i.hasNext()) {\r
180           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
181           commentOperations.put(operation.getName(), operation);\r
182         }\r
183       }\r
184 \r
185       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);\r
186     }\r
187     catch (Throwable t) {\r
188       throw new RuntimeException(t.toString());\r
189     }\r
190   }\r
191 \r
192 \r
193   public static List getLoggedInUsers() {\r
194     List result = new Vector();\r
195 \r
196     synchronized (loggedInUsers) {\r
197       Iterator i = loggedInUsers.entrySet().iterator();\r
198 \r
199       while (i.hasNext()) {\r
200         Map.Entry entry = (Map.Entry) i.next();\r
201 \r
202         Map item = new HashMap();\r
203         item.put("name", entry.getKey());\r
204         item.put("count", entry.getValue());\r
205         result.add(item);\r
206       }\r
207     }\r
208 \r
209     return result;\r
210   }\r
211 \r
212   public static void registerLogin(String aName) {\r
213     modifyLoggedInCount(aName, 1);\r
214   }\r
215 \r
216   public static void registerLogout(String aName) {\r
217     modifyLoggedInCount(aName, -1);\r
218   }\r
219 \r
220   private static void modifyLoggedInCount(String aName, int aModifier) {\r
221     synchronized (loggedInUsers) {\r
222       Integer count = (Integer) loggedInUsers.get(aName);\r
223       if (count==null)\r
224         count = new Integer(0);\r
225 \r
226       if (count.intValue()+aModifier<=0) {\r
227         loggedInUsers.remove(aName);\r
228       }\r
229       else {\r
230         loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));\r
231       }\r
232     }\r
233   }\r
234 }\r
235 \r
236 \r