1 package mir.storage.store;
4 * Title: ObjectStore for StorableObjects
5 * Description: ObjectStore holds a Map of @see StoreContainer for all possible
6 * @see StoreIdentifier.
8 * @see StorageIdentifier - identitfies one object in the ObjectStore
9 * i.e. in its apropriate bucket. It holds a unique identifier
10 * of a StorableObject and a reference on the StorableObject.
12 * @see StoreContainer - "Buckets" to store different types of Objects
13 * in one Container. These buckets are cofigurable via
16 * @see StoreContainerType - is a signature for all StoreContainer
17 * and StoreIdentifier.
19 * @see StorableObjects - Interface Object have to implement to
20 * be handled by the ObjectStore
22 * @see ServletStoreInfo - Maintenance Servlet for ObjectStore.
23 * Displays information about current content of the
27 * Copyright: Copyright (c) 2002
34 import mir.misc.Logfile;
36 public class ObjectStore {
38 private static ObjectStore INSTANCE=new ObjectStore();
39 private static HashMap containerMap=new HashMap(); // StoreContainerType/StoreContainer
40 private static Logfile storeLog;
41 private static long storeHit=0,storeMiss=0;
42 private static Class storableObjectInterface=StorableObject.class;
44 private ObjectStore() {
46 public static ObjectStore getInstance() { return INSTANCE; }
51 * Description: The ObjectStore tries to find the @see StoreIdentifier sid
52 * and returns the stored Object.
54 * @return StorableObject is null when no StorableObject for the
55 * StoreIdentifier sid is found.
57 public StorableObject use(StoreIdentifier sid) {
58 StorableObject storeObject=null;
59 StoreContainer stoc = getStoreContainerForSid( sid );
60 if (stoc!=null) storeObject=stoc.use(sid);
61 if (storeObject==null) storeMiss++; else storeHit++;
67 * Description: A StoreIdentifier is added to the ObjectStore, if it
68 * contains a reference to a @see StorableObject.
70 public void add(StoreIdentifier sid) {
71 if ( sid!=null && sid.hasReference() ) {
72 // find the right StoreContainer for sid
73 StoreContainer stoc = getStoreContainerForSid(sid);
75 // we have to make new StoreContainer
76 StoreContainerType stocType = sid.getStoreContainerType();
77 stoc = new StoreContainer(stocType);
78 containerMap.put(stocType, stoc);
86 * Description: Displays statistical information about the ObjectStore.
87 * Further information is gathered from all @see StoreContainer
91 public String toString() {
93 StringBuffer sb = new StringBuffer("Mir-ObjectStore v_");
94 sb.append(version()).append("\n");
95 sb.append("\nObjectStore hits : ").append(storeHit);
96 sb.append("\nObjectStore misses: ").append(storeMiss);
97 sb.append("\nCurrently ").append(containerMap.size());
98 sb.append("\nStoreContainer in use - listing information:\n");
100 // ask container for information
101 StoreContainer currentStoc;
102 for(Iterator it=containerMap.keySet().iterator();it.hasNext();) {
103 currentStoc=(StoreContainer)it.next();
104 sb.append(currentStoc.toString());
107 return sb.toString();
111 * Method: invalidate(StorableObject sto)
112 * Description: ObjectStore is notified of change of a @see StorableObject
113 * sto and invalidates all relevant cache entries.
116 public void invalidate(StorableObject sto) {
117 // propagate invalidation to StoreContainer
119 StoreIdentifier sid = sto.getStoreIdentifier();
121 StoreContainer stoc = getStoreContainerForSid(sid);
122 stoc.invalidate(sto);
127 // internal methods for StoreContainer managment
130 * Method: getStoreContainerForSid
131 * Description: private method to find the right @see StoreContainer for
132 * the @see StoreIdentifier sid.
134 * @return StoreContainer is null when no Container is found.
136 private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
137 // find apropriate container for a specific sid
139 StoreContainerType stoc_type = sid.getStoreContainerType();
140 if ( containerMap.containsKey(stoc_type) )
141 return (StoreContainer)containerMap.get(stoc_type);
147 * Method: implementsStorableObject
148 * Description: internall helper method to find out if a class implements
149 * interface StorableObject.
151 * @return true if yes, otherwise no.
153 private final static boolean implementsStorableObject(Class aClass) {
155 Class[] interfaces = aClass.getInterfaces();
156 if (interfaces.length>0) {
157 for (int i=0;i<interfaces.length;i++) {
158 if (interfaces[i]==storableObjectInterface) return true;
166 private boolean has(StoreIdentifier sid) {
167 StoreContainer stoc = getStoreContainerForSid( sid );
168 return ( stoc != null && stoc.has(sid) ) ? true:false;
174 * Description: returns ObjectStore version as String
178 private String version() { return "prototype_daytwo";}