froehlich 01/12/13 08:24:28 Modified: scratchpad/src/org/apache/cocoon/jispstore MRUMemoryStore.java JispFilesystemStore.java Log: added Parameterizable interface, and made MRUMemoryStore ready for the JispFilesystemStore Revision Changes Path 1.2 +67 -87 xml-cocoon2/scratchpad/src/org/apache/cocoon/jispstore/MRUMemoryStore.java Index: MRUMemoryStore.java =================================================================== RCS file: /home/cvs/xml-cocoon2/scratchpad/src/org/apache/cocoon/jispstore/MRUMemoryStore.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MRUMemoryStore.java 2001/12/13 00:04:34 1.1 +++ MRUMemoryStore.java 2001/12/13 16:24:28 1.2 @@ -7,18 +7,13 @@ *****************************************************************************/ package org.apache.cocoon.jispstore; -import org.apache.avalon.excalibur.collections.SynchronizedPriorityQueue; import org.apache.avalon.framework.activity.Disposable; -import org.apache.avalon.excalibur.collections.SynchronizedPriorityQueue; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; -import org.apache.avalon.framework.configuration.Configurable; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.context.Context; -import org.apache.avalon.framework.context.ContextException; -import org.apache.avalon.framework.context.Contextualizable; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.ParameterException; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; @@ -28,13 +23,7 @@ import org.apache.cocoon.components.store.Store; import org.apache.cocoon.components.store.StoreJanitor; import org.apache.cocoon.util.ClassUtils; -import org.apache.cocoon.util.IOUtils; -import org.apache.regexp.RE; -import org.apache.regexp.RESyntaxException; - -import java.io.File; -import java.io.IOException; -import java.net.URLEncoder; + import java.util.Enumeration; import java.util.Hashtable; import java.util.LinkedList; @@ -53,20 +42,19 @@ public final class MRUMemoryStore extends AbstractLoggable implements Store, - Configurable, + Parameterizable, ThreadSafe, Composable, Disposable { - private int maxobjects; - private boolean filesystem; - private Hashtable cache; - private LinkedList mrulist; - private File cachefile; - private Store fsstore; - private StoreJanitor storejanitor; - private FilesystemQueue filesystemQueue; - private ComponentManager manager; + private int mMaxobjects; + private boolean mFilesystem; + private Hashtable mCache; + private LinkedList mMRUList; + private Store mFsstore; + private StoreJanitor mStorejanitor; + private FilesystemQueue mFilesystemQueue; + private ComponentManager mComponetManager; /** * Get components of the ComponentManager @@ -74,50 +62,51 @@ * @param the ComponentManager */ public void compose(ComponentManager manager) throws ComponentException { - this.manager = manager; + this.mComponetManager = manager; getLogger().debug("Looking up " + Store.ROLE + "/JispFilesystemStore"); - this.fsstore = (Store)manager.lookup(Store.ROLE + "/JispFilesystemStore"); + this.mFsstore = (Store)manager.lookup(Store.ROLE + "/JispFilesystemStore"); getLogger().debug("Looking up " + StoreJanitor.ROLE); - this.storejanitor = (StoreJanitor)manager.lookup(StoreJanitor.ROLE); + this.mStorejanitor = (StoreJanitor)manager.lookup(StoreJanitor.ROLE); getLogger().debug("Looking up " + FilesystemQueue.ROLE); - this.filesystemQueue = (FilesystemQueue)manager.lookup(FilesystemQueue.ROLE); + this.mFilesystemQueue = (FilesystemQueue)manager.lookup(FilesystemQueue.ROLE); } /** - * Initialize the MRUMemoryStore. + * Configure the MRUMemoryStore. * A few options can be used : * <UL> - * <LI>maxobjects = how many objects will be stored in memory (Default: 10 objects)</LI> - * <LI>filesystem = use filesystem storage to keep object persistent (Default: false)</LI> + * <LI>mMaxobjects = how many objects will be stored in memory (Default: 10 objects)</LI> + * <LI>mFilesystem = use filesystem storage to keep object persistent (Default: false)</LI> * </UL> * - * @param the Configuration of the application - * @exception ConfigurationException + * @param the Parameters of the application + * @exception ParameterException */ - public void configure(Configuration conf) throws ConfigurationException { - Parameters params = Parameters.fromConfiguration(conf); - this.maxobjects = params.getParameterAsInteger("maxobjects",100); - this.filesystem = params.getParameterAsBoolean("filesystem",false); - if ((this.maxobjects < 1)) { - throw new ConfigurationException("MRUMemoryStore maxobjects must be at least 1 milli second!"); + public void parameterize(Parameters params) throws ParameterException { + this.mMaxobjects = params.getParameterAsInteger("maxobjects",100); + this.mFilesystem = params.getParameterAsBoolean("filesystem",false); + if ((this.mMaxobjects < 1)) { + throw new ParameterException("MRUMemoryStore maxobjects must be at least 1 milli second!"); } - this.cache = new Hashtable((int)(this.maxobjects * 1.2)); - this.mrulist = new LinkedList(); - this.storejanitor.register(this); + this.mCache = new Hashtable((int)(this.mMaxobjects * 1.2)); + this.mMRUList = new LinkedList(); + this.mStorejanitor.register(this); } /** * Dispose the component */ public void dispose() { - if (this.manager != null) { - this.manager.release(this.storejanitor); - this.storejanitor = null; - this.manager.release(this.filesystemQueue); - this.filesystemQueue = null; - this.manager.release(this.fsstore); - this.fsstore = null; + this.getLogger().debug("dispose()"); + + if (this.mComponetManager != null) { + this.mComponetManager.release(this.mStorejanitor); + this.mStorejanitor = null; + this.mComponetManager.release(this.mFilesystemQueue); + this.mFilesystemQueue = null; + this.mComponetManager.release(this.mFsstore); + this.mFsstore = null; } } @@ -146,28 +135,28 @@ getLogger().debug("Holding object in memory. value: " + value); /** ...first test if the max. objects in cache is reached... */ - while (this.mrulist.size() >= this.maxobjects) { + while (this.mMRUList.size() >= this.mMaxobjects) { /** ...ok, heapsize is reached, remove the last element... */ this.free(); } /** put the object on the filesystem */ - if(this.filesystem) { + if(this.mFilesystem) { if(this.checkSerializable(value) && - !this.fsstore.containsKey(key)) { + !this.mFsstore.containsKey(key)) { this.getLogger().debug("Storing object on fs"); try { - this.filesystemQueue.insert(new FilesystemQueueObject(key,value)); + this.mFilesystemQueue.insert(new FilesystemQueueObject(key,value)); } catch(Exception e) { this.getLogger().error("Error storing Object on fs",e); } } } /** ..put the new object in the cache, on the top of course ... */ - this.cache.put(key, value); - this.mrulist.remove(key); - this.mrulist.addFirst(key); - this.getLogger().debug("Cache size=" + cache.size()); + this.mCache.put(key, value); + this.mMRUList.remove(key); + this.mMRUList.addFirst(key); + this.getLogger().debug("Cache size=" + mCache.size()); } /** @@ -178,36 +167,27 @@ */ public Object get(Object key) { this.getLogger().debug("Getting object from memory. Key: " + key); - Object tmpobject = this.cache.get(key); + Object tmpobject = this.mCache.get(key); if ( tmpobject != null ) { /** put the accessed key on top of the linked list */ - this.mrulist.remove(key); - this.mrulist.addFirst(key); + this.mMRUList.remove(key); + this.mMRUList.addFirst(key); return tmpobject; } this.getLogger().debug("Object not found in memory"); /** try to fetch from filesystem */ - if(this.filesystem) { - tmpobject = this.fsstore.get(key); + if(this.mFilesystem) { + tmpobject = this.mFsstore.get(key); if (tmpobject == null) { this.getLogger().debug( "Object was NOT found on fs. Looked for: " + key); return null; } else { this.getLogger().debug("Object was found on fs"); - try { - tmpobject = IOUtils.deserializeObject((File)tmpobject); - if(!this.cache.containsKey(key)) { - this.hold(key,tmpobject); - } - return tmpobject; - } catch (ClassNotFoundException ce) { - this.getLogger().error("Error in get()!", ce); - return null; - } catch (IOException ioe) { - this.getLogger().error("Error in get()!", ioe); - return null; + if(!this.mCache.containsKey(key)) { + this.hold(key,tmpobject); } + return tmpobject; } } return null; @@ -220,10 +200,10 @@ */ public void remove(Object key) { this.getLogger().debug("Removing object from store"); - this.cache.remove(key); - this.mrulist.remove(key); - if(this.filesystem && key != null) { - this.fsstore.remove(key); + this.mCache.remove(key); + this.mMRUList.remove(key); + if(this.mFilesystem && key != null) { + this.mFsstore.remove(key); } } @@ -234,10 +214,10 @@ * @return true if the key exists */ public boolean containsKey(Object key) { - if(filesystem) { - return (this.cache.containsKey(key) || this.fsstore.containsKey(key)); + if(mFilesystem) { + return (this.mCache.containsKey(key) || this.mFsstore.containsKey(key)); } else { - return this.cache.containsKey(key); + return this.mCache.containsKey(key); } } @@ -247,7 +227,7 @@ * @return the enumeration of the cache */ public Enumeration keys() { - return null; + return this.mCache.keys(); } /** @@ -256,11 +236,11 @@ */ public void free() { try { - if(this.cache.size() > 0) { + if(this.mCache.size() > 0) { this.getLogger().debug("Freeing cache"); - this.cache.remove(this.mrulist.getLast()); - this.mrulist.removeLast(); - this.getLogger().debug("Cache size=" + cache.size()); + this.mCache.remove(this.mMRUList.getLast()); + this.mMRUList.removeLast(); + this.getLogger().debug("Cache size=" + mCache.size()); } } catch (Exception e) { this.getLogger().error("Error in free()", e); 1.4 +33 -18 xml-cocoon2/scratchpad/src/org/apache/cocoon/jispstore/JispFilesystemStore.java Index: JispFilesystemStore.java =================================================================== RCS file: /home/cvs/xml-cocoon2/scratchpad/src/org/apache/cocoon/jispstore/JispFilesystemStore.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JispFilesystemStore.java 2001/12/13 14:52:36 1.3 +++ JispFilesystemStore.java 2001/12/13 16:24:28 1.4 @@ -13,6 +13,9 @@ import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.avalon.framework.thread.ThreadSafe; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.parameters.Parameterizable; +import org.apache.avalon.framework.parameters.ParameterException; import org.apache.cocoon.Constants; import org.apache.cocoon.util.IOUtils; @@ -26,23 +29,27 @@ import java.io.Serializable; import java.util.Enumeration; +/** + * JispFilesystemStore + * + * @author <a href="mailto:[EMAIL PROTECTED]">Gerhard Froehlich</a> + */ public final class JispFilesystemStore extends AbstractLoggable implements Store, Contextualizable, ThreadSafe, - Initializable { + Initializable, + Parameterizable { /** The directory repository */ protected File mDirectoryFile; protected volatile String mDirectoryPath; - /** some statics for the moment */ - private static String DATABASE_NAME = "cocoon.data"; - private static String INDEX_NAME = "cocoon.idx"; - private static int ORDER = 1001; - /** The database */ + private String mDatabaseName; + private String mIndexName; + private int mOrder; private IndexedObjectDatabase mDatabase; private BTreeIndex mIndex; @@ -68,22 +75,20 @@ getLogger().debug("initialize() JispFilesystemStore"); try { - - getLogger().debug("Path to data file=" + this.getDirectoryPath() + DATABASE_NAME); - File myFile = new File(this.getDirectoryPath() + DATABASE_NAME); - getLogger().debug("File exists=" + myFile.exists()); + getLogger().debug("initialize(): Path to Datafile=" + this.getDirectoryPath() + mDatabaseName); + File myFile = new File(this.getDirectoryPath() + mDatabaseName); if (myFile.exists()) { - this.getLogger().debug("Data file exists"); - mDatabase = new IndexedObjectDatabase(getDirectoryPath() + DATABASE_NAME,false); - mIndex = new BTreeIndex(this.getDirectoryPath() + INDEX_NAME); + this.getLogger().debug("initialize(): Datafile exists"); + mDatabase = new IndexedObjectDatabase(getDirectoryPath() + mDatabaseName,false); + mIndex = new BTreeIndex(this.getDirectoryPath() + mIndexName); mDatabase.attachIndex(mIndex); mIndex.dumpTree(); } else { - this.getLogger().debug("Data file not exists"); - mDatabase = new IndexedObjectDatabase(getDirectoryPath() + DATABASE_NAME,false); - mIndex = new BTreeIndex(this.getDirectoryPath() + INDEX_NAME, - ORDER, new JispKey(),false); + this.getLogger().debug("initialize(): Datafile not exists"); + mDatabase = new IndexedObjectDatabase(getDirectoryPath() + mDatabaseName,false); + mIndex = new BTreeIndex(this.getDirectoryPath() + mIndexName, + mOrder, new JispKey(),false); mDatabase.attachIndex(mIndex); mIndex.dumpTree(); } @@ -92,6 +97,16 @@ } } + public void parameterize(Parameters params) throws ParameterException { + mDatabaseName = params.getParameter("datafile","cocoon.dat"); + mIndexName = params.getParameter("indexfile","cocoon.idx"); + mOrder = params.getParameterAsInteger("order",1001); + + this.getLogger().debug("parameterize(..): mDatabaseName=" + mDatabaseName); + this.getLogger().debug("parameterize(..): mIndexName=" + mIndexName); + this.getLogger().debug("parameterize(..): mOrder=" + mOrder); + } + /** * Sets the repository's location */ @@ -141,7 +156,7 @@ if(readObj != null) { this.getLogger().debug("get(): FOUND!!= " + readObj); } else { - this.getLogger().debug("get(): NOT FOUND!!= " + readObj); + this.getLogger().debug("get(): NOT_FOUND!!"); } } catch (Exception e) {
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]