avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl DefaultContainmentModelAssemblyHelper.java DefaultContainmentModelComponentHelper.java DefaultContainmentModelNavigationHelper.java DefaultContainmentModel.java Resources.properties ComponentModelContextHelper.java
Date Sat, 07 Feb 2004 19:31:08 GMT
mcconnell    2004/02/07 11:31:08

  Modified:    merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        DefaultContainmentModel.java Resources.properties
  Added:       merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        DefaultContainmentModelAssemblyHelper.java
                        DefaultContainmentModelComponentHelper.java
                        DefaultContainmentModelNavigationHelper.java
  Removed:     merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        ComponentModelContextHelper.java
  Log:
  Seperation of the navigation and assembly functions into helper classes.
  
  Revision  Changes    Path
  1.28      +61 -573   avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java
  
  Index: DefaultContainmentModel.java
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- DefaultContainmentModel.java	7 Feb 2004 17:46:24 -0000	1.27
  +++ DefaultContainmentModel.java	7 Feb 2004 19:31:08 -0000	1.28
  @@ -29,9 +29,10 @@
   import java.util.ArrayList;
   import java.util.Map;
   
  +import org.apache.avalon.logging.data.CategoriesDirective;
  +
   import org.apache.avalon.composition.data.BlockCompositionDirective;
   import org.apache.avalon.composition.data.BlockIncludeDirective;
  -import org.apache.avalon.logging.data.CategoriesDirective;
   import org.apache.avalon.composition.data.ContainmentProfile;
   import org.apache.avalon.composition.data.ComponentProfile;
   import org.apache.avalon.composition.data.NamedComponentProfile;
  @@ -314,344 +315,21 @@
               }
   
               getLogger().debug( "assembly phase" );
  +            DefaultContainmentModelAssemblyHelper helper = 
  +              new DefaultContainmentModelAssemblyHelper( m_context, this );
  +
               DeploymentModel[] models = 
                 m_context.getModelRepository().getModels();
               for( int i=0; i<models.length; i++ )
               {
                   DeploymentModel model = models[i];
  -                assembleModel( model );
  +                helper.assembleModel( model );
               }
   
               m_assembly.setEnabled( true );
           }
       }
   
  -    private void assembleModel( DeploymentModel model ) throws AssemblyException
  -    {
  -         if( null == model )
  -         {
  -             throw new NullPointerException( "model" );
  -         }
  -
  -         if( model.isAssembled() ) 
  -         {
  -             return;
  -         }
  -         else
  -         {
  -             if( model instanceof ComponentModel )
  -             {
  -                 assembleComponent( (ComponentModel) model );
  -             }
  -             else
  -             {
  -                 model.assemble();
  -             }
  -        }
  -    }
  -
  -    private void assembleComponent( ComponentModel model ) throws AssemblyException
  -    {
  -        ModelRepository repository = m_context.getModelRepository();
  -
  -        //
  -        // locate and assemble the component context handler
  -        //
  -
  -        if( model.getContextModel() != null )
  -        {
  -            ContextModel context = model.getContextModel();
  -            Class clazz = context.getStrategyClass();
  -            if( !clazz.getName().equals( 
  -              ContextModel.DEFAULT_STRATEGY_CLASSNAME ) )
  -            {
  -                if( null == context.getProvider() )
  -                {
  -                    try
  -                    {
  -                        StageDescriptor stage = 
  -                          new StageDescriptor( clazz.getName() );
  -                        DeploymentModel provider = 
  -                          findExtensionProvider( repository, stage );
  -                        context.setProvider( provider );
  -                    }
  -                    catch( Throwable e )
  -                    {
  -                        final String error = 
  -                          "Unable to assemble component: " 
  -                          + model 
  -                         + " due to a component context phase handler establishment failure.";
  -                        throw new AssemblyException( error, e );
  -                    }
  -                }
  -            }
  -        }
  -
  -        //
  -        // locate and resolve the stage providers
  -        //
  -
  -        StageModel[] stages = model.getStageModels();
  -        for( int i=0; i<stages.length; i++ )
  -        {
  -            StageModel stage = stages[i];
  -            if( null == stage.getProvider() )
  -            {
  -                try
  -                {
  -                    DeploymentModel provider =
  -                      findExtensionProvider( repository, stage );
  -                    stage.setProvider( provider );
  -                }
  -                catch( Throwable e )
  -                {
  -                    final String error = 
  -                      "Unable to assemble component: " 
  -                      + model 
  -                      + " due to a component extension handler establishment failure.";
  -                    throw new AssemblyException( error, e );
  -                }
  -            }
  -        }
  -
  -        //
  -        // locate and resolve the service providers
  -        //
  -
  -        DependencyModel[] dependencies = model.getDependencyModels();
  -        for( int i=0; i<dependencies.length; i++ )
  -        {
  -            DependencyModel dependency = dependencies[i];
  -            if( null == dependency.getProvider() )
  -            {
  -                try
  -                {
  -                    DeploymentModel provider =
  -                      findDependencyProvider( repository, dependency );
  -                    dependency.setProvider( provider );
  -                }
  -                catch( Throwable e )
  -                {
  -                    final String error = 
  -                      "Unable to assemble component: " + model 
  -                      + " due to a service provider establishment failure.";
  -                    throw new AssemblyException( error, e );
  -                }
  -            }
  -        }
  -    }
  -
  -    private DeploymentModel findDependencyProvider( 
  -      ModelRepository repository, DependencyModel dependency )
  -      throws AssemblyException
  -    {
  -        String path = dependency.getPath();
  -        if( null != path )
  -        {
  -            DeploymentModel model = getModel( path );
  -            if( null == model )
  -            {
  -                final String error = 
  -                  "Could not locate a model at the address: [" 
  -                  + path + "] in " + this + ".";
  -                throw new AssemblyException( error );
  -            }
  -            assembleModel( model );
  -            return model;
  -        }
  -        else
  -        {
  -            return findDependencyProvider( 
  -              repository, dependency.getDependency() );
  -        }
  -    }
  -
  -    private DeploymentModel findDependencyProvider( 
  -      ModelRepository repository, DependencyDescriptor dependency )
  -      throws AssemblyException
  -    {
  -        DeploymentModel[] candidates = 
  -          repository.getCandidateProviders( dependency );
  -        ModelSelector selector = new DefaultModelSelector();
  -        DeploymentModel model = selector.select( candidates, dependency );
  -        if( model != null )
  -        {
  -            assembleModel( model );
  -            return model;
  -        }
  -
  -        //
  -        // otherwise, check for any packaged profiles that 
  -        // we could use to construct the model
  -        //
  -
  -        DeploymentProfile[] profiles = findDependencyProfiles( dependency );
  -        ProfileSelector profileSelector = new DefaultProfileSelector();
  -        DeploymentProfile profile = profileSelector.select( profiles, dependency );
  -        if( profile != null ) 
  -        {
  -            try
  -            {
  -                DeploymentModel solution = addModel( profile );
  -                assembleModel( solution );
  -                return solution;
  -            }
  -            catch( AssemblyException ae )
  -            {
  -                final String error = 
  -                  "Nested assembly failure while attempting to construct model"
  -                  + " for the profile: [" + profile + "] for the dependency: ["
  -                  + dependency + "].";
  -                throw new AssemblyException( error, ae );
  -            }
  -            catch( ModelException me )
  -            {
  -                final String error = 
  -                  "Nested model failure while attempting to add model"
  -                  + " for the profile: [" + profile + "] for the dependency: ["
  -                  + dependency + "].";
  -                throw new AssemblyException( error, me );
  -            }
  -        }
  -        else
  -        {
  -            final String error = 
  -              "Unable to locate a service provider for the dependency: [ "
  -              + dependency + "].";
  -            throw new AssemblyException( error );
  -        }
  -    }
  -
  -    private DeploymentModel findExtensionProvider( 
  -      ModelRepository repository, StageModel stage )
  -      throws AssemblyException
  -    {
  -        String path = stage.getPath();
  -        if( null != path )
  -        {
  -            DeploymentModel model = getModel( path );
  -            if( null == model )
  -            {
  -                final String error = 
  -                  "Could not locate a model at the address: [" 
  -                  + path + "] in " + this + ".";
  -                throw new AssemblyException( error );
  -            }
  -            assembleModel( model );
  -            return model;
  -        }
  -        else
  -        {
  -            return findExtensionProvider( repository, stage.getStage() );
  -        }
  -    }
  -
  -    private DeploymentModel findExtensionProvider( 
  -      ModelRepository repository, StageDescriptor stage )
  -      throws AssemblyException
  -    {
  -        DeploymentModel[] candidates = 
  -          repository.getCandidateProviders( stage );
  -        ModelSelector selector = new DefaultModelSelector();
  -        DeploymentModel model = selector.select( candidates, stage );
  -        if( model != null )
  -        {
  -            assembleModel( model );
  -            return model;
  -        }
  -
  -        //
  -        // otherwise, check for any packaged profiles that 
  -        // we could use to construct the model
  -        //
  -
  -        DeploymentProfile[] profiles = findExtensionProfiles( stage );
  -        ProfileSelector profileSelector = new DefaultProfileSelector();
  -        DeploymentProfile profile = profileSelector.select( profiles, stage );
  -        if( profile != null ) 
  -        {
  -            try
  -            {
  -                DeploymentModel solution = addModel( profile );
  -                assembleModel( solution );
  -                return solution;
  -            }
  -            catch( AssemblyException ae )
  -            {
  -                final String error = 
  -                  "Nested assembly failure while attempting to construct model"
  -                  + " for the extension profile: [" + profile 
  -                  + "] for the stage dependency: ["
  -                  + stage + "].";
  -                throw new AssemblyException( error, ae );
  -            }
  -            catch( ModelException me )
  -            {
  -                final String error = 
  -                  "Nested model failure while attempting to add model"
  -                  + " for the extension profile: [" + profile 
  -                  + "] for the stage dependency: ["
  -                  + stage + "].";
  -                throw new AssemblyException( error, me );
  -            }
  -        }
  -        else
  -        {
  -            final String error = 
  -              "Unable to locate a extension provider for the stage: [ "
  -              + stage + "].";
  -            throw new AssemblyException( error );
  -        }
  -    }
  -
  -    private DeploymentProfile[] findExtensionProfiles( StageDescriptor stage )
  -    {
  -        TypeRepository repository = getClassLoaderModel().getTypeRepository();
  -        Type[] types = repository.getTypes( stage );
  -        try
  -        {
  -            return getProfiles( repository, types );
  -        }
  -        catch( TypeUnknownException tue )
  -        {
  -            // will not happen
  -            final String error = "An irrational condition has occured.";
  -            throw new ModelRuntimeException( error, tue );
  -        }
  -    }
  -
  -    private DeploymentProfile[] findDependencyProfiles( DependencyDescriptor dependency )
  -    {
  -        TypeRepository repository = getClassLoaderModel().getTypeRepository();
  -        Type[] types = repository.getTypes( dependency );
  -        try
  -        {
  -            return getProfiles( repository, types );
  -        }
  -        catch( TypeUnknownException tue )
  -        {
  -            // will not happen
  -            final String error = "An irrational condition has occured.";
  -            throw new ModelRuntimeException( error, tue );
  -        }
  -    }
  -
  -    private DeploymentProfile[] getProfiles( TypeRepository repository, Type[] types )
  -      throws TypeUnknownException
  -    {
  -        ArrayList list = new ArrayList();
  -        for( int i=0; i<types.length; i++ )
  -        {
  -            DeploymentProfile[] profiles = 
  -            repository.getProfiles( types[i] );
  -            for( int j=0; j<profiles.length; j++ )
  -            {
  -                list.add( profiles[j] );
  -            }
  -        }
  -        return (DeploymentProfile[]) list.toArray( new DeploymentProfile[0] );
  -    }
  -
       /**
        * Disassemble the model.
        */
  @@ -970,8 +648,8 @@
       private ComponentModel createComponentModel( final ComponentProfile profile ) 
         throws ModelException
       {
  -        ComponentModelContextHelper helper = 
  -          new ComponentModelContextHelper( m_context, this );
  +        DefaultContainmentModelComponentHelper helper = 
  +          new DefaultContainmentModelComponentHelper( m_context, this );
           ComponentContext context = 
             helper.createComponentContext( profile );
           ModelFactory factory = 
  @@ -1059,13 +737,9 @@
                   classLoaderModel, modelRepository, graph, 
                   home, temp, this, profile, partition, name );
   
  -            //
  -            // TODO: lookup the profile for a factory declaration, then 
  -            // use the factory to create the model using the context as 
  -            // the argument.
  -            //
  -
  -            return new DefaultContainmentModel( context );
  +            ModelFactory factory = 
  +              m_context.getSystemContext().getModelFactory();
  +            return factory.createContainmentModel( context );
           }
           catch( ModelException e )
           {
  @@ -1118,48 +792,7 @@
           }
   
           TargetDirective[] targets = directive.getTargetDirectives();
  -        for( int i=0; i<targets.length; i++ )
  -        {
  -            TargetDirective target = targets[i];
  -            DeploymentModel child = model.getModel( target.getPath() );
  -            if( child != null )
  -            {
  -                if( target.getConfiguration() != null )
  -                {
  -                    if( child instanceof ComponentModel )
  -                    {
  -                        ((ComponentModel)child).setConfiguration( 
  -                          target.getConfiguration() );
  -                    }
  -                    else if( child instanceof ContainmentModel )
  -                    {
  -                        final String warn = 
  -                          "Ignoring target configuration as the path [" 
  -                          + target.getPath() 
  -                          + "] does not refer to a deployment model";
  -                    }
  -                }
  -                if( target.getCategoriesDirective() != null )
  -                {
  -                    if( child instanceof ComponentModel )
  -                    {
  -                        ((ComponentModel)child).setCategories( 
  -                           target.getCategoriesDirective() );
  -                    }
  -                    else if( child instanceof ContainmentModel )
  -                    {
  -                        ((ContainmentModel)child).setCategories( 
  -                          target.getCategoriesDirective() );
  -                    }
  -                }
  -            }
  -            else
  -            {
  -                final String warning = 
  -                  "Unrecognized target path: " + target.getPath();
  -                getLogger().warn( warning );
  -            }
  -        }
  +        model.applyTargets( targets );
           return model;
       }
   
  @@ -1413,156 +1046,72 @@
       */
       public DeploymentModel getModel( String path )
       {
  -        ContainmentModel parent = 
  -          m_context.getParentContainmentModel();
  +        DefaultContainmentModelNavigationHelper helper = 
  +          new DefaultContainmentModelNavigationHelper( m_context, this );
  +        return helper.getModel( path );
  +    }
   
  -        if( path.equals( "" ) )
  +   /**
  +    * Apply a set of override targets resolvable from a supplied url.
  +    * @param config a url resolvable to a TargetDirective[]
  +    * @exception ModelException if an error occurs
  +    */
  +    public void applyTargets( URL config )
  +      throws ModelException
  +    {
  +        if( config != null )
           {
  -            return this;
  +            TargetDirective[] targets = getTargets( config );
  +            applyTargets( targets );
           }
  -        else if( path.startsWith( "/" ) )
  -        {
  -            //
  -            // its a absolute reference that need to be handled by the 
  -            // root container
  -            //
  -
  -            if( null != parent )
  -            {
  -                return parent.getModel( path );
  -            }
  -            else
  -            {
  -                //
  -                // this is the root container thereforw the 
  -                // path can be transfored to a relative reference
  -                //
  +    }
   
  -                return getModel( path.substring( 1 ) );
  -            }
  -        }
  -        else
  +   /**
  +    * Apply a set of override targets.
  +    * @param targets a set of target directives
  +    */
  +    public void applyTargets( TargetDirective[]targets )
  +    {
  +        for( int i=0; i<targets.length; i++ )
           {
  -            //
  -            // its a relative reference in the form xxx/yyy/zzz
  -            // so if the path contains "/", then locate the token 
  -            // proceeding the "/" (i.e. xxx) and apply the remainder 
  -            // (i.e. yyy/zzz) as the path argument , otherwise, its 
  -            // a local reference that we can pull from the model 
  -            // repository
  -            //
  -
  -            final String root = getRootName( path );
  -
  -            if( root.equals( ".." ) )
  -            {
  -                //
  -                // its a relative reference in the form "../xxx/yyy" 
  -                // in which case we simply redirect "xxx/yyy" to the 
  -                // parent container
  -                //
  - 
  -                if( null != parent )
  -                {
  -                    final String remainder = getRemainder( root, path );
  -                    return parent.getModel( remainder );
  -                }
  -                else
  -                {
  -                    final String error = 
  -                      "Supplied path ["
  -                      + path 
  -                      + "] references a container above the root container.";
  -                    throw new IllegalArgumentException( error );
  -                }
  -            }
  -            else if( root.equals( "." ) )
  -            {
  -                //
  -                // its a path with a redundant "./xxx/yyy" which is 
  -                // equivalent to "xxx/yyy"
  -                //
  - 
  -                final String remainder = getRemainder( root, path );
  -                return getModel( remainder );
  -            }
  -            else if( path.indexOf( "/" ) < 0 )
  -            {
  -                // 
  -                // its a path in the form "xxx" so we can use this
  -                // to lookup and return a local child
  -                //
  -
  -                return m_context.getModelRepository().getModel( path );
  -            }
  -            else
  +            TargetDirective target = targets[i];
  +            final String path = target.getPath();
  +            Object model = getModel( path );
  +            if( model != null )
               {
  -                //
  -                // locate the relative root container, and apply 
  -                // getModel to the container
  -                //
  -
  -                DeploymentModel model = 
  -                  m_context.getModelRepository().getModel( root );
  -                if( model != null )
  +                if( model instanceof ComponentModel )
                   {
  -                    //
  -                    // we have the sub-container so we can apply 
  -                    // the relative path after subtracting the name of 
  -                    // this container and the path seperator character
  -                    //
  -
  -                    if( model instanceof ContainmentModel )
  +                    ComponentModel deployment = (ComponentModel) model;
  +                    if( target.getConfiguration() != null )
                       {
  -                        ContainmentModel container = 
  -                          (ContainmentModel) model;
  -                        final String remainder = getRemainder( root, path );
  -                        return container.getModel( remainder );
  +                        deployment.setConfiguration( 
  +                          target.getConfiguration() );
                       }
  -                    else
  +                    if( target.getCategoriesDirective() != null )
                       {
  -                        final String error = 
  -                          "The path element [" + root 
  -                          + "] does not reference a containment model within ["
  -                          + this + "].";
  -                        throw new IllegalArgumentException( error );
  +                        deployment.setCategories( 
  +                          target.getCategoriesDirective() );
                       }
                   }
  -                else
  +                else if( model instanceof ContainmentModel )
                   {
  -                    //
  -                    // path contains a token that does not map to 
  -                    // known container
  -                    //
  -                    
  -                    final String error = 
  -                      "Unable to locate a container with name [" 
  -                      + root + "] within the container [" 
  -                      + this + "].";
  -                    throw new IllegalArgumentException( error );
  +                    ContainmentModel containment = (ContainmentModel) model;
  +                    if( target.getCategoriesDirective() != null )
  +                    {
  +                        containment.setCategories( 
  +                          target.getCategoriesDirective() );
  +                    }
                   }
               }
  +            else
  +            {
  +                final String warning = 
  +                  REZ.getString( "target.ignore", path, toString() );
  +                getLogger().warn( warning );
  +            }
           }
       }
   
  -    private String getRootName( String path )
  -    {
  -        int n = path.indexOf( "/" );
  -        if( n < 0 ) 
  -        {
  -            return path;
  -        }
  -        else
  -        {
  -            return path.substring( 0, n ); 
  -        }
  -    }
  -
  -    private String getRemainder( String name, String path )
  -    {
  -        return path.substring( name.length() + 1 );
  -    }
  -
       //==============================================================
       // implementation
       //==============================================================
  @@ -1620,67 +1169,6 @@
           }
       }
   
  -   /**
  -    * Apply a set of override targets resolvable from a supplied url.
  -    * @param config a url resolvable to a TargetDirective[]
  -    * @exception ModelException if an error occurs
  -    */
  -    public void applyTargets( URL config )
  -      throws ModelException
  -    {
  -        if( config != null )
  -        {
  -            TargetDirective[] targets = getTargets( config );
  -            applyTargets( targets );
  -        }
  -    }
  -
  -   /**
  -    * Apply a set of override targets.
  -    * @param targets a set of target directives
  -    */
  -    public void applyTargets( TargetDirective[]targets )
  -    {
  -        for( int i=0; i<targets.length; i++ )
  -        {
  -            TargetDirective target = targets[i];
  -            final String path = target.getPath();
  -            Object model = getModel( path );
  -            if( model != null )
  -            {
  -                if( model instanceof ComponentModel )
  -                {
  -                    ComponentModel deployment = (ComponentModel) model;
  -                    if( target.getConfiguration() != null )
  -                    {
  -                        deployment.setConfiguration( 
  -                          target.getConfiguration() );
  -                    }
  -                    if( target.getCategoriesDirective() != null )
  -                    {
  -                        deployment.setCategories( 
  -                          target.getCategoriesDirective() );
  -                    }
  -                }
  -                else if( model instanceof ContainmentModel )
  -                {
  -                    ContainmentModel containment = (ContainmentModel) model;
  -                    if( target.getCategoriesDirective() != null )
  -                    {
  -                        containment.setCategories( 
  -                          target.getCategoriesDirective() );
  -                    }
  -                }
  -            }
  -            else
  -            {
  -                final String warning = 
  -                  "Ignoring target directive as the path does not refer to a known component: " 
  -                  + path;
  -                getLogger().warn( warning );
  -            }
  -        }
  -    }
   
       private TargetDirective[] getTargets( final URL url )
         throws ModelException
  
  
  
  1.5       +1 -0      avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/Resources.properties,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Resources.properties	13 Jan 2004 11:41:26 -0000	1.4
  +++ Resources.properties	7 Feb 2004 19:31:08 -0000	1.5
  @@ -25,6 +25,7 @@
   containment.add=installing: {0}
   containment.model.create.error=Unexpected error while attempting to build a model within the containment context [{0}] for a dependency [{1}].
   containment.model.create.deployment.error=Unexpected error while attempting to build a deployment profile from the template [{0}] within the containment context [{1}] for a component type [{2}].
  +target.ignore=Ignoring target directive as the path [{0}] does not refer to a known component within the containment model {1}.
   
   
   # DefaultDeploymentModel
  
  
  
  1.1                  avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModelAssemblyHelper.java
  
  Index: DefaultContainmentModelAssemblyHelper.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.composition.model.impl;
  
  import java.util.ArrayList;
  
  import org.apache.avalon.composition.data.DeploymentProfile;
  import org.apache.avalon.composition.model.ContainmentModel;
  import org.apache.avalon.composition.model.ContainmentContext;
  import org.apache.avalon.composition.model.ComponentModel;
  import org.apache.avalon.composition.model.DeploymentModel;
  import org.apache.avalon.composition.model.AssemblyException;
  import org.apache.avalon.composition.model.ModelRepository;
  import org.apache.avalon.composition.model.ContextModel;
  import org.apache.avalon.composition.model.DependencyModel;
  import org.apache.avalon.composition.model.StageModel;
  import org.apache.avalon.composition.model.TypeUnknownException;
  import org.apache.avalon.composition.model.ModelRuntimeException;
  import org.apache.avalon.composition.model.ModelException;
  import org.apache.avalon.composition.model.TypeRepository;
  import org.apache.avalon.composition.model.ModelSelector;
  import org.apache.avalon.composition.model.ProfileSelector;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  import org.apache.avalon.meta.info.Type;
  import org.apache.avalon.meta.info.DependencyDescriptor;
  import org.apache.avalon.meta.info.ServiceDescriptor;
  import org.apache.avalon.meta.info.StageDescriptor;
  
  /**
   * A utility class that assists in the location of a model relative
   * a supplied path.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2004/02/07 19:31:08 $
   */
  class DefaultContainmentModelAssemblyHelper
  {
      //-------------------------------------------------------------------
      // static
      //-------------------------------------------------------------------
  
      private static final Resources REZ =
        ResourceManager.getPackageResources( 
          DefaultContainmentModelAssemblyHelper.class );
  
      //-------------------------------------------------------------------
      // immutable state
      //-------------------------------------------------------------------
  
      private final ContainmentContext m_context;
      private final ContainmentModel m_model;
  
      //-------------------------------------------------------------------
      // constructor
      //-------------------------------------------------------------------
  
      public DefaultContainmentModelAssemblyHelper( 
        ContainmentContext context, ContainmentModel model )
      {
          m_context = context;
          m_model = model;
      }
  
      //-------------------------------------------------------------------
      // implementation
      //-------------------------------------------------------------------
  
      public void assembleModel( DeploymentModel model ) 
        throws AssemblyException
      {
           if( null == model )
           {
               throw new NullPointerException( "model" );
           }
  
           if( model.isAssembled() ) 
           {
               return;
           }
           else
           {
               if( model instanceof ComponentModel )
               {
                   assembleComponent( (ComponentModel) model );
               }
               else
               {
                   model.assemble();
               }
          }
      }
  
      private void assembleComponent( ComponentModel model ) throws AssemblyException
      {
          ModelRepository repository = m_context.getModelRepository();
  
          //
          // locate and assemble the component context handler
          //
  
          if( model.getContextModel() != null )
          {
              ContextModel context = model.getContextModel();
              Class clazz = context.getStrategyClass();
              if( !clazz.getName().equals( 
                ContextModel.DEFAULT_STRATEGY_CLASSNAME ) )
              {
                  if( null == context.getProvider() )
                  {
                      try
                      {
                          StageDescriptor stage = 
                            new StageDescriptor( clazz.getName() );
                          DeploymentModel provider = 
                            findExtensionProvider( repository, stage );
                          context.setProvider( provider );
                      }
                      catch( Throwable e )
                      {
                          final String error = 
                            "Unable to assemble component: " 
                            + model 
                           + " due to a component context phase handler establishment failure.";
                          throw new AssemblyException( error, e );
                      }
                  }
              }
          }
  
          //
          // locate and resolve the stage providers
          //
  
          StageModel[] stages = model.getStageModels();
          for( int i=0; i<stages.length; i++ )
          {
              StageModel stage = stages[i];
              if( null == stage.getProvider() )
              {
                  try
                  {
                      DeploymentModel provider =
                        findExtensionProvider( repository, stage );
                      stage.setProvider( provider );
                  }
                  catch( Throwable e )
                  {
                      final String error = 
                        "Unable to assemble component: " 
                        + model 
                        + " due to a component extension handler establishment failure.";
                      throw new AssemblyException( error, e );
                  }
              }
          }
  
          //
          // locate and resolve the service providers
          //
  
          DependencyModel[] dependencies = model.getDependencyModels();
          for( int i=0; i<dependencies.length; i++ )
          {
              DependencyModel dependency = dependencies[i];
              if( null == dependency.getProvider() )
              {
                  try
                  {
                      DeploymentModel provider =
                        findDependencyProvider( repository, dependency );
                      dependency.setProvider( provider );
                  }
                  catch( Throwable e )
                  {
                      final String error = 
                        "Unable to assemble component: " + model 
                        + " due to a service provider establishment failure.";
                      throw new AssemblyException( error, e );
                  }
              }
          }
      }
  
      private DeploymentModel findDependencyProvider( 
        ModelRepository repository, DependencyModel dependency )
        throws AssemblyException
      {
          String path = dependency.getPath();
          if( null != path )
          {
              DeploymentModel model = m_model.getModel( path );
              if( null == model )
              {
                  final String error = 
                    "Could not locate a model at the address: [" 
                    + path + "] in " + this + ".";
                  throw new AssemblyException( error );
              }
              assembleModel( model );
              return model;
          }
          else
          {
              return findDependencyProvider( 
                repository, dependency.getDependency() );
          }
      }
  
      private DeploymentModel findDependencyProvider( 
        ModelRepository repository, DependencyDescriptor dependency )
        throws AssemblyException
      {
          DeploymentModel[] candidates = 
            repository.getCandidateProviders( dependency );
          ModelSelector selector = new DefaultModelSelector();
          DeploymentModel model = selector.select( candidates, dependency );
          if( model != null )
          {
              assembleModel( model );
              return model;
          }
  
          //
          // otherwise, check for any packaged profiles that 
          // we could use to construct the model
          //
  
          DeploymentProfile[] profiles = findDependencyProfiles( dependency );
          ProfileSelector profileSelector = new DefaultProfileSelector();
          DeploymentProfile profile = profileSelector.select( profiles, dependency );
          if( profile != null ) 
          {
              try
              {
                  DeploymentModel solution = m_model.addModel( profile );
                  assembleModel( solution );
                  return solution;
              }
              catch( AssemblyException ae )
              {
                  final String error = 
                    "Nested assembly failure while attempting to construct model"
                    + " for the profile: [" + profile + "] for the dependency: ["
                    + dependency + "].";
                  throw new AssemblyException( error, ae );
              }
              catch( ModelException me )
              {
                  final String error = 
                    "Nested model failure while attempting to add model"
                    + " for the profile: [" + profile + "] for the dependency: ["
                    + dependency + "].";
                  throw new AssemblyException( error, me );
              }
          }
          else
          {
              final String error = 
                "Unable to locate a service provider for the dependency: [ "
                + dependency + "].";
              throw new AssemblyException( error );
          }
      }
  
      private DeploymentModel findExtensionProvider( 
        ModelRepository repository, StageModel stage )
        throws AssemblyException
      {
          String path = stage.getPath();
          if( null != path )
          {
              DeploymentModel model = m_model.getModel( path );
              if( null == model )
              {
                  final String error = 
                    "Could not locate a model at the address: [" 
                    + path + "] in " + this + ".";
                  throw new AssemblyException( error );
              }
              assembleModel( model );
              return model;
          }
          else
          {
              return findExtensionProvider( repository, stage.getStage() );
          }
      }
  
      private DeploymentModel findExtensionProvider( 
        ModelRepository repository, StageDescriptor stage )
        throws AssemblyException
      {
          DeploymentModel[] candidates = 
            repository.getCandidateProviders( stage );
          ModelSelector selector = new DefaultModelSelector();
          DeploymentModel model = selector.select( candidates, stage );
          if( model != null )
          {
              assembleModel( model );
              return model;
          }
  
          //
          // otherwise, check for any packaged profiles that 
          // we could use to construct the model
          //
  
          DeploymentProfile[] profiles = findExtensionProfiles( stage );
          ProfileSelector profileSelector = new DefaultProfileSelector();
          DeploymentProfile profile = profileSelector.select( profiles, stage );
          if( profile != null ) 
          {
              try
              {
                  DeploymentModel solution = m_model.addModel( profile );
                  assembleModel( solution );
                  return solution;
              }
              catch( AssemblyException ae )
              {
                  final String error = 
                    "Nested assembly failure while attempting to construct model"
                    + " for the extension profile: [" + profile 
                    + "] for the stage dependency: ["
                    + stage + "].";
                  throw new AssemblyException( error, ae );
              }
              catch( ModelException me )
              {
                  final String error = 
                    "Nested model failure while attempting to add model"
                    + " for the extension profile: [" + profile 
                    + "] for the stage dependency: ["
                    + stage + "].";
                  throw new AssemblyException( error, me );
              }
          }
          else
          {
              final String error = 
                "Unable to locate a extension provider for the stage: [ "
                + stage + "].";
              throw new AssemblyException( error );
          }
      }
  
      private DeploymentProfile[] findExtensionProfiles( StageDescriptor stage )
      {
          TypeRepository repository = m_context.getClassLoaderModel().getTypeRepository();
          Type[] types = repository.getTypes( stage );
          try
          {
              return getProfiles( repository, types );
          }
          catch( TypeUnknownException tue )
          {
              // will not happen
              final String error = "An irrational condition has occured.";
              throw new ModelRuntimeException( error, tue );
          }
      }
  
      private DeploymentProfile[] findDependencyProfiles( DependencyDescriptor dependency )
      {
          TypeRepository repository = m_context.getClassLoaderModel().getTypeRepository();
          Type[] types = repository.getTypes( dependency );
          try
          {
              return getProfiles( repository, types );
          }
          catch( TypeUnknownException tue )
          {
              // will not happen
              final String error = "An irrational condition has occured.";
              throw new ModelRuntimeException( error, tue );
          }
      }
  
      private DeploymentProfile[] getProfiles( TypeRepository repository, Type[] types )
        throws TypeUnknownException
      {
          ArrayList list = new ArrayList();
          for( int i=0; i<types.length; i++ )
          {
              DeploymentProfile[] profiles = 
              repository.getProfiles( types[i] );
              for( int j=0; j<profiles.length; j++ )
              {
                  list.add( profiles[j] );
              }
          }
          return (DeploymentProfile[]) list.toArray( new DeploymentProfile[0] );
      }
  
  }
  
  
  
  1.1                  avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModelComponentHelper.java
  
  Index: DefaultContainmentModelComponentHelper.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.composition.model.impl;
  
  import java.io.File;
  import java.io.InputStream;
  import java.net.URL;
  import java.net.URLConnection;
  
  import org.apache.avalon.composition.data.ComponentProfile;
  import org.apache.avalon.composition.data.ContainmentProfile;
  import org.apache.avalon.composition.data.ClassLoaderDirective;
  import org.apache.avalon.composition.data.builder.ContainmentProfileBuilder;
  import org.apache.avalon.composition.data.builder.XMLContainmentProfileCreator;
  import org.apache.avalon.composition.model.ClassLoaderModel;
  import org.apache.avalon.composition.model.ClassLoaderContext;
  import org.apache.avalon.composition.model.ContainmentModel;
  import org.apache.avalon.composition.model.ContainmentContext;
  import org.apache.avalon.composition.model.ComponentContext;
  import org.apache.avalon.composition.model.ComponentModel;
  import org.apache.avalon.composition.model.ModelFactory;
  import org.apache.avalon.composition.model.ModelException;
  import org.apache.avalon.composition.model.SystemContext;
  import org.apache.avalon.composition.model.DependencyGraph;
  
  import org.apache.avalon.logging.provider.LoggingManager;
  import org.apache.avalon.logging.data.CategoriesDirective;
  
  import org.apache.avalon.repository.Repository;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  
  import org.apache.avalon.meta.info.Type;
  
  /**
   * A utility class that handles creation of a component model context.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2004/02/07 19:31:08 $
   */
  class DefaultContainmentModelComponentHelper
  {
      //-------------------------------------------------------------------
      // static
      //-------------------------------------------------------------------
  
      private static final Resources REZ =
        ResourceManager.getPackageResources( 
          DefaultContainmentModelComponentHelper.class );
  
      //-------------------------------------------------------------------
      // immutable state
      //-------------------------------------------------------------------
  
      private final ContainmentContext m_context;
      private final ContainmentModel m_model;
  
      //-------------------------------------------------------------------
      // constructor
      //-------------------------------------------------------------------
  
     /**
      * Creation of a component context creation helper.
      * @param context the containment model context
      * @param model the containment model
      */
      public DefaultContainmentModelComponentHelper( 
         ContainmentContext context, ContainmentModel model )
      {
          if( context == null )
          {
              throw new NullPointerException( "context" );
          }
          if( model == null )
          {
              throw new NullPointerException( "model" );
          }
          m_context = context;
          m_model = model;
      }
  
      //-------------------------------------------------------------------
      // implementation
      //-------------------------------------------------------------------
  
     /**
      * Creation of a new component model relative to a supplied profile.
      *
      * @param profile the component profile
      * @return the component model context
      */
      public ComponentContext createComponentContext( final ComponentProfile profile )
        throws ModelException
      {
          if( null == profile )
          {
              throw new NullPointerException( "profile" );
          }
  
          SystemContext system = m_context.getSystemContext();
          final String name = profile.getName();
          final String partition = m_model.getPartition();
          LoggingManager logging = system.getLoggingManager();
          CategoriesDirective categories = profile.getCategories();
          if( null != categories )
          {
              logging.addCategories( partition, categories );
          }
  
          Logger logger = 
            logging.getLoggerForCategory( partition + name );
          DependencyGraph graph = m_context.getDependencyGraph();
          ClassLoader classloader = m_context.getClassLoader();
          final File home = new File( m_context.getHomeDirectory(), name );
          final File temp = new File( m_context.getTempDirectory(), name );
  
          try
          {
              Class base = classloader.loadClass( profile.getClassname() );
              Type type = 
                m_model.getClassLoaderModel().getTypeRepository().getType( base );
  
              return new DefaultComponentContext( 
                  logger, 
                  name, 
                  system, 
                  classloader, 
                  graph, 
                  m_model, 
                  profile, 
                  type, 
                  base, 
                  home, 
                  temp, 
                  partition );
  
          }
          catch( Throwable e )
          {
              final String error = 
                REZ.getString( 
                  "containment.deployment.create.error", 
                  m_model.getPath(), 
                  name );
              throw new ModelException( error, e );
          }
      }
  }
  
  
  
  1.1                  avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModelNavigationHelper.java
  
  Index: DefaultContainmentModelNavigationHelper.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.composition.model.impl;
  
  import org.apache.avalon.composition.model.ContainmentModel;
  import org.apache.avalon.composition.model.ContainmentContext;
  import org.apache.avalon.composition.model.DeploymentModel;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  /**
   * A utility class that assists in the location of a model relative
   * a supplied path.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   * @version $Revision: 1.1 $ $Date: 2004/02/07 19:31:08 $
   */
  class DefaultContainmentModelNavigationHelper
  {
      //-------------------------------------------------------------------
      // static
      //-------------------------------------------------------------------
  
      private static final Resources REZ =
        ResourceManager.getPackageResources( 
          DefaultContainmentModelNavigationHelper.class );
  
      //-------------------------------------------------------------------
      // immutable state
      //-------------------------------------------------------------------
  
      private final ContainmentContext m_context;
      private final ContainmentModel m_model;
  
      //-------------------------------------------------------------------
      // constructor
      //-------------------------------------------------------------------
  
      public DefaultContainmentModelNavigationHelper( 
        ContainmentContext context, ContainmentModel model )
      {
          m_context = context;
          m_model = model;
      }
  
      //-------------------------------------------------------------------
      // implementation
      //-------------------------------------------------------------------
  
      public DeploymentModel getModel( String path )
      {
          ContainmentModel parent = 
            m_context.getParentContainmentModel();
  
          if( path.equals( "" ) )
          {
              return m_model;
          }
          else if( path.startsWith( "/" ) )
          {
              //
              // its a absolute reference that need to be handled by the 
              // root container
              //
  
              if( null != parent )
              {
                  return parent.getModel( path );
              }
              else
              {
                  //
                  // this is the root container thereforw the 
                  // path can be transfored to a relative reference
                  //
  
                  return m_model.getModel( path.substring( 1 ) );
              }
          }
          else
          {
              //
              // its a relative reference in the form xxx/yyy/zzz
              // so if the path contains "/", then locate the token 
              // proceeding the "/" (i.e. xxx) and apply the remainder 
              // (i.e. yyy/zzz) as the path argument , otherwise, its 
              // a local reference that we can pull from the model 
              // repository
              //
  
              final String root = getRootName( path );
  
              if( root.equals( ".." ) )
              {
                  //
                  // its a relative reference in the form "../xxx/yyy" 
                  // in which case we simply redirect "xxx/yyy" to the 
                  // parent container
                  //
   
                  if( null != parent )
                  {
                      final String remainder = getRemainder( root, path );
                      return parent.getModel( remainder );
                  }
                  else
                  {
                      final String error = 
                        "Supplied path ["
                        + path 
                        + "] references a container above the root container.";
                      throw new IllegalArgumentException( error );
                  }
              }
              else if( root.equals( "." ) )
              {
                  //
                  // its a path with a redundant "./xxx/yyy" which is 
                  // equivalent to "xxx/yyy"
                  //
   
                  final String remainder = getRemainder( root, path );
                  return m_model.getModel( remainder );
              }
              else if( path.indexOf( "/" ) < 0 )
              {
                  // 
                  // its a path in the form "xxx" so we can use this
                  // to lookup and return a local child
                  //
  
                  return m_context.getModelRepository().getModel( path );
              }
              else
              {
                  //
                  // locate the relative root container, and apply 
                  // getModel to the container
                  //
  
                  DeploymentModel model = 
                    m_context.getModelRepository().getModel( root );
                  if( model != null )
                  {
                      //
                      // we have the sub-container so we can apply 
                      // the relative path after subtracting the name of 
                      // this container and the path seperator character
                      //
  
                      if( model instanceof ContainmentModel )
                      {
                          ContainmentModel container = 
                            (ContainmentModel) model;
                          final String remainder = getRemainder( root, path );
                          return container.getModel( remainder );
                      }
                      else
                      {
                          final String error = 
                            "The path element [" + root 
                            + "] does not reference a containment model within ["
                            + m_model + "].";
                          throw new IllegalArgumentException( error );
                      }
                  }
                  else
                  {
                      //
                      // path contains a token that does not map to 
                      // known container
                      //
                      
                      final String error = 
                        "Unable to locate a container with name [" 
                        + root + "] within the container [" 
                        + m_model + "].";
                      throw new IllegalArgumentException( error );
                  }
              }
          }
      }
  
      private String getRootName( String path )
      {
          int n = path.indexOf( "/" );
          if( n < 0 ) 
          {
              return path;
          }
          else
          {
              return path.substring( 0, n ); 
          }
      }
  
      private String getRemainder( String name, String path )
      {
          return path.substring( name.length() + 1 );
      }
  
  }
  
  
  

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


Mime
View raw message