mcconnell    2003/01/18 13:17:15

  Modified:    assembly/src/java/org/apache/avalon/assembly/lifecycle
                        DefaultDeploymentService.java
               assembly/src/java/org/apache/avalon/assembly/lifecycle/context
                        ContextualizationService.java
                        DefaultContextualizationService.java
  Added:       assembly/src/java/org/apache/avalon/assembly/lifecycle/context
                        ComponentInvocationHandler.java
                        DefaultCompositionService.java
                        StandardServiceManager.java
                        WrapperComponentManager.java
                        WrapperComponentSelector.java
  Removed:     assembly/src/java/org/apache/avalon/assembly/lifecycle/composition
                        ComponentInvocationHandler.java
                        CompositionService.java
                        DefaultCompositionService.java
                        StandardServiceManager.java
                        WrapperComponentManager.java
                        WrapperComponentSelector.java
  Log:
  Converged Contextualizable, Composable and Serviceable under a single 
Contextualization strategy.
  
  Revision  Changes    Path
  1.17      +6 -36     
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/DefaultDeploymentService.java
  
  Index: DefaultDeploymentService.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/DefaultDeploymentService.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- DefaultDeploymentService.java     18 Jan 2003 20:56:21 -0000      1.16
  +++ DefaultDeploymentService.java     18 Jan 2003 21:17:15 -0000      1.17
  @@ -71,8 +71,7 @@
   import 
org.apache.avalon.assembly.lifecycle.configuration.DefaultConfigurationService;
   import org.apache.avalon.assembly.lifecycle.context.ContextualizationService;
   import org.apache.avalon.assembly.lifecycle.context.DefaultContextualizationService;
  -import org.apache.avalon.assembly.lifecycle.composition.CompositionService;
  -import org.apache.avalon.assembly.lifecycle.composition.DefaultCompositionService;
  +import org.apache.avalon.assembly.lifecycle.context.DefaultCompositionService;
   import org.apache.avalon.assembly.lifecycle.initialization.InitializationService;
   import 
org.apache.avalon.assembly.lifecycle.initialization.ExtendedInitializationService;
   import 
org.apache.avalon.assembly.lifecycle.initialization.DefaultInitializationService;
  @@ -101,7 +100,6 @@
       private LoggingService m_logging;
       private ConfigurationService m_configuration;
       private ContextualizationService m_contextualization;
  -    private CompositionService m_composition;
       private InitializationService m_initialization;
       private DisposalService m_disposal;
   
  @@ -148,16 +146,6 @@
               m_logging = makeLoggingService();
           }
   
  -        key = "urn:assembly:lifecycle.configuration"; 
  -        if( m_context.hasEntry( key ) )
  -        {
  -            m_configuration = (ConfigurationService) m_context.get( key );
  -        }
  -        else
  -        {
  -            m_configuration = makeConfigurationService();
  -        }
  -
           key = "urn:assembly:lifecycle.context"; 
           if( m_context.hasEntry( key ) )
           {
  @@ -168,14 +156,14 @@
               m_contextualization = makeContextualizationService();
           }
   
  -        key = "urn:assembly:lifecycle.composition"; 
  +        key = "urn:assembly:lifecycle.configuration"; 
           if( m_context.hasEntry( key ) )
           {
  -            m_composition = (CompositionService) m_context.get( key );
  +            m_configuration = (ConfigurationService) m_context.get( key );
           }
           else
           {
  -            m_composition = makeCompositionService();
  +            m_configuration = makeConfigurationService();
           }
   
           key = "urn:assembly:lifecycle.initialization"; 
  @@ -221,7 +209,6 @@
           {
               throw new IllegalStateException( "initialization" );
           }
  -
           if( appliance == null )
           {
               throw new NullPointerException( "appliance" );
  @@ -256,7 +243,6 @@
           {
               m_logging.enableLogging( appliance, instance );
               m_contextualization.contextualize( appliance, instance );
  -            m_composition.service( appliance, instance );
               m_configuration.configure( appliance, instance );
               m_initialization.initialize( appliance, instance );
               return instance;
  @@ -357,7 +343,7 @@
       {
           try
           {
  -            DefaultContextualizationService service = new 
DefaultContextualizationService();
  +            DefaultCompositionService service = new DefaultCompositionService();
               service.enableLogging( getLogger().getChildLogger( "context" ) );
               return service;
           }
  @@ -365,22 +351,6 @@
           {
               final String error = 
                 "Internal error while attempting to build the bootstrap 
contextualization service.";
  -            throw new DeploymentException( error, e );
  -        }
  -    }
  -
  -    private CompositionService makeCompositionService() throws DeploymentException
  -    {
  -        try
  -        {
  -            DefaultCompositionService service = new DefaultCompositionService();
  -            service.enableLogging( getLogger().getChildLogger( "composer" ) );
  -            return service;
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Internal error while attempting to build the bootstrap assembly 
service.";
               throw new DeploymentException( error, e );
           }
       }
  
  
  
  1.3       +2 -4      
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/ContextualizationService.java
  
  Index: ContextualizationService.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/ContextualizationService.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ContextualizationService.java     21 Dec 2002 09:26:34 -0000      1.2
  +++ ContextualizationService.java     18 Jan 2003 21:17:15 -0000      1.3
  @@ -51,8 +51,6 @@
   package org.apache.avalon.assembly.lifecycle.context;
   
   import org.apache.avalon.assembly.appliance.Appliance;
  -import org.apache.avalon.framework.context.Context;
  -import org.apache.avalon.framework.context.ContextException;
   
   
   /**
  @@ -71,5 +69,5 @@
       */
       void contextualize( 
         Appliance appliance, Object object ) 
  -      throws ContextException;
  +      throws Exception;
   }
  
  
  
  1.12      +2 -2      
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/DefaultContextualizationService.java
  
  Index: DefaultContextualizationService.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/DefaultContextualizationService.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DefaultContextualizationService.java      15 Jan 2003 09:09:25 -0000      1.11
  +++ DefaultContextualizationService.java      18 Jan 2003 21:17:15 -0000      1.12
  @@ -87,7 +87,7 @@
       * @param appliance the appliace
       * @param object the object to contextualize
       */
  -    public void contextualize( Appliance appliance, Object object ) throws 
ContextException
  +    public void contextualize( Appliance appliance, Object object ) throws Exception
       {
           if( appliance == null )
           {
  
  
  
  1.1                  
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/ComponentInvocationHandler.java
  
  Index: ComponentInvocationHandler.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.avalon.assembly.lifecycle.context;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.lang.reflect.Proxy;
  import org.apache.avalon.meta.info.Type;
  import org.apache.avalon.meta.info.ServiceDescriptor;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  
  /**
   * This makes a dynamic proxy for an object.  The object can be represented
   * by one, some or all of it's interfaces.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Avalon Development Team</a>
   * @version CVS $Revision: 1.1 $ $Date: 2003/01/18 21:17:15 $
   */
  final class ComponentInvocationHandler
      implements InvocationHandler
  {
      private transient Object m_object;
      private transient Component m_proxy;
  
      /**
       * Create a proxy object that has interfaces implemented by proxy defined by the 
component type.
       *
       * @param object the underlying object
       * @param type the component type
       */
      protected ComponentInvocationHandler( final Object object, Type type ) throws 
ComponentException
      {
          final ClassLoader loader = object.getClass().getClassLoader();
          
          Class[] interfaces = getClasses( type, loader );
  
          m_object = object;
          m_proxy = (Component) Proxy.newProxyInstance( loader, interfaces, this );
      }
  
      /**
       * Invalidate Proxy making it impossible to call methods
       * of real-object.
       */
      public void invalidate()
      {
          m_object = null;
          m_proxy = null;
      }
  
      /**
       * Return the proxy object.
       *
       * @return the proxy object
       */
      public Component getProxy()
      {
          return m_proxy;
      }
  
      /**
       * Invoke the specified method on underlying object.
       * This is called by proxy object.
       *
       * @param proxy the proxy object
       * @param method the method invoked on proxy object
       * @param args the arguments supplied to method
       * @return the return value of method
       * @throws Throwable if an error occurs
       */
      public Object invoke( final Object proxy,
                            final Method method,
                            final Object[] args )
          throws Throwable
      {
          if( null == m_object )
          {
              throw new IllegalStateException( "Invalid proxy." );
          }
  
          try
          {
              return method.invoke( m_object, args );
          }
          catch( final InvocationTargetException ite )
          {
              throw ite.getTargetException();
          }
      }
  
      private Class[] getClasses( Type type, ClassLoader loader ) throws 
ComponentException
      {
          try
          {
              ServiceDescriptor[] services = type.getServices();
              Class[] classes = new Class[ services.length + 1 ];
              for( int i=0; i<services.length; i++ )
              {
                  classes[i] = loader.loadClass( 
services[i].getReference().getClassname() );
              }
              classes[ services.length ] = Component.class;
              return classes;
          }
          catch( Throwable e )
          {
              final String error = 
                "Unable to create a Component proxy for an type: " + type;
              throw new ComponentException( error, e );
          }
      }
  
  }
  
  
  
  1.1                  
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/DefaultCompositionService.java
  
  Index: DefaultCompositionService.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 [EMAIL PROTECTED]
  
   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.lifecycle.context;
  
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.assembly.appliance.Appliance;
  
  /**
   * A assembly service is responsible for the resolution of a component 
   * service depedencies, and the construction supply of a component or
   * service manager to a target object.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2003/01/18 21:17:15 $
   */
  public class DefaultCompositionService extends DefaultContextualizationService 
  {
  
      //==============================================================
      // AssemblyService
      //==============================================================
  
     /**
      * Runtime contextualization, servicing and or composition of a target object.
      * @param appliance the appliance
      * @param object the object to service
      */
      public void contextualize( Appliance appliance, Object object ) throws Exception
      {
          super.contextualize( appliance, object );
  
          if( object instanceof Serviceable )
          {
              if( getLogger().isDebugEnabled() )
              {
                  final String message =  "servicing: " + appliance.toString();
                  getLogger().debug( message );
              }
              StandardServiceManager manager = new StandardServiceManager( appliance );
              manager.enableLogging( getLogger().getChildLogger( "manager" ) );
              ((Serviceable)object).service( manager );
  
          }
          else if( object instanceof Composable )
          {
              if( getLogger().isDebugEnabled() )
              {
                  final String message =  "composing: " + appliance.toString();
                  getLogger().debug( message );
              }
              StandardServiceManager manager = new StandardServiceManager( appliance );
              manager.enableLogging( getLogger().getChildLogger( "manager" ) );
              ((Composable)object).compose( 
                    new WrapperComponentManager( manager ) );
          }
      }
  }
  
  
  
  1.1                  
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/StandardServiceManager.java
  
  Index: StandardServiceManager.java
  ===================================================================
  /* ==================================================================== 
   * The Apache Software License, Version 1.1 
   * 
   * Copyright (c) 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 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", "Avalon", 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 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 (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/>.
   */ 
  package org.apache.avalon.assembly.lifecycle.context;
  
  import java.util.Hashtable;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.assembly.appliance.Appliance;
  import org.apache.avalon.meta.info.DependencyDescriptor;
  
  /**
   * Helper class the implements the <code>ServiceManager</code> interface and
   * is supplied to componets during lifecyle processing.
   * @author Stephen McConnell <[EMAIL PROTECTED]>
   */
  public class StandardServiceManager extends AbstractLogEnabled implements 
ServiceManager
  {
  
      //========================================================================
      // state
      //========================================================================
  
      /**
       * The appliance from which provide appliances are resolved.
       */
      private Appliance m_appliance;
  
      /**
       * A table of appliance instances keyed by the object that was provided
       * by the appliance so that we can locate the correct appliance when 
       * releasing the object.
       */
      private Hashtable m_table = new Hashtable();
  
      //========================================================================
      // constructor
      //========================================================================
  
      /**
       * Construct a new ServiceManager.
       * @param profile the component profile
       */
      public StandardServiceManager( Appliance appliance )
      {
          m_appliance = appliance;
      }
  
      //========================================================================
      // ServiceManager
      //========================================================================
  
      /**
       * Returns true if a provider exists for the supplied role.
       * @param role the service identifier
       * @return boolean TRUE if the service is available else FALSE
       */
      public boolean hasService( String role )
      {
          return ( m_appliance.getServiceProvider( role ) != null );
      }
  
      /**
       * Retrieve Object by role from ComponentLocator.
       * @param role the role
       * @return the Object
       * @throws ServiceException if an error occurs
       */
      public Object lookup( String role ) throws ServiceException
      {
          if( role == null )
          {
              throw new NullPointerException( "role" );
          }
  
          DependencyDescriptor dependency = 
            m_appliance.getType().getDependency( role );
          Appliance provider = m_appliance.getServiceProvider( role );
          if( provider == null )
          {
              final String error =
                "Internal error - incorrect assembly in appliance: " + m_appliance
                + ". A null provider was retured for the dependency: " + dependency;
              throw new ServiceException( role, error );
          }
  
          try
          {
              //Object object = provider.access( dependency );
              Object object = provider.resolve( this );
              m_table.put( object, provider );
              return object;
          }
          catch( Throwable e )
          {
              final String error = 
                "Unexpected internal error while accessing a service provider in 
appliance: "
                + m_appliance
                + " for the role: " + role;
              throw new ServiceException( role, error, e );
          }
      }
  
      /**
       * Release a pooled object.
       * @param object a pooled object
       */
      public void release( Object object )
      {
          Appliance provider = (Appliance) m_table.get( object );
          if( provider == null )
          {
              if( getLogger().isWarnEnabled() )
              {
                  final String warning =
                    "Inconsistent release request - the object: "
                    + object.getClass().getName() 
                    + "/" + System.identityHashCode( object )
                    + " was not provided by this manager.";
                  getLogger().warn( warning );
              }
          }
          else
          {
              //provider.release( object );
              provider.release( object, this );
              m_table.remove( object );
          }
      }
  }
  
  
  1.1                  
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/WrapperComponentManager.java
  
  Index: WrapperComponentManager.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.avalon.assembly.lifecycle.context;
  
  import java.lang.reflect.Proxy;
  
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceSelector;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.assembly.engine.Engine;
  import org.apache.avalon.meta.info.Type;
  import org.apache.avalon.meta.info.ServiceDescriptor;
  
  /**
   * This is a {@link ComponentManager} implementation that can wrap around a
   * {@link ServiceManager} object effectively adapting a {@link ServiceManager}
   * interface to a {@link ComponentManager} interface.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2003/01/18 21:17:15 $
   */
  public class WrapperComponentManager
      implements ComponentManager
  {
      /**
       * The service manager we are adapting.
       */
      private final ServiceManager m_manager;
  
      public WrapperComponentManager( final ServiceManager manager )
      {
          if( null == manager )
          {
              throw new NullPointerException( "manager" );
          }
  
          m_manager = manager;
      }
  
      /**
       * Retrieve a component via a key.
       *
       * @param key the key
       * @return the component
       * @throws ComponentException if unable to aquire component
       */
      public Component lookup( final String key )
          throws ComponentException
      {
          try
          {
              final Object object = m_manager.lookup( key );
              if( object instanceof ServiceSelector )
              {
                  return new WrapperComponentSelector( key, (ServiceSelector)object );
              }
              else 
              {
                  if( object instanceof Component )
                  {
                      return (Component)object;
                  }
                  else
                  {
                      //
                      // use the services defined by the type and use them to 
                      // construct a proxy together with the Component interface
                      //
  
                      Class clazz = object.getClass();
                      ClassLoader loader = clazz.getClassLoader();
                      if( !( loader instanceof Engine ) )
                      {
                          final String error = 
                            "Resolved component is not contained within an engine 
classloader. "
                            + "Cannot build a Component interface proxy.";
                          throw new ComponentException( key, error );
                      }
  
                      Type type = 
((Engine)loader).getRepository().getTypeManager().getType( clazz );
                      return new ComponentInvocationHandler( object, type ).getProxy();
                  }
              }
          }
          catch( final ServiceException se )
          {
              throw new ComponentException( se.getKey(), se.getMessage(), 
se.getCause() );
          }
          catch( final Throwable e )
          {
              final String error = 
                "Unexpected error while handling component lookup request.";
              throw new ComponentException( key, error, e );
          }
      }
  
      /**
       * Check to see if a <code>Component</code> exists for a key.
       *
       * @param key  a string identifying the key to check.
       * @return True if the component exists, False if it does not.
       */
      public boolean hasComponent( final String key )
      {
          return m_manager.hasService( key );
      }
  
      /**
       * Return the <code>Component</code> when you are finished with it.  This
       * allows the <code>ComponentManager</code> to handle the End-Of-Life Lifecycle
       * events associated with the Component.  Please note, that no Exceptions
       * should be thrown at this point.  This is to allow easy use of the
       * ComponentManager system without having to trap Exceptions on a release.
       *
       * @param component The Component we are releasing.
       */
      public void release( final Component component )
      {
          if( component instanceof WrapperComponentSelector )
          {
              m_manager.
                  release( ( (WrapperComponentSelector)component 
).getWrappedSelector() );
          }
          else
          {
              m_manager.release( component );
          }
      }
  }
  
  
  
  1.1                  
avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/context/WrapperComponentSelector.java
  
  Index: WrapperComponentSelector.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 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", "Avalon", 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 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 (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/>.
   */
  
  package org.apache.avalon.assembly.lifecycle.context;
  
  import org.apache.avalon.framework.service.ServiceSelector;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.component.ComponentException;
  
  /**
   * This is a {@link ServiceSelector} implementation that can wrap around a legacy
   * {@link ComponentSelector} object effectively adapting a {@link ComponentSelector}
   * interface to a {@link ServiceSelector} interface.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Avalon Development Team</a>
   * @version CVS $Revision: 1.1 $ $Date: 2003/01/18 21:17:15 $
   */
  public class WrapperComponentSelector
      implements ComponentSelector
  {
      /**
       * The Selector we are wrapping.
       */
      private final ServiceSelector m_selector;
  
      /**
       * The role that this selector was aquired via.
       */
      private final String m_key;
  
      /**
       * This constructor is a constructor for a WrapperComponentSelector.
       *
       * @param key the key used to aquire this selector
       * @param selector the selector to wrap
       */
      public WrapperComponentSelector( final String key,
                                       final ServiceSelector selector )
      {
          if( null == key )
          {
              throw new NullPointerException( "key" );
          }
          if( null == selector )
          {
              throw new NullPointerException( "selector" );
          }
  
          m_key = key + "/";
          m_selector = selector;
      }
  
      /**
       * Select a Component based on a policy.
       *
       * @param policy the policy
       * @return the Component
       * @throws ComponentException if unable to select service
       */
      public Component select( final Object policy )
          throws ComponentException
      {
          try
          {
              final Object object = m_selector.select( policy );
              if( object instanceof Component )
              {
                  return (Component)object;
              }
          }
          catch( final ServiceException se )
          {
              throw new ComponentException( m_key + policy, se.getMessage(), se );
          }
  
          final String message = "Role does not implement the Component " +
              "interface and thus can not be accessed via ComponentSelector";
          throw new ComponentException( m_key + policy, message );
      }
  
      /**
       * Check to see if a {@link Component} exists relative to the supplied policy.
       *
       * @param policy a {@link Object} containing the selection criteria
       * @return True if the component is available, False if it not.
       */
      public boolean hasComponent( final Object policy )
      {
          return m_selector.isSelectable( policy );
      }
  
      /**
       * Return the {@link Object} when you are finished with it.  This
       * allows the {@link ServiceSelector} to handle the End-Of-Life Lifecycle
       * events associated with the {@link Object}.  Please note, that no
       * Exception should be thrown at this point.  This is to allow easy use of the
       * ServiceSelector system without having to trap Exceptions on a release.
       *
       * @param object The {@link Object} we are releasing.
       */
      public void release( final Component object )
      {
          m_selector.release( object );
      }
  
      /**
       * The {@link WrapperComponentManager} wraps ServiceSelectors in
       *  WrapperServiceSelectors when they are looked up.  This method
       *  makes it possible to release the original component selector.
       *
       * @return The {@link ServiceSelector} being wrapped.
       */
      ServiceSelector getWrappedSelector()
      {
          return m_selector;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to