All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.plexus.component.manager.AbstractComponentManager Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.codehaus.plexus.component.manager;

/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.builder.AbstractComponentBuildListener;
import org.codehaus.plexus.component.builder.ComponentBuilder;
import org.codehaus.plexus.component.builder.XBeanComponentBuilder;
import org.codehaus.plexus.component.factory.ComponentInstantiationException;
import org.codehaus.plexus.component.repository.ComponentDescriptor;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
import org.codehaus.plexus.lifecycle.LifecycleHandler;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.PhaseExecutionException;

public abstract class AbstractComponentManager
    implements ComponentManager
{
    protected final MutablePlexusContainer container;

    private final ClassRealm realm;

    protected final ComponentDescriptor componentDescriptor;

    private final Class type;

    private final String role;
    
    private final String roleHint;

    protected final ComponentBuilder builder = new XBeanComponentBuilder(this);


    private final LifecycleHandler lifecycleHandler;

    /**
     * Contains a mapping from singleton instances to the realms
     * they were used to configure with. This realm will be used to
     * call all lifecycle methods.
     * @return a synchronized map, make sure to synchronize the map when iterating.
     */
    protected final Map componentContextRealms = Collections.synchronizedMap(new HashMap());

    private int connections;

    public AbstractComponentManager( MutablePlexusContainer container,
                       LifecycleHandler lifecycleHandler,
                       ComponentDescriptor componentDescriptor,
                       String role,
                       String roleHint)
    {
        if ( container == null )
        {
            throw new NullPointerException( "container is null" );
        }
        this.container = container;

        if ( lifecycleHandler == null )
        {
            throw new NullPointerException( "lifecycleHandler is null" );
        }
        this.lifecycleHandler = lifecycleHandler;

        if ( componentDescriptor == null )
        {
            throw new NullPointerException( "componentDescriptor is null" );
        }
        this.componentDescriptor = componentDescriptor;

        if ( role == null )
        {
            throw new NullPointerException( "role is null" );
        }
        this.role = role;

        if ( roleHint == null )
        {
            throw new NullPointerException( "roleHint is null" );
        }
        this.roleHint = roleHint;

        this.realm = componentDescriptor.getRealm();

        this.type = componentDescriptor.getImplementationClass();
    }

    public ComponentDescriptor getComponentDescriptor()
    {
        return componentDescriptor;
    }

    public Class getType()
    {
        return type;
    }

    public ClassRealm getRealm()
    {
        return realm;
    }

    public String getRole()
    {
        return role;
    }

    public String getRoleHint()
    {
        return roleHint;
    }

    public LifecycleHandler getLifecycleHandler()
    {
        return lifecycleHandler;
    }

    protected void incrementConnectionCount()
    {
        connections++;
    }

    protected void decrementConnectionCount()
    {
        connections--;
    }

    protected boolean connected()
    {
        return connections > 0;
    }

    public int getConnections()
    {
        return connections;
    }

    // ----------------------------------------------------------------------
    // Lifecylce Management
    // ----------------------------------------------------------------------

    protected T createComponentInstance()
        throws ComponentInstantiationException, ComponentLifecycleException
    {
        return builder.build(componentDescriptor, realm, new AbstractComponentBuildListener() {
            public void componentCreated( ComponentDescriptor componentDescriptor, Object component, ClassRealm realm) {
                componentContextRealms.put( component, realm );
            }
        });
    }

    protected void endComponentLifecycle( Object component )
        throws ComponentLifecycleException
    {
        ClassRealm contextRealm = componentContextRealms.remove( component );
        if ( contextRealm == null )
        {
            contextRealm = container.getLookupRealm( component );
        }

        try
        {
            getLifecycleHandler().end( component, this, contextRealm );
        }
        catch ( PhaseExecutionException e )
        {
            throw new ComponentLifecycleException( "Error ending component lifecycle", e );
        }
    }

    public MutablePlexusContainer getContainer()
    {
        return container;
    }

    public Logger getLogger()
    {
        return container.getLogger();
    }

    public void dissociateComponentRealm( ClassRealm realm )
        throws ComponentLifecycleException
    {
        synchronized ( componentContextRealms )
        {
            for ( Iterator> iterator = componentContextRealms.entrySet().iterator(); iterator.hasNext(); )
            {
                Entry entry = iterator.next();
                ClassRealm componentRealm = entry.getValue();

                if ( componentRealm.getId().equals( realm.getId() ) )
                {
                    iterator.remove();
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy