1.1 restoration
[mir.git] / source / mir / storage / store / ObjectStore.java
index b50f0a8..0c65b1c 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 the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package mir.storage.store;\r
-\r
-/**\r
- * Title:         ObjectStore for StorableObjects\r
- * Description:   ObjectStore holds a Map of @see StoreContainer for all possible\r
- *                @see StoreIdentifier.\r
- *\r
- *                @see StorageIdentifier - identitfies one object in the ObjectStore\r
- *                      i.e. in its apropriate bucket. It holds a unique identifier\r
- *                      of a StorableObject and a reference on the StorableObject.\r
- *\r
- *                @see StoreContainer - "Buckets" to store different types of Objects\r
- *                      in one Container. These buckets are cofigurable via\r
- *                      config.properties.\r
- *\r
- *                @see StoreContainerType - is a signature for all StoreContainer\r
- *                      and StoreIdentifier.\r
- *\r
- *                @see StorableObjects - Interface Object have to implement to\r
- *                      be handled by the ObjectStore\r
- *\r
- *                @see ServletStoreInfo - Maintenance Servlet for ObjectStore.\r
- *                      Displays information about current content of the\r
- *                      ObjectStore.\r
- *\r
- *\r
- * Copyright:     Copyright (c) 2002\r
- * Company:       indy\r
- * @author        rk\r
- * @version 1.0\r
- */\r
-\r
-import java.io.BufferedInputStream;\r
-import java.io.FileInputStream;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.MissingResourceException;\r
-import java.util.Properties;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.log.LoggerWrapper;\r
-\r
-public class ObjectStore {\r
-\r
-  private final static ObjectStore INSTANCE = new ObjectStore();\r
-  private final static HashMap containerMap = new HashMap(); // StoreContainerType/StoreContainer\r
-  private static long storeHit = 0, storeMiss = 0;\r
-  private Properties ostoreConf;\r
-  private LoggerWrapper logger;\r
-\r
-  private ObjectStore() {\r
-    String confName = null;\r
-\r
-    logger = new LoggerWrapper("Database.ObjectStore");\r
-    Properties conf = new Properties();\r
-\r
-    try {\r
-      confName =\r
-          MirPropertiesConfiguration.instance().getString("Home") +\r
-          "etc/objectstore.properties";\r
-      conf.load(new BufferedInputStream(new FileInputStream(confName)));\r
-    }\r
-    catch (java.io.FileNotFoundException fnfe) {\r
-      logger.error("could not read config file. not found: " + confName);\r
-    }\r
-    catch (Throwable t) {\r
-      logger.error("could not get config: " + t.getMessage());\r
-    }\r
-    ostoreConf = conf;\r
-  }\r
-\r
-  public static ObjectStore getInstance() {\r
-    return INSTANCE;\r
-  }\r
-\r
-  /**\r
-   *  Method:       use\r
-   *  Description:  The ObjectStore tries to find the @see StoreIdentifier sid\r
-   *                and returns the stored Object.\r
-   *\r
-   *  @return       StorableObject is null when no StorableObject for the\r
-   *                StoreIdentifier sid is found.\r
-   */\r
-  public StorableObject use(StoreIdentifier sid) {\r
-    if (sid != null) {\r
-      StorableObject storeObject = null;\r
-      StoreContainer stoc = getStoreContainerForSid(sid);\r
-      if (stoc != null)\r
-        storeObject = stoc.use(sid);\r
-      else\r
-        logger.warn("container not found for: " + sid.toString());\r
-      if (storeObject != null) {\r
-        storeHit++;\r
-        return storeObject;\r
-      }\r
-    }\r
-    storeMiss++;\r
-    return null;\r
-\r
-  }\r
-\r
-  /**\r
-   *  Method:       add\r
-   *  Description:  A StoreIdentifier is added to the ObjectStore, if it\r
-   *                contains a reference to a @see StorableObject.\r
-   */\r
-  public void add(StoreIdentifier sid) {\r
-    if (sid != null && sid.hasReference()) {\r
-      // find the right StoreContainer for sid\r
-      StoreContainer stoc = getStoreContainerForSid(sid);\r
-      if (stoc == null) {\r
-        // we have to make new StoreContainer\r
-        StoreContainerType stocType = sid.getStoreContainerType();\r
-        stoc = new StoreContainer(stocType);\r
-        containerMap.put(stocType, stoc);\r
-      }\r
-      stoc.add(sid);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *  Method:       invalidate(StorableObject sto)\r
-   *  Description:  ObjectStore is notified of change of a @see StorableObject\r
-   *                sto and invalidates all relevant cache entries.\r
-   */\r
-\r
-  public void invalidate(StoreIdentifier sid) {\r
-    // propagate invalidation to StoreContainer\r
-    if (sid != null) {\r
-      StoreContainer stoc = getStoreContainerForSid(sid);\r
-      stoc.invalidate(sid);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *  Method:       invalidate(StoreContainerType)\r
-   *  Description:  serves to invalidate a whole StoreContainer\r
-   *\r
-   *  @return\r
-   */\r
-  public void invalidate(StoreContainerType stoc_type) {\r
-    if (stoc_type != null) {\r
-      /** @todo invalidates too much:\r
-       *  improvement: if instanceof StoreContainerEntity && EntityList\r
-       *  then invalidate only StoreIdentifier matching the right table\r
-       */\r
-      StoreContainer stoc = getStoreContainerForStocType(stoc_type);\r
-      if (stoc != null)\r
-        stoc.invalidate();\r
-    }\r
-\r
-  }\r
-\r
-  // internal methods for StoreContainer managment\r
-\r
-  /**\r
-   *  Method:       getStoreContainerForSid\r
-   *  Description:  private method to find the right @see StoreContainer for\r
-   *                the @see StoreIdentifier sid.\r
-   *\r
-   *  @return       StoreContainer is null when no Container is found.\r
-   */\r
-  private StoreContainer getStoreContainerForSid(StoreIdentifier sid) {\r
-    // find apropriate container for a specific sid\r
-    if (sid != null) {\r
-      StoreContainerType stoc_type = sid.getStoreContainerType();\r
-      return getStoreContainerForStocType(stoc_type);\r
-    }\r
-    return null;\r
-  }\r
-\r
-  private StoreContainer getStoreContainerForStocType(StoreContainerType\r
-      stoc_type) {\r
-    if (stoc_type != null && containerMap.containsKey(stoc_type))\r
-      return (StoreContainer) containerMap.get(stoc_type);\r
-    return null;\r
-  }\r
-\r
-  private boolean has(StoreIdentifier sid) {\r
-    StoreContainer stoc = getStoreContainerForSid(sid);\r
-    return (stoc != null && stoc.has(sid)) ? true : false;\r
-  }\r
-\r
-  public String getConfProperty(String name) {\r
-    if (name != null) {\r
-      String returnValue = "";\r
-      try {\r
-        return ostoreConf.getProperty(name);\r
-      }\r
-      catch (MissingResourceException e) {\r
-        logger.error("getConfProperty: " + e.toString());\r
-      }\r
-    }\r
-    return null;\r
-  }\r
-\r
-  /**\r
-   *  Method:       toString()\r
-   *  Description:  Displays statistical information about the ObjectStore.\r
-       *                Further information is gathered from all @see StoreContainer\r
-   *\r
-   *  @return       String\r
-   */\r
-  public String toString() {\r
-    return toHtml(null);\r
-  }\r
-\r
-  public String toHtml(HttpServletRequest req) {\r
-    float hitRatio = 0;\r
-    long divisor = storeHit + storeMiss;\r
-    if (divisor > 0)\r
-      hitRatio = (float) storeHit / (float) divisor;\r
-    hitRatio *= 100;\r
-\r
-    StringBuffer sb = new StringBuffer("Mir-ObjectStore ");\r
-    sb.append( ( (req != null) ? html_version() : version())).append("\n");\r
-    sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);\r
-    sb.append("/").append(storeMiss).append("/").append(hitRatio);\r
-    sb.append("%\nCurrently ").append(containerMap.size());\r
-    sb.append(" StoreContainer in use - listing information:\n");\r
-\r
-    // ask container for information\r
-    StoreContainer currentStoc;\r
-    for (Iterator it = containerMap.keySet().iterator(); it.hasNext(); ) {\r
-      currentStoc = (StoreContainer) containerMap.get(it.next());\r
-      sb.append(currentStoc.toHtml(req));\r
-    }\r
-\r
-    return sb.toString();\r
-  }\r
-\r
-  /**\r
-   *  Method:       html_version()\r
-       *  Description:  returns ObjectStore version as String for HTML representation\r
-   *\r
-   *  @return       String\r
-   */\r
-  private String html_version() {\r
-    return "<i>" + version() + "</i>";\r
-  }\r
-\r
-  /**\r
-   *  Method:       version()\r
-   *  Description:  returns ObjectStore version as String\r
-   *\r
-   *  @return       String\r
-   */\r
-  private String version() {\r
-    return "v_sstart3__1.0";\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 mir.storage.store;
+
+/**
+ * Title:         ObjectStore for StorableObjects
+ * Description:   ObjectStore holds a Map of @see StoreContainer for all possible
+ *                @see StoreIdentifier.
+ *
+ *                @see StorageIdentifier - identitfies one object in the ObjectStore
+ *                      i.e. in its apropriate bucket. It holds a unique identifier
+ *                      of a StorableObject and a reference on the StorableObject.
+ *
+ *                @see StoreContainer - "Buckets" to store different types of Objects
+ *                      in one Container. These buckets are cofigurable via
+ *                      config.properties.
+ *
+ *                @see StoreContainerType - is a signature for all StoreContainer
+ *                      and StoreIdentifier.
+ *
+ *                @see StorableObjects - Interface Object have to implement to
+ *                      be handled by the ObjectStore
+ *
+ *                @see ServletStoreInfo - Maintenance Servlet for ObjectStore.
+ *                      Displays information about current content of the
+ *                      ObjectStore.
+ *
+ *
+ * Copyright:     Copyright (c) 2002
+ * Company:       indy
+ * @author        rk
+ * @version 1.0
+ */
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.MissingResourceException;
+import javax.servlet.http.HttpServletRequest;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerWrapper;
+
+public class ObjectStore {
+
+  private final static ObjectStore INSTANCE = new ObjectStore();
+  private final static Map containerMap = new HashMap(); // StoreContainerType/StoreContainer
+  private static long storeHit = 0, storeMiss = 0;
+  private MirPropertiesConfiguration configuration;
+  private LoggerWrapper logger;
+
+  private ObjectStore() {
+    logger = new LoggerWrapper("Database.ObjectStore");
+    configuration = MirPropertiesConfiguration.instance();
+  }
+
+  public static ObjectStore getInstance() {
+    return INSTANCE;
+  }
+
+  /**
+   *  Method:       use
+   *  Description:  The ObjectStore tries to find the @see StoreIdentifier sid
+   *                and returns the stored Object.
+   *
+   *  @return       StorableObject is null when no StorableObject for the
+   *                StoreIdentifier sid is found.
+   */
+  public StorableObject use(StoreIdentifier sid) {
+    if (sid != null) {
+      StorableObject storeObject = null;
+      StoreContainer stoc = getStoreContainerForSid(sid);
+      if (stoc != null)
+        storeObject = stoc.use(sid);
+      else
+        logger.warn("container not found for: " + sid.toString());
+      if (storeObject != null) {
+        storeHit++;
+        return storeObject;
+      }
+    }
+    storeMiss++;
+    return null;
+
+  }
+
+  /**
+   *  Method:       add
+   *  Description:  A StoreIdentifier is added to the ObjectStore, if it
+   *                contains a reference to a @see StorableObject.
+   */
+  public void add(StoreIdentifier sid) {
+    if (sid != null && sid.hasReference()) {
+      // find the right StoreContainer for sid
+      StoreContainer stoc = getStoreContainerForSid(sid);
+      if (stoc == null) {
+        // we have to make new StoreContainer
+        StoreContainerType stocType = sid.getStoreContainerType();
+        stoc = new StoreContainer(stocType);
+        containerMap.put(stocType, stoc);
+      }
+      stoc.add(sid);
+    }
+  }
+
+  /**
+   *  Method:       invalidate(StorableObject sto)
+   *  Description:  ObjectStore is notified of change of a @see StorableObject
+   *                sto and invalidates all relevant cache entries.
+   */
+
+  public void invalidate(StoreIdentifier sid) {
+    // propagate invalidation to StoreContainer
+    if (sid != null) {
+      StoreContainer stoc = getStoreContainerForSid(sid);
+      stoc.invalidate(sid);
+    }
+  }
+
+  /**
+   * serves to invalidate a whole StoreContainer
+   */
+  public void invalidate(StoreContainerType stoc_type) {
+    if (stoc_type != null) {
+      /*  @todo invalidates too much:
+       *  improvement: if instanceof StoreContainerEntity && EntityList
+       *  then invalidate only StoreIdentifier matching the right table
+       */
+      StoreContainer stoc = getStoreContainerForStocType(stoc_type);
+      if (stoc != null)
+        stoc.invalidate();
+    }
+
+  }
+
+  // internal methods for StoreContainer managment
+
+  /**
+   *  Method:       getStoreContainerForSid
+   *  Description:  private method to find the right @see StoreContainer for
+   *                the @see StoreIdentifier sid.
+   *
+   *  @return       StoreContainer is null when no Container is found.
+   */
+  private StoreContainer getStoreContainerForSid(StoreIdentifier sid) {
+    // find apropriate container for a specific sid
+    if (sid != null) {
+      StoreContainerType stoc_type = sid.getStoreContainerType();
+      return getStoreContainerForStocType(stoc_type);
+    }
+    return null;
+  }
+
+  private StoreContainer getStoreContainerForStocType(StoreContainerType
+      stoc_type) {
+    if (stoc_type != null && containerMap.containsKey(stoc_type))
+      return (StoreContainer) containerMap.get(stoc_type);
+    return null;
+  }
+
+  private boolean has(StoreIdentifier sid) {
+    StoreContainer stoc = getStoreContainerForSid(sid);
+    return (stoc != null && stoc.has(sid)) ? true : false;
+  }
+
+  public String getConfProperty(String name) {
+    if (name != null) {
+      try {
+        return configuration.getString(name);
+      }
+      catch (MissingResourceException e) {
+        logger.error("getConfProperty: " + e.toString());
+      }
+    }
+    return null;
+  }
+
+  /**
+   *  Method:       toString()
+   *  Description:  Displays statistical information about the ObjectStore.
+       *                Further information is gathered from all @see StoreContainer
+   *
+   *  @return       String
+   */
+  public String toString() {
+    return toHtml(null);
+  }
+
+  public String toHtml(HttpServletRequest req) {
+    float hitRatio = 0;
+    long divisor = storeHit + storeMiss;
+    if (divisor > 0)
+      hitRatio = (float) storeHit / (float) divisor;
+    hitRatio *= 100;
+
+    StringBuffer sb = new StringBuffer("Mir-ObjectStore ");
+    sb.append( ( (req != null) ? html_version() : version())).append("\n");
+    sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);
+    sb.append("/").append(storeMiss).append("/").append(hitRatio);
+    sb.append("%\nCurrently ").append(containerMap.size());
+    sb.append(" StoreContainer in use - listing information:\n");
+
+    // ask container for information
+    StoreContainer currentStoc;
+    for (Iterator it = containerMap.keySet().iterator(); it.hasNext(); ) {
+      currentStoc = (StoreContainer) containerMap.get(it.next());
+      sb.append(currentStoc.toHtml(req));
+    }
+
+    return sb.toString();
+  }
+
+  /**
+   *  Method:       html_version()
+       *  Description:  returns ObjectStore version as String for HTML representation
+   *
+   *  @return       String
+   */
+  private String html_version() {
+    return "<i>" + version() + "</i>";
+  }
+
+  /**
+   *  Method:       version()
+   *  Description:  returns ObjectStore version as String
+   *
+   *  @return       String
+   */
+  private String version() {
+    return "v_sstart3__1.0";
+  }
+
 }
\ No newline at end of file