You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2001/11/30 22:33:13 UTC
cvs commit: jakarta-avalon/src/test/org/apache/avalon/framework/test ComponentTestCase.java FullLifecycleComponent.java
bloritsch 01/11/30 13:33:13
Added: src/java/org/apache/avalon/framework AbstractComponent.java
src/test/org/apache/avalon/framework/test
ComponentTestCase.java FullLifecycleComponent.java
Removed: src/java/org/apache/avalon/framework ComponentUtil.java
Log:
first pass at converting to an abstract component
Revision Changes Path
1.1 jakarta-avalon/src/java/org/apache/avalon/framework/AbstractComponent.java
Index: AbstractComponent.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.framework;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.activity.Suspendable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Loggable;
import org.apache.avalon.framework.parameters.Parameterizable;
/**
* This class provides basic facilities for enforcing Avalon's contracts
* within your own code.
*
* @author <a href="bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/11/30 21:33:13 $
*/
public abstract class AbstractComponent implements Component
{
private static final String OUT_OF_ORDER = "Initialization perfomed out of order";
private static final long LOG_ENABLED = 0x00000001;
private static final long CONTEXTUALIZED = 0x00000002;
private static final long PARAMETERIZED = 0x00000004;
private static final long CONFIGURED = 0x00000008;
private static final long COMPOSED = 0x00000010;
private static final long ACTIVE = 0x10000000;
private static final long INITIALIZED = 0x00000012;
private static final long STARTED = 0x00000014;
private static final long SUSPENDED = 0x01000000;
private static final long STOPPED = 0x00000018;
private static final long DISPOSED = 0x00000020;
private static final long INIT_MASK = LOG_ENABLED | CONTEXTUALIZED |
PARAMETERIZED | CONFIGURED | COMPOSED | INITIALIZED | STARTED;
private final long m_mask;
private long m_state;
/**
* Create state mask from this component instance.
*/
public AbstractComponent()
{
long mask = 0;
if( this instanceof LogEnabled ||
this instanceof Loggable )
{
mask |= LOG_ENABLED;
}
if( this instanceof Contextualizable )
{
mask |= CONTEXTUALIZED;
}
if( this instanceof Parameterizable )
{
mask |= PARAMETERIZED;
}
if( this instanceof Configurable )
{
mask |= CONFIGURED;
}
if( this instanceof Composable )
{
mask |= COMPOSED;
}
if( this instanceof Initializable )
{
mask |= INITIALIZED;
}
if( this instanceof Disposable )
{
mask |= DISPOSED;
}
if( this instanceof Startable )
{
mask |= STARTED | STOPPED;
}
if( this instanceof Suspendable )
{
mask |= SUSPENDED;
}
m_mask = mask &(~ACTIVE);
}
/**
* Throw an exception if a value is already set on a write-once object.
*
* @param source the source object to test against
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the source is already set.
*/
protected final void checkAssigned( Object source, final String message )
{
if ( null != source )
{
throw new IllegalStateException( message );
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the LOG_ENABLED state has already been set, if the component implements
* LogEnabled or Logger, and if the state has progressed beyond the Logger
* stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
public final void checkLogEnabled( final String message )
{
if( ( (m_state & m_mask & LOG_ENABLED) > 0 ) ||
( (m_mask & LOG_ENABLED) == 0 ) ||
( m_state > LOG_ENABLED ) )
{
throw new IllegalStateException( message );
}
m_state |= LOG_ENABLED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the CONTEXTUALIZED state has already been set, if the component implements
* Contextualizable, and if the state has progressed beyond the Context stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkContextualized( final String message )
{
if ( ( (m_state & m_mask & CONTEXTUALIZED) > 0 ) ||
( (m_mask & CONTEXTUALIZED) == 0 ) || ( m_state > CONTEXTUALIZED ) )
{
throw new IllegalStateException( message );
}
m_state |= CONTEXTUALIZED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the PARAMETERIZED state has already been set, if the component implements
* Parameterizable, and if the state has progressed beyond the Parameters stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkParameterized( final String message )
{
if ( ( (m_state & m_mask & PARAMETERIZED) > 0 ) ||
( (m_mask & PARAMETERIZED) == 0 ) || ( m_state > PARAMETERIZED ) )
{
throw new IllegalStateException( message );
}
m_state |= PARAMETERIZED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the CONFIGURED state has already been set, if the component implements
* Configurable, and if the state has progressed beyond the Configuration stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkConfigured( final String message )
{
if ( ( (m_state & m_mask & CONFIGURED) > 0 ) ||
( (m_mask & CONFIGURED) == 0 ) || ( m_state > CONFIGURED ) )
{
throw new IllegalStateException( message );
}
m_state |= CONFIGURED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the COMPOSED state has already been set, if the component implements
* Composable, and if the state has progressed beyond the Compose stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkComposed( final String message )
{
if ( ( (m_state & m_mask & COMPOSED) > 0 ) ||
( (m_mask & COMPOSED) == 0 ) || ( m_state > COMPOSED ) )
{
throw new IllegalStateException( message );
}
m_state |= COMPOSED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the INITIALIZED state has already been set, if the component implements
* Initializable, and if the state has progressed beyond the <code>initialize</code> stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkInitialized( final String message )
{
if ( ( (m_state & m_mask & INITIALIZED) > 0 ) ||
( (m_mask & INITIALIZED) == 0 ) || ( m_state > INITIALIZED ) )
{
throw new IllegalStateException( message );
}
m_state |= INITIALIZED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the STARTED state has already been set, if the component implements
* Startable, and if the state has progressed beyond the <code>start</code> stage.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkStarted( final String message )
{
if ( ( (m_state & m_mask & STARTED) > 0 ) ||
( (m_mask & STARTED) == 0 ) || ( m_state > STARTED ) )
{
throw new IllegalStateException( message );
}
m_state |= STARTED;
if ( (m_state & INIT_MASK) == (m_mask & INIT_MASK) )
{
m_state |= ACTIVE;
}
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the SUSPENDED state has already been set, if the component implements
* Suspendable, and if the Component is active.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkSuspended( final String message )
{
ComponentUtil.checkActive( m_state, m_mask, message );
if ( ( (m_state & m_mask & SUSPENDED) > 0 ) || ( (m_mask & SUSPENDED) == 0 ) )
{
throw new IllegalStateException( message );
}
m_state |= SUSPENDED;
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the SUSPENDED state has not been set, if the component implements
* Suspendable, and if the Component is active.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkResumed( final String message )
{
ComponentUtil.checkActive( m_state, m_mask, message );
if ( ( (m_state & m_mask & SUSPENDED) == 0 ) || ( (m_mask & SUSPENDED) == 0 ) )
{
throw new IllegalStateException( message );
}
m_state &= ~SUSPENDED;
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the STOPPED state has not been set, if the component implements
* Startable, and if the Component is active.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkStopped( final String message )
{
if ( ( (m_state & m_mask & STOPPED) > 0 ) ||
( (m_mask & STOPPED) == 0 ) || ( (m_state & m_mask) > STOPPED ) )
{
throw new IllegalStateException( message );
}
m_state &= ~ACTIVE;
m_state |= STOPPED;
}
/**
* Throw an exception if the initialization is out of order. It tests to see
* if the DISPOSED state has not been set, if the component implements
* Disposable.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the state is manage out of order
*/
protected final void checkDisposed( final String message )
{
if ( ( (m_state & m_mask & DISPOSED) > 0 ) || ( (m_mask & DISPOSED) == 0 ) )
{
throw new IllegalStateException( message );
}
m_state &= ~ACTIVE;
m_state |= DISPOSED;
}
/**
* Checks to see if the state is active.
*
* @param message the message to include in the thrown exception
* @throws IllegalStateException if the component is not active
*/
protected final void checkActive( final String message )
{
if ( (ACTIVE & m_state) > 0 ) {
return;
}
throw new IllegalStateException( message );
}
}
1.1 jakarta-avalon/src/test/org/apache/avalon/framework/test/ComponentTestCase.java
Index: ComponentTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.framework.test;
import org.apache.avalon.framework.component.DefaultComponentManager;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.logger.LogKitLogger;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.log.Hierarchy;
import junit.framework.TestCase;
/**
* This class provides basic facilities for enforcing Avalon's contracts
* within your own code.
*
* @author <a href="bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/11/30 21:33:13 $
*/
public final class ComponentTestCase
extends TestCase
{
public ComponentTestCase( String test )
{
super( test );
}
public void testCorrectLifecycle()
{
FullLifecycleComponent component = new FullLifecycleComponent();
try
{
component.enableLogging(new LogKitLogger(Hierarchy.getDefaultHierarchy().getLoggerFor("")));
component.contextualize(new DefaultContext());
component.parameterize(new Parameters());
component.configure(new DefaultConfiguration("", ""));
component.compose(new DefaultComponentManager());
component.initialize();
component.start();
component.suspend();
component.resume();
component.stop();
component.dispose();
}
catch ( Exception ise )
{
fail(ise.getMessage());
}
}
}
1.1 jakarta-avalon/src/test/org/apache/avalon/framework/test/FullLifecycleComponent.java
Index: FullLifecycleComponent.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.avalon.framework.test;
import org.apache.avalon.framework.AbstractComponent;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.activity.Suspendable;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentException;
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.Contextualizable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.thread.ThreadSafe;
/**
* This test class is used to test the AbstractComponent facilities for you.
*
* @author <a href="bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/11/30 21:33:13 $
*/
public final class FullLifecycleComponent
extends AbstractComponent
implements LogEnabled, Contextualizable, Parameterizable, Configurable,
Composable, Initializable, Startable, Suspendable, Disposable,
ThreadSafe
{
private Logger m_logger;
private Context m_context;
private Parameters m_parameters;
private Configuration m_configuration;
private ComponentManager m_componentManager;
public void enableLogging( Logger logger )
{
checkAssigned( m_logger, "Logger already set!" );
checkLogEnabled( "Logger: Initialization out of order." );
m_logger = logger;
}
public void contextualize( Context context )
throws ContextException
{
checkAssigned( m_context, "Context already set!" );
checkContextualized( "Context: Initialization out of order." );
m_context = context;
}
public void parameterize( Parameters params )
throws ParameterException
{
checkAssigned( m_parameters, "Parameters already set!" );
checkParameterized( "Parameters: Initialization out of order." );
m_parameters = params;
}
public void configure( Configuration config )
throws ConfigurationException
{
checkAssigned( m_configuration, "Configuration already set!" );
checkConfigured( "Configuration: Initialization out of order." );
m_configuration = config;
}
public void compose( ComponentManager manager )
throws ComponentException
{
checkAssigned( m_componentManager, "Component Manager already set!" );
checkComposed( "ComponentManager: Initialization out of order." );
}
public void initialize()
throws Exception
{
checkInitialized( "Initialize: Initialization out of order." );
}
public void start()
throws Exception
{
checkStarted( "Start: Initialization out of order." );
}
public void suspend()
{
checkSuspended( "Suspend: Initialization out of order." );
}
public void resume()
{
checkResumed( "Resume: Initialization out of order." );
}
public void stop()
throws Exception
{
checkStopped( "Stop: Initialization out of order." );
}
public void dispose()
{
checkDisposed( "Dispose: Initialization out of order." );
m_logger = null;
m_context = null;
m_parameters = null;
m_configuration = null;
m_componentManager = null;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>