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 javax.servlet.http.*;
35 import javax.servlet.*;
36 import mir.misc.Logfile;
38 public class ObjectStore {
40 private final static ObjectStore INSTANCE=new ObjectStore();
41 private final static HashMap containerMap=new HashMap(); // StoreContainerType/StoreContainer
42 private static Logfile storeLog;
43 private static long storeHit=0,storeMiss=0;
44 private ResourceBundle ostoreConf;
46 private ObjectStore() {
47 ostoreConf = ResourceBundle.getBundle("objectstore");
48 if ( ostoreConf == null )
49 System.err.println("FATAL: could not find objectstore.properties");
51 public static ObjectStore getInstance() { return INSTANCE; }
56 * Description: The ObjectStore tries to find the @see StoreIdentifier sid
57 * and returns the stored Object.
59 * @return StorableObject is null when no StorableObject for the
60 * StoreIdentifier sid is found.
62 public StorableObject use(StoreIdentifier sid) {
64 StorableObject storeObject=null;
65 StoreContainer stoc = getStoreContainerForSid( sid );
66 if (stoc!=null) storeObject=stoc.use(sid);
67 else System.out.println("Warning: container not found for: " + sid.toString());
68 if (storeObject!=null) {
73 storeMiss++; return null;
79 * Description: A StoreIdentifier is added to the ObjectStore, if it
80 * contains a reference to a @see StorableObject.
82 public void add(StoreIdentifier sid) {
83 if ( sid!=null && sid.hasReference() ) {
84 // find the right StoreContainer for sid
85 StoreContainer stoc = getStoreContainerForSid(sid);
87 // we have to make new StoreContainer
88 StoreContainerType stocType = sid.getStoreContainerType();
89 stoc = new StoreContainer(stocType);
90 containerMap.put(stocType, stoc);
97 * Method: invalidate(StorableObject sto)
98 * Description: ObjectStore is notified of change of a @see StorableObject
99 * sto and invalidates all relevant cache entries.
102 public void invalidate(StoreIdentifier sid) {
103 // propagate invalidation to StoreContainer
105 StoreContainer stoc = getStoreContainerForSid(sid);
106 stoc.invalidate(sid);
111 * Method: invalidate(StoreContainerType)
112 * Description: serves to invalidate a whole StoreContainer
116 public void invalidate(StoreContainerType stoc_type) {
117 if ( stoc_type != null ) {
118 /** @todo invalidates too much:
119 * improvement: if instanceof StoreContainerEntity && EntityList
120 * then invalidate only StoreIdentifier matching the right table
122 StoreContainer stoc = getStoreContainerForStocType(stoc_type);
129 // internal methods for StoreContainer managment
132 * Method: getStoreContainerForSid
133 * Description: private method to find the right @see StoreContainer for
134 * the @see StoreIdentifier sid.
136 * @return StoreContainer is null when no Container is found.
138 private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
139 // find apropriate container for a specific sid
141 StoreContainerType stoc_type = sid.getStoreContainerType();
142 return getStoreContainerForStocType(stoc_type);
147 private StoreContainer getStoreContainerForStocType(StoreContainerType stoc_type) {
148 if ( stoc_type!=null && containerMap.containsKey(stoc_type) )
149 return (StoreContainer)containerMap.get(stoc_type);
153 private boolean has(StoreIdentifier sid) {
154 StoreContainer stoc = getStoreContainerForSid( sid );
155 return ( stoc != null && stoc.has(sid) ) ? true:false;
158 public String getConfProperty(String name) {
160 String returnValue="";
162 return ostoreConf.getString(name);
164 catch (MissingResourceException e) {
165 System.err.println("ObjectStore: " + e.toString());
173 * Description: Displays statistical information about the ObjectStore.
174 * Further information is gathered from all @see StoreContainer
178 public String toString() {
182 public String toHtml(HttpServletRequest req) {
184 long divisor=storeHit+storeMiss;
185 if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
188 StringBuffer sb = new StringBuffer("Mir-ObjectStore ");
189 sb.append( ((req!=null) ? html_version():version()) ).append("\n");
190 sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);
191 sb.append("/").append(storeMiss).append("/").append(hitRatio);
192 sb.append("%\nCurrently ").append(containerMap.size());
193 sb.append(" StoreContainer in use - listing information:\n");
195 // ask container for information
196 StoreContainer currentStoc;
197 for(Iterator it=containerMap.keySet().iterator();it.hasNext();) {
198 currentStoc=(StoreContainer)containerMap.get(it.next());
199 sb.append(currentStoc.toHtml(req));
202 return sb.toString();
207 * Method: html_version()
208 * Description: returns ObjectStore version as String for HTML representation
212 private String html_version() { return "<i>"+version()+"</i>"; }
216 * Description: returns ObjectStore version as String
220 private String version() { return "v_sstart3__1.0"; }