-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two. You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries. If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package mircoders.global;\r
-\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-\r
-import mir.bundle.BasicBundleFactory;\r
-import mir.bundle.BundleFactory;\r
-import mir.bundle.CascadingBundleFactory;\r
-import mir.bundle.PropertiesFileBundleLoader;\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.entity.adapter.EntityAdapter;\r
-import mir.log.LoggerEngine;\r
-import mir.log.LoggerWrapper;\r
-import mir.misc.ConfigException;\r
-import mircoders.accesscontrol.AccessControl;\r
-import mircoders.entity.EntityComment;\r
-import mircoders.entity.EntityContent;\r
-import mircoders.entity.EntityUsers;\r
-import mircoders.localizer.MirAdminInterfaceLocalizer;\r
-import mircoders.localizer.MirCachingLocalizerDecorator;\r
-import mircoders.localizer.MirLocalizer;\r
-\r
-public class MirGlobal {\r
- static private MirLocalizer localizer;\r
- static private ProducerEngine producerEngine;\r
- static private Abuse abuse;\r
- static private MRUCache mruCache;\r
- static private AccessControl accessControl;\r
- static private Map articleOperations;\r
- static private Map commentOperations;\r
- static private Map loggedInUsers = new HashMap();\r
- static private Map loggedInUserIds = new HashMap();\r
- static private LoggerWrapper logger = new LoggerWrapper("Global");\r
- static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");\r
-// static private ChangeEngine changeEngine = new ChangeEngine();\r
- static private DatabaseEngine databaseEngine;\r
-\r
- static private BundleFactory bundleFactory =\r
- new CascadingBundleFactory(\r
- new BasicBundleFactory(\r
- new PropertiesFileBundleLoader(\r
- config().getHome())));\r
-\r
- public synchronized static MirLocalizer localizer() {\r
- String localizerClassName;\r
- Class localizerClass;\r
-\r
- if (localizer == null ) {\r
- localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");\r
-\r
- try {\r
- localizerClass = Class.forName(localizerClassName);\r
- }\r
- catch (Throwable t) {\r
- throw new ConfigException("localizer class '" +\r
- localizerClassName + "' not found: " + t.toString());\r
- }\r
-\r
- if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))\r
- throw new ConfigException("localizer class '" +\r
- localizerClassName + "' is not assignable from MirLocalizer");\r
-\r
- try {\r
- localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());\r
- }\r
- catch (Throwable t) {\r
- throw new ConfigException("localizer class '" +\r
- localizerClassName + "' cannot be instantiated: " + t.toString());\r
- }\r
- }\r
-\r
- return localizer;\r
- }\r
-\r
- /**\r
- * Returns a string that provides some global status information\r
- */\r
- public static String getStatus() {\r
- StringBuffer result = new StringBuffer();\r
-\r
- result.append((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/(1024*1024));\r
- result.append("M in use, ");\r
- result.append(Thread.currentThread().activeCount()).append(" threads, ");\r
- result.append(getDatabaseEngine().getStatus());\r
-\r
- return result.toString();\r
- }\r
-\r
- public synchronized static Abuse abuse() {\r
- if (abuse==null)\r
- abuse = new Abuse();\r
-\r
- return abuse;\r
- }\r
-\r
- /**\r
- * returns the global change engine (used to track changed files)\r
- */\r
-// public static ChangeEngine getChangeEngine() {\r
-// return changeEngine;\r
-// }\r
-\r
- public static MirPropertiesConfiguration config() {\r
- return MirPropertiesConfiguration.instance();\r
- }\r
-\r
- public synchronized static DatabaseEngine getDatabaseEngine() {\r
- if (databaseEngine==null)\r
- databaseEngine = new DatabaseEngine();\r
-\r
- return databaseEngine;\r
- }\r
-\r
- public static ProducerEngine getProducerEngine() {\r
- if (producerEngine == null) {\r
- producerEngine = new ProducerEngine();\r
- }\r
-\r
- return producerEngine;\r
- }\r
-\r
- public static MRUCache mruCache() {\r
- synchronized(MirGlobal.class) {\r
- if (mruCache == null) {\r
- mruCache = new MRUCache();\r
- }\r
- return mruCache;\r
- }\r
- }\r
-\r
- public static synchronized AccessControl accessControl() {\r
- if (accessControl == null) {\r
- accessControl=new AccessControl();\r
- }\r
-\r
- return accessControl;\r
- }\r
-\r
- public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {\r
- MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getArticleOperationForName(anOperation);\r
-\r
- try {\r
- EntityAdapter user = null;\r
- if (aUser!=null)\r
- user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);\r
-\r
- if (operation!=null)\r
- operation.perform(\r
- user,\r
- localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));\r
- }\r
- catch (Throwable t) {\r
- t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
-\r
- throw new RuntimeException(t.toString());\r
- }\r
- }\r
-\r
- public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {\r
- MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = getCommentOperationForName(anOperation);\r
-\r
- try {\r
- EntityAdapter user = null;\r
- if (aUser!=null)\r
- user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);\r
-\r
- if (operation!=null)\r
- operation.perform(\r
- user,\r
- localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));\r
- }\r
- catch (Throwable t) {\r
- throw new RuntimeException(t.toString());\r
- }\r
- }\r
-\r
- private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation\r
- getArticleOperationForName(String aName) {\r
- try {\r
- if (articleOperations == null) {\r
- articleOperations = new HashMap();\r
- Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();\r
- while (i.hasNext()) {\r
- MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =\r
- (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
- articleOperations.put(operation.getName(), operation);\r
- }\r
- }\r
-\r
- return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);\r
- }\r
- catch (Throwable t) {\r
- throw new RuntimeException(t.toString());\r
- }\r
- }\r
-\r
- private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation\r
- getCommentOperationForName(String aName) {\r
- try {\r
- if (commentOperations == null) {\r
- commentOperations = new HashMap();\r
- Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();\r
- while (i.hasNext()) {\r
- MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation =\r
- (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();\r
- commentOperations.put(operation.getName(), operation);\r
- }\r
- }\r
-\r
- return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);\r
- }\r
- catch (Throwable t) {\r
- throw new RuntimeException(t.toString());\r
- }\r
- }\r
-\r
- public static boolean isUserLoggedIn(String anId) {\r
- synchronized (loggedInUserIds) {\r
- return loggedInUserIds.containsKey(anId);\r
- }\r
- }\r
-\r
- public static List getLoggedInUsers() {\r
- List result = new Vector();\r
-\r
- synchronized (loggedInUsers) {\r
- Iterator i = loggedInUsers.entrySet().iterator();\r
-\r
- while (i.hasNext()) {\r
- Map.Entry entry = (Map.Entry) i.next();\r
-\r
- Map item = new HashMap();\r
- item.put("name", entry.getKey());\r
- item.put("count", entry.getValue());\r
- result.add(item);\r
- }\r
- }\r
-\r
- return result;\r
- }\r
-\r
- public static BundleFactory getBundleFactory() {\r
- return bundleFactory;\r
- }\r
-\r
- public static void registerLogin(String aName, String anId) {\r
- modifyLoggedInCount(aName, anId, 1);\r
- }\r
-\r
- public static void registerLogout(String aName, String anId) {\r
- modifyLoggedInCount(aName, anId, -1);\r
- }\r
-\r
- private static void modifyLoggedInCount(String aName, String anId, int aModifier) {\r
- synchronized (loggedInUsers) {\r
- Integer count = (Integer) loggedInUsers.get(aName);\r
- if (count==null)\r
- count = new Integer(0);\r
-\r
- if (count.intValue()+aModifier<=0) {\r
- loggedInUsers.remove(aName);\r
- }\r
- else {\r
- loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));\r
- }\r
- }\r
-\r
- synchronized (loggedInUserIds) {\r
- Integer count = (Integer) loggedInUserIds.get(anId);\r
- if (count==null)\r
- count = new Integer(0);\r
-\r
- if (count.intValue()+aModifier<=0) {\r
- loggedInUserIds.remove(anId);\r
- }\r
- else {\r
- loggedInUserIds.put(anId, new Integer(count.intValue() + aModifier));\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Called whenever a modifying admin action occurs. Used to log\r
- * the action, if admin activity logging is turned on,\r
- */\r
- public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {\r
- try {\r
- if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {\r
- String user = "unknown (" + aUser.toString() + ")";\r
- if (aUser != null)\r
- user = aUser.getFieldValue("login");\r
- adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);\r
- }\r
- }\r
- catch (Throwable t) {\r
- logger.error("Error while logging admin usage ("+\r
- aUser.toString()+", "+aDescription+"): " +t.toString());\r
- }\r
- }\r
-\r
- /**\r
- * Reloads all reloadable configurations, such as the producer subsystem.\r
- */\r
- public static void reloadConfigurations() throws MirGlobalExc, MirGlobalFailure {\r
- getProducerEngine().reloadConfiguration();\r
- try {\r
- LoggerEngine.reload();\r
- }\r
- catch (Throwable e) {\r
- throw new MirGlobalFailure(e);\r
- }\r
- getBundleFactory().reload();\r
- }\r
-}\r
-\r
-\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+
+package mircoders.global;
+
+import mir.bundle.BasicBundleFactory;
+import mir.bundle.BundleFactory;
+import mir.bundle.CascadingBundleFactory;
+import mir.bundle.PropertiesFileBundleLoader;
+import mir.config.MirPropertiesConfiguration;
+import mir.entity.adapter.EntityAdapter;
+import mir.log.LoggerEngine;
+import mir.log.LoggerWrapper;
+import mircoders.accesscontrol.AccessControl;
+import mircoders.entity.EntityComment;
+import mircoders.entity.EntityContent;
+import mircoders.entity.EntityUsers;
+import mircoders.localizer.MirAdminInterfaceLocalizer;
+import mircoders.localizer.LocalizerCache;
+import mircoders.localizer.MirLocalizer;
+import mircoders.localizer.MirLocalizerExc;
+import mircoders.localizer.MirLocalizerFailure;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public class MirGlobal {
+ static private MirLocalizer localizer;
+ static private ProducerEngine producerEngine;
+ static private Abuse abuse;
+ static private MRUCache mruCache;
+ static private AccessControl accessControl;
+ static private Map articleOperations;
+ static private Map commentOperations;
+ static private final Map loggedInUsers = new HashMap();
+ static private final Map loggedInUserIds = new HashMap();
+ static private LoggerWrapper logger = new LoggerWrapper("Global");
+ static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");
+
+ static private ChangeEngine changeEngine = new ChangeEngine();
+
+ static private DatabaseEngine databaseEngine;
+
+ static private BundleFactory bundleFactory =
+ new CascadingBundleFactory(
+ new BasicBundleFactory(
+ new PropertiesFileBundleLoader(
+ config().getHome())));
+
+ private MirGlobal() {
+ }
+
+ public synchronized static MirLocalizer localizer() {
+ String localizerClassName;
+ Class localizerClass;
+
+ if (localizer == null ) {
+ localizerClassName = config().getString("Mir.Localizer", "mirlocal.localizer.basic.MirBasicLocalizer");
+
+ try {
+ localizerClass = Class.forName(localizerClassName);
+ }
+ catch (Throwable t) {
+ throw new MirLocalizerFailure("localizer class '" +
+ localizerClassName + "' not found: ", t);
+ }
+
+ if (!MirLocalizer.class.isAssignableFrom(localizerClass)) {
+ throw new MirLocalizerFailure("localizer class '" +
+ localizerClassName + "' is not assignable from MirLocalizer");
+ }
+
+ try {
+ localizer = new LocalizerCache((MirLocalizer) localizerClass.newInstance());
+ }
+ catch (Throwable t) {
+ throw new MirLocalizerFailure("localizer class '" +
+ localizerClassName + "' cannot be instantiated: " + t.getMessage(), t);
+ }
+ }
+
+ return localizer;
+ }
+
+ /**
+ * Returns a string that provides some global status information
+ */
+ public static String getStatus() {
+ StringBuffer result = new StringBuffer();
+
+ result.append((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/(1024*1024));
+ result.append("M in use, ");
+ result.append(Thread.activeCount()).append(" threads, ");
+ result.append(getDatabaseEngine().getStatus());
+
+ return result.toString();
+ }
+
+ synchronized public static Abuse abuse() {
+ if (abuse==null) {
+ try {
+ abuse = new Abuse(localizer().dataModel().adapterModel());
+ }
+ catch (MirLocalizerExc e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ return abuse;
+ }
+
+ /**
+ * returns the global change engine (used to track changed files)
+ */
+ public static ChangeEngine getChangeEngine() {
+ return changeEngine;
+ }
+
+ public static MirPropertiesConfiguration config() {
+ return MirPropertiesConfiguration.instance();
+ }
+
+ synchronized public static DatabaseEngine getDatabaseEngine() {
+ if (databaseEngine==null) {
+ databaseEngine = new DatabaseEngine();
+ }
+
+ return databaseEngine;
+ }
+
+ public static ProducerEngine getProducerEngine() {
+ if (producerEngine == null) {
+ producerEngine = new ProducerEngine();
+ }
+
+ return producerEngine;
+ }
+
+ public static MRUCache mruCache() {
+ synchronized(MirGlobal.class) {
+ if (mruCache == null) {
+ mruCache = new MRUCache();
+ }
+ return mruCache;
+ }
+ }
+
+ synchronized public static AccessControl accessControl() {
+ if (accessControl == null) {
+ accessControl=new AccessControl();
+ }
+
+ return accessControl;
+ }
+
+ public static void performArticleOperation(EntityUsers aUser, EntityContent anArticle, String anOperation) {
+ MirAdminInterfaceLocalizer.EntityOperation operation = getArticleOperationForName(anOperation);
+
+ try {
+ EntityAdapter user = null;
+ if (aUser!=null) {
+ user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
+ }
+
+ if (operation!=null) {
+ operation.perform(
+ user,
+ localizer().dataModel().adapterModel().makeEntityAdapter("content", anArticle));
+ }
+ }
+ catch (Throwable t) {
+ throw new MirGlobalFailure(t.getMessage(), t);
+ }
+ }
+
+ public static void performCommentOperation(EntityUsers aUser, EntityComment aComment, String anOperation) {
+ MirAdminInterfaceLocalizer.EntityOperation operation = getCommentOperationForName(anOperation);
+
+ try {
+ EntityAdapter user = null;
+ if (aUser!=null) {
+ user = localizer().dataModel().adapterModel().makeEntityAdapter("user", aUser);
+ }
+
+ if (operation!=null) {
+ operation.perform(
+ user,
+ localizer().dataModel().adapterModel().makeEntityAdapter("comment", aComment));
+ }
+ }
+ catch (Throwable t) {
+ throw new MirGlobalFailure(t.getMessage(), t);
+ }
+ }
+
+ private synchronized static MirAdminInterfaceLocalizer.EntityOperation
+ getArticleOperationForName(String aName) {
+ try {
+ if (articleOperations == null) {
+ articleOperations = new HashMap();
+ Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
+ while (i.hasNext()) {
+ MirAdminInterfaceLocalizer.EntityOperation operation =
+ (MirAdminInterfaceLocalizer.EntityOperation) i.next();
+ articleOperations.put(operation.getName(), operation);
+ }
+ }
+
+ return (MirAdminInterfaceLocalizer.EntityOperation) articleOperations.get(aName);
+ }
+ catch (Throwable t) {
+ throw new MirGlobalFailure(t.getMessage(), t);
+ }
+ }
+
+ private synchronized static MirAdminInterfaceLocalizer.EntityOperation
+ getCommentOperationForName(String aName) {
+ try {
+ if (commentOperations == null) {
+ commentOperations = new HashMap();
+ Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
+ while (i.hasNext()) {
+ MirAdminInterfaceLocalizer.EntityOperation operation =
+ (MirAdminInterfaceLocalizer.EntityOperation) i.next();
+ commentOperations.put(operation.getName(), operation);
+ }
+ }
+
+ return (MirAdminInterfaceLocalizer.EntityOperation) commentOperations.get(aName);
+ }
+ catch (Throwable t) {
+ throw new MirGlobalFailure(t.getMessage(), t);
+ }
+ }
+
+ public static boolean isUserLoggedIn(String anId) {
+ synchronized (loggedInUserIds) {
+ return loggedInUserIds.containsKey(anId);
+ }
+ }
+
+ public static List getLoggedInUsers() {
+ List result = new ArrayList();
+
+ synchronized (loggedInUsers) {
+ Iterator i = loggedInUsers.entrySet().iterator();
+
+ while (i.hasNext()) {
+ Map.Entry entry = (Map.Entry) i.next();
+
+ Map item = new HashMap();
+ item.put("name", entry.getKey());
+ item.put("count", entry.getValue());
+ result.add(item);
+ }
+ }
+
+ return result;
+ }
+
+ public static BundleFactory getBundleFactory() {
+ return bundleFactory;
+ }
+
+ public static void registerLogin(String aName, String anId) {
+ modifyLoggedInCount(aName, anId, 1);
+ }
+
+ public static void registerLogout(String aName, String anId) {
+ modifyLoggedInCount(aName, anId, -1);
+ }
+
+ private static void modifyLoggedInCount(String aName, String anId, int aModifier) {
+ synchronized (loggedInUsers) {
+ Integer count = (Integer) loggedInUsers.get(aName);
+ if (count==null)
+ count = new Integer(0);
+
+ if (count.intValue()+aModifier<=0) {
+ loggedInUsers.remove(aName);
+ }
+ else {
+ loggedInUsers.put(aName, new Integer(count.intValue() + aModifier));
+ }
+ }
+
+ synchronized (loggedInUserIds) {
+ Integer count = (Integer) loggedInUserIds.get(anId);
+ if (count==null)
+ count = new Integer(0);
+
+ if (count.intValue()+aModifier<=0) {
+ loggedInUserIds.remove(anId);
+ }
+ else {
+ loggedInUserIds.put(anId, new Integer(count.intValue() + aModifier));
+ }
+ }
+ }
+
+ /**
+ * Called whenever a modifying admin action occurs. Used to log
+ * the action, if admin activity logging is turned on,
+ */
+ public static void logAdminUsage(EntityUsers aUser, String anObject, String aDescription) {
+ try {
+ if (config().getString("Mir.Admin.LogAdminActivity", "0").equals("1")) {
+ String user = "unknown (" + aUser.toString() + ")";
+ if (aUser != null)
+ user = aUser.getFieldValue("login");
+ adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
+ }
+ }
+ catch (Throwable t) {
+ logger.error("Error while logging admin usage ("+
+ aUser.toString()+", "+aDescription+"): " +t.toString());
+ }
+ }
+
+ /**
+ * Reloads all reloadable configurations, such as the producer subsystem.
+ */
+ public static void reloadConfigurations() throws MirGlobalExc, MirGlobalFailure {
+ getProducerEngine().reloadConfiguration();
+ try {
+ LoggerEngine.reload();
+ }
+ catch (Throwable e) {
+ throw new MirGlobalFailure(e);
+ }
+ getBundleFactory().reload();
+ }
+}
+
+