Example usage for org.apache.commons.collections BoundedFifoBuffer toArray

List of usage examples for org.apache.commons.collections BoundedFifoBuffer toArray

Introduction

In this page you can find the example usage for org.apache.commons.collections BoundedFifoBuffer toArray.

Prototype

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) 

Source Link

Usage

From source file:org.apache.avalon.fortress.impl.AbstractContainer.java

/**
 * Initializes the impl and all the components it hosts so that they are ready to be used.
 * Unless components ask for lazy activation, this is where they are activated.
 *
 * @throws CompositeException if one or more components could not be initialized.
 *                   The system <i>is</i> running properly so if the missing components are
 *                   not vital to operation, it should be possible to recover gracefully
 *///from www .j  a  v  a2s  .c o  m
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");
    }
}