data. ... got lost
[mir.git] / source / mir / storage / store / ObjectStore.java
1 package mir.storage.store;
2
3 /**
4  * Title:         ObjectStore for StorableObjects
5  * Description:   ObjectStore holds a Map of @see StoreContainer for all possible
6  *                @see StoreIdentifier.
7  *
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.
11  *
12  *                @see StoreContainer - "Buckets" to store different types of Objects
13  *                      in one Container. These buckets are cofigurable via
14  *                      config.properties.
15  *
16  *                @see StoreContainerType - is a signature for all StoreContainer
17  *                      and StoreIdentifier.
18  *
19  *                @see StorableObjects - Interface Object have to implement to
20  *                      be handled by the ObjectStore
21  *
22  *                @see ServletStoreInfo - Maintenance Servlet for ObjectStore.
23  *                      Displays information about current content of the
24  *                      ObjectStore.
25  *
26  *
27  * Copyright:     Copyright (c) 2002
28  * Company:       indy
29  * @author        rk
30  * @version 1.0
31  */
32
33 import java.util.*;
34 import javax.servlet.http.*;
35 import javax.servlet.*;
36 import mir.misc.Logfile;
37
38 public class ObjectStore {
39
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;
45
46         private ObjectStore() {
47     ostoreConf = ResourceBundle.getBundle("objectstore");
48     if ( ostoreConf == null )
49       System.err.println("FATAL: could not find objectstore.properties");
50         }
51         public static ObjectStore getInstance() { return INSTANCE; }
52
53
54         /**
55          *  Method:       use
56          *  Description:  The ObjectStore tries to find the @see StoreIdentifier sid
57          *                and returns the stored Object.
58          *
59          *  @return       StorableObject is null when no StorableObject for the
60          *                StoreIdentifier sid is found.
61          */
62         public StorableObject use(StoreIdentifier sid) {
63     if (sid!=null ) {
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) {
69         storeHit++;
70                     return storeObject;
71       }
72     }
73     storeMiss++; return null;
74
75         }
76
77         /**
78          *  Method:       add
79          *  Description:  A StoreIdentifier is added to the ObjectStore, if it
80          *                contains a reference to a @see StorableObject.
81          */
82         public void add(StoreIdentifier sid) {
83                 if ( sid!=null && sid.hasReference() ) {
84                         // find the right StoreContainer for sid
85                         StoreContainer stoc = getStoreContainerForSid(sid);
86                         if (stoc==null) {
87                                 // we have to make new StoreContainer
88                                 StoreContainerType stocType = sid.getStoreContainerType();
89                                 stoc = new StoreContainer(stocType);
90                                 containerMap.put(stocType, stoc);
91                         }
92                         stoc.add(sid);
93                 }
94         }
95
96         /**
97          *  Method:       invalidate(StorableObject sto)
98          *  Description:  ObjectStore is notified of change of a @see StorableObject
99          *                sto and invalidates all relevant cache entries.
100          */
101
102         public void invalidate(StoreIdentifier sid) {
103                 // propagate invalidation to StoreContainer
104                 if (sid!=null) {
105       StoreContainer stoc = getStoreContainerForSid(sid);
106       stoc.invalidate(sid);
107                 }
108         }
109
110   /**
111    *  Method:       invalidate(StoreContainerType)
112    *  Description:  serves to invalidate a whole StoreContainer
113    *
114    *  @return
115    */
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
121        */
122       StoreContainer stoc = getStoreContainerForStocType(stoc_type);
123       if ( stoc!=null )
124         stoc.invalidate();
125     }
126
127   }
128
129         // internal methods for StoreContainer managment
130
131         /**
132          *  Method:       getStoreContainerForSid
133          *  Description:  private method to find the right @see StoreContainer for
134          *                the @see StoreIdentifier sid.
135          *
136          *  @return       StoreContainer is null when no Container is found.
137          */
138         private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
139                 // find apropriate container for a specific sid
140                 if (sid!=null) {
141                         StoreContainerType stoc_type = sid.getStoreContainerType();
142                         return getStoreContainerForStocType(stoc_type);
143                 }
144                 return null;
145         }
146
147   private StoreContainer getStoreContainerForStocType(StoreContainerType stoc_type) {
148     if ( stoc_type!=null && containerMap.containsKey(stoc_type) )
149                                 return (StoreContainer)containerMap.get(stoc_type);
150     return null;
151   }
152
153         private boolean has(StoreIdentifier sid) {
154                 StoreContainer stoc = getStoreContainerForSid( sid );
155                 return ( stoc != null && stoc.has(sid) ) ? true:false;
156         }
157
158   public String getConfProperty(String name) {
159     if (name!=null ) {
160       String returnValue="";
161       try {
162         return ostoreConf.getString(name);
163       }
164       catch (MissingResourceException e) {
165         System.err.println("ObjectStore: " + e.toString());
166       }
167     }
168     return null;
169   }
170
171         /**
172          *  Method:       toString()
173          *  Description:  Displays statistical information about the ObjectStore.
174          *                Further information is gathered from all @see StoreContainer
175          *
176          *  @return       String
177          */
178         public String toString() {
179           return toHtml(null);
180   }
181
182   public String toHtml(HttpServletRequest req) {
183     float hitRatio=0;
184     long divisor=storeHit+storeMiss;
185     if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
186     hitRatio*=100;
187
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");
194
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));
200                 }
201
202                 return sb.toString();
203         }
204
205
206   /**
207          *  Method:       html_version()
208          *  Description:  returns ObjectStore version as String for HTML representation
209          *
210          *  @return       String
211          */
212   private String html_version() { return "<i>"+version()+"</i>"; }
213
214         /**
215          *  Method:       version()
216          *  Description:  returns ObjectStore version as String
217          *
218          *  @return       String
219          */
220         private String version() { return "v_sstart3__1.0"; }
221
222 }