some cosmetic change, organzied imports, and made some accesses in a static way
[mir.git] / source / mircoders / global / MirGlobal.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.global;
32
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Vector;
38
39 import mir.config.MirPropertiesConfiguration;
40 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
41 import mir.entity.adapter.EntityAdapter;
42 import mir.log.LoggerWrapper;
43 import mir.misc.ConfigException;
44 import mircoders.accesscontrol.AccessControl;
45 import mircoders.entity.EntityComment;
46 import mircoders.entity.EntityContent;
47 import mircoders.entity.EntityUsers;
48 import mircoders.localizer.MirAdminInterfaceLocalizer;
49 import mircoders.localizer.MirCachingLocalizerDecorator;
50 import mircoders.localizer.MirLocalizer;
51
52 public class MirGlobal {
53   static private MirPropertiesConfiguration configuration;
54   static private MirLocalizer localizer;
55   static private ProducerEngine producerEngine;
56   static private Abuse abuse;
57   static private MRUCache mruCache;
58   static private AccessControl accessControl;
59   static private Map articleOperations;
60   static private Map commentOperations;
61   static private Map loggedInUsers = new HashMap();
62   static private LoggerWrapper logger = new LoggerWrapper("Global");
63   static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");
64
65   public synchronized static MirLocalizer localizer() {
66     String localizerClassName;
67     Class localizerClass;
68
69     if (localizer == null ) {
70       localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
71
72       try {
73         localizerClass = Class.forName(localizerClassName);
74       }
75       catch (Throwable t) {
76         throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
77       }
78
79       if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
80         throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
81
82       try {
83         localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
84       }
85       catch (Throwable t) {
86         throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
87       }
88     }
89
90     return localizer;
91   }
92
93   public static Abuse abuse() {
94     if (abuse==null) {
95       synchronized(MirGlobal.class) {
96         if (abuse==null)
97           abuse = new Abuse();
98       }
99     }
100
101     return abuse;
102   }
103
104   public static MirPropertiesConfiguration config() {
105     try {
106       return MirPropertiesConfiguration.instance();
107     }
108     catch (PropertiesConfigExc e) {
109       throw new RuntimeException(e.getMessage());
110     }
111   }
112
113   public static ProducerEngine producerEngine() {
114     if (producerEngine == null) {
115       producerEngine = new ProducerEngine();
116     }
117
118     return producerEngine;
119   }
120
121   public static MRUCache mruCache() {
122     synchronized(MirGlobal.class) {
123       if (mruCache == null) {
124         mruCache = new MRUCache();
125       }
126       return mruCache;
127     }
128   }
129
130   public static synchronized AccessControl accessControl() {
131     if (accessControl == null) {
132       accessControl=new AccessControl();
133     }
134
135     return accessControl;
136   }
137
138   public static void performArticleOperation(EntityUsers aUser, EntityContent  anArticle, String anOperation) {
139     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);
140
141     try {
142       EntityAdapter user = null;
143       if (aUser!=null)
144           user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
145
146       if (operation!=null)
147         operation.perform(
148             user,
149             localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
150     }
151     catch (Throwable t) {
152       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
153
154       throw new RuntimeException(t.toString());
155     }
156   }
157
158   public static void performCommentOperation(EntityUsers aUser, EntityComment  aComment, String anOperation) {
159     MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);
160
161     try {
162       EntityAdapter user = null;
163       if (aUser!=null)
164           user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
165
166       if (operation!=null)
167         operation.perform(
168             user,
169             localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
170     }
171     catch (Throwable t) {
172       throw new RuntimeException(t.toString());
173     }
174   }
175
176   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {
177     try {
178       if (articleOperations == null) {
179         articleOperations = new HashMap();
180         Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
181         while (i.hasNext()) {
182           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
183           articleOperations.put(operation.getName(), operation);
184         }
185       }
186
187       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
188     }
189     catch (Throwable t) {
190       throw new RuntimeException(t.toString());
191     }
192   }
193
194   private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {
195     try {
196       if (commentOperations == null) {
197         commentOperations = new HashMap();
198         Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
199         while (i.hasNext()) {
200           MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
201           commentOperations.put(operation.getName(), operation);
202         }
203       }
204
205       return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
206     }
207     catch (Throwable t) {
208       throw new RuntimeException(t.toString());
209     }
210   }
211
212
213   public static List getLoggedInUsers() {
214     List result = new Vector();
215
216     synchronized (loggedInUsers) {
217       Iterator i = loggedInUsers.entrySet().iterator();
218
219       while (i.hasNext()) {
220         Map.Entry entry = (Map.Entry) i.next();
221
222         Map item = new HashMap();
223         item.put("name", entry.getKey());
224         item.put("count", entry.getValue());
225         result.add(item);
226       }
227     }
228
229     return result;
230   }
231
232   public static void registerLogin(String aName) {
233     modifyLoggedInCount(aName, 1);
234   }
235
236   public static void registerLogout(String aName) {
237     modifyLoggedInCount(aName, -1);
238   }
239
240   private static void modifyLoggedInCount(String aName, int aModifier) {
241     synchronized (loggedInUsers) {
242       Integer count = (Integer) loggedInUsers.get(aName);
243       if (count==null)
244         count = new Integer(0);
245
246       if (count.intValue()+aModifier<=0) {
247         loggedInUsers.remove(aName);
248       }
249       else {
250         loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
251       }
252     }
253   }
254
255   public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {
256     try {
257       if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {
258         String user = "unknown (" + aUser.toString() + ")";
259         if (aUser != null)
260           user = aUser.getValue("login");
261         adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
262       }
263     }
264     catch (Throwable t) {
265       logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());
266     }
267   }
268 }
269
270