2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
31 package mircoders.global;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Vector;
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;
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");
65 public synchronized static MirLocalizer localizer() {
66 String localizerClassName;
69 if (localizer == null ) {
70 localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
73 localizerClass = Class.forName(localizerClassName);
76 throw new ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
79 if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
80 throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
83 localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
86 throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
93 public static Abuse abuse() {
95 synchronized(MirGlobal.class) {
104 public static MirPropertiesConfiguration config() {
106 return MirPropertiesConfiguration.instance();
108 catch (PropertiesConfigExc e) {
109 throw new RuntimeException(e.getMessage());
113 public static ProducerEngine producerEngine() {
114 if (producerEngine == null) {
115 producerEngine = new ProducerEngine();
118 return producerEngine;
121 public static MRUCache mruCache() {
122 synchronized(MirGlobal.class) {
123 if (mruCache == null) {
124 mruCache = new MRUCache();
130 public static synchronized AccessControl accessControl() {
131 if (accessControl == null) {
132 accessControl=new AccessControl();
135 return accessControl;
138 public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {
139 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);
142 EntityAdapter user = null;
144 user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
149 localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
151 catch (Throwable t) {
152 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
154 throw new RuntimeException(t.toString());
158 public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {
159 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);
162 EntityAdapter user = null;
164 user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
169 localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
171 catch (Throwable t) {
172 throw new RuntimeException(t.toString());
176 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {
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);
187 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
189 catch (Throwable t) {
190 throw new RuntimeException(t.toString());
194 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {
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);
205 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
207 catch (Throwable t) {
208 throw new RuntimeException(t.toString());
213 public static List getLoggedInUsers() {
214 List result = new Vector();
216 synchronized (loggedInUsers) {
217 Iterator i = loggedInUsers.entrySet().iterator();
219 while (i.hasNext()) {
220 Map.Entry entry = (Map.Entry) i.next();
222 Map item = new HashMap();
223 item.put("name", entry.getKey());
224 item.put("count", entry.getValue());
232 public static void registerLogin(String aName) {
233 modifyLoggedInCount(aName, 1);
236 public static void registerLogout(String aName) {
237 modifyLoggedInCount(aName, -1);
240 private static void modifyLoggedInCount(String aName, int aModifier) {
241 synchronized (loggedInUsers) {
242 Integer count = (Integer) loggedInUsers.get(aName);
244 count = new Integer(0);
246 if (count.intValue()+aModifier<=0) {
247 loggedInUsers.remove(aName);
250 loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
255 public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {
257 if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {
258 String user = "unknown (" + aUser.toString() + ")";
260 user = aUser.getValue("login");
261 adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
264 catch (Throwable t) {
265 logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());