avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/service ServiceRepository.java ServiceManager.java
Date Fri, 27 Dec 2002 16:11:42 GMT
mcconnell    2002/12/27 08:11:42

  Added:       assembly/src/java/org/apache/avalon/assembly/engine/service
                        ServiceRepository.java
  Removed:     assembly/src/java/org/apache/avalon/assembly/engine/service
                        ServiceManager.java
  Log:
  Renamed ServiceManager to ServiceRepository to eliminate naming conflict with framework
ServiceManager.
  
  Revision  Changes    Path
  1.1                  avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/service/ServiceRepository.java
  
  Index: ServiceRepository.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Jakarta", "Apache Avalon", "Avalon Framework" 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 apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   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 (INCLU-
   DING, 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/>.
  
  */
  
  package org.apache.avalon.assembly.engine.service;
  
  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;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  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 repository provides support for the storage and retrival 
   * of service defintions.
   *
   * @author <a href="mailto:avalon-dev@jakarta.apache.org">Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2002/12/27 16:11:42 $
   */
  public class ServiceRepository extends AbstractLogEnabled
  {
      //==============================================================
      // state
      //==============================================================
  
     /**
      * The classloader supplied to the manager.
      */
      private ClassLoader m_classloader;
  
     /**
      * The parent service manager (may be null)
      */
      private ServiceRepository m_parent;
  
     /**
      * The service builder.
      */
      private static final ServiceBuilder DEFAULT_BUILDER = new ServiceBuilder();
  
      /**
       * List of service entries.
       */
      private List m_services = new ArrayList();
  
      //==============================================================
      // constructor
      //==============================================================
  
     /**
      * Creation of a new root service manager.
      * @param classloader the classloder to use
      * @exception NullPointerException if the classloader is null
      */
      public ServiceRepository( ClassLoader classloader ) throws NullPointerException
      {
          this( classloader, null );
      }
  
     /**
      * Creation of a new service manager.
      * @param classloader the classloder to use
      * @param parent the parent type manager
      * @exception NullPointerException if the classloader is null
      */
      public ServiceRepository( ClassLoader classloader, ServiceRepository parent ) throws
NullPointerException
      {
          if( classloader == null )
          {
              throw new NullPointerException("classloader");
          }
          m_classloader = classloader;
          m_parent = parent;
      }
  
      //==============================================================
      // implemetation
      //==============================================================
  
      /**
       * Create a services associated from a supplied path. 
       *
       * @param clazz the service class
       * @return the service defintions
       * @exception ServiceException if an error occurs during service creation
       */
      public Service createService( Class clazz ) throws ServiceException
      {
          if( clazz == null )
          {
              throw new NullPointerException("clazz");
          }
  
          try
          {
              return DEFAULT_BUILDER.build( clazz.getName(), clazz.getClassLoader() );
          }
          catch( Throwable e )
          {
              final String error = 
                 "Could not create a service relative to the path: " 
                 + clazz.getName() + " due to a service build error.";
              throw new ServiceException( error, e );
          }
      }
  
      /**
       * Create a service instance based on a supplied classname.
       *
       * @param classname the component implementation classname
       * @return the service defintion
       * @exception ServiceException if an error occurs during service creation
       */
      public Service createService( String classname ) throws ServiceException
      {
          if( classname == null )
          {
              throw new NullPointerException("classname");
          }
  
          try
          {
              Class clazz = m_classloader.loadClass( classname );
              return createService( clazz );
          }
          catch( Throwable e )
          {
              final String error = 
                "Unexpected error while attempting to build a service from the classname:
" + classname;
              throw new ServiceException( error, e );
          }
      }
  
      /**
       * Locate a {@link Service} instances associated with the 
       * supplied classname and version. 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 version the service version
       * @return the service matching the supplied classname and version.
       * @exception UnknownServiceException if a matching service cannot be found
       */
      public Service getService( String classname, Version version ) throws UnknownServiceException
      {
          return getService( new ReferenceDescriptor( classname, version ) );
      }
  
      /**
       * Locate a {@link 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 )
          {
              if( m_parent != null )
              {
                  return m_parent.getService( reference );
              }
              else
              {
                  final String error = "Unknown service defintion: " + reference;
                  throw new UnknownServiceException( error );
              }
          }
          return service;
      }
  
      private Service getLocalService( ReferenceDescriptor reference )
      {
          Iterator iterator = m_services.iterator();
          while( iterator.hasNext() )
          {
              Service service = (Service) iterator.next();
              if( service.equals( reference ) )
              {
                  return service;
              }
          }
          return null;
      }
  
      /**
       * Add a service to the manager.
       *
       * @param service the service defintion
       * @exception DuplicateServiceException if the service already exists
       * @exception ServiceException if the service definition is invalid
      */
      public void addService( Service service ) throws DuplicateServiceException, ServiceException
      {
          if( getLogger() == null )
          {
              throw new IllegalStateException("logging");
          }
  
          //
          // 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
          //
  
          verifyService( service );
  
          //
          // make sure that there is not already a local defintion for 
          // this service
          //
  
          m_services.add( service );
   
          if( getLogger().isDebugEnabled() )
          {
              StringBuffer buffer = new StringBuffer();
              buffer.append( "add: " + service.getClassname() );
              buffer.append( ":" + service.getVersion() );
              String[] names = service.getAttributeNames();
              buffer.append( ", entries: " + service.getEntries().length );
              buffer.append( ", attributes: " + service.getAttributeNames().length );
              getLogger().debug( buffer.toString() );
          }
      }
  
     /**
      * Verify that a class exists within the classloader representing the 
      * service type.
      * @param service the service to verify
      * @exception ServiceException if a verification error occurs
      */
      private void verifyService( Service service ) throws ServiceException
      {
          Class clazz;
          try
          {
              clazz = m_classloader.loadClass( service.getClassname() );
          }
          catch( Throwable e )
          {
              final String error = "Unresolvable service class.";
              throw new ServiceException( error, e );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message