2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mir.storage.store;
35 * Title: ObjectStore for StorableObjects
36 * Description: ObjectStore holds a Map of @see StoreContainer for all possible
37 * @see StoreIdentifier.
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.
43 * @see StoreContainer - "Buckets" to store different types of Objects
44 * in one Container. These buckets are cofigurable via
47 * @see StoreContainerType - is a signature for all StoreContainer
48 * and StoreIdentifier.
50 * @see StorableObjects - Interface Object have to implement to
51 * be handled by the ObjectStore
53 * @see ServletStoreInfo - Maintenance Servlet for ObjectStore.
54 * Displays information about current content of the
58 * Copyright: Copyright (c) 2002
64 import java.io.BufferedInputStream;
65 import java.io.FileInputStream;
66 import java.util.HashMap;
67 import java.util.Iterator;
68 import java.util.MissingResourceException;
69 import java.util.Properties;
71 import javax.servlet.http.HttpServletRequest;
73 import mir.config.MirPropertiesConfiguration;
74 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
75 import mir.misc.Logfile;
77 public class ObjectStore {
79 private final static ObjectStore INSTANCE=new ObjectStore();
80 private final static HashMap containerMap=new HashMap(); // StoreContainerType/StoreContainer
81 private static Logfile storeLog;
82 private static long storeHit=0,storeMiss=0;
83 private Properties ostoreConf;
85 private ObjectStore() {
86 String confName = null;
89 MirPropertiesConfiguration.instance().getString("Home")
90 + "etc/objectstore.properties";
91 } catch (PropertiesConfigExc e) {
92 e.printStackTrace(System.err);
94 Properties conf = new Properties();
96 conf.load( new BufferedInputStream(new FileInputStream(confName)));
98 catch ( java.io.FileNotFoundException fnfe ) {
99 System.err.println("could not read config file. not found: "+confName);
101 catch ( java.io.IOException ioex ) {
102 System.err.println("could not read config file: "+confName);
106 public static ObjectStore getInstance() { return INSTANCE; }
111 * Description: The ObjectStore tries to find the @see StoreIdentifier sid
112 * and returns the stored Object.
114 * @return StorableObject is null when no StorableObject for the
115 * StoreIdentifier sid is found.
117 public StorableObject use(StoreIdentifier sid) {
119 StorableObject storeObject=null;
120 StoreContainer stoc = getStoreContainerForSid( sid );
121 if (stoc!=null) storeObject=stoc.use(sid);
122 else System.out.println("Warning: container not found for: " + sid.toString());
123 if (storeObject!=null) {
128 storeMiss++; return null;
134 * Description: A StoreIdentifier is added to the ObjectStore, if it
135 * contains a reference to a @see StorableObject.
137 public void add(StoreIdentifier sid) {
138 if ( sid!=null && sid.hasReference() ) {
139 // find the right StoreContainer for sid
140 StoreContainer stoc = getStoreContainerForSid(sid);
142 // we have to make new StoreContainer
143 StoreContainerType stocType = sid.getStoreContainerType();
144 stoc = new StoreContainer(stocType);
145 containerMap.put(stocType, stoc);
152 * Method: invalidate(StorableObject sto)
153 * Description: ObjectStore is notified of change of a @see StorableObject
154 * sto and invalidates all relevant cache entries.
157 public void invalidate(StoreIdentifier sid) {
158 // propagate invalidation to StoreContainer
160 StoreContainer stoc = getStoreContainerForSid(sid);
161 stoc.invalidate(sid);
166 * Method: invalidate(StoreContainerType)
167 * Description: serves to invalidate a whole StoreContainer
171 public void invalidate(StoreContainerType stoc_type) {
172 if ( stoc_type != null ) {
173 /** @todo invalidates too much:
174 * improvement: if instanceof StoreContainerEntity && EntityList
175 * then invalidate only StoreIdentifier matching the right table
177 StoreContainer stoc = getStoreContainerForStocType(stoc_type);
184 // internal methods for StoreContainer managment
187 * Method: getStoreContainerForSid
188 * Description: private method to find the right @see StoreContainer for
189 * the @see StoreIdentifier sid.
191 * @return StoreContainer is null when no Container is found.
193 private StoreContainer getStoreContainerForSid(StoreIdentifier sid){
194 // find apropriate container for a specific sid
196 StoreContainerType stoc_type = sid.getStoreContainerType();
197 return getStoreContainerForStocType(stoc_type);
202 private StoreContainer getStoreContainerForStocType(StoreContainerType stoc_type) {
203 if ( stoc_type!=null && containerMap.containsKey(stoc_type) )
204 return (StoreContainer)containerMap.get(stoc_type);
208 private boolean has(StoreIdentifier sid) {
209 StoreContainer stoc = getStoreContainerForSid( sid );
210 return ( stoc != null && stoc.has(sid) ) ? true:false;
213 public String getConfProperty(String name) {
215 String returnValue="";
217 return ostoreConf.getProperty(name);
219 catch (MissingResourceException e) {
220 System.err.println("ObjectStore: " + e.toString());
228 * Description: Displays statistical information about the ObjectStore.
229 * Further information is gathered from all @see StoreContainer
233 public String toString() {
237 public String toHtml(HttpServletRequest req) {
239 long divisor=storeHit+storeMiss;
240 if (divisor>0) hitRatio=(float)storeHit/(float)divisor;
243 StringBuffer sb = new StringBuffer("Mir-ObjectStore ");
244 sb.append( ((req!=null) ? html_version():version()) ).append("\n");
245 sb.append("ObjectStore overall hits/misses/ratio: ").append(storeHit);
246 sb.append("/").append(storeMiss).append("/").append(hitRatio);
247 sb.append("%\nCurrently ").append(containerMap.size());
248 sb.append(" StoreContainer in use - listing information:\n");
250 // ask container for information
251 StoreContainer currentStoc;
252 for(Iterator it=containerMap.keySet().iterator();it.hasNext();) {
253 currentStoc=(StoreContainer)containerMap.get(it.next());
254 sb.append(currentStoc.toHtml(req));
257 return sb.toString();
262 * Method: html_version()
263 * Description: returns ObjectStore version as String for HTML representation
267 private String html_version() { return "<i>"+version()+"</i>"; }
271 * Description: returns ObjectStore version as String
275 private String version() { return "v_sstart3__1.0"; }