Mir goes GPL
[mir.git] / source / mir / storage / store / ObjectStore.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mir.storage.store;
33
34 /**
35  * Title:         ObjectStore for StorableObjects
36  * Description:   ObjectStore holds a Map of @see StoreContainer for all possible
37  *                @see StoreIdentifier.
38  *
39  *                @see StorageIdentifier - identitfies one object in the ObjectStore
40  *                      i.e. in its apropriate bucket. It holds a unique identifier
41  *                      of a StorableObject and a reference on the StorableObject.
42  *
43  *                @see StoreContainer - "Buckets" to store different types of Objects
44  *                      in one Container. These buckets are cofigurable via
45  *                      config.properties.
46  *
47  *                @see StoreContainerType - is a signature for all StoreContainer
48  *                      and StoreIdentifier.
49  *
50  *                @see StorableObjects - Interface Object have to implement to
51  *                      be handled by the ObjectStore
52  *
53  *                @see ServletStoreInfo - Maintenance Servlet for ObjectStore.
54  *                      Displays information about current content of the
55  *                      ObjectStore.
56  *
57  *
58  * Copyright:     Copyright (c) 2002
59  * Company:       indy
60  * @author        rk
61  * @version 1.0
62  */
63
64 import java.util.*;
65 import javax.servlet.http.*;
66 import javax.servlet.*;
67 import mir.misc.Logfile;
68
69 public class ObjectStore {
70
71         private final static ObjectStore    INSTANCE=new ObjectStore();
72         private final static HashMap        containerMap=new HashMap(); // StoreContainerType/StoreContainer
73         private static Logfile              storeLog;
74         private static long                 storeHit=0,storeMiss=0;
75   private ResourceBundle              ostoreConf;
76
77         private ObjectStore() {
78     ostoreConf = ResourceBundle.getBundle("objectstore");
79     if ( ostoreConf == null )
80       System.err.println("FATAL: could not find objectstore.properties");
81         }
82         public static ObjectStore getInstance() { return INSTANCE; }
83
84
85         /**
86          *  Method:       use
87          *  Description:  The ObjectStore tries to find the @see StoreIdentifier sid
88          *                and returns the stored Object.
89          *
90          *  @return       StorableObject is null when no StorableObject for the
91          *                StoreIdentifier sid is found.
92          */
93         public StorableObject use(StoreIdentifier sid) {
94     if (sid!=null ) {
95       StorableObject storeObject=null;
96       StoreContainer stoc = getStoreContainerForSid( sid );
97       if (stoc!=null) storeObject=stoc.use(sid);
98       else System.out.println("Warning: container not found for: " + sid.toString());
99       if (storeObject!=null) {
100         storeHit++;
101                     return storeObject;
102       }
103     }
104     storeMiss++; return null;
105
106         }
107
108         /**
109          *  Method:       add
110          *  Description:  A StoreIdentifier is added to the ObjectStore, if it
111          *                contains a reference to a @see StorableObject.
112          */
113         public void add(StoreIdentifier sid) {
114                 if ( sid!=null && sid.hasReference() ) {
115                         // find the right StoreContainer for sid
116                         StoreContainer stoc = getStoreContainerForSid(sid);
117                         if (stoc==null) {
118                                 // we have to make new StoreContainer
119                                 StoreContainerType stocType = sid.getStoreContainerType();
120                                 stoc = new StoreContainer(stocType);
121                                 containerMap.put(stocType, stoc);
122                         }
123                         stoc.add(sid);
124                 }
125         }
126
127         /**
128          *  Method:       invalidate(StorableObject sto)
129          *  Description:  ObjectStore is notified of change of a @see StorableObject
130          *                sto and invalidates all relevant cache entries.
131          */
132
133         public void invalidate(StoreIdentifier sid) {
134                 // propagate invalidation to StoreContainer
135                 if (sid!=null) {
136       StoreContainer stoc = getStoreContainerForSid(sid);
137       stoc.invalidate(sid);
138                 }
139         }
140
141   /**
142    *  Method:       invalidate(StoreContainerType)
143    *  Description:  serves to invalidate a whole StoreContainer
144    *
145    *  @return
146    */
147   public void invalidate(StoreContainerType stoc_type) {
148     if ( stoc_type != null ) {
149       /** @todo invalidates too much:
150        *  improvement: if instanceof StoreContainerEntity && EntityList
151        *  then invalidate only StoreIdentifier matching the right table
152        */
153       StoreContainer stoc = getStoreContainerForStocType(stoc_type);
154       if ( stoc!=null )
155         stoc.invalidate();
156     }
157
158   }
159
160         // internal methods for StoreContainer managment
161
162         /**
163          *  Method:       getStoreContainerForSid
164          *  Description:  private method to find the right @see StoreContainer for
165          *                the @see StoreIdentifier sid.
166          *
167          *  @return       StoreContainer is null when no Container is found.
168          */
169         private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
170                 // find apropriate container for a specific sid
171                 if (sid!=null) {
172                         StoreContainerType stoc_type = sid.getStoreContainerType();
173                         return getStoreContainerForStocType(stoc_type);
174                 }
175                 return null;
176         }
177
178   private StoreContainer getStoreContainerForStocType(StoreContainerType stoc_type) {
179     if ( stoc_type!=null && containerMap.containsKey(stoc_type) )
180                                 return (StoreContainer)containerMap.get(stoc_type);
181     return null;
182   }
183
184         private boolean has(StoreIdentifier sid) {
185                 StoreContainer stoc = getStoreContainerForSid( sid );
186                 return ( stoc != null && stoc.has(sid) ) ? true:false;
187         }
188
189   public String getConfProperty(String name) {
190     if (name!=null ) {
191       String returnValue="";
192       try {
193         return ostoreConf.getString(name);
194       }
195       catch (MissingResourceException e) {
196         System.err.println("ObjectStore: " + e.toString());
197       }
198     }
199     return null;
200   }
201
202         /**
203          *  Method:       toString()
204          *  Description:  Displays statistical information about the ObjectStore.
205          *                Further information is gathered from all @see StoreContainer
206          *
207          *  @return       String
208          */
209         public String toString() {
210           return toHtml(null);
211   }
212
213   public String toHtml(HttpServletRequest req) {
214     float hitRatio=0;
215     long divisor=storeHit+storeMiss;
216     if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
217     hitRatio*=100;
218
219     StringBuffer sb = new StringBuffer("Mir-ObjectStore ");
220                 sb.append( ((req!=null) ? html_version():version()) ).append("\n");
221                 sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);
222                 sb.append("/").append(storeMiss).append("/").append(hitRatio);
223                 sb.append("%\nCurrently ").append(containerMap.size());
224                 sb.append(" StoreContainer in use - listing information:\n");
225
226                 // ask container for information
227                 StoreContainer currentStoc;
228                 for(Iterator it=containerMap.keySet().iterator();it.hasNext();) {
229                         currentStoc=(StoreContainer)containerMap.get(it.next());
230                         sb.append(currentStoc.toHtml(req));
231                 }
232
233                 return sb.toString();
234         }
235
236
237   /**
238          *  Method:       html_version()
239          *  Description:  returns ObjectStore version as String for HTML representation
240          *
241          *  @return       String
242          */
243   private String html_version() { return "<i>"+version()+"</i>"; }
244
245         /**
246          *  Method:       version()
247          *  Description:  returns ObjectStore version as String
248          *
249          *  @return       String
250          */
251         private String version() { return "v_sstart3__1.0"; }
252
253 }