ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/service DefaultServiceManager.java Resources.properties
Date Thu, 14 Feb 2002 02:03:25 GMT
adammurdoch    02/02/13 18:03:25

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec
                        Execute.java
               proposal/myrmidon/src/java/org/apache/myrmidon/framework/factorys
                        ExecManagerFactory.java
  Added:       proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service
                        ComponentManagerAdaptor.java
                        MultiSourceServiceManager.java Resources.properties
                        ServiceException.java ServiceFactory.java
                        ServiceManager.java
               proposal/myrmidon/src/java/org/apache/myrmidon/components/service
                        DefaultServiceManager.java Resources.properties
  Removed:     proposal/myrmidon/src/java/org/apache/myrmidon/services
                        ServiceException.java ServiceFactory.java
  Log:
  * Moved package org.apache.myrmidon.services to org.apache.myrmidon.interfaces.service.
  * Added ServiceManager interface, and several implementations.
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/ComponentManagerAdaptor.java
  
  Index: ComponentManagerAdaptor.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.interfaces.service;
  
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  
  /**
   * An adaptor from {@link ComponentManager} to {@link ServiceManager}.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public class ComponentManagerAdaptor
      implements ServiceManager
  {
      private final ComponentManager m_componentManager;
  
      public ComponentManagerAdaptor( final ComponentManager componentManager )
      {
          m_componentManager = componentManager;
      }
  
      /**
       * Determines if this service manager contains a particular service.
       */
      public boolean hasService( Class serviceType )
      {
          return m_componentManager.hasComponent( serviceType.getName() );
      }
  
      /**
       * Locates a service instance.
       */
      public Object getService( Class serviceType )
          throws ServiceException
      {
          try
          {
              return m_componentManager.lookup( serviceType.getName() );
          }
          catch( ComponentException e )
          {
              throw new ServiceException( e.getMessage(), e );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/MultiSourceServiceManager.java
  
  Index: MultiSourceServiceManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.interfaces.service;
  
  import java.util.ArrayList;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  /**
   * A service manager that aggregates services from several
   * {@link ServiceManager} objects.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public class MultiSourceServiceManager
      implements ServiceManager
  {
      private final static Resources REZ
          = ResourceManager.getPackageResources( MultiSourceServiceManager.class );
  
      /** The source service managers, in order. */
      private final ArrayList m_sources = new ArrayList();
  
      /**
       * Adds a service manager to the end of the source list.
       */
      public void add( final ServiceManager mgr )
      {
          m_sources.add( mgr );
      }
  
      /**
       * Determines if this service manager contains a particular service.
       *
       * @param serviceType The service interface.
       */
      public boolean hasService( Class serviceType )
      {
          for( int i = 0; i < m_sources.size(); i++ )
          {
              final ServiceManager serviceManager = (ServiceManager)m_sources.get( i );
              if( serviceManager.hasService( serviceType ) )
              {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Locates a service instance.
       *
       * @param serviceType The service interface.
       * @return The service instance.  The returned object is guaranteed to
       *         implement the service interface.
       * @throws ServiceException If the service does not exist.
       */
      public Object getService( Class serviceType )
          throws ServiceException
      {
          for( int i = 0; i < m_sources.size(); i++ )
          {
              final ServiceManager serviceManager = (ServiceManager)m_sources.get( i );
              if( serviceManager.hasService( serviceType ) )
              {
                  return serviceManager.getService( serviceType );
              }
          }
  
          final String message = REZ.getString( "unknown-service.error", serviceType.getName()
);
          throw new ServiceException( message );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  unknown-service.error=Unknown service {0}.
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/ServiceException.java
  
  Index: ServiceException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.interfaces.service;
  
  import org.apache.avalon.framework.CascadingException;
  
  /**
   * ServiceException thrown when a service can not be created for
   * some reason.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public class ServiceException
      extends CascadingException
  {
      /**
       * Basic constructor for exception that does not specify a message
       */
      public ServiceException()
      {
          this( "", null );
      }
  
      /**
       * Basic constructor with a message
       *
       * @param message the message
       */
      public ServiceException( final String message )
      {
          this( message, null );
      }
  
      /**
       * Constructor that builds cascade so that other exception information can be retained.
       *
       * @param message the message
       * @param throwable the throwable
       */
      public ServiceException( final String message, final Throwable throwable )
      {
          super( message, throwable );
      }
  }
  
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/ServiceFactory.java
  
  Index: ServiceFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.interfaces.service;
  
  /**
   * A ServiceFactory is used to create a service for use in the
   * Myrmidon runtime. The factory is responsible for creating and
   * preparing the service for use.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public interface ServiceFactory
  {
      String ROLE = ServiceFactory.class.getName();
  
      /**
       * Create a service that coresponds to this factory.
       * This method is usually called after the factory has been
       * prepared and configured as appropriate.
       */
      Object createService()
          throws ServiceException;
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/service/ServiceManager.java
  
  Index: ServiceManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.interfaces.service;
  
  import org.apache.avalon.framework.component.Component;
  
  /**
   * Manages a set of services.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public interface ServiceManager
      extends Component
  {
      String ROLE = ServiceManager.class.getName();
  
      /**
       * Determines if this service manager contains a particular service.
       *
       * @param serviceType The service interface.
       */
      boolean hasService( Class serviceType );
  
      /**
       * Locates a service instance.
       *
       * @param serviceType The service interface.
       * @return The service instance.  The returned object is guaranteed to
       *         implement the service interface.
       * @throws ServiceException If the service does not exist.
       */
      Object getService( Class serviceType )
          throws ServiceException;
  }
  
  
  
  1.28      +1 -1      jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/Execute.java
  
  Index: Execute.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/Execute.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- Execute.java	26 Jan 2002 04:53:53 -0000	1.27
  +++ Execute.java	14 Feb 2002 02:03:25 -0000	1.28
  @@ -17,7 +17,7 @@
   import org.apache.avalon.excalibur.io.IOUtil;
   import org.apache.myrmidon.api.TaskException;
   import org.apache.myrmidon.framework.factorys.ExecManagerFactory;
  -import org.apache.myrmidon.services.ServiceException;
  +import org.apache.myrmidon.interfaces.service.ServiceException;
   
   /**
    * Runs an external program.
  
  
  
  1.3       +3 -3      jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/framework/factorys/ExecManagerFactory.java
  
  Index: ExecManagerFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/framework/factorys/ExecManagerFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExecManagerFactory.java	28 Jan 2002 09:25:11 -0000	1.2
  +++ ExecManagerFactory.java	14 Feb 2002 02:03:25 -0000	1.3
  @@ -12,14 +12,14 @@
   import org.apache.aut.nativelib.impl.DefaultExecManager;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  -import org.apache.myrmidon.services.ServiceException;
  -import org.apache.myrmidon.services.ServiceFactory;
  +import org.apache.myrmidon.interfaces.service.ServiceException;
  +import org.apache.myrmidon.interfaces.service.ServiceFactory;
   
   /**
    * A Factory responsible for creating the ExecManager service.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.2 $ $Date: 2002/01/28 09:25:11 $
  + * @version $Revision: 1.3 $ $Date: 2002/02/14 02:03:25 $
    */
   public class ExecManagerFactory
       implements ServiceFactory
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/service/DefaultServiceManager.java
  
  Index: DefaultServiceManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon.components.service;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.myrmidon.interfaces.service.ServiceException;
  import org.apache.myrmidon.interfaces.service.ServiceFactory;
  import org.apache.myrmidon.interfaces.service.ServiceManager;
  import org.apache.myrmidon.interfaces.type.TypeException;
  import org.apache.myrmidon.interfaces.type.TypeFactory;
  import org.apache.myrmidon.interfaces.type.TypeManager;
  
  /**
   * A service manager implementation.  This implementation takes care of
   * creating service instances, using a {@link ServiceFactory}, and running the
   * service instances through the service lifecycle.  Service creation happens
   * on demand.
   *
   * <p>This implementation uses a TypeManager to locate the service factories.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/14 02:03:25 $
   */
  public class DefaultServiceManager
      implements ServiceManager, Composable, Disposable
  {
      private final static Resources REZ
          = ResourceManager.getPackageResources( DefaultServiceManager.class );
  
      /** Map from service class -> service object. */
      private Map m_services = new HashMap();
  
      private TypeFactory m_typeFactory;
  
      /**
       * Locate the components used by this service manager.
       */
      public void compose( final ComponentManager componentManager ) throws ComponentException
      {
          final TypeManager typeManager = (TypeManager)componentManager.lookup( TypeManager.ROLE
);
          try
          {
              m_typeFactory = typeManager.getFactory( ServiceFactory.class );
          }
          catch( final TypeException e )
          {
              throw new ComponentException( e.getMessage(), e );
          }
      }
  
      /**
       * Disposes this service manager, and all services created by it.
       */
      public void dispose()
      {
          // Dispose the services
          for( Iterator iterator = m_services.values().iterator(); iterator.hasNext(); )
          {
              final Object object = iterator.next();
              if( object instanceof Disposable )
              {
                  ( (Disposable)object ).dispose();
              }
          }
  
          // Ditch state
          m_services = null;
          m_typeFactory = null;
      }
  
      /**
       * Determines if this service manager contains a particular service.
       */
      public boolean hasService( Class serviceType )
      {
          // If we have already instantiated the service, or if we know how
          // to instantiate it, then return true
          if( m_services.containsKey( serviceType ) )
          {
              return true;
          }
          if( m_typeFactory.canCreate( serviceType.getName() ) )
          {
              return true;
          }
  
          return false;
      }
  
      /**
       * Locates a service instance.
       */
      public Object getService( Class serviceType )
          throws ServiceException
      {
          Object service = m_services.get( serviceType );
          if( service == null )
          {
              // Create the service
              service = createService( serviceType );
              m_services.put( serviceType, service );
          }
  
          return service;
      }
  
      /**
       * Creates the service object for a service class.
       */
      private Object createService( Class serviceType ) throws ServiceException
      {
          try
          {
              final ServiceFactory factory = (ServiceFactory)m_typeFactory.create( serviceType.getName()
);
  
              // Create the service
              final Object service = factory.createService();
              if( ! serviceType.isInstance( service ) )
              {
                  final String message = REZ.getString( "mismatched-service-type.error", serviceType.getName(),
service.getClass().getName() );
                  throw new ServiceException( message );
              }
              return service;
          }
          catch( Exception e )
          {
              final String message = REZ.getString( "create-service.error", serviceType.getName()
);
              throw new ServiceException( message, e );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/service/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  unknown-service-type.error=Unknown service type {0}.
  mismatched-service-type.error=Service factory for type {0} produced an object of unexpected
type {1}.
  create-service.error=Could not create service {0}.
  
  

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


Mime
View raw message