public void initialize()
throws CompositeException, Exception
{
// go over all components
final Iterator i = m_components.iterator();
final BoundedFifoBuffer buffer = new BoundedFifoBuffer( Math.max( m_components.size(), 1 ) );
// just to be on the safe side
m_extManager.makeReadOnly();
verifyComponents();
ComponentHandlerEntry entry;
while ( i.hasNext() )
{
entry = (ComponentHandlerEntry) i.next();
try
{
final ComponentHandler handler = entry.getHandler();
// Depending on the activation policy of the component decide
// how to initialize the component. Make sure that we can
// perform the specified activation policy, if not modify it.
int activation = entry.getMetaData().getActivation();
if ( activation == ComponentHandlerMetaData.ACTIVATION_BACKGROUND )
{
// If a sink is not set then we must change to inline.
if ( null == m_commandSink )
{
activation = ComponentHandlerMetaData.ACTIVATION_INLINE;
}
}
// We now have an activation policy we can handle.
switch ( activation )
{
case ComponentHandlerMetaData.ACTIVATION_BACKGROUND:
// Add a command to initialize the component to the command
// sink so it will be initialized asynchronously in the
// background.
final PrepareHandlerCommand element =
new PrepareHandlerCommand( handler, getLogger() );
m_commandSink.enqueue( element );
break;
case ComponentHandlerMetaData.ACTIVATION_INLINE:
// Initialize the component now.
handler.prepareHandler();
break;
default: // ComponentHandlerMetaData.ACTIVATION_LAZY
if ( getLogger().isDebugEnabled() )
{
final String message = "ComponentHandler (" + handler +
") has specified a lazy activation policy, " +
"initialization deferred until first use";
getLogger().debug( message );
}
break;
}
}
catch ( final CascadingException e )
{
final String cName = entry.getMetaData().getName();
if ( getLogger().isWarnEnabled() )
{
final String message = "Could not initialize component " + cName;
getLogger().warn( message, e );
final String cause = "Cause for exception";
getLogger().warn( cause, e.getCause() );
}
buffer.add( e );
}
catch ( final Exception e )
{
final String cName = entry.getMetaData().getName();
if ( getLogger().isWarnEnabled() )
{
final String message = "Could not initialize component " + cName;
getLogger().warn( message, e );
}
buffer.add( e );
}
catch ( final LinkageError le )
{
final String cName = entry.getMetaData().getName();
if ( getLogger().isWarnEnabled() )
{
final String message = "Could not initialize component " + cName;
getLogger().warn( message, le );
}
buffer.add( le );
}
}
// if we were unable to activate one or more components,
// throw an exception
if ( buffer.size() > 0 )
{
throw new CompositeException( (Exception[]) buffer.toArray( new Exception[0] ),
"unable to instantiate one or more components" );
}
}