Mir goes GPL
[mir.git] / source / mir / storage / store / ObjectStore.java
index 0bf8dbd..ab83ab1 100755 (executable)
@@ -1,3 +1,34 @@
+/*
+ * 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 mir.storage.store;
 
 /**
@@ -31,17 +62,22 @@ package mir.storage.store;
  */
 
 import java.util.*;
+import javax.servlet.http.*;
+import javax.servlet.*;
 import mir.misc.Logfile;
 
 public class ObjectStore {
 
        private final static ObjectStore    INSTANCE=new ObjectStore();
        private final static HashMap        containerMap=new HashMap(); // StoreContainerType/StoreContainer
-       private final static Class          storableObjectInterface=StorableObject.class;
        private static Logfile              storeLog;
        private static long                 storeHit=0,storeMiss=0;
+  private ResourceBundle              ostoreConf;
 
        private ObjectStore() {
+    ostoreConf = ResourceBundle.getBundle("objectstore");
+    if ( ostoreConf == null )
+      System.err.println("FATAL: could not find objectstore.properties");
        }
        public static ObjectStore getInstance() { return INSTANCE; }
 
@@ -60,8 +96,10 @@ public class ObjectStore {
       StoreContainer stoc = getStoreContainerForSid( sid );
       if (stoc!=null) storeObject=stoc.use(sid);
       else System.out.println("Warning: container not found for: " + sid.toString());
-      if (storeObject!=null) storeHit++;
-                 return storeObject;
+      if (storeObject!=null) {
+        storeHit++;
+                   return storeObject;
+      }
     }
     storeMiss++; return null;
 
@@ -87,37 +125,6 @@ public class ObjectStore {
        }
 
        /**
-        *  Method:       toString()
-        *  Description:  Displays statistical information about the ObjectStore.
-        *                Further information is gathered from all @see StoreContainer
-        *
-        *  @return       String
-        */
-       public String toString() {
-
-    float hitRatio=0;
-    long divisor=storeHit+storeMiss;
-    if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
-    hitRatio*=100;
-
-    StringBuffer sb = new StringBuffer("Mir-ObjectStore v_");
-               sb.append(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.toString());
-               }
-
-               return sb.toString();
-       }
-
-       /**
         *  Method:       invalidate(StorableObject sto)
         *  Description:  ObjectStore is notified of change of a @see StorableObject
         *                sto and invalidates all relevant cache entries.
@@ -139,6 +146,10 @@ public class ObjectStore {
    */
   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();
@@ -170,37 +181,73 @@ public class ObjectStore {
     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 ) {
+      String returnValue="";
+      try {
+        return ostoreConf.getString(name);
+      }
+      catch (MissingResourceException e) {
+        System.err.println("ObjectStore: " + e.toString());
+      }
+    }
+    return null;
+  }
+
        /**
-        *  Method:       implementsStorableObject
-        *  Description:  internall helper method to find out if a class implements
-        *                interface StorableObject.
+        *  Method:       toString()
+        *  Description:  Displays statistical information about the ObjectStore.
+        *                Further information is gathered from all @see StoreContainer
         *
-        *  @return       true if yes, otherwise no.
+        *  @return       String
         */
-       private final static boolean implementsStorableObject(Class aClass) {
-               if (aClass!=null) {
-                       Class[] interfaces = aClass.getInterfaces();
-                       if (interfaces.length>0) {
-                               for (int i=0;i<interfaces.length;i++) {
-                                       if (interfaces[i]==storableObjectInterface) return true;
-                               }
-                       }
-               }
-               return false;
-       }
+       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;
 
-       private boolean has(StoreIdentifier sid) {
-               StoreContainer stoc = getStoreContainerForSid( sid );
-               return ( stoc != null && stoc.has(sid) ) ? true:false;
+    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 "00.d5";}
+       private String version() { return "v_sstart3__1.0"; }
+
 }
\ No newline at end of file