some cosmetic change, organzied imports, and made some accesses in a static way
[mir.git] / source / mircoders / global / MirGlobal.java
index 4eb40e6..ea8e599 100755 (executable)
-/*\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.config.MirPropertiesConfiguration;\r
-import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
-import mir.entity.adapter.EntityAdapter;\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 MirPropertiesConfiguration configuration;\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 LoggerWrapper logger = new LoggerWrapper("Global");\r
-  static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");\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 '" + localizerClassName + "' not found: " + t.toString());\r
-      }\r
-\r
-      if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))\r
-        throw new ConfigException("localizer class '" + 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 '" + localizerClassName + "' cannot be instantiated: " + t.toString());\r
-      }\r
-    }\r
-\r
-    return localizer;\r
-  }\r
-\r
-  public static Abuse abuse() {\r
-    if (abuse==null) {\r
-      synchronized(MirGlobal.class) {\r
-        if (abuse==null)\r
-          abuse = new Abuse();\r
-      }\r
-    }\r
-\r
-    return abuse;\r
-  }\r
-\r
-  public static MirPropertiesConfiguration config() {\r
-    try {\r
-      return MirPropertiesConfiguration.instance();\r
-    }\r
-    catch (PropertiesConfigExc e) {\r
-      throw new RuntimeException(e.getMessage());\r
-    }\r
-  }\r
-\r
-  public static ProducerEngine producerEngine() {\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(logger.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 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 = (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 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 = (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
-\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 void registerLogin(String aName) {\r
-    modifyLoggedInCount(aName, 1);\r
-  }\r
-\r
-  public static void registerLogout(String aName) {\r
-    modifyLoggedInCount(aName, -1);\r
-  }\r
-\r
-  private static void modifyLoggedInCount(String aName, 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
-\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.getValue("login");\r
-        adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);\r
-      }\r
-    }\r
-    catch (Throwable t) {\r
-      logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());\r
-    }\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 java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.entity.adapter.EntityAdapter;
+import mir.log.LoggerWrapper;
+import mir.misc.ConfigException;
+import mircoders.accesscontrol.AccessControl;
+import mircoders.entity.EntityComment;
+import mircoders.entity.EntityContent;
+import mircoders.entity.EntityUsers;
+import mircoders.localizer.MirAdminInterfaceLocalizer;
+import mircoders.localizer.MirCachingLocalizerDecorator;
+import mircoders.localizer.MirLocalizer;
+
+public class MirGlobal {
+  static private MirPropertiesConfiguration configuration;
+  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 Map loggedInUsers = new HashMap();
+  static private LoggerWrapper logger = new LoggerWrapper("Global");
+  static private LoggerWrapper adminUsageLogger = new LoggerWrapper("AdminUsage");
+
+  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 ConfigException("localizer class '" + localizerClassName + "' not found: " + t.toString());
+      }
+
+      if (!(MirLocalizer.class.isAssignableFrom(localizerClass)))
+        throw new ConfigException("localizer class '" + localizerClassName + "' is not assignable from MirLocalizer");
+
+      try {
+        localizer = new MirCachingLocalizerDecorator((MirLocalizer) localizerClass.newInstance());
+      }
+      catch (Throwable t) {
+        throw new ConfigException("localizer class '" + localizerClassName + "' cannot be instantiated: " + t.toString());
+      }
+    }
+
+    return localizer;
+  }
+
+  public static Abuse abuse() {
+    if (abuse==null) {
+      synchronized(MirGlobal.class) {
+        if (abuse==null)
+          abuse = new Abuse();
+      }
+    }
+
+    return abuse;
+  }
+
+  public static MirPropertiesConfiguration config() {
+    try {
+      return MirPropertiesConfiguration.instance();
+    }
+    catch (PropertiesConfigExc e) {
+      throw new RuntimeException(e.getMessage());
+    }
+  }
+
+  public static ProducerEngine producerEngine() {
+    if (producerEngine == null) {
+      producerEngine = new ProducerEngine();
+    }
+
+    return producerEngine;
+  }
+
+  public static MRUCache mruCache() {
+    synchronized(MirGlobal.class) {
+      if (mruCache == null) {
+        mruCache = new MRUCache();
+      }
+      return mruCache;
+    }
+  }
+
+  public static synchronized AccessControl accessControl() {
+    if (accessControl == null) {
+      accessControl=new AccessControl();
+    }
+
+    return accessControl;
+  }
+
+  public static void performArticleOperation(EntityUsers aUser, EntityContent  anArticle, String anOperation) {
+    MirAdminInterfaceLocalizer.MirSimpleEntityOperation 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) {
+      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+
+      throw new RuntimeException(t.toString());
+    }
+  }
+
+  public static void performCommentOperation(EntityUsers aUser, EntityComment  aComment, String anOperation) {
+    MirAdminInterfaceLocalizer.MirSimpleEntityOperation 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 RuntimeException(t.toString());
+    }
+  }
+
+  private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getArticleOperationForName(String aName) {
+    try {
+      if (articleOperations == null) {
+        articleOperations = new HashMap();
+        Iterator i = localizer().adminInterface().simpleArticleOperations().iterator();
+        while (i.hasNext()) {
+          MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
+          articleOperations.put(operation.getName(), operation);
+        }
+      }
+
+      return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) articleOperations.get(aName);
+    }
+    catch (Throwable t) {
+      throw new RuntimeException(t.toString());
+    }
+  }
+
+  private synchronized static MirAdminInterfaceLocalizer.MirSimpleEntityOperation getCommentOperationForName(String aName) {
+    try {
+      if (commentOperations == null) {
+        commentOperations = new HashMap();
+        Iterator i = localizer().adminInterface().simpleCommentOperations().iterator();
+        while (i.hasNext()) {
+          MirAdminInterfaceLocalizer.MirSimpleEntityOperation operation = (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) i.next();
+          commentOperations.put(operation.getName(), operation);
+        }
+      }
+
+      return (MirAdminInterfaceLocalizer.MirSimpleEntityOperation) commentOperations.get(aName);
+    }
+    catch (Throwable t) {
+      throw new RuntimeException(t.toString());
+    }
+  }
+
+
+  public static List getLoggedInUsers() {
+    List result = new Vector();
+
+    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 void registerLogin(String aName) {
+    modifyLoggedInCount(aName, 1);
+  }
+
+  public static void registerLogout(String aName) {
+    modifyLoggedInCount(aName, -1);
+  }
+
+  private static void modifyLoggedInCount(String aName, 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));
+      }
+    }
+  }
+
+  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.getValue("login");
+        adminUsageLogger.info(user + " | " + anObject + " | " + aDescription);
+      }
+    }
+    catch (Throwable t) {
+      logger.error("Error while logging admin usage ("+aUser.toString()+", "+aDescription+"): " +t.toString());
+    }
+  }
+}
+
+