bloritsch 2003/05/29 06:32:48
Modified: fortress/src/java/org/apache/avalon/fortress/util
LifecycleExtensionManager.java
Log:
Commit Anton's change to LifecycleExtensionManager.java to enable read-only locking
of the object.
Revision Changes Path
1.9 +96 -10
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/LifecycleExtensionManager.java
Index: LifecycleExtensionManager.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/LifecycleExtensionManager.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- LifecycleExtensionManager.java 22 Apr 2003 12:37:09 -0000 1.8
+++ LifecycleExtensionManager.java 29 May 2003 13:32:48 -0000 1.9
@@ -81,13 +81,34 @@
* @version CVS $Revision$ $Date$
*/
public final class LifecycleExtensionManager
- extends AbstractLogEnabled
+ extends AbstractLogEnabled
{
public static final String ROLE = LifecycleExtensionManager.class.getName();
// extensions objects
private final CachedArrayList m_accessorExtensions = new CachedArrayList();
private final CachedArrayList m_creatorExtensions = new CachedArrayList();
+ private boolean m_readOnly = false;
+
+ /**
+ * Make the extension manager read only (immutable).
+ */
+ public void makeReadOnly()
+ {
+ m_readOnly = true;
+ }
+
+ /**
+ * Create a copy; it will be writeable even if the original was not.
+ */
+ public LifecycleExtensionManager writeableCopy()
+ {
+ final LifecycleExtensionManager copy = new LifecycleExtensionManager();
+ copy.m_accessorExtensions.copyFrom( m_accessorExtensions );
+ copy.m_creatorExtensions.copyFrom( m_creatorExtensions );
+
+ return copy;
+ }
/**
* <code>executeAccessExtensions</code> method, executes all access
@@ -98,7 +119,7 @@
* @exception Exception if an error occurs
*/
public void executeAccessExtensions( final Object component, final Context
context )
- throws Exception
+ throws Exception
{
executeExtensions( m_accessorExtensions.toArray(), component, context,
ACCESS );
}
@@ -112,7 +133,7 @@
* @exception Exception if an error occurs
*/
public void executeReleaseExtensions( final Object component, final Context
context )
- throws Exception
+ throws Exception
{
executeExtensions( m_accessorExtensions.toArray(), component, context,
RELEASE );
}
@@ -126,7 +147,7 @@
* @exception Exception if an error occurs
*/
public void executeCreationExtensions( final Object component, final Context
context )
- throws Exception
+ throws Exception
{
executeExtensions( m_creatorExtensions.toArray(), component, context,
CREATE );
}
@@ -140,7 +161,7 @@
* @exception Exception if an error occurs
*/
public void executeDestructionExtensions( final Object component, final Context
context )
- throws Exception
+ throws Exception
{
executeExtensions( m_creatorExtensions.toArray(), component, context,
DESTROY );
}
@@ -168,6 +189,7 @@
*/
public void addAccessorExtension( final Accessor extension )
{
+ checkWriteable();
m_accessorExtensions.add( extension );
}
@@ -178,6 +200,7 @@
*/
public void addCreatorExtension( final Creator extension )
{
+ checkWriteable();
m_creatorExtensions.add( extension );
}
@@ -189,6 +212,7 @@
*/
public void insertAccessorExtension( final int position, final Accessor
extension )
{
+ checkWriteable();
m_accessorExtensions.insert( position, extension );
}
@@ -200,6 +224,7 @@
*/
public void insertCreatorExtension( final int position, final Creator extension
)
{
+ checkWriteable();
m_creatorExtensions.insert( position, extension );
}
@@ -211,6 +236,7 @@
*/
public Accessor removeAccessorExtension( final int position )
{
+ checkWriteable();
return (Accessor) m_accessorExtensions.remove( position );
}
@@ -222,6 +248,7 @@
*/
public Creator removeCreatorExtension( final int position )
{
+ checkWriteable();
return (Creator) m_creatorExtensions.remove( position );
}
@@ -292,6 +319,7 @@
*/
public void clearAccessorExtensions()
{
+ checkWriteable();
m_accessorExtensions.clear();
}
@@ -300,6 +328,7 @@
*/
public void clearCreatorExtensions()
{
+ checkWriteable();
m_creatorExtensions.clear();
}
@@ -324,7 +353,7 @@
final Object component,
final Context context,
final int type )
- throws Exception
+ throws Exception
{
switch ( type )
{
@@ -360,13 +389,30 @@
if ( getLogger().isErrorEnabled() )
{
final String message =
- "Incorrect extension phase specified: " + type;
+ "Incorrect extension phase specified: " + type;
getLogger().error( message );
}
}
}
/**
+ * Utility method to check if LifecycleExtensionsManager
+ * is writeable and if not throw exception.
+ *
+ * @throws IllegalStateException if context is read only
+ */
+ protected final void checkWriteable()
+ throws IllegalStateException
+ {
+ if ( m_readOnly )
+ {
+ final String message =
+ "LifecycleExtensionsManager is read only and can not be
modified";
+ throw new IllegalStateException( message );
+ }
+ }
+
+ /**
* <code>CachedArrayList</code> class.
*
* <p>
@@ -404,6 +450,16 @@
private Object[] m_cache = EMPTY_ARRAY;
/**
+ * Become a copy of another CachedArrayList.
+ */
+ public void copyFrom( final CachedArrayList original )
+ {
+ m_proxy.clear();
+ m_proxy.addAll( original.m_proxy );
+ m_cache = original.m_cache; // it won't mutate anyway :-)
+ }
+
+ /**
* Add an object to the list
*
* @param object an <code>Object</code> value
@@ -440,13 +496,14 @@
}
/**
- * Obtain an iterator
+ * Obtain an iterator. This iterator is read-only.
*
* @return an <code>Iterator</code> value
*/
public Iterator iterator()
{
- return m_proxy.iterator();
+ final Iterator base = m_proxy.iterator();
+ return new UnmodifiableIterator( base );
}
/**
@@ -500,6 +557,35 @@
public Object[] toArray()
{
return m_cache;
+ }
+ }
+
+ /**
+ * Read only iterator.
+ */
+ private static class UnmodifiableIterator implements Iterator
+ {
+ private final Iterator m_base;
+
+ UnmodifiableIterator( final Iterator base )
+ {
+ if ( base == null ) throw new NullPointerException( "base can not be
null" );
+ m_base = base;
+ }
+
+ public boolean hasNext()
+ {
+ return m_base.hasNext();
+ }
+
+ public Object next()
+ {
+ return m_base.next();
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException( "Unmodifiable iterator" );
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]