You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/09/14 06:22:26 UTC

cvs commit: jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder LegacyBlockInfoCreator.java blockinfo.dtd ComponentInfoBuilder.java ConfigurationBuilder.java DTDResolver.java Resources.properties

donaldp     2002/09/13 21:22:25

  Modified:    info/src/java/org/apache/avalon/framework/tools/infobuilder
                        ComponentInfoBuilder.java ConfigurationBuilder.java
                        DTDResolver.java Resources.properties
  Added:       info/src/java/org/apache/avalon/framework/tools/infobuilder
                        LegacyBlockInfoCreator.java blockinfo.dtd
  Log:
  Integrate in support for building ComponentInfo files from descriptors defined using Phoenixs BlockInfo format.
  
  Revision  Changes    Path
  1.3       +54 -2     jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/ComponentInfoBuilder.java
  
  Index: ComponentInfoBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/ComponentInfoBuilder.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ComponentInfoBuilder.java	13 Sep 2002 15:42:12 -0000	1.2
  +++ ComponentInfoBuilder.java	14 Sep 2002 04:22:25 -0000	1.3
  @@ -32,6 +32,7 @@
   
       private final InfoCreator m_xmlInfoCreator = createXMLInfoCreator();
       private final InfoCreator m_serialInfoCreator = new SerializedInfoCreator();
  +    private final InfoCreator m_legacyInfoCreator = createLegacyInfoCreator();
   
       /**
        * Setup logging for all subcomponents
  @@ -72,7 +73,13 @@
                                   final ClassLoader classLoader )
           throws Exception
       {
  -        final ComponentInfo info = buildFromSerDescriptor( classname, classLoader );
  +        ComponentInfo info = buildFromSerDescriptor( classname, classLoader );
  +        if( null != info )
  +        {
  +            return info;
  +        }
  +
  +        info = buildFromLegacyDescriptor( classname, classLoader );
           if( null != info )
           {
               return info;
  @@ -108,6 +115,30 @@
       }
   
       /**
  +     * Build ComponentInfo from the legacy XML descriptor format.
  +     *
  +     * @param classname The classname of Component
  +     * @param classLoader the ClassLoader to load info from
  +     * @return the created ComponentInfo
  +     * @throws Exception if an error occurs
  +     */
  +    private ComponentInfo buildFromLegacyDescriptor( final String classname,
  +                                                  final ClassLoader classLoader )
  +        throws Exception
  +    {
  +        final String xinfo =
  +            classname.replace( '.', '/' ) + ".xinfo";
  +        final InputStream inputStream =
  +            classLoader.getResourceAsStream( xinfo );
  +        if( null == inputStream )
  +        {
  +            return null;
  +        }
  +
  +        return m_legacyInfoCreator.createComponentInfo( classname, inputStream );
  +    }
  +
  +    /**
        * Build ComponentInfo from the XML descriptor format.
        *
        * @param classname The classname of Component
  @@ -169,6 +200,27 @@
           try
           {
               xmlInfoCreator = new XMLInfoCreator();
  +        }
  +        catch( final Exception e )
  +        {
  +            //Ignore it if ClassNot found due to no
  +            //XML Classes on classpath
  +        }
  +        return xmlInfoCreator;
  +    }
  +
  +    /**
  +     * Utility to get LegacyInfoCreator if XML files are on
  +     * ClassPath.
  +     *
  +     * @return the Legacy {@link InfoCreator}
  +     */
  +    private static InfoCreator createLegacyInfoCreator()
  +    {
  +        InfoCreator xmlInfoCreator = null;
  +        try
  +        {
  +            xmlInfoCreator = new LegacyBlockInfoCreator();
           }
           catch( final Exception e )
           {
  
  
  
  1.5       +9 -2      jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/ConfigurationBuilder.java
  
  Index: ConfigurationBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/ConfigurationBuilder.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ConfigurationBuilder.java	13 Sep 2002 08:41:55 -0000	1.4
  +++ ConfigurationBuilder.java	14 Sep 2002 04:22:25 -0000	1.5
  @@ -30,10 +30,17 @@
           new DTDInfo( "-//AVALON/Component Info DTD Version 1.0//EN",
                        "http://jakarta.apache.org/avalon/dtds/info/componentinfo_1_0.dtd",
                        "org/apache/avalon/framework/tools/infobuilder/componentinfo.dtd" ),
  +        new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN",
  +                     "http://jakarta.apache.org/avalon/dtds/phoenix/blockinfo_1_0.dtd",
  +                     "org/apache/avalon/phoenix/tools/blockinfo.dtd" ),
  +        new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN",
  +                     "http://jakarta.apache.org/avalon/dtds/phoenix/blockinfo_1_0.dtd",
  +                     "org/apache/avalon/phoenix/tools/blockinfo.dtd" )
       };
   
       private static final DTDResolver c_resolver =
  -        new DTDResolver( c_dtdInfo, ConfigurationBuilder.class.getClassLoader() );
  +        new DTDResolver( c_dtdInfo,
  +                         ConfigurationBuilder.class.getClassLoader() );
   
       /**
        * Private constructor to block instantiation.
  
  
  
  1.2       +3 -2      jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/DTDResolver.java
  
  Index: DTDResolver.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/DTDResolver.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DTDResolver.java	31 Aug 2002 08:17:01 -0000	1.1
  +++ DTDResolver.java	14 Sep 2002 04:22:25 -0000	1.2
  @@ -38,7 +38,8 @@
        * Construct a resolver using specified DTDInfos where resources are loaded
        * from specified ClassLoader.
        */
  -    public DTDResolver( final DTDInfo[] dtdInfos, final ClassLoader classLoader )
  +    public DTDResolver( final DTDInfo[] dtdInfos,
  +                        final ClassLoader classLoader )
       {
           m_dtdInfos = dtdInfos;
           m_classLoader = classLoader;
  
  
  
  1.3       +4 -1      jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/Resources.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Resources.properties	8 Sep 2002 04:32:51 -0000	1.2
  +++ Resources.properties	14 Sep 2002 04:22:25 -0000	1.3
  @@ -3,4 +3,7 @@
   builder.created-info.notice=Constructed ComponentInfo object for class {0}. ComponentInfo contains {1} services, {2} dependencies, {3} context entrys and {4} loggers.
   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}".
  -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
  +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}".
  +
  +legacy.bad-toplevel-element.error=Error the block info descriptor has an invalid top level element. Expected: "blockinfo". Actual: "{1}"
  +legacy.created-info.notice=Created ComponentInfo object from BlockInfo descriptor, for class {0}. ComponentInfo contains {1} services and {2} dependencies.
  \ No newline at end of file
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/LegacyBlockInfoCreator.java
  
  Index: LegacyBlockInfoCreator.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.framework.tools.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.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.info.ComponentDescriptor;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.ContextDescriptor;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  import org.apache.avalon.framework.info.EntryDescriptor;
  import org.apache.avalon.framework.info.LoggerDescriptor;
  import org.apache.avalon.framework.info.ServiceDescriptor;
  import org.apache.avalon.framework.info.Tag;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.xml.sax.InputSource;
  
  /**
   * A LegacyBlockInfoCreator is responsible for building {@link ComponentInfo}
   * objects from <a href="http://jakarta.apache.org/avalon/phoenix">Phoenixs</a>
   * BlockInfo descriptors. The format for descriptor is specified in the
   * <a href="package-summary.html#external">package summary</a>.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/14 04:22:25 $
   */
  public final class LegacyBlockInfoCreator
      extends AbstractLogEnabled
      implements InfoCreator
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( LegacyBlockInfoCreator.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( final String implementationKey,
                                                final InputStream inputStream )
          throws Exception
      {
          final InputSource input = new InputSource( inputStream );
          final Configuration configuration = ConfigurationBuilder.build( input );
          return build( implementationKey, configuration );
      }
  
      /**
       * Create a {@link ComponentInfo} 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( "blockinfo" ) )
          {
              final String message =
                  REZ.getString( "legacy.bad-toplevel-element.error",
                                 classname,
                                 topLevelName );
              throw new ConfigurationException( message );
          }
  
          Configuration configuration = null;
  
          configuration = info.getChild( "block" );
          final ComponentDescriptor descriptor =
              buildComponentDescriptor( classname, configuration );
          final String implementationKey = descriptor.getImplementationKey();
  
          configuration = info.getChild( "services" );
          final ServiceDescriptor[] services = buildServices( configuration );
  
          configuration = info.getChild( "dependencies" );
          final DependencyDescriptor[] dependencies =
              buildDependencies( implementationKey, configuration );
  
          if( getLogger().isDebugEnabled() )
          {
              final String message =
                  REZ.getString( "legacy.created-info.notice",
                                 classname,
                                 new Integer( services.length ),
                                 new Integer( dependencies.length ) );
              getLogger().debug( message );
          }
  
          return new ComponentInfo( descriptor,
                                    new LoggerDescriptor[ 0 ],
                                    buildPhoenixContext(),
                                    services,
                                    dependencies );
      }
  
      /**
       * A utility method to build a descriptor for Phoenixs BlockContext
       * object,
       *
       * @return the a descriptor for Phoenixs BlockContext object,
       */
      private ContextDescriptor buildPhoenixContext()
      {
          return new ContextDescriptor( "org.apache.avalon.phoenix.BlockContext",
                                        new EntryDescriptor[ 0 ],
                                        new Tag[ 0 ] );
      }
  
      /**
       * A utility method to build an array of {@link DependencyDescriptor}
       * objects from specified configuration 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 String implementationKey =
              dependency.getChild( "service" ).getAttribute( "name" );
          String key = dependency.getChild( "role" ).getValue( null );
  
          //default to name of service if key unspecified
          if( null == key )
          {
              key = implementationKey;
          }
          else
          {
              //If key is specified and it is the same as
              //service name then warn that it is redundent.
              if( key.equals( implementationKey ) )
              {
                  final String message =
                      REZ.getString( "builder.redundent-key.notice",
                                     classname,
                                     key );
                  getLogger().warn( message );
              }
          }
  
          return new DependencyDescriptor( key,
                                           implementationKey,
                                           false,
                                           new Tag[ 0 ] );
      }
  
      /**
       * 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 ArrayList services = new ArrayList();
  
          Configuration[] elements = servicesSet.getChildren( "service" );
          for( int i = 0; i < elements.length; i++ )
          {
              final ServiceDescriptor service = buildService( elements[ i ], false );
              services.add( service );
          }
  
          elements = servicesSet.getChildren( "management-access-points" );
          for( int i = 0; i < elements.length; i++ )
          {
              final ServiceDescriptor service = buildService( elements[ i ], true );
              services.add( service );
          }
  
          return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] );
      }
  
      /**
       * A utility method to build a {@link ServiceDescriptor}
       * 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,
                                              final boolean isManagement )
          throws ConfigurationException
      {
          final String implementationKey = service.getAttribute( "name" );
          final String version = service.getAttribute( "version", "1.0.0" );
  
          final ArrayList tagSet = new ArrayList();
          final Tag tag = createSimpleTag( "avalon", "version", version );
          tagSet.add( tag );
          if( isManagement )
          {
              final Tag mxTag =
                  createSimpleTag( "phoenix", "mx", "true" );
              tagSet.add( mxTag );
          }
  
          final Tag[] tags = (Tag[])tagSet.toArray( new Tag[ tagSet.size() ] );
          return new ServiceDescriptor( implementationKey, tags );
      }
  
      /**
       * A utility method to build a {@link ComponentDescriptor}
       * object from specified configuraiton data and classname.
       *
       * @param config the Component Configuration
       * @return the created ComponentDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ComponentDescriptor buildComponentDescriptor( final String classname,
                                                            final Configuration config )
          throws ConfigurationException
      {
          final String name = config.getChild( "name" ).getValue( null );
          final String version = config.getChild( "version" ).getValue();
          final String schemaType = config.getChild( "schema-type" ).getValue();
  
          final ArrayList tagSet = new ArrayList();
          final Tag tag = createSimpleTag( "avalon", "version", version );
          tagSet.add( tag );
          if( null != schemaType )
          {
              final Tag schemaTag =
                  createSimpleTag( "phoenix", "schema-type", schemaType );
              tagSet.add( schemaTag );
          }
  
          final Tag[] tags = (Tag[])tagSet.toArray( new Tag[ tagSet.size() ] );
          return new ComponentDescriptor( name, classname, tags );
      }
  
      /**
       * Helper method to create simple tags with one parameter.
       *
       * @param name the name of the parameter
       * @param value the value of the parameter
       * @param tagName the name of the tag
       * @return
       */
      private Tag createSimpleTag( final String tagName,
                                   final String name,
                                   final String value )
      {
          final Properties parameters = new Properties();
          parameters.setProperty( name, value );
          return new Tag( tagName, parameters );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/blockinfo.dtd
  
  Index: blockinfo.dtd
  ===================================================================
  <!--
  
     This is the DTD defining the Phoenix BlockInfo 1.0
     descriptor (XML) file format/syntax.
  
     Author: Peter Donald <peter at apache.org>
  
     A BlockInfo is an XML file used to describe Blocks and located side-by-side with
     the Block .class file. It describes the services the Block requires to operate,
     the services the Block is capable of offerring other Blocks and other support
     meta data.
  
     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.
  
    -->
  
  <!--
  The blockinfo is the document root, it defines:
  
  block	     the specifc details about this block
  services     the services offered by this block
  dependencies the services that this block require to operate
  -->
  <!ELEMENT blockinfo (block, services?, management-access-points?, dependencies?)>
  <!ATTLIST blockinfo id ID #IMPLIED
            xmlns CDATA #FIXED "http://jakarta.apache.org/avalon/dtds/phoenix/blockinfo_1_0.dtd" >
  
  <!--
  The block element describes the block, it defines:
  
  name	        the human readable name of block type. Must be a string
               containing alphanumeric characters, '.', '_' and starting
               with a letter.
  version	     the version of the block in (in the format #.#.#, #.# or # where
               # is a integer
  schema-type  string representing the type of schema information available
               to validate block configuration
  -->
  <!ELEMENT block          (name?,version)>
    <!ELEMENT name         (#PCDATA) >
    <!ELEMENT version      (#PCDATA) >
    <!ELEMENT schema-type  (#PCDATA) >
  
  <!--
  The service element describes a service that the block
  can provide to other blocks, or this block depends upon.
  It defines:
  
  name         the name of the service. This must be equal to the class name of the
               interface that defines the service.
  version	     the version of the block in (in the format #.#.#, #.# or # where
               # is a integer
  -->
  <!ELEMENT service   EMPTY >
    <!ATTLIST service
         name CDATA #REQUIRED
         version CDATA #IMPLIED
    >
  
  <!--
  The service dependency describes a service that the block
  requires. It defines:
  
  role         the role of the service. This is the value that is used to lookup the
               service in the ComponentManager. If not provided it defaults to the
               value specified in the name attribute of service element
  service	     the service that is required
  -->
  <!ELEMENT dependency  (role?,service) >
    <!ELEMENT role        (#PCDATA) >
  
  <!--
  The services element contains a list of services that this Block supports.
  It contains service elements.
  -->
  <!ELEMENT services    (service*)>
  
  <!--
  The services element contains a list of services that this
  Block exports to the Management system. It contains service
  elements.
  -->
  <!ELEMENT management-access-points    (service*)>
  
  <!--
  The dependencies element contains a list of services that this Block requires.
  It contains dependency elements.
  -->
  <!ELEMENT dependencies    (dependency*)>
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>