CVS: plexus-container-new/src/java/org/apache/plexus/service/repository ComponentDescriptor.java,NONE,1.1 ComponentHousing.java,NONE,1.1 ComponentManager.java,NONE,1.1 ComponentRepository.java,NONE,1.1 ComponentRepositoryFactory.java,NONE,1.1 DefaultComponentRepository.java,NONE,1.1 DefaultServiceRepository.java,1.1.1.1,NONE ServiceCapsule.java,1.1.1.1,NONE ServiceDescriptor.java,1.1.1.1,NONE ServiceRepository.java,1.2,NONE ServiceRepositoryFactory.java,1.1,NONE
[email protected] Thu, 1 May 2003 14:35:38 -0500
| Newsgroups | gmane.comp.java.plexus.devel |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/service/repository In directory eng.werken.com:/tmp/cvs-serv18124/src/java/org/apache/plexus/service/repository Added Files: ComponentDescriptor.java ComponentHousing.java ComponentManager.java ComponentRepository.java ComponentRepositoryFactory.java DefaultComponentRepository.java Removed Files: DefaultServiceRepository.java ServiceCapsule.java ServiceDescriptor.java ServiceRepository.java ServiceRepositoryFactory.java Log Message: o This will require a separate message as more than a few changes have occurred in order to support handling for various instantiation strategies and scriptable components. --- NEW FILE: ComponentDescriptor.java --- package org.apache.plexus.service.repository; /* ---------------------------------------------------------------------------- * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Plexus", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [email protected]. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ---------------------------------------------------------------------------- * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * ---------------------------------------------------------------------------- */ import org.apache.avalon.framework.configuration.Configuration; import java.util.Map; /** Component instantiation description. * * @author <a href="mailto:[email protected]">Jason van Zyl</a> * @author <a href="mailto:[email protected]">bob mcwhirter</a> * * @version $Id: ComponentDescriptor.java,v 1.1 2003/05/01 19:35:36 jvanzyl Exp $ */ public class ComponentDescriptor { // ---------------------------------------------------------------------- // Instance members // ---------------------------------------------------------------------- /** Component role name. */ private String role; /** Role hint. */ private String roleHint; /** Component id, unique within a role, for identified component instance. */ private String id; /** Name of the component class. */ private String implementation; /** Configuration for the component. */ private Configuration configuration; /** Parameters for the component. */ private Map parameters; /** Instantiation strategy. */ private String instantiationStrategy; // ---------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------- /** Construct. */ public ComponentDescriptor() { this.id = "*"; } // ---------------------------------------------------------------------- // Instance methods // ---------------------------------------------------------------------- public String getComponentKey() { if ( getId() != null ) { return getRole() + getId(); } else if ( getRoleHint() != null ) { return getRole() + getRoleHint(); } return getRole(); } /** Set the id. * * <p> * The id must be unique within a role if this component * is an identified component. If it is a factory-produced * or other unindentified component, an id of '*' is already * the default. * </p> * * @param id The component's id. */ public void setId( String id ) { this.id = id; } /** Retrieve the id. * * <p> * This id will be unique within a role if this component * is an identified component. If it is a factory-produced * or other unindentified component, the id is '*". * </p> * * @return The id. */ public String getId() { return this.id; } /** Set the role name of the component. * * @param role The role name. */ public void setRole( String role ) { this.role = role; } /** Retrieve the role name of the component. * * @return THe role name. */ public String getRole() { return role; } public String getRoleHint() { return roleHint; } public void setRoleHint( String roleHint ) { this.roleHint = roleHint; } /** Set the class name of the component. * * @param implementation The class name of the component. */ public void setImplementation( String implementation ) { this.implementation = implementation; } /** Retrieve the class name of the component. * * @return The class name. */ public String getImplementation() { return implementation; } /** * * @return */ public String getInstantiationStrategy() { return instantiationStrategy; } /** * * @param instantiationStrategy */ public void setInstantiationStrategy( String instantiationStrategy ) { this.instantiationStrategy = instantiationStrategy; } /** Set the <code>Configuration</code> for the component. * * @param configuration The configuration. */ public void setConfiguration( Configuration configuration ) { this.configuration = configuration; } /** Retrieve the <code>Configuration</code> for the component. * * @return The configuration. */ public Configuration getConfiguration() { return configuration; } /** Set the <code>Configuration</code> for the component. * * @param parameters The configuration. */ public void setParameters( Map parameters ) { this.parameters = parameters; } /** Retrieve the <code>Configuration</code> for the component. * * @return The configuration. */ public Map getParameters() { return parameters; } } --- NEW FILE: ComponentHousing.java --- package org.apache.plexus.service.repository; /** * * * @author <a href="mailto:[email protected]">Jason van Zyl</a> * * @version $Id: ComponentHousing.java,v 1.1 2003/05/01 19:35:36 jvanzyl Exp $ */ public class ComponentHousing { /** Component Manager that oversees this intance. */ private ComponentManager componentManager; /** The component being housed. */ private Object component; /** How many clients are connected to this component. */ private int connections; // ---------------------------------------------------------------------- // Accessors // ---------------------------------------------------------------------- public Object getComponent() { return component; } public void setComponent( Object component ) { this.component = component; } public int getConnections() { return connections; } public void setConnections( int connections ) { this.connections = connections; } public ComponentManager getComponentManager() { return componentManager; } public void setComponentManager( ComponentManager componentManager ) { this.componentManager = componentManager; } } --- NEW FILE: ComponentManager.java --- package org.apache.plexus.service.repository; import org.apache.avalon.framework.service.ServiceException; import org.apache.plexus.service.repository.instance.InstanceManager; /** House the information for a single instantiated service. * */ public class ComponentManager { /** Component descriptor. */ private ComponentDescriptor componentDescriptor; /** Instance Manager descriptor. */ private ComponentDescriptor instanceManagerDescriptor; /** Instance Manager. */ private InstanceManager instanceManager; /** ClassLoader */ private ClassLoader classLoader; /** Constuctor. */ public ComponentManager( ComponentDescriptor componentDescriptor, ComponentDescriptor instanceManagerDescriptor, ClassLoader classLoader ) { this.componentDescriptor = componentDescriptor; this.instanceManagerDescriptor = instanceManagerDescriptor; this.classLoader = classLoader; } // ---------------------------------------------------------------------- // Lifecylce Management // ---------------------------------------------------------------------- public void initialize() throws Exception { Class c = classLoader.loadClass( instanceManagerDescriptor.getImplementation() ); instanceManager = (InstanceManager) c.newInstance(); instanceManager.setClassLoader( classLoader ); instanceManager.setImplementation( componentDescriptor.getImplementation() ); instanceManager.setComponentManager( this ); instanceManager.initialize(); } // ---------------------------------------------------------------------- // Accessors // ---------------------------------------------------------------------- /** * * @return */ public ComponentDescriptor getComponentDescriptor() { return componentDescriptor; } /** * * @param serviceDescriptor */ public void setComponentDescriptor( ComponentDescriptor serviceDescriptor ) { this.componentDescriptor = serviceDescriptor; } /** * * @return */ public ComponentHousing getComponentHousing() throws ServiceException { try { return getInstanceManager().getInstance(); } catch ( Exception e ) { throw new ServiceException( e.getMessage(), e ); } } public InstanceManager getInstanceManager() { return instanceManager; } public void setInstanceManager( InstanceManager instanceManager ) { this.instanceManager = instanceManager; } } --- NEW FILE: ComponentRepository.java --- package org.apache.plexus.service.repository; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.logger.Logger; import org.apache.avalon.framework.service.ServiceException; import org.apache.plexus.PlexusContainer; public interface ComponentRepository { void configure( Configuration defaultConfiguration, Configuration configuration ); void initialize() throws Exception; Object lookup( String role ) throws ServiceException; Object lookup( String role, String id ) throws ServiceException; boolean hasService( String role ); boolean hasService( String role, String id ); void release( Object service ); void dispose(); void setPlexusContainer( PlexusContainer container ); // Information int configuredComponents(); int instantiatedComponents(); ClassLoader getClassLoader(); void enableLogging( Logger logger ); } --- NEW FILE: ComponentRepositoryFactory.java --- package org.apache.plexus.service.repository; import org.apache.avalon.framework.configuration.Configuration; import org.apache.plexus.PlexusContainer; import org.apache.plexus.factory.AbstractPlexusFactory; import org.apache.plexus.logging.LoggerManager; public class ComponentRepositoryFactory extends AbstractPlexusFactory { public static ComponentRepository create( Configuration defaultConfiguration, Configuration configuration, LoggerManager loggerManager, PlexusContainer container, ClassLoader classLoader ) throws Exception { String implementation; if ( configuration.getChild( "service-repository", false ) != null ) { implementation = configuration.getChild( "service-repository" ).getChild( "implementation" ).getValue(); } else { implementation = defaultConfiguration.getChild( "service-repository" ).getChild( "implementation" ).getValue(); } ComponentRepository sr = (ComponentRepository) getInstance( implementation, classLoader ); sr.enableLogging( loggerManager.getLogger( "service-repository" ) ); sr.setPlexusContainer( container ); sr.configure( defaultConfiguration, configuration ); sr.initialize(); return sr; } } --- NEW FILE: DefaultComponentRepository.java --- package org.apache.plexus.service.repository; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.service.ServiceException; import org.apache.plexus.PlexusContainer; import org.apache.plexus.lifecycle.LifecycleHandler; import org.apache.plexus.lifecycle.UndefinedLifecycleHandlerException; import org.apache.plexus.logging.AbstractLogEnabled; import org.apache.plexus.service.repository.instance.InstanceManager; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * @todo find appropriate lifecycle handler */ public class DefaultComponentRepository extends AbstractLogEnabled implements ComponentRepository { // ---------------------------------------------------------------------- // Constants // ---------------------------------------------------------------------- /** Components tag. */ private static String COMPONENTS = "components"; /** Component tag. */ private static String COMPONENT = "component"; /** Id tag. */ private static String ID = "id"; /** Role tag. */ private static String ROLE = "role"; /** Role tag. */ private static String ROLE_HINT = "role-hint"; /** Implementation tag. */ private static String IMPLEMENTATION = "implementation"; /** Configuration tag. */ private static String INSTANTIATION_STRATEGY = "instantiation-strategy"; /** Configuration tag. */ private static String CONFIGURATION = "configuration"; // The instantiation strategies will be broken out into separate classes so // breath easy :-) private static String PER_LOOKUP_STRATEGY = "per-lookup"; private static String POOLABLE_STRATEGY = "poolable"; private static String SINGLETON_STRATEGY = "singleton"; // ---------------------------------------------------------------------- // Instance Members // ---------------------------------------------------------------------- /** Default configuration */ private Configuration defaultConfiguration; /** Configuration */ private Configuration configuration; /** Map of service descriptors keyed by role. */ private Map componentDescriptors; /** Map of component managers by component key. */ private Map componentManagers; /** Map of service capsules key by the service object. */ private Map componentHousings; private PlexusContainer plexusContainer; /** Constructor. */ public DefaultComponentRepository() { componentDescriptors = new HashMap(); componentManagers = new HashMap(); componentHousings = new HashMap(); } // take the lifecycle handler stuff out of here // ---------------------------------------------------------------------- // Lifecylce Management // ---------------------------------------------------------------------- /** Configure the service repository. * * @param configuration */ public void configure( Configuration defaultConfiguration, Configuration configuration ) { this.defaultConfiguration = defaultConfiguration; this.configuration = configuration; } /** Initialize the service repository. * * @throws Exception */ public void initialize() throws Exception { Configuration[] defaultComponentConfigurations = defaultConfiguration.getChild( COMPONENTS ).getChildren( COMPONENT ); for ( int i = 0; i < defaultComponentConfigurations.length; i++ ) { addComponentDescriptor( createComponentDescriptor( defaultComponentConfigurations[i] ) ); } Configuration[] componentConfigurations = configuration.getChild( COMPONENTS ).getChildren( COMPONENT ); for ( int i = 0; i < componentConfigurations.length; i++ ) { addComponentDescriptor( createComponentDescriptor( componentConfigurations[i] ) ); } } // ---------------------------------------------------------------------- // Accessors // ---------------------------------------------------------------------- /** * * @return */ public PlexusContainer getPlexusContainer() { return plexusContainer; } /** * * @param plexusContainer */ public void setPlexusContainer( PlexusContainer plexusContainer ) { this.plexusContainer = plexusContainer; } /** * * @return */ public ClassLoader getClassLoader() { return getPlexusContainer().getClassLoader(); } public int configuredComponents() { return getComponentDescriptors().size(); } public int instantiatedComponents() { return getComponentManagers().size(); } // ---------------------------------------------------------------------- // Package Scoped Accessors // ---------------------------------------------------------------------- /** * * @return */ Map getComponentDescriptors() { return componentDescriptors; } /** * * @return */ Map getComponentManagers() { return componentManagers; } ComponentManager getComponentManager( String componentKey ) { return (ComponentManager) getComponentManagers().get( componentKey ); } /** * * @return */ Map getComponentHousings() { return componentHousings; } // ---------------------------------------------------------------------- // Component Descriptor processing and Holder creation. // ---------------------------------------------------------------------- /** * * @return The new component instance. * * @throws Exception If an error occurs while attempting to locate * the class or instantiate the component object. */ ComponentManager instantiateComponentManager( ComponentDescriptor descriptor ) throws Exception { ComponentDescriptor imd = (ComponentDescriptor) getComponentDescriptors().get( InstanceManager.ROLE + descriptor.getInstantiationStrategy() ); ComponentManager componentManager = new ComponentManager( descriptor, imd, getClassLoader() ); componentManager.initialize(); getComponentManagers().put( descriptor.getComponentKey(), componentManager ); return componentManager; } /** * Create a service descriptor. * * @param configuration * @return * @throws Exception */ ComponentDescriptor createComponentDescriptor( Configuration configuration ) throws Exception { ComponentDescriptor componentDescriptor = new ComponentDescriptor(); componentDescriptor.setRole( configuration.getChild( ROLE ).getValue() ); componentDescriptor.setRoleHint( configuration.getChild( ROLE_HINT ).getValue( null ) ); componentDescriptor.setImplementation( configuration.getChild( IMPLEMENTATION ).getValue() ); componentDescriptor.setId( configuration.getChild( ID ).getValue( null ) ); componentDescriptor.setInstantiationStrategy( configuration.getChild( INSTANTIATION_STRATEGY ).getValue( SINGLETON_STRATEGY ) ); componentDescriptor.setConfiguration( configuration.getChild( CONFIGURATION ) ); return componentDescriptor; } /** * Adds a component to the ServiceBroker. If the component has a * ServiceSelector, the appropriate action is taken. * * @param descriptor */ void addComponentDescriptor( ComponentDescriptor descriptor ) { getComponentDescriptors().put( descriptor.getComponentKey(), descriptor ); getLogger().debug( "Adding descriptor for service key: " + descriptor.getComponentKey() ); } // ---------------------------------------------------------------------- // Service lookup methods // ---------------------------------------------------------------------- public Object lookup( String key ) throws ServiceException { // Attempt to lookup the componentManager by key. ComponentManager componentManager = getComponentManager( key ); Object component = null; if ( componentManager == null ) { // We need to create an instance of this componentManager. ComponentDescriptor descriptor = (ComponentDescriptor) getComponentDescriptors().get( key ); if ( descriptor == null ) { getLogger().error( "Non existant component: " + key ); throw new ServiceException( key, "Non existant component for key " + key + "." ); } getLogger().debug( "Found a service descriptor for key: " + key ); try { componentManager = instantiateComponentManager( descriptor ); } catch ( Exception e ) { getLogger().error( "Could not create component: " + key, e ); throw new ServiceException( key, "Could not create component for key " + key + "!", e ); } // We do this so we know what to do when releasing. ComponentHousing housing = componentManager.getComponentHousing(); if ( housing == null ) { throw new ServiceException( key, "ComponentHousing is null."); } component = componentManager.getComponentHousing().getComponent(); if ( component == null ) { throw new ServiceException( key, "Component is null."); } getComponentHousings().put( component, housing ); // Start the component lifecycle startComponentLifecycle( housing ); } else { component = componentManager.getComponentHousing().getComponent(); } return component; } public Object lookup( String role, String id ) throws ServiceException { return lookup( role + id ); } /** * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String) */ public boolean hasService( String role ) { return getComponentDescriptors().containsKey( role ); } public boolean hasService( String role, String id ) { return getComponentDescriptors().containsKey( role + id ); } /** * Release the specified component. * * @see org.apache.avalon.framework.service.ServiceManager#release(java.lang.Object) */ public void release( Object component ) { ComponentHousing housing = (ComponentHousing) getComponentHousings().get( component ); if ( housing != null ) { // Only call the end of lifecyle events when there are // no more users of this component, doing so otherwise // might lead in plexus giving out a component that has // been effectively extinguished or even worse, a client // with an existing reference to the valid component may // have the rug pulled out from under them by another // client releasing the component. try { endComponentLifecycle( housing ); } catch ( Exception e ) { getLogger().error( "Error ending component lifecycle", e ); } // This is where we need to track the count for pools and reuse. // Now get rid of the Service capsule references. String serviceKey = housing.getComponentManager().getComponentDescriptor().getComponentKey(); getComponentManagers().remove( serviceKey ); getComponentHousings().remove( component ); housing = null; component = null; } } /** * @see org.apache.avalon.framework.activity.Disposable#dispose() */ public void dispose() { disposeAllComponents(); } /** * Method disposeAllComponents. */ protected void disposeAllComponents() { Set roles = getComponentManagers().keySet(); for ( Iterator i = roles.iterator(); i.hasNext(); ) { String eachRole = (String) i.next(); //disposeComponents( (Collection) getComponentManagers().get( eachRole ) ); i.remove(); } } // ---------------------------------------------------------------------- // Lifecycle Handling // ---------------------------------------------------------------------- protected LifecycleHandler getLifecycleHandler( String role ) throws UndefinedLifecycleHandlerException { return getPlexusContainer().getLifecycleHandler(); } /** Start a component's lifecycle. * */ protected void startComponentLifecycle( ComponentHousing housing ) { try { LifecycleHandler lh = getLifecycleHandler( housing.getComponentManager().getComponentDescriptor().getRole() ); lh.startLifecycle( housing ); } catch ( Exception e ) { getLogger().error( "Cannot start component lifecycle with role : " + housing.getComponentManager().getComponentDescriptor().getRole(), e ); } } /** End a component's lifecycle. * */ protected void endComponentLifecycle( ComponentHousing housing ) { try { LifecycleHandler lh = getLifecycleHandler( housing.getComponentManager().getComponentDescriptor().getRole() ); lh.endLifecycle( housing ); } catch ( Exception e ) { getLogger().error( "Cannot start component lifecycle with role : " + housing.getComponentManager().getComponentDescriptor().getRole(), e ); } } } --- DefaultServiceRepository.java DELETED --- --- ServiceCapsule.java DELETED --- --- ServiceDescriptor.java DELETED --- --- ServiceRepository.java DELETED --- --- ServiceRepositoryFactory.java DELETED ---