You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ha...@apache.org on 2004/08/23 03:22:50 UTC
svn commit: rev 36730 - in avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore: . xml
Author: hammant
Date: Sun Aug 22 18:22:49 2004
New Revision: 36730
Added:
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentObjectRepository.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentStreamRepository.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AvalonFileRepositoryMonitor.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/FileRepositoryMonitor.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/AbstractXMLFilePersistentObjectRepository.java
Modified:
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFileRepository.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Object_Repository.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Stream_Repository.java
avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/XMLFilePersistentObjectRepository.java
Log:
start of store refactoring to allow non avalon use
Added: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentObjectRepository.java
==============================================================================
--- (empty file)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentObjectRepository.java Sun Aug 22 18:22:49 2004
@@ -0,0 +1,114 @@
+package org.apache.avalon.cornerstone.blocks.masterstore;
+
+import org.apache.avalon.cornerstone.services.store.ObjectRepository;
+
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.OutputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * @author Paul Hammant
+ * @version $Revision: 1.8 $
+ */
+public abstract class AbstractFilePersistentObjectRepository extends AbstractFileRepository
+ implements ObjectRepository {
+ /**
+ * Get the object associated to the given unique key.
+ */
+ public synchronized Object get( final String key )
+ {
+ try
+ {
+ final InputStream inputStream = getInputStream( key );
+
+ if( inputStream == null )
+ throw new NullPointerException( "Null input stream returned for key: " + key );
+ try
+ {
+ final ObjectInputStream stream = new ObjectInputStream( inputStream );
+
+ if( stream == null )
+ throw new NullPointerException( "Null stream returned for key: " + key );
+
+ final Object object = stream.readObject();
+ if( DEBUG )
+ {
+ monitor.returningObjectForKey(File_Persistent_Object_Repository.class, object, key);
+ }
+ return object;
+ }
+ finally
+ {
+ inputStream.close();
+ }
+ }
+ catch( final Throwable e )
+ {
+ throw new RuntimeException(
+ "Exception caught while retrieving an object, cause: " + e.toString() );
+ }
+ }
+
+ public synchronized Object get( final String key, final ClassLoader classLoader )
+ {
+ try
+ {
+ final InputStream inputStream = getInputStream( key );
+
+ if( inputStream == null )
+ throw new NullPointerException( "Null input stream returned for key: " + key );
+
+ try
+ {
+ final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
+
+ if( stream == null )
+ throw new NullPointerException( "Null stream returned for key: " + key );
+
+ final Object object = stream.readObject();
+
+ if( DEBUG )
+ {
+ monitor.returningObjectForKey(File_Persistent_Object_Repository.class, object, key);
+ }
+ return object;
+ }
+ finally
+ {
+ inputStream.close();
+ }
+ }
+ catch( final Throwable e )
+ {
+ throw new RuntimeException( "Exception caught while retrieving an object: " + e );
+ }
+
+ }
+
+ /**
+ * Store the given object and associates it to the given key
+ */
+ public synchronized void put( final String key, final Object value )
+ {
+ try
+ {
+ final OutputStream outputStream = getOutputStream( key );
+
+ try
+ {
+ final ObjectOutputStream stream = new ObjectOutputStream( outputStream );
+ stream.writeObject( value );
+ if( DEBUG ) monitor.storingObjectForKey(File_Persistent_Object_Repository.class, value, key);
+ }
+ finally
+ {
+ outputStream.close();
+ }
+ }
+ catch( final Exception e )
+ {
+ throw new RuntimeException( "Exception caught while storing an object: " + e );
+ }
+ }
+}
Added: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentStreamRepository.java
==============================================================================
--- (empty file)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFilePersistentStreamRepository.java Sun Aug 22 18:22:49 2004
@@ -0,0 +1,135 @@
+package org.apache.avalon.cornerstone.blocks.masterstore;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.BufferedOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+/**
+ * @author Paul Hammant
+ * @version $Revision: 1.8 $
+ */
+public abstract class AbstractFilePersistentStreamRepository extends AbstractFileRepository {
+
+ protected final HashMap m_inputs = new HashMap();
+ protected final HashMap m_outputs = new HashMap();
+
+ /**
+ * Get the object associated to the given unique key.
+ */
+ public synchronized InputStream get( final String key )
+ {
+ try
+ {
+ final ResettableFileInputStream stream =
+ new ResettableFileInputStream( getFile( key ) );
+
+ final Object o = m_inputs.get( key );
+ if( null == o )
+ {
+ m_inputs.put( key, stream );
+ }
+ else if( o instanceof ArrayList )
+ {
+ ( (ArrayList)o ).add( stream );
+ }
+ else
+ {
+ final ArrayList list = new ArrayList();
+ list.add( o );
+ list.add( stream );
+ m_inputs.put( key, stream );
+ }
+
+ return stream;
+ }
+ catch( final IOException ioe )
+ {
+ final String message = "Exception caught while retrieving a stream ";
+ monitor.unExpectedIOException(File_Persistent_Stream_Repository.class, message, ioe);
+ throw new RuntimeException( message + ": " + ioe );
+ }
+ }
+
+ /**
+ * Store the given object and associates it to the given key
+ */
+ public synchronized OutputStream put( final String key )
+ {
+ try
+ {
+ final OutputStream outputStream = getOutputStream( key );
+ final BufferedOutputStream stream = new BufferedOutputStream( outputStream );
+
+ final Object o = m_outputs.get( key );
+ if( null == o )
+ {
+ m_outputs.put( key, stream );
+ }
+ else if( o instanceof ArrayList )
+ {
+ ( (ArrayList)o ).add( stream );
+ }
+ else
+ {
+ final ArrayList list = new ArrayList();
+ list.add( o );
+ list.add( stream );
+ m_outputs.put( key, stream );
+ }
+
+ return stream;
+ }
+ catch( final IOException ioe )
+ {
+ final String message = "Exception caught while storing a stream ";
+ monitor.unExpectedIOException(File_Persistent_Stream_Repository.class, message, ioe);
+ throw new RuntimeException( message + ": " + ioe );
+ }
+ }
+
+ public void remove( final String key )
+ {
+ Object o = m_inputs.remove( key );
+ if( null != o )
+ {
+ if( o instanceof InputStream )
+ {
+ IOUtil.shutdownStream( (InputStream)o );
+ }
+ else
+ {
+ final ArrayList list = (ArrayList)o;
+ final int size = list.size();
+
+ for( int i = 0; i < size; i++ )
+ {
+ IOUtil.shutdownStream( (InputStream)list.get( i ) );
+ }
+ }
+ }
+
+ o = m_outputs.remove( key );
+ if( null != o )
+ {
+ if( o instanceof OutputStream )
+ {
+ IOUtil.shutdownStream( (OutputStream)o );
+ }
+ else
+ {
+ final ArrayList list = (ArrayList)o;
+ final int size = list.size();
+
+ for( int i = 0; i < size; i++ )
+ {
+ IOUtil.shutdownStream( (OutputStream)list.get( 0 ) );
+ }
+ }
+ }
+
+ super.remove( key );
+ }
+}
Modified: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFileRepository.java
==============================================================================
--- avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFileRepository.java (original)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AbstractFileRepository.java Sun Aug 22 18:22:49 2004
@@ -17,6 +17,8 @@
package org.apache.avalon.cornerstone.blocks.masterstore;
+import org.apache.avalon.cornerstone.services.store.Repository;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -26,18 +28,6 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
-import org.apache.avalon.cornerstone.services.store.Repository;
-import org.apache.avalon.framework.activity.Initializable;
-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.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
/**
* This an abstract class implementing functionality for creating a file-store.
@@ -45,8 +35,7 @@
* @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
*/
public abstract class AbstractFileRepository
- extends AbstractLogEnabled
- implements Repository, Contextualizable, Serviceable, Configurable, Initializable
+ implements Repository
{
protected static final boolean DEBUG = false;
@@ -63,83 +52,17 @@
protected String m_name;
protected FilenameFilter m_filter;
protected File m_baseDirectory;
-
- protected ServiceManager m_serviceManager;
+ protected FileRepositoryMonitor monitor;
protected abstract String getExtensionDecorator();
- /**
- * Contextualization of the component by the container during
- * which the working home directory will be provided.
- *
- * @param context the supplied context object
- * @avalon.entry key="urn:avalon:home" type="java.io.File"
- */
- public void contextualize( final Context context ) throws ContextException
- {
- try
- {
- m_baseDirectory = (File)context.get( "urn:avalon:home" );
- }
- catch( ContextException e )
- {
- m_baseDirectory = (File)context.get( "app.home" );
- }
- }
-
- /**
- * Serviceing of the component by the container. This implementation
- * is somewhat suspect in that it does not declare any servicable
- * dependencies.
- *
- * @param serviceManager a service manager
- */
- public void service( final ServiceManager serviceManager )
- throws ServiceException
- {
- m_serviceManager = serviceManager;
- }
-
- /**
- * Configuration of the component by the container.
- * @param configuration the configuration
- * @exception ConfigurationException if a configuration error occurs
- */
- public void configure( final Configuration configuration )
- throws ConfigurationException
- {
- if( null == m_destination )
- {
- final String destination = configuration.getAttribute( "destinationURL" );
- setDestination( destination );
- }
- }
-
- /**
- * Initialization of the component by the container.
- * @exception Exception if a initialization stage error occurs
- */
- public void initialize()
- throws Exception
- {
- getLogger().info( "Init " + getClass().getName() + " Store" );
-
- m_name = RepositoryManager.getName();
- m_extension = "." + m_name + getExtensionDecorator();
- m_filter = new ExtensionFileFilter( m_extension );
-
- final File directory = new File( m_path );
- directory.mkdirs();
-
- getLogger().info( getClass().getName() + " opened in " + m_path );
- }
protected void setDestination( final String destination )
- throws ConfigurationException
+ throws IOException
{
if( !destination.startsWith( HANDLED_URL ) )
{
- throw new ConfigurationException( "cannot handle destination " + destination );
+ throw new IOException( "cannot handle destination " + destination );
}
m_path = destination.substring( HANDLED_URL.length() );
@@ -162,7 +85,7 @@
}
catch( final IOException ioe )
{
- throw new ConfigurationException( "Unable to form canonical representation of " +
+ throw new IOException( "Unable to form canonical representation of " +
directory );
}
@@ -193,30 +116,19 @@
try
{
- child.service( m_serviceManager );
- }
- catch( final ServiceException cme )
- {
- throw new RuntimeException( "Cannot service child " +
- "repository " + childName +
- " : " + cme );
- }
-
- try
- {
child.setDestination( m_destination + File.pathSeparatorChar +
childName + File.pathSeparator );
}
- catch( final ConfigurationException ce )
+ catch( final IOException ioe )
{
throw new RuntimeException( "Cannot set destination for child child " +
"repository " + childName +
- " : " + ce );
+ " : " + ioe );
}
try
{
- child.initialize();
+ initializeChild(child);
}
catch( final Exception e )
{
@@ -227,14 +139,14 @@
if( DEBUG )
{
- getLogger().debug( "Child repository of " + m_name + " created in " +
- m_destination + File.pathSeparatorChar +
- childName + File.pathSeparator );
+ monitor.repositoryCreated(AbstractFileRepository.class, m_name, m_destination, childName);
}
return child;
}
+ protected abstract void initializeChild(AbstractFileRepository child) throws Exception;
+
protected File getFile( final String key )
throws IOException
{
@@ -262,7 +174,8 @@
{
final File file = getFile( key );
file.delete();
- if( DEBUG ) getLogger().debug( "removed key " + key );
+ if( DEBUG )
+ monitor.keyRemoved(AbstractFileRepository.class, key);
}
catch( final Exception e )
{
@@ -279,7 +192,7 @@
try
{
final File file = getFile( key );
- if( DEBUG ) getLogger().debug( "checking key " + key );
+ if( DEBUG ) monitor.checkingKey(AbstractFileRepository.class, key);
return file.exists();
}
catch( final Exception e )
Added: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AvalonFileRepositoryMonitor.java
==============================================================================
--- (empty file)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/AvalonFileRepositoryMonitor.java Sun Aug 22 18:22:49 2004
@@ -0,0 +1,52 @@
+package org.apache.avalon.cornerstone.blocks.masterstore;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Paul Hammant
+ * @version $Revision: 1.8 $
+ */
+public class AvalonFileRepositoryMonitor extends AbstractLogEnabled implements FileRepositoryMonitor {
+ public void repositoryCreated(Class aClass, String m_name, String m_destination, String childName) {
+ getLogger().debug( "Child repository of " + m_name + " created in " +
+ m_destination + File.pathSeparatorChar +
+ childName + File.pathSeparator );
+ }
+
+ public void keyRemoved(Class aClass, String key) {
+ getLogger().debug( "removed key " + key );
+ }
+
+ public void checkingKey(Class aClass, String key) {
+ getLogger().debug( "checking key " + key );
+ }
+
+ public void returningKey(Class aClass, String key) {
+ getLogger().debug( "returning key " + key );
+
+ }
+
+ public void returningObjectForKey(Class aClass, Object object, String key) {
+ getLogger().debug( "returning object " + object + " for key " + key );
+ }
+
+
+ public void storingObjectForKey(Class xmlFilePersistentObjectRepository, Object value, String key) {
+ getLogger().debug( "storing object " + value + " for key " + key );
+ }
+
+ public void initialized(Class aClass) {
+ getLogger().info( "Init " + aClass.getName() + " Store" );
+ }
+
+ public void pathOpened(Class aClass, String m_path) {
+ getLogger().info( aClass.getName() + " opened in " + m_path );
+ }
+
+ public void unExpectedIOException(Class aClass, String message, IOException ioe) {
+ getLogger().warn( message, ioe );
+ }
+}
Added: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/FileRepositoryMonitor.java
==============================================================================
--- (empty file)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/FileRepositoryMonitor.java Sun Aug 22 18:22:49 2004
@@ -0,0 +1,19 @@
+package org.apache.avalon.cornerstone.blocks.masterstore;
+
+import java.io.IOException;
+
+/**
+ * @author Paul Hammant
+ * @version $Revision: 1.8 $
+ */
+public interface FileRepositoryMonitor {
+ void repositoryCreated(Class aClass, String m_name, String m_destination, String childName);
+ void keyRemoved(Class aClass, String key);
+ void checkingKey(Class aClass, String key);
+ void returningKey(Class aClass, String key);
+ void returningObjectForKey(Class aClass, Object object, String key);
+ void storingObjectForKey(Class xmlFilePersistentObjectRepository, Object value, String key);
+ void initialized(Class aClass);
+ void pathOpened(Class aClass, String m_path);
+ void unExpectedIOException(Class aClass, String message, IOException ioe);
+}
Modified: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Object_Repository.java
==============================================================================
--- avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Object_Repository.java (original)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Object_Repository.java Sun Aug 22 18:22:49 2004
@@ -17,11 +17,19 @@
package org.apache.avalon.cornerstone.blocks.masterstore;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
import org.apache.avalon.cornerstone.services.store.ObjectRepository;
+import org.apache.avalon.framework.activity.Initializable;
+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.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+
+import java.io.File;
+import java.io.IOException;
/**
* This is a simple implementation of persistent object store using
@@ -32,111 +40,77 @@
* @author <a href="mailto:paul_hammant@yahoo.com">Paul Hammant</a>
*/
public class File_Persistent_Object_Repository
- extends AbstractFileRepository
- implements ObjectRepository
+ extends AbstractFilePersistentObjectRepository
+ implements ObjectRepository, Contextualizable, Initializable, Configurable, LogEnabled
{
protected String getExtensionDecorator()
{
return ".FileObjectStore";
}
- /**
- * Get the object associated to the given unique key.
- */
- public synchronized Object get( final String key )
- {
- try
- {
- final InputStream inputStream = getInputStream( key );
-
- if( inputStream == null )
- throw new NullPointerException( "Null input stream returned for key: " + key );
- try
- {
- final ObjectInputStream stream = new ObjectInputStream( inputStream );
-
- if( stream == null )
- throw new NullPointerException( "Null stream returned for key: " + key );
-
- final Object object = stream.readObject();
- if( DEBUG )
- {
- getLogger().debug( "returning object " + object + " for key " + key );
- }
- return object;
- }
- finally
- {
- inputStream.close();
- }
- }
- catch( final Throwable e )
- {
- throw new RuntimeException(
- "Exception caught while retrieving an object, cause: " + e.toString() );
- }
+ public void enableLogging(Logger logger) {
+ AvalonFileRepositoryMonitor avMonitor = new AvalonFileRepositoryMonitor();
+ avMonitor.enableLogging(logger);
}
- public synchronized Object get( final String key, final ClassLoader classLoader )
- {
- try
- {
- final InputStream inputStream = getInputStream( key );
-
- if( inputStream == null )
- throw new NullPointerException( "Null input stream returned for key: " + key );
-
- try
- {
- final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
-
- if( stream == null )
- throw new NullPointerException( "Null stream returned for key: " + key );
-
- final Object object = stream.readObject();
-
- if( DEBUG )
- {
- getLogger().debug( "returning object " + object + " for key " + key );
- }
- return object;
- }
- finally
- {
- inputStream.close();
- }
- }
- catch( final Throwable e )
- {
- throw new RuntimeException( "Exception caught while retrieving an object: " + e );
- }
-
+ protected void initializeChild(AbstractFileRepository child) throws Exception {
+ ((Initializable) child).initialize();
}
/**
- * Store the given object and associates it to the given key
+ * Contextualization of the component by the container during
+ * which the working home directory will be provided.
+ *
+ * @param context the supplied context object
+ * @avalon.entry key="urn:avalon:home" type="java.io.File"
*/
- public synchronized void put( final String key, final Object value )
- {
- try
- {
- final OutputStream outputStream = getOutputStream( key );
-
- try
- {
- final ObjectOutputStream stream = new ObjectOutputStream( outputStream );
- stream.writeObject( value );
- if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
- }
- finally
- {
- outputStream.close();
- }
- }
- catch( final Exception e )
- {
- throw new RuntimeException( "Exception caught while storing an object: " + e );
- }
- }
+ public void contextualize( final Context context ) throws ContextException
+ {
+ try
+ {
+ m_baseDirectory = (File)context.get( "urn:avalon:home" );
+ }
+ catch( ContextException e )
+ {
+ m_baseDirectory = (File)context.get( "app.home" );
+ }
+ }
+ /**
+ * Initialization of the component by the container.
+ * @exception Exception if a initialization stage error occurs
+ */
+ public void initialize()
+ throws Exception
+ {
+ monitor.initialized(File_Persistent_Object_Repository.class);
+
+ m_name = RepositoryManager.getName();
+ m_extension = "." + m_name + getExtensionDecorator();
+ m_filter = new ExtensionFileFilter( m_extension );
+
+ final File directory = new File( m_path );
+ directory.mkdirs();
+
+ monitor.pathOpened(File_Persistent_Object_Repository.class, m_path);
+ }
+
+ /**
+ * Configuration of the component by the container.
+ * @param configuration the configuration
+ * @exception org.apache.avalon.framework.configuration.ConfigurationException if a configuration error occurs
+ */
+ public void configure( final Configuration configuration )
+ throws ConfigurationException
+ {
+ if( null == m_destination )
+ {
+ final String destination = configuration.getAttribute( "destinationURL" );
+ try {
+ setDestination( destination );
+ } catch (IOException ioe) {
+ throw new ConfigurationException("Unexpected IOException " + ioe.getMessage(), ioe);
+ }
+ }
+ }
}
Modified: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Stream_Repository.java
==============================================================================
--- avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Stream_Repository.java (original)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/File_Persistent_Stream_Repository.java Sun Aug 22 18:22:49 2004
@@ -17,13 +17,19 @@
package org.apache.avalon.cornerstone.blocks.masterstore;
-import java.io.BufferedOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.HashMap;
import org.apache.avalon.cornerstone.services.store.StreamRepository;
+import org.apache.avalon.framework.activity.Initializable;
+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.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+
+import java.io.File;
+import java.io.IOException;
/**
* Implementation of a StreamRepository to a File.
@@ -33,133 +39,80 @@
* @author Federico Barbieri <fe...@apache.org>
*/
public class File_Persistent_Stream_Repository
- extends AbstractFileRepository
- implements StreamRepository
+ extends AbstractFilePersistentStreamRepository
+ implements StreamRepository, Contextualizable, Initializable, Configurable, LogEnabled
{
- protected final HashMap m_inputs = new HashMap();
- protected final HashMap m_outputs = new HashMap();
protected String getExtensionDecorator()
{
return ".FileStreamStore";
}
+ public void enableLogging(Logger logger) {
+ AvalonFileRepositoryMonitor avMonitor = new AvalonFileRepositoryMonitor();
+ avMonitor.enableLogging(logger);
+ }
+
+ protected void initializeChild(AbstractFileRepository child) throws Exception {
+ ((Initializable) child).initialize();
+ }
+
/**
- * Get the object associated to the given unique key.
+ * Contextualization of the component by the container during
+ * which the working home directory will be provided.
+ *
+ * @param context the supplied context object
+ * @avalon.entry key="urn:avalon:home" type="java.io.File"
*/
- public synchronized InputStream get( final String key )
- {
- try
- {
- final ResettableFileInputStream stream =
- new ResettableFileInputStream( getFile( key ) );
-
- final Object o = m_inputs.get( key );
- if( null == o )
- {
- m_inputs.put( key, stream );
- }
- else if( o instanceof ArrayList )
- {
- ( (ArrayList)o ).add( stream );
- }
- else
- {
- final ArrayList list = new ArrayList();
- list.add( o );
- list.add( stream );
- m_inputs.put( key, stream );
- }
-
- return stream;
- }
- catch( final IOException ioe )
- {
- final String message = "Exception caught while retrieving a stream ";
- getLogger().warn( message, ioe );
- throw new RuntimeException( message + ": " + ioe );
- }
- }
+ public void contextualize( final Context context ) throws ContextException
+ {
+ try
+ {
+ m_baseDirectory = (File)context.get( "urn:avalon:home" );
+ }
+ catch( ContextException e )
+ {
+ m_baseDirectory = (File)context.get( "app.home" );
+ }
+ }
/**
- * Store the given object and associates it to the given key
+ * Initialization of the component by the container.
+ * @exception Exception if a initialization stage error occurs
*/
- public synchronized OutputStream put( final String key )
- {
- try
- {
- final OutputStream outputStream = getOutputStream( key );
- final BufferedOutputStream stream = new BufferedOutputStream( outputStream );
-
- final Object o = m_outputs.get( key );
- if( null == o )
- {
- m_outputs.put( key, stream );
- }
- else if( o instanceof ArrayList )
- {
- ( (ArrayList)o ).add( stream );
- }
- else
- {
- final ArrayList list = new ArrayList();
- list.add( o );
- list.add( stream );
- m_outputs.put( key, stream );
- }
-
- return stream;
- }
- catch( final IOException ioe )
- {
- final String message = "Exception caught while storing a stream ";
- getLogger().warn( message, ioe );
- throw new RuntimeException( message + ": " + ioe );
- }
- }
+ public void initialize()
+ throws Exception
+ {
+ monitor.initialized(File_Persistent_Stream_Repository.class);
+
+ m_name = RepositoryManager.getName();
+ m_extension = "." + m_name + getExtensionDecorator();
+ m_filter = new ExtensionFileFilter( m_extension );
+
+ final File directory = new File( m_path );
+ directory.mkdirs();
+ monitor.pathOpened(File_Persistent_Stream_Repository.class, m_path);
- public void remove( final String key )
- {
- Object o = m_inputs.remove( key );
- if( null != o )
- {
- if( o instanceof InputStream )
- {
- IOUtil.shutdownStream( (InputStream)o );
- }
- else
- {
- final ArrayList list = (ArrayList)o;
- final int size = list.size();
-
- for( int i = 0; i < size; i++ )
- {
- IOUtil.shutdownStream( (InputStream)list.get( i ) );
- }
- }
- }
-
- o = m_outputs.remove( key );
- if( null != o )
- {
- if( o instanceof OutputStream )
- {
- IOUtil.shutdownStream( (OutputStream)o );
- }
- else
- {
- final ArrayList list = (ArrayList)o;
- final int size = list.size();
-
- for( int i = 0; i < size; i++ )
- {
- IOUtil.shutdownStream( (OutputStream)list.get( 0 ) );
- }
- }
- }
+ }
- super.remove( key );
- }
+ /**
+ * Configuration of the component by the container.
+ * @param configuration the configuration
+ * @exception org.apache.avalon.framework.configuration.ConfigurationException if a configuration error occurs
+ */
+ public void configure( final Configuration configuration )
+ throws ConfigurationException
+ {
+ if( null == m_destination )
+ {
+ final String destination = configuration.getAttribute( "destinationURL" );
+ try {
+ setDestination( destination );
+ } catch (IOException ioe) {
+ throw new ConfigurationException("Unexpected IOException " + ioe.getMessage(), ioe);
+ }
+ }
+ }
}
Added: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/AbstractXMLFilePersistentObjectRepository.java
==============================================================================
--- (empty file)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/AbstractXMLFilePersistentObjectRepository.java Sun Aug 22 18:22:49 2004
@@ -0,0 +1,104 @@
+package org.apache.avalon.cornerstone.blocks.masterstore.xml;
+
+import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.beans.XMLDecoder;
+import java.beans.XMLEncoder;
+
+/**
+ * @author Paul Hammant
+ * @version $Revision: 1.8 $
+ */
+public abstract class AbstractXMLFilePersistentObjectRepository extends AbstractFileRepository {
+
+ /**
+ * Get the object associated to the given unique key.
+ */
+ public synchronized Object get( final String key )
+ {
+ try
+ {
+ final InputStream inputStream = getInputStream( key );
+
+ try
+ {
+ final XMLDecoder decoder = new XMLDecoder( inputStream );
+ final Object object = decoder.readObject();
+ if( DEBUG )
+ {
+ monitor.returningKey(XMLFilePersistentObjectRepository.class, key);
+ }
+ return object;
+ }
+ finally
+ {
+ inputStream.close();
+ }
+ }
+ catch( final Exception e )
+ {
+ throw new RuntimeException( "Exception caught while retrieving an object: " + e );
+ }
+ }
+
+ public synchronized Object get( final String key, final ClassLoader classLoader )
+ {
+ try
+ {
+ final InputStream inputStream = getInputStream( key );
+ final ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader( classLoader );
+ try
+ {
+ final XMLDecoder decoder = new XMLDecoder( inputStream );
+ final Object object = decoder.readObject();
+ if( DEBUG )
+ {
+ monitor.returningObjectForKey(XMLFilePersistentObjectRepository.class, object, key);
+ }
+ return object;
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader( oldCL );
+ inputStream.close();
+ }
+ }
+ catch( final Exception e )
+ {
+ e.printStackTrace();
+ throw new RuntimeException( "Exception caught while retrieving an object: " + e );
+ }
+
+ }
+
+ /**
+ * Store the given object and associates it to the given key
+ */
+ public synchronized void put( final String key, final Object value )
+ {
+ try
+ {
+ final OutputStream outputStream = getOutputStream( key );
+
+ try
+ {
+ //System.out.println("Putting key!:" + key + " " + value + " " + value.getClass().getName());
+ final XMLEncoder encoder = new XMLEncoder( outputStream );
+ encoder.writeObject( value );
+ encoder.flush();
+ if( DEBUG ) monitor.storingObjectForKey(XMLFilePersistentObjectRepository.class, value, key);
+ }
+ finally
+ {
+ outputStream.close();
+ }
+ }
+ catch( final Exception e )
+ {
+ throw new RuntimeException( "Exception caught while storing an object: " + e );
+ }
+ }
+}
Modified: avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/XMLFilePersistentObjectRepository.java
==============================================================================
--- avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/XMLFilePersistentObjectRepository.java (original)
+++ avalon/trunk/planet/cornerstone/store/impl/src/main/org/apache/avalon/cornerstone/blocks/masterstore/xml/XMLFilePersistentObjectRepository.java Sun Aug 22 18:22:49 2004
@@ -17,12 +17,23 @@
package org.apache.avalon.cornerstone.blocks.masterstore.xml;
-import java.beans.XMLDecoder;
-import java.beans.XMLEncoder;
-import java.io.InputStream;
-import java.io.OutputStream;
import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository;
+import org.apache.avalon.cornerstone.blocks.masterstore.AvalonFileRepositoryMonitor;
+import org.apache.avalon.cornerstone.blocks.masterstore.ExtensionFileFilter;
+import org.apache.avalon.cornerstone.blocks.masterstore.RepositoryManager;
import org.apache.avalon.cornerstone.services.store.ObjectRepository;
+import org.apache.avalon.framework.activity.Initializable;
+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.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+
+import java.io.File;
+import java.io.IOException;
/**
* This is a simple implementation of persistent object store using
@@ -44,100 +55,77 @@
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
*/
public class XMLFilePersistentObjectRepository
- extends AbstractFileRepository
- implements ObjectRepository
+ extends AbstractXMLFilePersistentObjectRepository
+ implements ObjectRepository, Contextualizable, Initializable, Configurable, LogEnabled
{
protected String getExtensionDecorator()
{
return ".FileObjectStore";
}
+ public void enableLogging(Logger logger) {
+ AvalonFileRepositoryMonitor avMonitor = new AvalonFileRepositoryMonitor();
+ avMonitor.enableLogging(logger);
+ }
+
+ protected void initializeChild(AbstractFileRepository child) throws Exception {
+ ((Initializable) child).initialize();
+ }
+
/**
- * Get the object associated to the given unique key.
+ * Contextualization of the component by the container during
+ * which the working home directory will be provided.
+ *
+ * @param context the supplied context object
+ * @avalon.entry key="urn:avalon:home" type="java.io.File"
*/
- public synchronized Object get( final String key )
- {
- try
- {
- final InputStream inputStream = getInputStream( key );
-
- try
- {
- final XMLDecoder decoder = new XMLDecoder( inputStream );
- final Object object = decoder.readObject();
- if( DEBUG )
- {
- getLogger().debug( "returning object " + object + " for key " + key );
- }
- return object;
- }
- finally
- {
- inputStream.close();
- }
- }
- catch( final Exception e )
- {
- throw new RuntimeException( "Exception caught while retrieving an object: " + e );
- }
- }
+ public void contextualize( final Context context ) throws ContextException
+ {
+ try
+ {
+ m_baseDirectory = (File)context.get( "urn:avalon:home" );
+ }
+ catch( ContextException e )
+ {
+ m_baseDirectory = (File)context.get( "app.home" );
+ }
+ }
- public synchronized Object get( final String key, final ClassLoader classLoader )
- {
- try
- {
- final InputStream inputStream = getInputStream( key );
- final ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
- Thread.currentThread().setContextClassLoader( classLoader );
- try
- {
- final XMLDecoder decoder = new XMLDecoder( inputStream );
- final Object object = decoder.readObject();
- if( DEBUG )
- {
- getLogger().debug( "returning object " + object + " for key " + key );
- }
- return object;
- }
- finally
- {
- Thread.currentThread().setContextClassLoader( oldCL );
- inputStream.close();
- }
- }
- catch( final Exception e )
- {
- e.printStackTrace();
- throw new RuntimeException( "Exception caught while retrieving an object: " + e );
- }
+ /**
+ * Initialization of the component by the container.
+ * @exception Exception if a initialization stage error occurs
+ */
+ public void initialize()
+ throws Exception
+ {
+ monitor.initialized(XMLFilePersistentObjectRepository.class);
+
+ m_name = RepositoryManager.getName();
+ m_extension = "." + m_name + getExtensionDecorator();
+ m_filter = new ExtensionFileFilter( m_extension );
- }
+ final File directory = new File( m_path );
+ directory.mkdirs();
+
+ monitor.pathOpened(XMLFilePersistentObjectRepository.class, m_path);
+ }
/**
- * Store the given object and associates it to the given key
+ * Configuration of the component by the container.
+ * @param configuration the configuration
+ * @exception org.apache.avalon.framework.configuration.ConfigurationException if a configuration error occurs
*/
- public synchronized void put( final String key, final Object value )
- {
- try
- {
- final OutputStream outputStream = getOutputStream( key );
-
- try
- {
- //System.out.println("Putting key!:" + key + " " + value + " " + value.getClass().getName());
- final XMLEncoder encoder = new XMLEncoder( outputStream );
- encoder.writeObject( value );
- encoder.flush();
- if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
- }
- finally
- {
- outputStream.close();
- }
- }
- catch( final Exception e )
- {
- throw new RuntimeException( "Exception caught while storing an object: " + e );
- }
- }
+ public void configure( final Configuration configuration )
+ throws ConfigurationException
+ {
+ if( null == m_destination )
+ {
+ final String destination = configuration.getAttribute( "destinationURL" );
+ try {
+ setDestination( destination );
+ } catch (IOException ioe) {
+ throw new ConfigurationException("Unexpected IOException " + ioe.getMessage(), ioe);
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org