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 mir.bundle.BasicBundleFactory;
34 import mir.bundle.BundleFactory;
35 import mir.bundle.CascadingBundleFactory;
36 import mir.bundle.PropertiesFileBundleLoader;
37 import mir.config.MirPropertiesConfiguration;
38 import mir.entity.adapter.EntityAdapter;
39 import mir.log.LoggerEngine;
40 import mir.log.LoggerWrapper;
41 import mircoders.accesscontrol.AccessControl;
42 import mircoders.entity.EntityComment;
43 import mircoders.entity.EntityContent;
44 import mircoders.entity.EntityUsers;
45 import mircoders.localizer.MirAdminInterfaceLocalizer;
46 import mircoders.localizer.LocalizerCache;
47 import mircoders.localizer.MirLocalizer;
48 import mircoders.localizer.MirLocalizerExc;
49 import mircoders.localizer.MirLocalizerFailure;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.List;
57 public class MirGlobal {
58 static private MirLocalizer localizer;
59 static private ProducerEngine producerEngine;
60 static private Abuse abuse;
61 static private MRUCache mruCache;
62 static private AccessControl accessControl;
63 static private Map articleOperations;
64 static private Map commentOperations;
65 static private final Map loggedInUsers = new HashMap();
66 static private final Map loggedInUserIds = new HashMap();
67 static private LoggerWrapper logger = new LoggerWrapper("Global");
68 static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");
70 static private ChangeEngine changeEngine = new ChangeEngine();
72 static private DatabaseEngine databaseEngine;
74 static private BundleFactory bundleFactory =
75 new CascadingBundleFactory(
76 new BasicBundleFactory(
77 new PropertiesFileBundleLoader(
78 config().getHome())));
83 public synchronized static MirLocalizer localizer() {
84 String localizerClassName;
87 if (localizer == null ) {
88 localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
91 localizerClass = Class.forName(localizerClassName);
94 throw new MirLocalizerFailure("localizer class '" +
95 localizerClassName + "' not found: ", t);
98 if (!MirLocalizer.class.isAssignableFrom(localizerClass)) {
99 throw new MirLocalizerFailure("localizer class '" +
100 localizerClassName + "' is not assignable from MirLocalizer");
104 localizer = new LocalizerCache((MirLocalizer) localizerClass.newInstance());
106 catch (Throwable t) {
107 throw new MirLocalizerFailure("localizer class '" +
108 localizerClassName + "' cannot be instantiated: " + t.getMessage(), t);
116 * Returns a string that provides some global status information
118 public static String getStatus() {
119 StringBuffer result = new StringBuffer();
121 result.append((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/(1024*1024));
122 result.append("M in use, ");
123 result.append(Thread.activeCount()).append(" threads, ");
124 result.append(getDatabaseEngine().getStatus());
126 return result.toString();
129 synchronized public static Abuse abuse() {
132 abuse = new Abuse(localizer().dataModel().adapterModel());
134 catch (MirLocalizerExc e) {
135 throw new RuntimeException(e.getMessage());
143 * returns the global change engine (used to track changed files)
145 public static ChangeEngine getChangeEngine() {
149 public static MirPropertiesConfiguration config() {
150 return MirPropertiesConfiguration.instance();
153 synchronized public static DatabaseEngine getDatabaseEngine() {
154 if (databaseEngine==null) {
155 databaseEngine = new DatabaseEngine();
158 return databaseEngine;
161 public static ProducerEngine getProducerEngine() {
162 if (producerEngine == null) {
163 producerEngine = new ProducerEngine();
166 return producerEngine;
169 public static MRUCache mruCache() {
170 synchronized(MirGlobal.class) {
171 if (mruCache == null) {
172 mruCache = new MRUCache();
178 synchronized public static AccessControl accessControl() {
179 if (accessControl == null) {
180 accessControl=new AccessControl();
183 return accessControl;
186 public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {
187 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);
190 EntityAdapter user = null;
192 user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
195 if (operation!=null) {
198 localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
201 catch (Throwable t) {
202 throw new MirGlobalFailure(t.getMessage(), t);
206 public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {
207 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);
210 EntityAdapter user = null;
212 user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
215 if (operation!=null) {
218 localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
221 catch (Throwable t) {
222 throw new MirGlobalFailure(t.getMessage(), t);
226 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation
227 getArticleOperationForName(String aName) {
229 if (articleOperations == null) {
230 articleOperations = new HashMap();
231 Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
232 while (i.hasNext()) {
233 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =
234 (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
235 articleOperations.put(operation.getName(), operation);
239 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
241 catch (Throwable t) {
242 throw new MirGlobalFailure(t.getMessage(), t);
246 private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation
247 getCommentOperationForName(String aName) {
249 if (commentOperations == null) {
250 commentOperations = new HashMap();
251 Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
252 while (i.hasNext()) {
253 MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =
254 (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
255 commentOperations.put(operation.getName(), operation);
259 return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
261 catch (Throwable t) {
262 throw new MirGlobalFailure(t.getMessage(), t);
266 public static boolean isUserLoggedIn(String anId) {
267 synchronized (loggedInUserIds) {
268 return loggedInUserIds.containsKey(anId);
272 public static List getLoggedInUsers() {
273 List result = new ArrayList();
275 synchronized (loggedInUsers) {
276 Iterator i = loggedInUsers.entrySet().iterator();
278 while (i.hasNext()) {
279 Map.Entry entry = (Map.Entry) i.next();
281 Map item = new HashMap();
282 item.put("name", entry.getKey());
283 item.put("count", entry.getValue());
291 public static BundleFactory getBundleFactory() {
292 return bundleFactory;
295 public static void registerLogin(String aName, String anId) {
296 modifyLoggedInCount(aName, anId, 1);
299 public static void registerLogout(String aName, String anId) {
300 modifyLoggedInCount(aName, anId, -1);
303 private static void modifyLoggedInCount(String aName, String anId, int aModifier) {
304 synchronized (loggedInUsers) {
305 Integer count = (Integer) loggedInUsers.get(aName);
307 count = new Integer(0);
309 if (count.intValue()+aModifier<=0) {
310 loggedInUsers.remove(aName);
313 loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
317 synchronized (loggedInUserIds) {
318 Integer count = (Integer) loggedInUserIds.get(anId);
320 count = new Integer(0);
322 if (count.intValue()+aModifier<=0) {
323 loggedInUserIds.remove(anId);
326 loggedInUserIds.put(anId, new Integer(count.intValue() + aModifier));
332 * Called whenever a modifying admin action occurs. Used to log
333 * the action, if admin activity logging is turned on,
335 public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {
337 if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {
338 String user = "unknown (" + aUser.toString() + ")";
340 user = aUser.getFieldValue("login");
341 adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
344 catch (Throwable t) {
345 logger.error("Error while logging admin usage ("+
346 aUser.toString()+", "+aDescription+"): " +t.toString());
351 * Reloads all reloadable configurations, such as the producer subsystem.
353 public static void reloadConfigurations() throws MirGlobalExc, MirGlobalFailure {
354 getProducerEngine().reloadConfiguration();
356 LoggerEngine.reload();
358 catch (Throwable e) {
359 throw new MirGlobalFailure(e);
361 getBundleFactory().reload();