2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
31 package mircoders.global;
\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
39 import mir.config.MirPropertiesConfiguration;
\r
40 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
41 import mir.log.LoggerWrapper;
\r
42 import mir.misc.ConfigException;
\r
43 import mircoders.accesscontrol.AccessControl;
\r
44 import mircoders.entity.EntityComment;
\r
45 import mircoders.entity.EntityContent;
\r
46 import mircoders.entity.EntityUsers;
\r
47 import mircoders.localizer.MirAdminInterfaceLocalizer;
\r
48 import mircoders.localizer.MirCachingLocalizerDecorator;
\r
49 import mircoders.localizer.MirLocalizer;
\r
51 public class MirGlobal {
\r
52 static private MirPropertiesConfiguration configuration;
\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 LoggerWrapper logger = new LoggerWrapper("Global");
\r
62 static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");
\r
64 public synchronized static MirLocalizer localizer() {
\r
65 String localizerClassName;
\r
66 Class localizerClass;
\r
68 if (localizer == null ) {
\r
69 localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
\r
72 localizerClass = Class.forName(localizerClassName);
\r
74 catch (Throwable t) {
\r
75 throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
\r
78 if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
\r
79 throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
\r
82 localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
\r
84 catch (Throwable t) {
\r
85 throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
\r
92 public static Abuse abuse() {
\r
94 synchronized(MirGlobal.class) {
\r
96 abuse = new Abuse();
\r
103 public static MirPropertiesConfiguration config() {
\r
105 return MirPropertiesConfiguration.instance();
\r
107 catch (PropertiesConfigExc e) {
\r
108 throw new RuntimeException(e.getMessage());
\r
112 public static ProducerEngine producerEngine() {
\r
113 if (producerEngine == null) {
\r
114 producerEngine = new ProducerEngine();
\r
117 return producerEngine;
\r
120 public static MRUCache mruCache() {
\r
121 synchronized(MirGlobal.class) {
\r
122 if (mruCache == null) {
\r
123 mruCache = new MRUCache();
\r
129 public static synchronized AccessControl accessControl() {
\r
130 if (accessControl == null) {
\r
131 accessControl=new AccessControl();
\r
134 return accessControl;
\r
137 public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {
\r
138 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);
\r
141 if (operation!=null)
\r
143 localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),
\r
144 localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
\r
146 catch (Throwable t) {
\r
147 throw new RuntimeException(t.toString());
\r
151 public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {
\r
152 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);
\r
155 if (operation!=null)
\r
157 localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser),
\r
158 localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
\r
160 catch (Throwable t) {
\r
161 throw new RuntimeException(t.toString());
\r
165 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {
\r
167 if (articleOperations == null) {
\r
168 articleOperations = new HashMap();
\r
169 Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
\r
170 while (i.hasNext()) {
\r
171 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
\r
172 articleOperations.put(operation.getName(), operation);
\r
176 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
\r
178 catch (Throwable t) {
\r
179 throw new RuntimeException(t.toString());
\r
183 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {
\r
185 if (commentOperations == null) {
\r
186 commentOperations = new HashMap();
\r
187 Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
\r
188 while (i.hasNext()) {
\r
189 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
\r
190 commentOperations.put(operation.getName(), operation);
\r
194 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
\r
196 catch (Throwable t) {
\r
197 throw new RuntimeException(t.toString());
\r
202 public static List getLoggedInUsers() {
\r
203 List result = new Vector();
\r
205 synchronized (loggedInUsers) {
\r
206 Iterator i = loggedInUsers.entrySet().iterator();
\r
208 while (i.hasNext()) {
\r
209 Map.Entry entry = (Map.Entry) i.next();
\r
211 Map item = new HashMap();
\r
212 item.put("name", entry.getKey());
\r
213 item.put("count", entry.getValue());
\r
221 public static void registerLogin(String aName) {
\r
222 modifyLoggedInCount(aName, 1);
\r
225 public static void registerLogout(String aName) {
\r
226 modifyLoggedInCount(aName, -1);
\r
229 private static void modifyLoggedInCount(String aName, int aModifier) {
\r
230 synchronized (loggedInUsers) {
\r
231 Integer count = (Integer) loggedInUsers.get(aName);
\r
233 count = new Integer(0);
\r
235 if (count.intValue()+aModifier<=0) {
\r
236 loggedInUsers.remove(aName);
\r
239 loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
\r
244 public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {
\r
246 String user = "unknown (" + aUser.toString() +")";
\r
248 user = aUser.getValue("login");
\r
249 adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
\r
251 catch (Throwable t) {
\r
252 logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());
\r