You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/11/07 05:58:39 UTC
cvs commit: jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl MRUMemoryStore.java
donaldp 2002/11/06 20:58:39
Modified: store/src/java/org/apache/excalibur/store/impl
MRUMemoryStore.java
Log:
Restyle.
Revision Changes Path
1.7 +192 -135 jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/MRUMemoryStore.java
Index: MRUMemoryStore.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/store/src/java/org/apache/excalibur/store/impl/MRUMemoryStore.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MRUMemoryStore.java 14 Aug 2002 15:33:53 -0000 1.6
+++ MRUMemoryStore.java 7 Nov 2002 04:58:39 -0000 1.7
@@ -7,16 +7,15 @@
*/
package org.apache.excalibur.store.impl;
-
import org.apache.avalon.framework.activity.Disposable;
-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.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
import org.apache.excalibur.store.Store;
import org.apache.excalibur.store.StoreJanitor;
@@ -37,31 +36,35 @@
* @version CVS $Id$
*/
public final class MRUMemoryStore
-extends AbstractLogEnabled
-implements Store, Parameterizable, Composable, Disposable, ThreadSafe {
-
- private int maxobjects;
- private boolean persistent;
- private Hashtable cache;
- private LinkedList mrulist;
- private Store persistentStore;
- private StoreJanitor storeJanitor;
- private ComponentManager manager;
+ extends AbstractLogEnabled
+ implements Store, Parameterizable, Serviceable, Disposable, ThreadSafe
+{
+ private int m_maxobjects;
+ private boolean m_persistent;
+ private Hashtable m_cache;
+ private LinkedList m_mrulist;
+ private Store m_persistentStore;
+ private StoreJanitor m_storeJanitor;
+ private ServiceManager m_manager;
/**
* Get components of the ComponentLocator
*
* @param manager The ComponentLocator
+ * @avalon.service interface="Store"
+ * @avalon.service interface="StoreJanitor"
*/
- public void compose(ComponentManager manager)
- throws ComponentException {
- this.manager = manager;
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Looking up " + Store.PERSISTENT_STORE);
- getLogger().debug("Looking up " + StoreJanitor.ROLE);
+ public void service( ServiceManager manager )
+ throws ServiceException
+ {
+ m_manager = manager;
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Looking up " + Store.PERSISTENT_STORE );
+ getLogger().debug( "Looking up " + StoreJanitor.ROLE );
}
- this.persistentStore = (Store)manager.lookup(Store.PERSISTENT_STORE);
- this.storeJanitor = (StoreJanitor)manager.lookup(StoreJanitor.ROLE);
+ m_persistentStore = (Store)manager.lookup( Store.PERSISTENT_STORE );
+ m_storeJanitor = (StoreJanitor)manager.lookup( StoreJanitor.ROLE );
}
/**
@@ -76,54 +79,67 @@
* @param params Store parameters
* @exception ParameterException
*/
- public void parameterize(Parameters params) throws ParameterException {
- this.maxobjects = params.getParameterAsInteger("maxobjects", 100);
- this.persistent = params.getParameterAsBoolean("use-persistent-cache", false);
- if ((this.maxobjects < 1)) {
- throw new ParameterException("MRUMemoryStore maxobjects must be at least 1!");
+ public void parameterize( Parameters params ) throws ParameterException
+ {
+ m_maxobjects = params.getParameterAsInteger( "maxobjects", 100 );
+ m_persistent = params.getParameterAsBoolean( "use-persistent-cache", false );
+ if( ( m_maxobjects < 1 ) )
+ {
+ throw new ParameterException( "MRUMemoryStore maxobjects must be at least 1!" );
}
- this.cache = new Hashtable((int)(this.maxobjects * 1.2));
- this.mrulist = new LinkedList();
- this.storeJanitor.register(this);
+ m_cache = new Hashtable( (int)( m_maxobjects * 1.2 ) );
+ m_mrulist = new LinkedList();
+ m_storeJanitor.register( this );
}
/**
* Dispose the component
*/
- public void dispose() {
- if (this.manager != null) {
- getLogger().debug("Disposing component!");
-
- if (this.storeJanitor != null)
- this.storeJanitor.unregister(this);
- this.manager.release(this.storeJanitor);
- this.storeJanitor = null;
+ public void dispose()
+ {
+ if( m_manager != null )
+ {
+ getLogger().debug( "Disposing component!" );
+
+ if( m_storeJanitor != null )
+ {
+ m_storeJanitor.unregister( this );
+ }
+ m_manager.release( m_storeJanitor );
+ m_storeJanitor = null;
// save all cache entries to filesystem
- if (this.persistent) {
- getLogger().debug("Final cache size: " + this.cache.size());
- Enumeration enum = this.cache.keys();
- while (enum.hasMoreElements()) {
+ if( m_persistent )
+ {
+ getLogger().debug( "Final cache size: " + m_cache.size() );
+ Enumeration enum = m_cache.keys();
+ while( enum.hasMoreElements() )
+ {
Object key = enum.nextElement();
- if (key == null) {
+ if( key == null )
+ {
continue;
}
- try {
- Object value = this.cache.remove(key);
- if(checkSerializable(value)) {
- persistentStore.store(key, value);
+ try
+ {
+ Object value = m_cache.remove( key );
+ if( checkSerializable( value ) )
+ {
+ m_persistentStore.store( key, value );
}
- } catch (IOException ioe) {
- getLogger().error("Error in dispose()", ioe);
+ }
+ catch( IOException ioe )
+ {
+ getLogger().error( "Error in dispose()", ioe );
}
}
}
- this.manager.release(this.persistentStore);
- this.persistentStore = null;
+ m_manager.release( m_persistentStore );
+ m_persistentStore = null;
}
- this.manager = null;
+ m_manager = null;
}
/**
@@ -134,8 +150,9 @@
* @param key The key for the object to store
* @param value The object to store
*/
- public synchronized void store(Object key, Object value) {
- this.hold(key,value);
+ public synchronized void store( Object key, Object value )
+ {
+ hold( key, value );
}
/**
@@ -146,21 +163,24 @@
* @param key The key of the object to be stored
* @param value The object to be stored
*/
- public synchronized void hold(Object key, Object value) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Holding object in memory:");
- getLogger().debug(" key: " + key);
- getLogger().debug(" value: " + value);
+ public synchronized void hold( Object key, Object value )
+ {
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Holding object in memory:" );
+ getLogger().debug( " key: " + key );
+ getLogger().debug( " value: " + value );
}
/** ...first test if the max. objects in cache is reached... */
- while (this.mrulist.size() >= this.maxobjects) {
+ while( m_mrulist.size() >= m_maxobjects )
+ {
/** ...ok, heapsize is reached, remove the last element... */
- this.free();
+ free();
}
/** ..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);
+ m_cache.put( key, value );
+ m_mrulist.remove( key );
+ m_mrulist.addFirst( key );
}
/**
@@ -169,33 +189,43 @@
* @param key The key of the requested object
* @return the requested object
*/
- public synchronized Object get(Object key) {
- Object value = this.cache.get(key);
- if (value != null) {
+ public synchronized Object get( Object key )
+ {
+ Object value = m_cache.get( key );
+ if( value != null )
+ {
/** put the accessed key on top of the linked list */
- this.mrulist.remove(key);
- this.mrulist.addFirst(key);
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Found key: " + key.toString());
+ m_mrulist.remove( key );
+ m_mrulist.addFirst( key );
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Found key: " + key.toString() );
}
return value;
}
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("NOT Found key: " + key.toString());
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "NOT Found key: " + key.toString() );
}
/** try to fetch from filesystem */
- if (this.persistent) {
- value = this.persistentStore.get(key);
- if (value != null) {
- try {
- if(!this.cache.containsKey(key)) {
- this.hold(key, value);
+ if( m_persistent )
+ {
+ value = m_persistentStore.get( key );
+ if( value != null )
+ {
+ try
+ {
+ if( !m_cache.containsKey( key ) )
+ {
+ hold( key, value );
}
return value;
- } catch (Exception e) {
- getLogger().error("Error in get()!", e);
+ }
+ catch( Exception e )
+ {
+ getLogger().error( "Error in get()!", e );
return null;
}
}
@@ -209,30 +239,36 @@
*
* @param key The key of to be removed object
*/
- public synchronized void remove(Object key) {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Removing object from store");
- getLogger().debug(" key: " + key);
- }
- this.cache.remove(key);
- this.mrulist.remove(key);
- if(this.persistent && key != null) {
- this.persistentStore.remove(key);
+ public synchronized void remove( Object key )
+ {
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Removing object from store" );
+ getLogger().debug( " key: " + key );
+ }
+ m_cache.remove( key );
+ m_mrulist.remove( key );
+ if( m_persistent && key != null )
+ {
+ m_persistentStore.remove( key );
}
}
/**
- * Clear the Store of all elements
- */
- public synchronized void clear() {
- Enumeration enum = this.cache.keys();
- while (enum.hasMoreElements()) {
- Object key = enum.nextElement();
- if (key == null) {
- continue;
- }
- this.remove(key);
- }
+ * Clear the Store of all elements
+ */
+ public synchronized void clear()
+ {
+ Enumeration enum = m_cache.keys();
+ while( enum.hasMoreElements() )
+ {
+ Object key = enum.nextElement();
+ if( key == null )
+ {
+ continue;
+ }
+ remove( key );
+ }
}
/**
@@ -241,11 +277,15 @@
* @param key The key of the object
* @return true if the key exists
*/
- public synchronized boolean containsKey(Object key) {
- if(persistent) {
- return (cache.containsKey(key) || persistentStore.containsKey(key));
- } else {
- return cache.containsKey(key);
+ public synchronized boolean containsKey( Object key )
+ {
+ if( m_persistent )
+ {
+ return ( m_cache.containsKey( key ) || m_persistentStore.containsKey( key ) );
+ }
+ else
+ {
+ return m_cache.containsKey( key );
}
}
@@ -254,53 +294,69 @@
*
* @return the enumeration of the cache
*/
- public synchronized Enumeration keys() {
- return this.cache.keys();
+ public synchronized Enumeration keys()
+ {
+ return m_cache.keys();
}
/**
* Returns count of the objects in the store, or -1 if could not be
* obtained.
*/
- public synchronized int size() {
- return this.cache.size();
+ public synchronized int size()
+ {
+ return m_cache.size();
}
/**
* Frees some of the fast memory used by this store.
* It removes the last element in the store.
*/
- public synchronized void free() {
- try {
- if (this.cache.size() > 0) {
+ public synchronized void free()
+ {
+ try
+ {
+ if( m_cache.size() > 0 )
+ {
// This can throw NoSuchElementException
- Object key = this.mrulist.removeLast();
- Object value = this.cache.remove(key);
- if (value == null) {
- getLogger().warn("Concurrency condition in free()");
+ Object key = m_mrulist.removeLast();
+ Object value = m_cache.remove( key );
+ if( value == null )
+ {
+ getLogger().warn( "Concurrency condition in free()" );
}
- if (getLogger().isDebugEnabled()) {
- getLogger().debug("Freeing cache.");
- getLogger().debug(" key: " + key);
- getLogger().debug(" value: " + value);
+ if( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug( "Freeing cache." );
+ getLogger().debug( " key: " + key );
+ getLogger().debug( " value: " + value );
}
- if (this.persistent) {
+ if( m_persistent )
+ {
// Swap object on fs.
- if(checkSerializable(value)) {
- try {
- this.persistentStore.store(key, value);
- } catch(Exception e) {
- getLogger().error("Error storing object on fs", e);
+ if( checkSerializable( value ) )
+ {
+ try
+ {
+ m_persistentStore.store( key, value );
+ }
+ catch( Exception e )
+ {
+ getLogger().error( "Error storing object on fs", e );
}
}
}
}
- } catch (NoSuchElementException e) {
- getLogger().warn("Concurrency error in free()", e);
- } catch (Exception e) {
- getLogger().error("Error in free()", e);
+ }
+ catch( NoSuchElementException e )
+ {
+ getLogger().warn( "Concurrency error in free()", e );
+ }
+ catch( Exception e )
+ {
+ getLogger().error( "Error in free()", e );
}
}
@@ -310,11 +366,12 @@
* @param object The object to be checked
* @return true if the object is storeable
*/
- private boolean checkSerializable(Object object) {
+ private boolean checkSerializable( Object object )
+ {
- if (object == null) return false;
+ if( object == null ) return false;
- return (object instanceof java.io.Serializable);
+ return ( object instanceof java.io.Serializable );
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>