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/06/23 04:45:37 UTC

cvs commit: jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder ComponentInfoBuilder.java Resources.proeprties package.html

donaldp     2002/06/22 19:45:37

  Added:       containerkit/src/java/org/apache/excalibur/containerkit/infobuilder
                        ComponentInfoBuilder.java Resources.proeprties
                        package.html
  Log:
  Add in start of info building from XML configuration.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/ComponentInfoBuilder.java
  
  Index: ComponentInfoBuilder.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.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;
  
  /**
   * A ComponentInfoBuilder is responsible for building <code>ComponentInfo</code>
   * objects from Configuration objects. The format for Configuration object
   * is specified in the ComponentInfo specification.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/23 02:45:37 $
   */
  public final class ComponentInfoBuilder
      extends AbstractLogEnabled
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( ComponentInfoBuilder.class );
  
      /**
       * Create a <code>ComponentInfo</code> object for specified classname from
       * specified configuration data.
       *
       * @param classname The classname of Block
       * @param info the ComponentInfo configuration
       * @return the created ComponentInfo
       * @throws ConfigurationException if an error occurs
       */
      public ComponentInfo build( final String classname, final Configuration info )
          throws Exception
      {
          if( getLogger().isDebugEnabled() )
          {
              final String message = REZ.getString( "creating-ComponentInfo", classname );
              getLogger().debug( 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( "ComponentInfo-created",
                                 classname,
                                 new Integer( services.length ),
                                 new Integer( dependencies.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 Block (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" ) );
          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( "redundent-role",
                                     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 ServiceDesignator designator = buildServiceDesignator( service );
          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 <code>ComponentDescriptor</code>
       * object from specified configuraiton data and classname.
       *
       * <p>Note that if a &lt;block/&gt; section is not specified then a warning
       * is generated as previous versions of Phoenix did not require such sections.
       * In the future this section will be required.</p>
       *
       * @param classname The classname of Block (used to create descriptor)
       * @param component the Block Configuration
       * @return the created ComponentDescriptor
       * @throws ConfigurationException if an error occurs
       */
      private ComponentDescriptor buildComponentDescriptor( final String classname,
                                                            final Configuration component )
          throws ConfigurationException
      {
          if( 0 == component.getChildren().length )
          {
              final String message =
                  REZ.getString( "missing-block", classname );
              getLogger().warn( message );
              System.err.println( message );
              return null;
          }
  
          final String name = component.getChild( "name" ).getValue( null );
          final Version version = buildVersion( component.getChild( "version" ).getValue() );
          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 );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/Resources.proeprties
  
  Index: Resources.proeprties
  ===================================================================
  blockinfo-created=Constructed BlockInfo object for class {0}. BlockInfo contains {1} services and {2} dependencies.
  missing-block=Warning: Unspecified <block/> section in BlockInfo for class {0}.
  redundent-role=Warning: BlockInfo for class {0} redundently specifies role name "{1}" in dependency when it is identical to the name of service. It is recomended that the <role/> section be elided.
  creating-blockinfo=Creating a BlockInfo for class "{0}".
  deprecated-management-declaration=The BlockInfo for "{0}" uses the deprecated mechanism to declare management services. It is recomended you replace <management> with <management-access-points>.
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/infobuilder/package.html
  
  Index: package.html
  ===================================================================
  <html><body>
  Component information builder.
  </body></html>
  
  
  

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