mcconnell 2002/12/19 02:43:59 Modified: assembly/src/java/org/apache/avalon/assembly/service ServiceManager.java Log: Improved internal implementation. Revision Changes Path 1.4 +65 -69 avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/service/ServiceManager.java Index: ServiceManager.java =================================================================== RCS file: /home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/service/ServiceManager.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ServiceManager.java 15 Dec 2002 17:48:40 -0000 1.3 +++ ServiceManager.java 19 Dec 2002 10:43:59 -0000 1.4 @@ -50,8 +50,9 @@ package org.apache.avalon.assembly.service; -import java.util.Map; -import java.util.Hashtable; +import java.util.List; +import java.util.ArrayList; +import java.util.Iterator; import org.apache.avalon.framework.Version; import org.apache.avalon.framework.logger.AbstractLogEnabled; @@ -61,6 +62,7 @@ import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.meta.info.Service; import org.apache.avalon.meta.info.builder.ServiceBuilder; +import org.apache.avalon.meta.info.ReferenceDescriptor; /** * A service manager implemetation provides support for the creation, @@ -91,9 +93,9 @@ private static final ServiceBuilder DEFAULT_BUILDER = new ServiceBuilder(); /** - * Table of component types keyed by implementation classname. + * List of service entries. */ - private Hashtable m_services = new Hashtable(); + private List m_services = new ArrayList(); //============================================================== // constructor @@ -196,29 +198,50 @@ */ public Service getService( String classname, Version version ) throws UnknownServiceException { - Map map = getServiceMap( classname, false ); - if( map != null ) + return getService( new ReferenceDescriptor( classname, version ) ); + } + + /** + * Locate a [EMAIL PROTECTED] Service} instances associated with the + * supplied referecne descriptor. If a service defintion is not + * found locally, the implementation redirects the request to + * the parent service manager. + * + * @param classname the service class name + * @param reference the reference descriptor + * @return the service matching the supplied descriptor. + * @exception UnknownServiceException if a matching service cannot be found + */ + public Service getService( ReferenceDescriptor reference ) throws UnknownServiceException + { + Service service = getLocalService( reference ); + if( service == null ) { - Service service = (Service) map.get( version ); - if( service != null ) + if( m_parent != null ) { - return service; + return m_parent.getService( reference ); + } + else + { + final String error = "Unknown service defintion: " + reference; + throw new UnknownServiceException( error ); } } + return service; + } - // - // check the parent first - // - - if( m_parent != null ) - { - return m_parent.getService( classname, version ); - } - else + private Service getLocalService( ReferenceDescriptor reference ) + { + Iterator iterator = m_services.iterator(); + while( iterator.hasNext() ) { - final String error = "Unknown service defintion: " + classname + "/" + version; - throw new UnknownServiceException( error ); + Service service = (Service) iterator.next(); + if( service.equals( reference ) ) + { + return service; + } } + return null; } /** @@ -236,6 +259,21 @@ } // + // make sure that there is not already a local defintion for + // this service + // + + try + { + getService( service.getReference() ); + throw new DuplicateServiceException( service.toString() ); + } + catch( UnknownServiceException use ) + { + // continue + } + + // // make sure we are dealing with a service that is verified // @@ -246,61 +284,19 @@ // this service // - String classname = service.getClassname(); - Version version = service.getVersion(); - Map map = getServiceMap( classname, false ); - if( map != null ) - { - if( map.get( version ) != null ) - { - final String error = - "Duplicate service defintion: " + service; - throw new ServiceException( error ); - } - } - else - { - map = getServiceMap( classname, true ); - } + m_services.add( service ); if( getLogger().isDebugEnabled() ) { - /* StringBuffer buffer = new StringBuffer(); - buffer.append( "add: " + classname ); - buffer.append( "/" + service.getVersion() ); + buffer.append( "add: " + service.getClassname() ); + buffer.append( ":" + service.getVersion() ); String[] names = service.getAttributeNames(); - for( int i = 0; i < names.length; i++ ) - { - String name = names[ i ]; - String value = service.getAttribute( name ); - buffer.append( "\n attribute " + name + " = " + value ); - } - */ - getLogger().debug( "add: " + service ); - } - - map.put( version, service ); - } - - /** - * Internal utility that returns a map of services keyed by version. The - * map is resolved from the m_services list based on the classname key. - * @param classname the classname key - * @param create if TRUE create a new map if no map found - * @return the service map for the class - */ - private Map getServiceMap( String classname, boolean create ) - { - Map map = (Map) m_services.get( classname ); - if(( map == null ) && create ) - { - map = new Hashtable(); - m_services.put( classname, map ); + buffer.append( ", entries: " + service.getEntries().length ); + buffer.append( ", attributes: " + service.getAttributeNames().length ); + getLogger().debug( buffer.toString() ); } - return map; } - /** * Verify that a class exists within the classloader representing the
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>