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/02/07 21:21:03 UTC

cvs commit: avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl DefaultContainmentModelExportHelper.java DefaultContainmentModel.java

mcconnell    2004/02/07 12:21:03

  Modified:    merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        DefaultContainmentModel.java
  Added:       merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl
                        DefaultContainmentModelExportHelper.java
  Log:
  Factory out the service export functionality into a helper class.
  
  Revision  Changes    Path
  1.29      +146 -244  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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- DefaultContainmentModel.java	7 Feb 2004 19:31:08 -0000	1.28
  +++ DefaultContainmentModel.java	7 Feb 2004 20:21:03 -0000	1.29
  @@ -104,7 +104,8 @@
       //--------------------------------------------------------------
   
       private static final Resources REZ =
  -            ResourceManager.getPackageResources( DefaultContainmentModel.class );
  +      ResourceManager.getPackageResources( 
  +        DefaultContainmentModel.class );
   
       private static final ContainmentProfileBuilder BUILDER = 
         new ContainmentProfileBuilder();
  @@ -194,18 +195,9 @@
           // setup the service export parameters
           //
   
  -        ServiceDirective[] export = 
  -          context.getContainmentProfile().getExportDirectives();
  -        m_services = new DefaultServiceModel[ export.length ];
  -        for( int i=0; i<export.length; i++ )
  -        {
  -            ServiceDirective service = export[i];
  -            Class clazz = getServiceExportClass( service );
  -            DeploymentModel provider = 
  -              locateImplementionProvider( service );
  -            m_services[i] = 
  -              new DefaultServiceModel( service, clazz, provider ); 
  -        }
  +        DefaultContainmentModelExportHelper helper =
  +          new DefaultContainmentModelExportHelper( m_context, this );
  +        m_services = helper.createServiceExport();
       }
   
       //--------------------------------------------------------------
  @@ -233,8 +225,8 @@
       **/ 
       public boolean isSecureExecutionEnabled()
       {
  -        SystemContext sc = m_context.getSystemContext();
  -        return sc.isCodeSecurityEnabled();
  +        SystemContext system = m_context.getSystemContext();
  +        return system.isCodeSecurityEnabled();
       }
      
      /** 
  @@ -485,7 +477,7 @@
       * @return the model 
       * @exception ModelException if a model related error occurs
       */
  -    public DeploymentModel addModel( URL url ) throws ModelException
  +    public ContainmentModel addContainmentModel( URL url ) throws ModelException
       {
           return addContainmentModel( url, null );
       }
  @@ -499,6 +491,14 @@
           return model;
       }
   
  +   /**
  +    * Addition of a new subsidiary model within
  +    * the containment context using a supplied profile.
  +    *
  +    * @param profile a containment or deployment profile 
  +    * @return the model based on the supplied profile
  +    * @exception ModelException if an error occurs during model establishment
  +    */
       public DeploymentModel addModel( DeploymentProfile profile ) throws ModelException
       {
           if( null == profile )
  @@ -549,6 +549,135 @@
           return addModel( name, model );
       }
   
  +   /**
  +    * Removal of a named model for the containment model.
  +    *
  +    * @param name the name of the subsidiary model to be removed
  +    * @exception IllegalArgumentException if the supplied name is unknown
  +    */
  +    public void removeModel( String name ) throws IllegalArgumentException
  +    {
  +        ModelRepository repository = m_context.getModelRepository();
  +        synchronized( repository )
  +        {
  +            DeploymentModel model = (DeploymentModel) repository.getModel( name );
  +            if( null == model )
  +            {
  +                final String error = 
  +                  "No model named [" + name 
  +                  + "] is referenced with the model [" 
  +                  + this + "].";
  +                throw new IllegalArgumentException( error ); 
  +            }
  +            else
  +            {
  +                m_context.getDependencyGraph().add( model );
  +                repository.removeModel( model );
  +                CompositionEvent event = new CompositionEvent( this, model );
  +                fireModelRemovedEvent( event );
  +            }
  +        }
  +    }
  +
  +   /**
  +    * Return the partition name established by this containment context.
  +    * @return the partition name
  +    */
  +    public String getPartition()
  +    {
  +        return m_partition;
  +    }
  +
  +   /**
  +    * Return the set of immediate child models nested 
  +    * within this model.
  +    *
  +    * @return the nested model
  +    */
  +    public DeploymentModel[] getModels()
  +    {
  +        return m_context.getModelRepository().getModels();
  +    }
  +
  +   /**
  +    * Return a child model relative to a supplied name.
  +    *
  +    * @param path a relative or absolute path
  +    * @return the named model or null if the name is unknown
  +    * @exception IllegalArgumentException if the name if badly formed
  +    */
  +    public DeploymentModel getModel( String path )
  +    {
  +        DefaultContainmentModelNavigationHelper helper = 
  +          new DefaultContainmentModelNavigationHelper( m_context, this );
  +        return helper.getModel( path );
  +    }
  +
  +   /**
  +    * 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 = 
  +                  REZ.getString( "target.ignore", path, toString() );
  +                getLogger().warn( warning );
  +            }
  +        }
  +    }
  +
  +    //--------------------------------------------------------------
  +    // private
  +    //--------------------------------------------------------------
  +
       private DeploymentModel addModel( 
         String name, DeploymentModel model ) throws ModelException
       {
  @@ -585,37 +714,6 @@
           }
       }
   
  -
  -   /**
  -    * Removal of a named model for the containment model.
  -    *
  -    * @param name the name of the subsidiary model to be removed
  -    * @exception IllegalArgumentException if the supplied name is unknown
  -    */
  -    public void removeModel( String name ) throws IllegalArgumentException
  -    {
  -        ModelRepository repository = m_context.getModelRepository();
  -        synchronized( repository )
  -        {
  -            DeploymentModel model = (DeploymentModel) repository.getModel( name );
  -            if( null == model )
  -            {
  -                final String error = 
  -                  "No model named [" + name 
  -                  + "] is referenced with the model [" 
  -                  + this + "].";
  -                throw new IllegalArgumentException( error ); 
  -            }
  -            else
  -            {
  -                m_context.getDependencyGraph().add( model );
  -                repository.removeModel( model );
  -                CompositionEvent event = new CompositionEvent( this, model );
  -                fireModelRemovedEvent( event );
  -            }
  -        }
  -    }
  -
       private void fireModelRemovedEvent( CompositionEvent event )
       {
           Iterator iterator = m_compositionListeners.iterator();
  @@ -1018,105 +1116,6 @@
       }
   
      /**
  -    * Return the partition name established by this containment context.
  -    * @return the partition name
  -    */
  -    public String getPartition()
  -    {
  -        return m_partition;
  -    }
  -
  -   /**
  -    * Return the set of immediate child models nested 
  -    * within this model.
  -    *
  -    * @return the nested model
  -    */
  -    public DeploymentModel[] getModels()
  -    {
  -        return m_context.getModelRepository().getModels();
  -    }
  -
  -   /**
  -    * Return a child model relative to a supplied name.
  -    *
  -    * @param path a relative or absolute path
  -    * @return the named model or null if the name is unknown
  -    * @exception IllegalArgumentException if the name if badly formed
  -    */
  -    public DeploymentModel getModel( String path )
  -    {
  -        DefaultContainmentModelNavigationHelper helper = 
  -          new DefaultContainmentModelNavigationHelper( m_context, this );
  -        return helper.getModel( path );
  -    }
  -
  -   /**
  -    * 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 = 
  -                  REZ.getString( "target.ignore", path, toString() );
  -                getLogger().warn( warning );
  -            }
  -        }
  -    }
  -
  -    //==============================================================
  -    // implementation
  -    //==============================================================
  -
  -   /**
       * Conver a classic url to a jar url.  If the supplied url protocol is not 
       * the "jar" protocol, a ne url is created by prepending jar: and adding the 
       * trailing "!/".
  @@ -1185,103 +1184,6 @@
               final String error = 
                 "Could not load the targets directive: " + url;
               throw new ModelException( error, e );
  -        }
  -    }
  -
  -   /**
  -    * Return the class declared by a container service export declaration.
  -    * @return the exported service interface class
  -    * @exception ModelException if the class cannot be resolved
  -    */
  -    private Class getServiceExportClass( ServiceDirective service )
  -      throws ModelException
  -    {
  -        String classname = service.getReference().getClassname();
  -        try
  -        {
  -            ClassLoader classloader = m_context.getClassLoader();
  -            return classloader.loadClass( classname );
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Cannot load service class [" 
  -              + classname 
  -              + "].";
  -            throw new ModelException( error, e );
  -        }
  -    }
  -
  -   /**
  -    * Given a service directive declared by a container, locate a model 
  -    * with this containment model to map as the provider.  If not model
  -    * is explicity declared, the implementation will attempt to construct
  -    * a new model based on packaged profiles and add the created model to
  -    * the set of models within this container.
  -    * 
  -    * @param service the service directive
  -    * @return the implementing deployment model
  -    * @exception ModelException if an implementation is not resolvable 
  -    */
  -    private DeploymentModel locateImplementionProvider( ServiceDirective service )
  -      throws ModelException
  -    {
  -        final String path = service.getPath();
  -        if( null != path )
  -        {
  -            DeploymentModel provider = getModel( path );
  -            if( null == provider )
  -            {
  -                final String error = 
  -                  "Implemention provider path [" 
  -                  + path 
  -                  + "] for the exported service [" 
  -                  + service.getReference()
  -                  + "] in the containment model "
  -                  + this
  -                  + " does not reference a known model.";
  -               throw new ModelException( error );
  -            }
  -            else
  -            {
  -                return provider;
  -            }
  -        }
  -        else
  -        {
  -            final DependencyDescriptor dependency = 
  -              new DependencyDescriptor( 
  -                "export", 
  -                service.getReference() );
  -
  -            final ModelRepository repository = m_context.getModelRepository();
  -            final DeploymentModel[] candidates = repository.getModels();
  -            final ModelSelector selector = new DefaultModelSelector();
  -            DeploymentModel provider = selector.select( candidates, dependency );
  -            if( null != provider )
  -            {
  -                return provider;
  -            }
  -            else
  -            {
  -                TypeRepository repo = 
  -                  getClassLoaderModel().getTypeRepository();
  -                DeploymentProfile profile = 
  -                  repo.getProfile( dependency, false );
  -                if( profile != null )
  -                {
  -                    return addModel( profile );
  -                }
  -                else
  -                {
  -                    final String error = 
  -                      "Could not locate a provider for the exported service [" 
  -                        + dependency.getReference()
  -                        + "] in the containment model "
  -                        + this;
  -                    throw new ModelException( error );
  -                }
  -            }
           }
       }
   }
  
  
  
  1.1                  avalon/merlin/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModelExportHelper.java
  
  Index: DefaultContainmentModelExportHelper.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.data.ServiceDirective;
  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.DeploymentModel;
  import org.apache.avalon.composition.model.ModelException;
  import org.apache.avalon.composition.model.ModelRepository;
  import org.apache.avalon.composition.model.ModelSelector;
  import org.apache.avalon.composition.model.TypeRepository;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  import org.apache.avalon.meta.info.DependencyDescriptor;
  
  /**
   * 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 20:21:03 $
   */
  class DefaultContainmentModelExportHelper
  {
      //-------------------------------------------------------------------
      // static
      //-------------------------------------------------------------------
  
      private static final Resources REZ =
        ResourceManager.getPackageResources( 
          DefaultContainmentModelExportHelper.class );
  
      //-------------------------------------------------------------------
      // immutable state
      //-------------------------------------------------------------------
  
      private final ContainmentContext m_context;
      private final ContainmentModel m_model;
  
      //-------------------------------------------------------------------
      // constructor
      //-------------------------------------------------------------------
  
      public DefaultContainmentModelExportHelper( 
        ContainmentContext context, ContainmentModel model )
      {
          m_context = context;
          m_model = model;
      }
  
      //-------------------------------------------------------------------
      // implementation
      //-------------------------------------------------------------------
  
      public DefaultServiceModel[] createServiceExport() throws ModelException
      {
  
          ServiceDirective[] export = 
            m_context.getContainmentProfile().getExportDirectives();
          DefaultServiceModel[] services = new DefaultServiceModel[ export.length ];
          for( int i=0; i<export.length; i++ )
          {
              ServiceDirective service = export[i];
              Class clazz = getServiceExportClass( service );
              DeploymentModel provider = 
                locateImplementionProvider( service );
              services[i] = 
                new DefaultServiceModel( service, clazz, provider ); 
          }
          return services;
      }
  
     /**
      * Return the class declared by a container service export declaration.
      * @return the exported service interface class
      * @exception ModelException if the class cannot be resolved
      */
      private Class getServiceExportClass( ServiceDirective service )
        throws ModelException
      {
          String classname = service.getReference().getClassname();
          try
          {
              ClassLoader classloader = m_context.getClassLoader();
              return classloader.loadClass( classname );
          }
          catch( Throwable e )
          {
              final String error = 
                "Cannot load service class [" 
                + classname 
                + "].";
              throw new ModelException( error, e );
          }
      }
  
     /**
      * Given a service directive declared by a container, locate a model 
      * with this containment model to map as the provider.  If not model
      * is explicity declared, the implementation will attempt to construct
      * a new model based on packaged profiles and add the created model to
      * the set of models within this container.
      * 
      * @param service the service directive
      * @return the implementing deployment model
      * @exception ModelException if an implementation is not resolvable 
      */
      private DeploymentModel locateImplementionProvider( ServiceDirective service )
        throws ModelException
      {
          final String path = service.getPath();
          if( null != path )
          {
              DeploymentModel provider = m_model.getModel( path );
              if( null == provider )
              {
                  final String error = 
                    "Implemention provider path [" 
                    + path 
                    + "] for the exported service [" 
                    + service.getReference()
                    + "] in the containment model "
                    + m_model
                    + " does not reference a known model.";
                 throw new ModelException( error );
              }
              else
              {
                  return provider;
              }
          }
          else
          {
              final DependencyDescriptor dependency = 
                new DependencyDescriptor( 
                  "export", 
                  service.getReference() );
  
              final ModelRepository repository = m_context.getModelRepository();
              final DeploymentModel[] candidates = repository.getModels();
              final ModelSelector selector = new DefaultModelSelector();
              DeploymentModel provider = selector.select( candidates, dependency );
              if( null != provider )
              {
                  return provider;
              }
              else
              {
                  TypeRepository repo = 
                    m_context.getClassLoaderModel().getTypeRepository();
                  DeploymentProfile profile = 
                    repo.getProfile( dependency, false );
                  if( profile != null )
                  {
                      return m_model.addModel( profile );
                  }
                  else
                  {
                      final String error = 
                        "Could not locate a provider for the exported service [" 
                          + dependency.getReference()
                          + "] in the containment model "
                          + m_model;
                      throw new ModelException( error );
                  }
              }
          }
      }
  
  }
  
  
  

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