rebuilding head
[mir.git] / source / mircoders / global / MirGlobal.java
index 6cb0e0b..2306d96 100755 (executable)
-/*
- * 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 the com.oreilly.servlet library, 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.misc.*;
-import mircoders.localizer.*;
-
-public class MirGlobal {
-  static private MirConfig configuration;
-  static private MirLocalizer localizer;
-  static private ProducerEngine producerEngine;
-
-  public static MirLocalizer localizer() {
-    String localizerClassName;
-    Class localizerClass;
-
-    if (localizer == null ) {
-      synchronized(MirGlobal.class) {
-        if (localizer == null ) {
-          localizerClassName = getConfigPropertyWithDefault("Mir.Localizer", "mirlocal.loaclizer.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 MirConfig config() {
-    if (configuration == null) {
-      configuration = new MirConfig();
-    }
-
-    return configuration;
-  }
-
-  public static ProducerEngine producerEngine() {
-    if (producerEngine == null) {
-      producerEngine = new ProducerEngine();
-    }
-
-    return producerEngine;
-  }
-
-  public static String getConfigPropertyWithDefault(String aPropertyName, String aDefault) {
-    String result;
-
-    result = config().getProp(aPropertyName);
-
-    if (result==null)
-      result = aDefault;
-
-    return result;
-  }
-
-  public static String getConfigProperty(String aPropertyName) {
-    String result;
-
-    result = config().getProp(aPropertyName);
-
-    if (result==null)
-      throw new ConfigException("Property '" + aPropertyName + "' not present");
-
-    return result;
-  }
-
-  public static int getConfigIntegerProperty(String aPropertyName) {
-    String result;
-
-    result = config().getProp(aPropertyName);
-
-    return Integer.parseInt(result);
-  }
-
-  public static boolean getConfigBooleanProperty(String aPropertyName) {
-    String result;
-
-    result = config().getProp(aPropertyName);
-
-    if (result==null)
-      throw new ConfigException("Boolean property '" + aPropertyName + "' not present");
-
-    return (result.equals("yes") || result.equals("1"));
-  }
-}
+/*\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