You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2004/03/11 19:13:15 UTC

cvs commit: avalon/merlin/composition/impl/src/test/org/apache/avalon/composition/model/test ContextTestCase.java

mcconnell    2004/03/11 10:13:15

  Modified:    merlin/activation/impl/src/java/org/apache/avalon/activation/impl
                        AbstractLifestyleManager.java
                        DefaultComponentFactory.java Resources.properties
               merlin/composition/api/src/java/org/apache/avalon/composition/model
                        ContextModel.java
               merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        DefaultContextModel.java
               merlin/composition/impl/src/test/org/apache/avalon/composition/model/test
                        ContextTestCase.java
  Log:
  Update the context model to enable context arguemnts that are independent of the avalon framework.
  
  Revision  Changes    Path
  1.6       +2 -2      avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/AbstractLifestyleManager.java
  
  Index: AbstractLifestyleManager.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/AbstractLifestyleManager.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AbstractLifestyleManager.java	8 Mar 2004 11:28:35 -0000	1.5
  +++ AbstractLifestyleManager.java	11 Mar 2004 18:13:14 -0000	1.6
  @@ -234,7 +234,7 @@
                   Accessor handler = (Accessor) provider.resolve();
                   try
                   {
  -                    Context context = m_model.getContextModel().getContext();
  +                    Context context = (Context) m_model.getContextModel().getContext();
                       if( flag )
                       {
                           if( getLogger().isDebugEnabled() )
  
  
  
  1.7       +68 -28    avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/DefaultComponentFactory.java
  
  Index: DefaultComponentFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/DefaultComponentFactory.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DefaultComponentFactory.java	8 Mar 2004 11:28:35 -0000	1.6
  +++ DefaultComponentFactory.java	11 Mar 2004 18:13:14 -0000	1.7
  @@ -133,7 +133,7 @@
   
           try
           {
  -            return doIncarnation();
  +            return incarnation();
           }
           finally 
           {
  @@ -141,7 +141,6 @@
           }
       }
   
  -
      /**
       * Termination of the instance including all end-of-life processing.
       * @param instance the component instance to etherialize
  @@ -246,17 +245,20 @@
       * Creation of a new instance including all deployment stage handling.
       * @return the new instance
       */
  -    private Object doIncarnation() throws LifecycleException
  +    private Object incarnation() throws LifecycleException
       {
           Class clazz = m_model.getDeploymentClass();
           final Logger logger = m_model.getLogger();
           final Configuration config = m_model.getConfiguration();
           final Parameters params = m_model.getParameters();
           final ServiceManager manager = new DefaultServiceManager( m_model );
  -        final Context context = getTargetContext();
  +        final Object context = getTargetContext();
  +
  +        final Class contextClass = getContextCastingClass();
   
           final Object instance = 
  -          instantiate( clazz, logger, config, params, context, manager );
  +          instantiate( 
  +            clazz, logger, config, params, context, contextClass, manager );
   
           try
           {
  @@ -437,7 +439,20 @@
           }
       }
   
  -    private Context getTargetContext()
  +
  +    private Class getContextCastingClass()
  +    {
  +        if( null == m_model.getContextModel() ) 
  +        {
  +            return null;
  +        }
  +        else
  +        {
  +            return m_model.getContextModel().getCastingClass();
  +        }
  +    }
  +
  +    private Object getTargetContext()
       {
          ContextModel model = m_model.getContextModel();
          if( null == model ) return null;
  @@ -446,24 +461,11 @@
   
       private Object instantiate( 
         Class clazz, Logger logger, Configuration config, Parameters params, 
  -      Context context, ServiceManager manager )
  +      Object context, Class contextClass, ServiceManager manager )
         throws LifecycleException
       {
  -        Constructor[] constructors = clazz.getConstructors();
  -        if( constructors.length < 1 ) 
  -        {
  -            final String error = 
  -              REZ.getString( 
  -                "lifecycle.error.no-constructor", 
  -                clazz.getName() );
  -            throw new LifecycleException( error );
  -        }
  -
  -        //
  -        // assume components have only one constructor for now
  -        //
  +        Constructor constructor = getConstructor( clazz );
   
  -        Constructor constructor = constructors[0];
           Class[] classes = constructor.getParameterTypes();
           Object[] args = new Object[ classes.length ];
           for( int i=0; i<classes.length; i++ )
  @@ -477,7 +479,7 @@
                   }
                   args[i] = logger;
               }
  -            else if( Context.class.isAssignableFrom( c ) )
  +            else if( ( null != contextClass ) && contextClass.isAssignableFrom( c ) )
               {
                   if( null == context )
                   {
  @@ -527,6 +529,44 @@
           return instantiateComponent( constructor, args );
       }
   
  +    private Constructor getConstructor( Class clazz ) throws LifecycleException
  +    {
  +        Constructor[] constructors = clazz.getConstructors();
  +        if( constructors.length < 1 ) 
  +        {
  +            final String error = 
  +              REZ.getString( 
  +                "lifecycle.error.no-constructor", 
  +                clazz.getName() );
  +            throw new LifecycleException( error );
  +        }
  +
  +        if( constructors.length > 1 )
  +        {
  +            //
  +            // we risk conflicting with an object designed for 4.1.2 or 
  +            // earlier that has a null arg constructor - so if this class
  +            // has a null arg constructor then invoke it, otherwise we 
  +            // we are dealing with an ambigouse object
  +            //
  +
  +            try
  +            {
  +                return clazz.getConstructor( new Class[0] );
  +            }
  +            catch( NoSuchMethodException e )
  +            {
  +                final String error =
  +                  "Multiple constructor ambiguity.";
  +                throw new LifecycleException( error );
  +            }
  +        }
  +        else
  +        {
  +            return constructors[0];
  +        }
  +    }
  +
      /**
       * Instantiation of a component instance using a supplied constructor 
       * and arguments.
  @@ -602,7 +642,7 @@
                   getLogger().debug( "processing create: " + c.getName() );
   
                   Creator handler = getCreator( provider );
  -                Context context = m_model.getContextModel().getContext();
  +                Context context = (Context) m_model.getContextModel().getContext();
   
                   try
                   {
  @@ -775,7 +815,7 @@
           }
       }
   
  -    private void applyContext( final Object instance, final Context context ) 
  +    private void applyContext( final Object instance, final Object context ) 
         throws LifecycleException
       {
           if( null == context ) return;
  @@ -801,7 +841,7 @@
                         {
                             public Object run() throws Exception
                             {
  -                             ((Contextualizable)instance).contextualize( context );
  +                             ((Contextualizable)instance).contextualize( (Context) context );
                                return null;
                             }
                         }, 
  @@ -809,7 +849,7 @@
                   }
                   else
                   {
  -                    ContainerUtil.contextualize( instance, context );
  +                    ContainerUtil.contextualize( instance, (Context) context );
                   }
               }
               catch( Throwable e )
  @@ -828,7 +868,7 @@
               {
                   ContextualizationHandler handler =
                     (ContextualizationHandler) provider.resolve();
  -                handler.contextualize( instance, context );
  +                handler.contextualize( instance, (Context) context );
               }
               catch( Throwable e )
               {
  
  
  
  1.4       +1 -1      avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/impl/Resources.properties,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Resources.properties	4 Mar 2004 03:42:30 -0000	1.3
  +++ Resources.properties	11 Mar 2004 18:13:15 -0000	1.4
  @@ -11,7 +11,7 @@
   # DefaultComponentFactory
   # -----------------------
   lifestyle.error.no-constructor=Supplied component class {0} does not declare a public constructor.
  -lifestyle.error.unrecognized-parameter=The constructor argument [{0}] in component class {1} is not recognized.
  +lifecycle.error.unrecognized-parameter=The constructor argument [{0}] in component class {1} is not recognized.
   lifecycle.error.instantiation=Component related exception during instantiation of the class [{0}]. 
   lifecycle.error.invalid-stage-provider=Assigned stage provider [{0}] is not a component model.
   lifecycle.error.stage.creator=Creation stage handler error raised by extension id: [{0}].
  
  
  
  1.6       +8 -2      avalon/merlin/composition/api/src/java/org/apache/avalon/composition/model/ContextModel.java
  
  Index: ContextModel.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/composition/api/src/java/org/apache/avalon/composition/model/ContextModel.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ContextModel.java	22 Feb 2004 17:28:29 -0000	1.5
  +++ ContextModel.java	11 Mar 2004 18:13:15 -0000	1.6
  @@ -34,6 +34,12 @@
       public static final String DEFAULT_STRATEGY_CLASSNAME = 
         "org.apache.avalon.framework.context.Contextualizable";
   
  +   /**
  +    * Return the class that the context is castable to.
  +    * 
  +    * @return the base context casting class
  +    */
  +    Class getCastingClass();
   
      /**
       * Return the class representing the contextualization 
  @@ -48,7 +54,7 @@
       * 
       * @return the context object
       */
  -    Context getContext();
  +    Object getContext();
   
      /**
       * Return the set of entry models associated with this context model.
  
  
  
  1.14      +27 -9     avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContextModel.java
  
  Index: DefaultContextModel.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContextModel.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DefaultContextModel.java	8 Mar 2004 11:28:36 -0000	1.13
  +++ DefaultContextModel.java	11 Mar 2004 18:13:15 -0000	1.14
  @@ -89,7 +89,9 @@
   
       private final Map m_map = new Hashtable();
   
  -    private final Context m_componentContext;
  +    private final Object m_componentContext;
  +
  +    private final Class m_castingClass;
   
       //==============================================================
       // constructor
  @@ -130,6 +132,8 @@
   
           ClassLoader classLoader = context.getClassLoader();
           m_strategy = loadStrategyClass( descriptor, classLoader );
  +        Class impl = loadContextClass( directive, classLoader );
  +        m_castingClass = getContextCastingClass( descriptor, classLoader, impl );
   
           //
           // get the set of context entries declared by the component type
  @@ -217,7 +221,8 @@
           }
   
           m_componentContext = 
  -          createComponentContext( classLoader, descriptor, directive, m_map );
  +          createComponentContext( 
  +            classLoader, impl, descriptor, directive, m_map );
       }
   
       
  @@ -281,11 +286,21 @@
       }
   
      /**
  +    * Return the class that the context is castable to.
  +    * 
  +    * @return the base context casting class
  +    */
  +    public Class getCastingClass()
  +    {
  +        return m_castingClass ;
  +    }
  +
  +   /**
       * Return the context object established for the component.
       * 
       * @return the context object
       */
  -    public Context getContext()
  +    public Object getContext()
       {
           return m_componentContext;
       }
  @@ -306,6 +321,7 @@
       {
           final String strategy = m_descriptor.getAttribute( 
             ContextDescriptor.STRATEGY_KEY, null );
  +
           if( strategy != null )
           {
               try
  @@ -375,14 +391,13 @@
       * @exception ModelException if an error occurs while attempting to 
       *   construct the context instance
       */
  -    private Context createComponentContext( 
  +    private Object createComponentContext( 
         ClassLoader classloader, 
  +      Class clazz,
         ContextDescriptor descriptor, 
         ContextDirective directive, Map map )
         throws ModelException
       {
  -        Class clazz = loadContextClass( directive, classloader );
  -        validateCastingConstraint( descriptor, classloader, clazz );
           Context base = new DefaultContext( map );
   
           if( clazz.equals( DefaultContext.class ) ) return base; 
  @@ -397,7 +412,7 @@
           {
               Constructor constructor = clazz.getConstructor(
                   new Class[]{ Context.class } );
  -            return (Context) constructor.newInstance( new Object[]{ base } );
  +            return constructor.newInstance( new Object[]{ base } );
           }
           catch( NoSuchMethodException e )
           {
  @@ -449,6 +464,7 @@
           }
       }
   
  +
      /**
       * Validate that the context implememtation class implements
       * any casting constraint declared or implied by the context 
  @@ -459,7 +475,7 @@
       * @param clazz the context implementation class
       * @exception if a validation failure occurs
       */
  -    private void validateCastingConstraint( 
  +    private Class getContextCastingClass( 
         ContextDescriptor descriptor, ClassLoader classLoader, Class clazz )
         throws ModelException
       {
  @@ -510,5 +526,7 @@
                 + ".";
               throw new ModelException( error );
           }
  +
  +        return castingClass;
       }
   }
  
  
  
  1.10      +1 -1      avalon/merlin/composition/impl/src/test/org/apache/avalon/composition/model/test/ContextTestCase.java
  
  Index: ContextTestCase.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/composition/impl/src/test/org/apache/avalon/composition/model/test/ContextTestCase.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ContextTestCase.java	27 Feb 2004 22:39:36 -0000	1.9
  +++ ContextTestCase.java	11 Mar 2004 18:13:15 -0000	1.10
  @@ -58,7 +58,7 @@
               throw new IllegalStateException( "null context model" );
           }
   
  -        context = contextModel.getContext();
  +        context = (Context) contextModel.getContext();
           if( null == context )
           {
               throw new IllegalStateException( "null context" );
  
  
  

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