contains only admin templates which should not be changed by users
[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 java.io.BufferedInputStream;
66 import java.io.FileInputStream;
67 import javax.servlet.http.*;
68 import javax.servlet.*;
69 import mir.misc.Logfile;
70 import mir.misc.MirConfig;
71
72 public class ObjectStore {
73
74         private final static ObjectStore    INSTANCE=new ObjectStore();
75         private final static HashMap        containerMap=new HashMap(); // StoreContainerType/StoreContainer
76         private static Logfile              storeLog;
77         private static long                 storeHit=0,storeMiss=0;
78   private Properties              ostoreConf;
79
80         private ObjectStore() {
81     String confName=MirConfig.getProp("Home")+"etc/objectstore.properties";
82     Properties conf = new Properties();
83     try {
84         conf.load( new BufferedInputStream(new FileInputStream(confName)));
85     }
86     catch ( java.io.FileNotFoundException fnfe ) {
87         System.err.println("could not read config file. not found: "+confName);
88     }
89     catch ( java.io.IOException ioex ) {
90         System.err.println("could not read config file: "+confName);
91     }
92     ostoreConf = conf;
93         }
94         public static ObjectStore getInstance() { return INSTANCE; }
95
96
97         /**
98          *  Method:       use
99          *  Description:  The ObjectStore tries to find the @see StoreIdentifier sid
100          *                and returns the stored Object.
101          *
102          *  @return       StorableObject is null when no StorableObject for the
103          *                StoreIdentifier sid is found.
104          */
105         public StorableObject use(StoreIdentifier sid) {
106     if (sid!=null ) {
107       StorableObject storeObject=null;
108       StoreContainer stoc = getStoreContainerForSid( sid );
109       if (stoc!=null) storeObject=stoc.use(sid);
110       else System.out.println("Warning: container not found for: " + sid.toString());
111       if (storeObject!=null) {
112         storeHit++;
113                     return storeObject;
114       }
115     }
116     storeMiss++; return null;
117
118         }
119
120         /**
121          *  Method:       add
122          *  Description:  A StoreIdentifier is added to the ObjectStore, if it
123          *                contains a reference to a @see StorableObject.
124          */
125         public void add(StoreIdentifier sid) {
126                 if ( sid!=null && sid.hasReference() ) {
127                         // find the right StoreContainer for sid
128                         StoreContainer stoc = getStoreContainerForSid(sid);
129                         if (stoc==null) {
130                                 // we have to make new StoreContainer
131                                 StoreContainerType stocType = sid.getStoreContainerType();
132                                 stoc = new StoreContainer(stocType);
133                                 containerMap.put(stocType, stoc);
134                         }
135                         stoc.add(sid);
136                 }
137         }
138
139         /**
140          *  Method:       invalidate(StorableObject sto)
141          *  Description:  ObjectStore is notified of change of a @see StorableObject
142          *                sto and invalidates all relevant cache entries.
143          */
144
145         public void invalidate(StoreIdentifier sid) {
146                 // propagate invalidation to StoreContainer
147                 if (sid!=null) {
148       StoreContainer stoc = getStoreContainerForSid(sid);
149       stoc.invalidate(sid);
150                 }
151         }
152
153   /**
154    *  Method:       invalidate(StoreContainerType)
155    *  Description:  serves to invalidate a whole StoreContainer
156    *
157    *  @return
158    */
159   public void invalidate(StoreContainerType stoc_type) {
160     if ( stoc_type != null ) {
161       /** @todo invalidates too much:
162        *  improvement: if instanceof StoreContainerEntity && EntityList
163        *  then invalidate only StoreIdentifier matching the right table
164        */
165       StoreContainer stoc = getStoreContainerForStocType(stoc_type);
166       if ( stoc!=null )
167         stoc.invalidate();
168     }
169
170   }
171
172         // internal methods for StoreContainer managment
173
174         /**
175          *  Method:       getStoreContainerForSid
176          *  Description:  private method to find the right @see StoreContainer for
177          *                the @see StoreIdentifier sid.
178          *
179          *  @return       StoreContainer is null when no Container is found.
180          */
181         private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
182                 // find apropriate container for a specific sid
183                 if (sid!=null) {
184                         StoreContainerType stoc_type = sid.getStoreContainerType();
185                         return getStoreContainerForStocType(stoc_type);
186                 }
187                 return null;
188         }
189
190   private StoreContainer getStoreContainerForStocType(StoreContainerType stoc_type) {
191     if ( stoc_type!=null && containerMap.containsKey(stoc_type) )
192                                 return (StoreContainer)containerMap.get(stoc_type);
193     return null;
194   }
195
196         private boolean has(StoreIdentifier sid) {
197                 StoreContainer stoc = getStoreContainerForSid( sid );
198                 return ( stoc != null && stoc.has(sid) ) ? true:false;
199         }
200
201   public String getConfProperty(String name) {
202     if (name!=null ) {
203       String returnValue="";
204       try {
205         return ostoreConf.getProperty(name);
206       }
207       catch (MissingResourceException e) {
208         System.err.println("ObjectStore: " + e.toString());
209       }
210     }
211     return null;
212   }
213
214         /**
215          *  Method:       toString()
216          *  Description:  Displays statistical information about the ObjectStore.
217          *                Further information is gathered from all @see StoreContainer
218          *
219          *  @return       String
220          */
221         public String toString() {
222           return toHtml(null);
223   }
224
225   public String toHtml(HttpServletRequest req) {
226     float hitRatio=0;
227     long divisor=storeHit+storeMiss;
228     if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
229     hitRatio*=100;
230
231     StringBuffer sb = new StringBuffer("Mir-ObjectStore ");
232                 sb.append( ((req!=null) ? html_version():version()) ).append("\n");
233                 sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);
234                 sb.append("/").append(storeMiss).append("/").append(hitRatio);
235                 sb.append("%\nCurrently ").append(containerMap.size());
236                 sb.append(" StoreContainer in use - listing information:\n");
237
238                 // ask container for information
239                 StoreContainer currentStoc;
240                 for(Iterator it=containerMap.keySet().iterator();it.hasNext();) {
241                         currentStoc=(StoreContainer)containerMap.get(it.next());
242                         sb.append(currentStoc.toHtml(req));
243                 }
244
245                 return sb.toString();
246         }
247
248
249   /**
250          *  Method:       html_version()
251          *  Description:  returns ObjectStore version as String for HTML representation
252          *
253          *  @return       String
254          */
255   private String html_version() { return "<i>"+version()+"</i>"; }
256
257         /**
258          *  Method:       version()
259          *  Description:  returns ObjectStore version as String
260          *
261          *  @return       String
262          */
263         private String version() { return "v_sstart3__1.0"; }
264
265 }