avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder InfoCreator.java SerializedInfoCreator.java XMLInfoCreator.java ComponentInfoBuilder.java Resources.properties
Date Fri, 28 Jun 2002 00:36:45 GMT
donaldp     2002/06/27 17:36:45

  Modified:    containerkit/src/java/org/apache/excalibur/containerkit/infobuilder
                        ComponentInfoBuilder.java Resources.properties
  Added:       containerkit/src/java/org/apache/excalibur/containerkit/infobuilder
                        InfoCreator.java SerializedInfoCreator.java
                        XMLInfoCreator.java
  Log:
  Made it possible to load descriptors from either a
  serialized ComponentInfo object or XML descriptor. This
  allows us to use this class even when XML classes are not
  present (or when we want a smaller foot print)
  
  Revision  Changes    Path
  1.14      +75 -337   jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/ComponentInfoBuilder.java
  
  Index: ComponentInfoBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/ComponentInfoBuilder.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ComponentInfoBuilder.java	27 Jun 2002 16:18:17 -0000	1.13
  +++ ComponentInfoBuilder.java	28 Jun 2002 00:36:45 -0000	1.14
  @@ -8,23 +8,12 @@
   package org.apache.excalibur.containerkit.infobuilder;
   
   import java.io.InputStream;
  -import java.util.ArrayList;
  -import java.util.Properties;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  -import org.apache.avalon.framework.Version;
  -import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.context.Context;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  -import org.apache.excalibur.containerkit.metainfo.ComponentDescriptor;
  +import org.apache.avalon.framework.logger.Logger;
   import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
  -import org.apache.excalibur.containerkit.metainfo.ContextDescriptor;
  -import org.apache.excalibur.containerkit.metainfo.DependencyDescriptor;
  -import org.apache.excalibur.containerkit.metainfo.EntryDescriptor;
  -import org.apache.excalibur.containerkit.metainfo.ServiceDescriptor;
  -import org.apache.excalibur.containerkit.metainfo.ServiceDesignator;
  -import org.xml.sax.InputSource;
   
   /**
    * A ComponentInfoBuilder is responsible for building {@link ComponentInfo}
  @@ -41,6 +30,22 @@
       private static final Resources REZ =
           ResourceManager.getPackageResources( ComponentInfoBuilder.class );
   
  +    private final InfoCreator m_xmlInfoCreator = createXMLInfoCreator();
  +    private final InfoCreator m_serialInfoCreator = new SerializedInfoCreator();
  +
  +    /**
  +     * Setup logging for all subcomponents
  +     */
  +    public void enableLogging( final Logger logger )
  +    {
  +        super.enableLogging( logger );
  +        setupLogger( m_serialInfoCreator );
  +        if( null != m_xmlInfoCreator )
  +        {
  +            setupLogger( m_xmlInfoCreator );
  +        }
  +    }
  +
       /**
        * Create a {@link ComponentInfo} object for specified Class.
        *
  @@ -67,376 +72,109 @@
                                   final ClassLoader classLoader )
           throws Exception
       {
  -        final String xinfo =
  -            classname.replace( '.', '/' ) + ".xinfo";
  -        final InputStream inputStream =
  -            classLoader.getResourceAsStream( xinfo );
  -
  -        if( null == inputStream )
  +        final ComponentInfo info = buildFromSerDescriptor( classname, classLoader );
  +        if( null != info )
           {
  -            final String message =
  -                REZ.getString( "builder.missing-info.error",
  -                               classname );
  -            throw new Exception( message );
  +            return info;
  +        }
  +        else
  +        {
  +            return buildFromXMLDescriptor( classname, classLoader );
           }
  -
  -        return build( classname, inputStream );
  -    }
  -
  -    /**
  -     * Create a {@link ComponentInfo} object for specified
  -     * classname, loaded from specified URI.
  -     *
  -     * @param classname The classname of Component
  -     * @param uri the URI to load ComponentInfo from
  -     * @return the created ComponentInfo
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    protected ComponentInfo build( final String classname,
  -                                   final String uri )
  -        throws Exception
  -    {
  -        final InputSource input = new InputSource( uri );
  -        return build( classname, input );
       }
   
       /**
  -     * Create a {@link ComponentInfo} object for specified
  -     * classname, loaded from specified {@link InputStream}.
  +     * Build ComponentInfo from the XML descriptor format.
        *
        * @param classname The classname of Component
  -     * @param inputSource the InputStream to load ComponentInfo from
  +     * @param classLoader the ClassLoader to load info from
        * @return the created ComponentInfo
  -     * @throws ConfigurationException if an error occurs
  +     * @throws Exception if an error occurs
        */
  -    private ComponentInfo build( final String classname,
  -                                 final InputStream inputSource )
  +    private ComponentInfo buildFromSerDescriptor( final String classname,
  +                                                  final ClassLoader classLoader )
           throws Exception
       {
  -        final InputSource input = new InputSource( inputSource );
  -        return build( classname, input );
  -    }
  +        final String xinfo =
  +            classname.replace( '.', '/' ) + ".sinfo";
  +        final InputStream inputStream =
  +            classLoader.getResourceAsStream( xinfo );
  +        if( null == inputStream )
  +        {
  +            return null;
  +        }
   
  -    /**
  -     * Create a {@link ComponentInfo} object for specified
  -     * classname, loaded from specified {@link InputSource}.
  -     *
  -     * @param classname The classname of Component
  -     * @param inputSource the InputSource to load ComponentInfo from
  -     * @return the created ComponentInfo
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private ComponentInfo build( final String classname,
  -                                 final InputSource inputSource )
  -        throws Exception
  -    {
  -        final Configuration configuration = ConfigurationBuilder.build( inputSource );
  -        return build( classname, configuration );
  +        return m_serialInfoCreator.createComponentInfo( classname, inputStream );
       }
   
       /**
  -     * Create a <code>ComponentInfo</code> object for specified classname from
  -     * specified configuration data.
  +     * Build ComponentInfo from the XML descriptor format.
        *
        * @param classname The classname of Component
  -     * @param info the ComponentInfo configuration
  +     * @param classLoader the ClassLoader to load info from
        * @return the created ComponentInfo
  -     * @throws ConfigurationException if an error occurs
  +     * @throws Exception if an error occurs
        */
  -    private ComponentInfo build( final String classname,
  -                                 final Configuration info )
  +    private ComponentInfo buildFromXMLDescriptor( final String classname,
  +                                                  final ClassLoader classLoader )
           throws Exception
       {
  -        if( getLogger().isDebugEnabled() )
  +        final String xinfo =
  +            classname.replace( '.', '/' ) + ".xinfo";
  +        final InputStream inputStream =
  +            classLoader.getResourceAsStream( xinfo );
  +        if( null == inputStream )
           {
               final String message =
  -                REZ.getString( "builder.creating-info.notice",
  +                REZ.getString( "builder.missing-info.error",
                                  classname );
  -            getLogger().debug( message );
  -        }
  -
  -        final String topLevelName = info.getName();
  -        if( !topLevelName.equals( "component-info" ) )
  -        {
  -            final String message =
  -                REZ.getString( "builder.bad-toplevel-element.error",
  -                               classname,
  -                               topLevelName );
  -            throw new ConfigurationException( message );
  -        }
  -
  -        Configuration configuration = null;
  -
  -        configuration = info.getChild( "context" );
  -        final ContextDescriptor context = buildContext( configuration );
  -
  -        configuration = info.getChild( "services" );
  -        final ServiceDescriptor[] services = buildServices( configuration );
  -
  -        configuration = info.getChild( "dependencies" );
  -        final DependencyDescriptor[] dependencies = buildDependencies( classname, configuration
);
  -
  -        configuration = info.getChild( "component" );
  -        final ComponentDescriptor descriptor =
  -            buildComponentDescriptor( classname, configuration );
  -
  -        if( getLogger().isDebugEnabled() )
  -        {
  -            final String message =
  -                REZ.getString( "builder.created-info.notice",
  -                               classname,
  -                               new Integer( services.length ),
  -                               new Integer( dependencies.length ),
  -                               new Integer( context.getEntrys().length ) );
  -            getLogger().debug( message );
  -        }
  -
  -        return new ComponentInfo( descriptor, context, services, dependencies );
  -    }
  -
  -    /**
  -     * A utility method to build an array of <code>DependencyDescriptor</code>
  -     * objects from specified configuraiton and classname.
  -     *
  -     * @param classname The classname of Component (used for logging purposes)
  -     * @param configuration the dependencies configuration
  -     * @return the created DependencyDescriptor
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private DependencyDescriptor[] buildDependencies( final String classname,
  -                                                      final Configuration configuration
)
  -        throws ConfigurationException
  -    {
  -        final Configuration[] elements = configuration.getChildren( "dependency" );
  -        final ArrayList dependencies = new ArrayList();
  -
  -        for( int i = 0; i < elements.length; i++ )
  -        {
  -            final DependencyDescriptor dependency =
  -                buildDependency( classname, elements[ i ] );
  -            dependencies.add( dependency );
  +            throw new Exception( message );
           }
   
  -        return (DependencyDescriptor[])dependencies.toArray( new DependencyDescriptor[
0 ] );
  +        final InfoCreator xmlInfoCreator = getXMLInfoCreator( classname );
  +        return xmlInfoCreator.createComponentInfo( classname, inputStream );
       }
   
       /**
  -     * A utility method to build a {@link DependencyDescriptor}
  -     * object from specified configuraiton.
  +     * Utility to get xml info builder, else throw
  +     * an exception if missing descriptor.
        *
  -     * @param classname The classname of Component (used for logging purposes)
  -     * @param dependency the dependency configuration
  -     * @return the created DependencyDescriptor
  -     * @throws ConfigurationException if an error occurs
  +     * @return the InfoCreator
        */
  -    private DependencyDescriptor buildDependency( final String classname,
  -                                                  final Configuration dependency )
  -        throws ConfigurationException
  +    private InfoCreator getXMLInfoCreator( final String classname )
  +        throws Exception
       {
  -        final ServiceDesignator service =
  -            buildServiceDesignator( dependency.getChild( "service-ref" ) );
  -        final boolean optional =
  -            dependency.getAttributeAsBoolean( "optional", false );
  -
  -        final Properties attributes =
  -            buildAttributes( dependency.getChild( "attributes" ) );
  -
  -        String role = dependency.getChild( "role" ).getValue( null );
  -
  -        //default to name of service if role unspecified
  -        if( null == role )
  +        if( null != m_xmlInfoCreator )
           {
  -            role = service.getClassname();
  +            return m_xmlInfoCreator;
           }
           else
           {
  -            //If role is specified and it is the same as
  -            //service name then warn that it is redundent.
  -            if( role.equals( service.getClassname() ) )
  -            {
  -                final String message =
  -                    REZ.getString( "builder.redundent-role.notice",
  -                                   classname,
  -                                   role );
  -                getLogger().warn( message );
  -            }
  -        }
  -
  -        return new DependencyDescriptor( role, service, optional, attributes );
  -    }
  -
  -    /**
  -     * A utility method to build a {@link ContextDescriptor}
  -     * object from specified configuraiton.
  -     *
  -     * @param context the dependency configuration
  -     * @return the created ContextDescriptor
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private ContextDescriptor buildContext( final Configuration context )
  -        throws ConfigurationException
  -    {
  -        final EntryDescriptor[] entrys =
  -            buildEntrys( context.getChildren( "entry" ) );
  -
  -        final Properties attributes =
  -            buildAttributes( context.getChild( "attributes" ) );
  -
  -        final String type =
  -            context.getAttribute( "type",
  -                                  Context.class.getName() );
  -
  -        return new ContextDescriptor( type, entrys, attributes );
  -    }
  -
  -    /**
  -     * A utility method to build an array of {@link EntryDescriptor}
  -     * objects from specified configuraiton.
  -     *
  -     * @param entrySet the set of entrys to build
  -     * @return the created {@link EntryDescriptor}s
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private EntryDescriptor[] buildEntrys( final Configuration[] entrySet )
  -        throws ConfigurationException
  -    {
  -        final ArrayList entrys = new ArrayList();
  -
  -        for( int i = 0; i < entrySet.length; i++ )
  -        {
  -            final EntryDescriptor service = buildEntry( entrySet[ i ] );
  -            entrys.add( service );
  +            final String message =
  +                REZ.getString( "builder.missing-xml-creator.error",
  +                               classname );
  +            throw new Exception( message );
           }
  -
  -        return (EntryDescriptor[])entrys.toArray( new EntryDescriptor[ entrys.size() ]
);
  -    }
  -
  -    /**
  -     * Create a {@link EntryDescriptor} from configuration.
  -     *
  -     * @param config the configuration
  -     * @return the created {@link EntryDescriptor}
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private EntryDescriptor buildEntry( final Configuration config )
  -        throws ConfigurationException
  -    {
  -        final String key = config.getAttribute( "key" );
  -        final String type = config.getAttribute( "type" );
  -        final boolean optional =
  -            config.getAttributeAsBoolean( "optional", false );
  -
  -        return new EntryDescriptor( key, type, optional );
       }
   
       /**
  -     * A utility method to build an array of {@link ServiceDescriptor}
  -     * objects from specified configuraiton.
  +     * Utility to get XMLInfoCreator if XML files are on
  +     * ClassPath.
        *
  -     * @param servicesSet the services configuration
  -     * @return the created ServiceDescriptor
  -     * @throws ConfigurationException if an error occurs
  +     * @return the XML {@link InfoCreator}
        */
  -    private ServiceDescriptor[] buildServices( final Configuration servicesSet )
  -        throws ConfigurationException
  +    private static InfoCreator createXMLInfoCreator()
       {
  -        final Configuration[] elements = servicesSet.getChildren( "service" );
  -        final ArrayList services = new ArrayList();
  -
  -        for( int i = 0; i < elements.length; i++ )
  +        InfoCreator xmlInfoCreator = null;
  +        try
           {
  -            final ServiceDescriptor service = buildService( elements[ i ] );
  -            services.add( service );
  +            xmlInfoCreator = new XMLInfoCreator();
           }
  -
  -        return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] );
  -    }
  -
  -    /**
  -     * A utility method to build a {@link ServiceDesignator}
  -     * object from specified configuraiton data.
  -     *
  -     * @param service the service Configuration
  -     * @return the created ServiceDesignator
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private ServiceDesignator buildServiceDesignator( final Configuration service )
  -        throws ConfigurationException
  -    {
  -        final String name = service.getAttribute( "type" );
  -        final String versionString = service.getAttribute( "version", "1.0" );
  -        final Version version = buildVersion( versionString );
  -        return new ServiceDesignator( name, version );
  -    }
  -
  -    /**
  -     * A utility method to build a <code>ServiceDescriptor</code>
  -     * object from specified configuraiton data.
  -     *
  -     * @param service the service Configuration
  -     * @return the created ServiceDescriptor
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private ServiceDescriptor buildService( final Configuration service )
  -        throws ConfigurationException
  -    {
  -        final Configuration serviceRef = service.getChild( "service-ref" );
  -        final ServiceDesignator designator = buildServiceDesignator( serviceRef );
  -        final Properties attributes =
  -            buildAttributes( service.getChild( "attributes" ) );
  -        return new ServiceDescriptor( designator, attributes );
  -    }
  -
  -    /**
  -     * Build up a list of attributes from specific config tree.
  -     *
  -     * @param config the attributes config
  -     * @return the Properties object representing attributes
  -     */
  -    private Properties buildAttributes( final Configuration config )
  -        throws ConfigurationException
  -    {
  -        final Properties attributes = new Properties();
  -        final Configuration[] children = config.getChildren( "attribute" );
  -        for( int i = 0; i < children.length; i++ )
  +        catch( final Exception e )
           {
  -            Configuration child = children[ i ];
  -            final String key = child.getAttribute( "key" );
  -            final String value = child.getAttribute( "value" );
  -            attributes.setProperty( key, value );
  +            //Ignore it if ClassNot found due to no
  +            //XML Classes on classpath
           }
  -        return attributes;
  -    }
  -
  -    /**
  -     * A utility method to build a {@link ComponentDescriptor}
  -     * object from specified configuraiton data and classname.
  -     *
  -     * @param classname The classname of Component (used to create descriptor)
  -     * @param component the Component Configuration
  -     * @return the created ComponentDescriptor
  -     * @throws ConfigurationException if an error occurs
  -     */
  -    private ComponentDescriptor buildComponentDescriptor( final String classname,
  -                                                          final Configuration component
)
  -        throws ConfigurationException
  -    {
  -        final String name = component.getChild( "name" ).getValue( null );
  -        final Version version = buildVersion( component.getChild( "version" ).getValue(
"1.0" ) );
  -        final Properties attributes =
  -            buildAttributes( component.getChild( "attributes" ) );
  -
  -        return new ComponentDescriptor( name, classname, version, attributes );
  -    }
  -
  -    /**
  -     * A utility method to parse a Version object from specified string.
  -     *
  -     * @param version the version string
  -     * @return the created Version object
  -     */
  -    private Version buildVersion( final String version )
  -    {
  -        return Version.getVersion( version );
  +        return xmlInfoCreator;
       }
   }
  
  
  
  1.4       +2 -1      jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/Resources.properties,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Resources.properties	27 Jun 2002 15:05:39 -0000	1.3
  +++ Resources.properties	28 Jun 2002 00:36:45 -0000	1.4
  @@ -2,4 +2,5 @@
   builder.creating-info.notice=Creating a ComponentInfo for class "{0}".
   builder.created-info.notice=Constructed ComponentInfo object for class {0}. ComponentInfo
contains {1} services, {2} dependencies and {3} context entrys.
   builder.bad-toplevel-element.error=Error the component implemented by "{0}" has an invalid
element at top level of component info descriptor. Expected: "component-info". Actual: "{1}"
  -builder.missing-info.error=Unable to locate resource from which to load info for component
implemented by class "{0}".
  \ No newline at end of file
  +builder.missing-info.error=Unable to locate resource from which to load info for component
implemented by class "{0}".
  +builder.missing-xml-creator.error=Unable to create XMLInfoCreator, usually due to not having
XML classes on Classpath. Thus unable to lookup XML descriptor for component type "{0}".
  \ No newline at end of file
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/InfoCreator.java
  
  Index: InfoCreator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.containerkit.infobuilder;
  
  import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
  import java.io.InputStream;
  
  /**
   * Simple interface used to create {@link ComponentInfo}
   * from stream. This abstraction was primarily created so
   * that the ComponentInfo could be built from non-XML
   * sources and no XML classes need be in the classpath.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/28 00:36:45 $
   */
  interface InfoCreator
  {
      /**
       * Create a {@link ComponentInfo} from stream
       *
       * @param implementationKey the name of component type that we are looking up
       * @param inputStream the stream that the resource is loaded from
       * @return the newly created {@link ComponentInfo}
       * @throws Exception
       */
      ComponentInfo createComponentInfo( String implementationKey,
                                         InputStream inputStream )
          throws Exception;
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/SerializedInfoCreator.java
  
  Index: SerializedInfoCreator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.containerkit.infobuilder;
  
  import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
  import java.io.InputStream;
  import java.io.ObjectInputStream;
  
  /**
   * Create {@link ComponentInfo} from stream made up of
   * serialized object.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/28 00:36:45 $
   */
  class SerializedInfoCreator
      implements InfoCreator
  {
      public ComponentInfo createComponentInfo( final String implementationKey,
                                                final InputStream inputStream )
          throws Exception
      {
          final ObjectInputStream ois = new ObjectInputStream( inputStream );
          return (ComponentInfo)ois.readObject();
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/XMLInfoCreator.java
  
  Index: XMLInfoCreator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.containerkit.infobuilder;
  
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Properties;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.Version;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.excalibur.containerkit.metainfo.ComponentDescriptor;
  import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
  import org.apache.excalibur.containerkit.metainfo.ContextDescriptor;
  import org.apache.excalibur.containerkit.metainfo.DependencyDescriptor;
  import org.apache.excalibur.containerkit.metainfo.EntryDescriptor;
  import org.apache.excalibur.containerkit.metainfo.ServiceDescriptor;
  import org.apache.excalibur.containerkit.metainfo.ServiceDesignator;
  import org.xml.sax.InputSource;
  
  /**
   * A ComponentInfoBuilder is responsible for building {@link ComponentInfo}
   * objects from Configuration objects. The format for Configuration object
   * is specified in the <a href="package-summary.html#external">package summary</a>.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/28 00:36:45 $
   */
  final class XMLInfoCreator
      extends AbstractLogEnabled
      implements InfoCreator
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( XMLInfoCreator.class );
  
      /**
       * Create a {@link ComponentInfo} object for specified
       * classname, loaded from specified {@link InputStream}.
       *
       * @param implementationKey The classname of Component
       * @param inputStream the InputStream to load ComponentInfo from
       * @return the created ComponentInfo
       * @throws ConfigurationException if an error occurs
       */
      public ComponentInfo createComponentInfo( String implementationKey,
                                                InputStream inputStream )
          throws Exception
      {
          final InputSource input = new InputSource( inputStream );
          final String classname = implementationKey;
          final Configuration configuration = ConfigurationBuilder.build( input );
          return build( classname, configuration );
      }
  
      /**
       * Create a <code>ComponentInfo</code> object for specified classname from
       * specified configuration data.
       *
       * @param classname The classname of Component
       * @param info the ComponentInfo configuration
       * @return the created ComponentInfo
       * @throws ConfigurationException if an error occurs
       */
      private ComponentInfo build( final String classname,
                                   final Configuration info )
          throws Exception
      {
          if( getLogger().isDebugEnabled() )
          {
              final String message =
                  REZ.getString( "builder.creating-info.notice",
                                 classname );
              getLogger().debug( message );
          }
  
          final String topLevelName = info.getName();
          if( !topLevelName.equals( "component-info" ) )
          {
              final String message =
                  REZ.getString( "builder.bad-toplevel-element.error",
                                 classname,
                                 topLevelName );
              throw new ConfigurationException( message );
          }
  
          Configuration configuration = null;
  
          configuration = info.getChild( "context" );
          final ContextDescriptor context = buildContext( configuration );
  
          configuration = info.getChild( "services" );
          final ServiceDescriptor[] services = buildServices( configuration );
  
          configuration = info.getChild( "dependencies" );
          final DependencyDescriptor[] dependencies = buildDependencies( classname, configuration
);
  
          configuration = info.getChild( "component" );
          final ComponentDescriptor descriptor =
              buildComponentDescriptor( classname, configuration );
  
          if( getLogger().isDebugEnabled() )
          {
              final String message =
                  REZ.getString( "builder.created-info.notice",
                                 classname,
                                 new Integer( services.length ),
                                 new Integer( dependencies.length ),
                                 new Integer( context.getEntrys().length ) );
              getLogger().debug( message );
          }
  
          return new ComponentInfo( descriptor, context, services, dependencies );
      }
  
      /**
       * A utility method to build an array of <code>DependencyDescriptor</code>
       * objects from specified configuraiton and classname.
       *
       * @param classname The classname of Component (used for logging purposes)
       * @param configuration the dependencies configuration
       * @return the created DependencyDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private DependencyDescriptor[] buildDependencies( final String classname,
                                                        final Configuration configuration
)
          throws ConfigurationException
      {
          final Configuration[] elements = configuration.getChildren( "dependency" );
          final ArrayList dependencies = new ArrayList();
  
          for( int i = 0; i < elements.length; i++ )
          {
              final DependencyDescriptor dependency =
                  buildDependency( classname, elements[ i ] );
              dependencies.add( dependency );
          }
  
          return (DependencyDescriptor[])dependencies.toArray( new DependencyDescriptor[ 0
] );
      }
  
      /**
       * A utility method to build a {@link DependencyDescriptor}
       * object from specified configuraiton.
       *
       * @param classname The classname of Component (used for logging purposes)
       * @param dependency the dependency configuration
       * @return the created DependencyDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private DependencyDescriptor buildDependency( final String classname,
                                                    final Configuration dependency )
          throws ConfigurationException
      {
          final ServiceDesignator service =
              buildServiceDesignator( dependency.getChild( "service-ref" ) );
          final boolean optional =
              dependency.getAttributeAsBoolean( "optional", false );
  
          final Properties attributes =
              buildAttributes( dependency.getChild( "attributes" ) );
  
          String role = dependency.getChild( "role" ).getValue( null );
  
          //default to name of service if role unspecified
          if( null == role )
          {
              role = service.getClassname();
          }
          else
          {
              //If role is specified and it is the same as
              //service name then warn that it is redundent.
              if( role.equals( service.getClassname() ) )
              {
                  final String message =
                      REZ.getString( "builder.redundent-role.notice",
                                     classname,
                                     role );
                  getLogger().warn( message );
              }
          }
  
          return new DependencyDescriptor( role, service, optional, attributes );
      }
  
      /**
       * A utility method to build a {@link ContextDescriptor}
       * object from specified configuraiton.
       *
       * @param context the dependency configuration
       * @return the created ContextDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ContextDescriptor buildContext( final Configuration context )
          throws ConfigurationException
      {
          final EntryDescriptor[] entrys =
              buildEntrys( context.getChildren( "entry" ) );
  
          final Properties attributes =
              buildAttributes( context.getChild( "attributes" ) );
  
          final String type =
              context.getAttribute( "type",
                                    Context.class.getName() );
  
          return new ContextDescriptor( type, entrys, attributes );
      }
  
      /**
       * A utility method to build an array of {@link EntryDescriptor}
       * objects from specified configuraiton.
       *
       * @param entrySet the set of entrys to build
       * @return the created {@link EntryDescriptor}s
       * @throws ConfigurationException if an error occurs
       */
      private EntryDescriptor[] buildEntrys( final Configuration[] entrySet )
          throws ConfigurationException
      {
          final ArrayList entrys = new ArrayList();
  
          for( int i = 0; i < entrySet.length; i++ )
          {
              final EntryDescriptor service = buildEntry( entrySet[ i ] );
              entrys.add( service );
          }
  
          return (EntryDescriptor[])entrys.toArray( new EntryDescriptor[ entrys.size() ] );
      }
  
      /**
       * Create a {@link EntryDescriptor} from configuration.
       *
       * @param config the configuration
       * @return the created {@link EntryDescriptor}
       * @throws ConfigurationException if an error occurs
       */
      private EntryDescriptor buildEntry( final Configuration config )
          throws ConfigurationException
      {
          final String key = config.getAttribute( "key" );
          final String type = config.getAttribute( "type" );
          final boolean optional =
              config.getAttributeAsBoolean( "optional", false );
  
          return new EntryDescriptor( key, type, optional );
      }
  
      /**
       * A utility method to build an array of {@link ServiceDescriptor}
       * objects from specified configuraiton.
       *
       * @param servicesSet the services configuration
       * @return the created ServiceDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ServiceDescriptor[] buildServices( final Configuration servicesSet )
          throws ConfigurationException
      {
          final Configuration[] elements = servicesSet.getChildren( "service" );
          final ArrayList services = new ArrayList();
  
          for( int i = 0; i < elements.length; i++ )
          {
              final ServiceDescriptor service = buildService( elements[ i ] );
              services.add( service );
          }
  
          return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] );
      }
  
      /**
       * A utility method to build a {@link ServiceDesignator}
       * object from specified configuraiton data.
       *
       * @param service the service Configuration
       * @return the created ServiceDesignator
       * @throws ConfigurationException if an error occurs
       */
      private ServiceDesignator buildServiceDesignator( final Configuration service )
          throws ConfigurationException
      {
          final String name = service.getAttribute( "type" );
          final String versionString = service.getAttribute( "version", "1.0" );
          final Version version = buildVersion( versionString );
          return new ServiceDesignator( name, version );
      }
  
      /**
       * A utility method to build a <code>ServiceDescriptor</code>
       * object from specified configuraiton data.
       *
       * @param service the service Configuration
       * @return the created ServiceDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ServiceDescriptor buildService( final Configuration service )
          throws ConfigurationException
      {
          final Configuration serviceRef = service.getChild( "service-ref" );
          final ServiceDesignator designator = buildServiceDesignator( serviceRef );
          final Properties attributes =
              buildAttributes( service.getChild( "attributes" ) );
          return new ServiceDescriptor( designator, attributes );
      }
  
      /**
       * Build up a list of attributes from specific config tree.
       *
       * @param config the attributes config
       * @return the Properties object representing attributes
       */
      private Properties buildAttributes( final Configuration config )
          throws ConfigurationException
      {
          final Properties attributes = new Properties();
          final Configuration[] children = config.getChildren( "attribute" );
          for( int i = 0; i < children.length; i++ )
          {
              Configuration child = children[ i ];
              final String key = child.getAttribute( "key" );
              final String value = child.getAttribute( "value" );
              attributes.setProperty( key, value );
          }
          return attributes;
      }
  
      /**
       * A utility method to build a {@link ComponentDescriptor}
       * object from specified configuraiton data and classname.
       *
       * @param classname The classname of Component (used to create descriptor)
       * @param component the Component Configuration
       * @return the created ComponentDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ComponentDescriptor buildComponentDescriptor( final String classname,
                                                            final Configuration component
)
          throws ConfigurationException
      {
          final String name = component.getChild( "name" ).getValue( null );
          final Version version = buildVersion( component.getChild( "version" ).getValue(
"1.0" ) );
          final Properties attributes =
              buildAttributes( component.getChild( "attributes" ) );
  
          return new ComponentDescriptor( name, classname, version, attributes );
      }
  
      /**
       * A utility method to parse a Version object from specified string.
       *
       * @param version the version string
       * @return the created Version object
       */
      private Version buildVersion( final String version )
      {
          return Version.getVersion( version );
      }
  }
  
  
  

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


Mime
View raw message