mcconnell    2003/01/15 01:21:04

  Added:       meta/src/java/org/apache/avalon/meta/info/builder
                        XMLLegacyCreator.java
  Log:
  Support for the create of Type meta-info descriptor from the Phoenix 
<blockinfo> descriptor.
  
  Revision  Changes    Path
  1.1                  
avalon-sandbox/meta/src/java/org/apache/avalon/meta/info/builder/XMLLegacyCreator.java
  
  Index: XMLLegacyCreator.java
  ===================================================================
  /* ==================================================================== 
   * The Apache Software License, Version 1.1 
   * 
   * Copyright (c) 2002 The Apache Software Foundation. All rights 
   * reserved. 
   * 
   * Redistribution and use in source and binary forms, with or without 
   * modification, are permitted provided that the following conditions 
   * are met: 
   * 
   * 1. Redistributions of source code must retain the above copyright 
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software 
   *    itself, if and wherever such third-party acknowledgments  
   *    normally appear.
   *
   * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation" 
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see 
   * <http://www.apache.org/>.
   */ 
  
  package org.apache.avalon.meta.info.builder;
  
  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.meta.ConfigurationBuilder;
  import org.apache.avalon.meta.info.InfoDescriptor;
  import org.apache.avalon.meta.info.ContextDescriptor;
  import org.apache.avalon.meta.info.EntryDescriptor;
  import org.apache.avalon.meta.info.DependencyDescriptor;
  import org.apache.avalon.meta.info.ExtensionDescriptor;
  import org.apache.avalon.meta.info.LoggerDescriptor;
  import org.apache.avalon.meta.info.ReferenceDescriptor;
  import org.apache.avalon.meta.info.ServiceDescriptor;
  import org.apache.avalon.meta.info.StageDescriptor;
  import org.apache.avalon.meta.info.Type;
  import org.apache.excalibur.configuration.ConfigurationUtil;
  import org.xml.sax.InputSource;
  
  /**
   * Handles internalization of a legacy Poenix XML based description of a 
[EMAIL PROTECTED] Type}
   * from a Configuration object.
   *
   * @author <a href="mailto:avalon-dev@jakarta.apache.org";>Avalon Development 
Team</a>
   * @version $Revision: 1.1 $ $Date: 2003/01/15 09:21:04 $
   */
  public class XMLLegacyCreator
      extends XMLTypeCreator
      implements TypeCreator
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( XMLLegacyCreator.class );
  
      /**
       * Create a [EMAIL PROTECTED] Type} object for specified
       * classname, loaded from specified [EMAIL PROTECTED] InputStream}.
       *
       * @param implementationKey The classname of Component
       * @param inputStream the InputStream to load Type from
       * @return the created Type
       * @throws Exception if an error occurs
       */
      public Type createType( String implementationKey,
                              InputStream inputStream )
          throws Exception
      {
          if( inputStream == null )
          {
              throw new NullPointerException( "input" );
          }
          final InputSource input = new InputSource( inputStream );
          final String classname = implementationKey;
          final Configuration xinfo = ConfigurationBuilder.build( input );
          return build( classname, xinfo );
      }
  
      /**
       * Create a [EMAIL PROTECTED] Type} object for specified
       * classname and configuration.
       *
       * @param classname The classname of the component
       * @param config the meta info configuration fragment
       * @return the created Type
       * @throws Exception if an error occurs
       */
      public Type createType( String classname, Configuration config )
          throws Exception
      {
          return build( classname, config );
      }
  
      /**
       * Create a [EMAIL PROTECTED] Type} object for specified classname from
       * specified configuration data.
       *
       * @param classname The classname of Component
       * @param info the Type configuration
       * @return the created Type
       * @throws ConfigurationException if an error occurs
       */
      private Type build( final String classname, final Configuration info )
          throws Exception
      {
          final String topLevelName = info.getName();
          if( !topLevelName.equals( "type" ) )
          {
              if( !topLevelName.equals( "blockinfo" ) )
              {
                  final String message =
                      REZ.getString( "builder.bad-toplevel-block-element.error",
                                     classname,
                                     topLevelName );
                  throw new ConfigurationException( message );
              }
          }
  
          Configuration configuration = null;
  
          configuration = info.getChild( "block" );
          final InfoDescriptor descriptor =
              buildInfoDescriptor( classname, configuration );
  
          configuration = info.getChild( "loggers" );
          final LoggerDescriptor[] loggers = buildLoggers( configuration );
  
          final ContextDescriptor context = buildPhoenixContext( );
  
          configuration = info.getChild( "services" );
          final ServiceDescriptor[] services = buildBlockServices( 
configuration );
  
          configuration = info.getChild( "dependencies" );
          final DependencyDescriptor[] dependencies = buildDependencies( 
classname, configuration );
  
          configuration = info.getChild( "stages" );
          final StageDescriptor[] phases = buildPhases( configuration );
  
          configuration = info.getChild( "extensions" );
          final ExtensionDescriptor[] extensions = buildExtensions( 
configuration );
  
          return new Type(
              descriptor, loggers, context, services, dependencies, phases, 
extensions );
      }
  
      private ContextDescriptor buildPhoenixContext()
      {
          ReferenceDescriptor reference = 
             createReference( "org.apache.avalon.framework.context.Context" );
          EntryDescriptor name = new EntryDescriptor( "urn:avalon:name", 
"block.name", "java.lang.String", false );
          EntryDescriptor home = new EntryDescriptor( "urn:avalon:home", 
"app.home", "java.io.File", false );
          return new ContextDescriptor( reference, new EntryDescriptor[]{ name, 
home }, null );
      }
  
      /**
       * A utility method to build an array of [EMAIL PROTECTED] 
ServiceDescriptor}
       * objects from specified configuraiton.
       *
       * @param servicesSet the services configuration
       * @return the created ServiceDescriptor
       * @throws ConfigurationException if an error occurs
       */
      protected ServiceDescriptor[] buildBlockServices( 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 = buildBlockService( elements[ i 
] );
              services.add( service );
          }
  
          return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 
0 ] );
      }
  
      /**
       * 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
       */
      protected ServiceDescriptor buildBlockService( final Configuration 
service )
          throws ConfigurationException
      {
          final ReferenceDescriptor designator = buildReferenceDescriptor( 
service );
          final Properties attributes =
              buildAttributes( service.getChild( "attributes" ) );
          return new ServiceDescriptor( designator, attributes );
      }
  
      /**
       * A utility method to build a [EMAIL PROTECTED] ReferenceDescriptor}
       * object from specified configuraiton data.
       *
       * @param service the service Configuration
       * @return the created ReferenceDescriptor
       * @throws ConfigurationException if an error occurs
       */
      protected ReferenceDescriptor buildReferenceDescriptor( final 
Configuration service )
          throws ConfigurationException
      {
          try
          {
              final String type = service.getAttribute( "name" );
              final String versionString = service.getAttribute( "version", 
"1.0" );
              final Version version = buildVersion( versionString );
              return new ReferenceDescriptor( type, version );
          }
          catch( Throwable e )
          {
              final String error =
                "Error occured while attempting to build reference descriptor 
from configuration: "
                + ConfigurationUtil.list( service );
              throw new ConfigurationException( error, e );
          }
      }
  
      /**
       * A utility method to build a [EMAIL PROTECTED] 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
       */
      protected DependencyDescriptor buildDependency( final String classname,
                                                      final Configuration 
dependency )
          throws ConfigurationException
      {
  
          Configuration serviceRef = dependency.getChild( "service" );
          final ReferenceDescriptor service =
              buildReferenceDescriptor( serviceRef );
          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();
          }
  
          return new DependencyDescriptor( role, service, optional, attributes 
);
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to