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/info/src/java/org/apache/avalon/framework/tools/infobuilder InfoBuilder.java ComponentInfoBuilder.java
Date Fri, 04 Oct 2002 00:42:48 GMT
donaldp     2002/10/03 17:42:48

  Modified:    info/src/java/org/apache/avalon/framework/tools/ant
                        ComponentVerifierTask.java
  Added:       info/src/java/org/apache/avalon/framework/tools/infobuilder
                        InfoBuilder.java
  Removed:     info/src/java/org/apache/avalon/framework/tools/infobuilder
                        ComponentInfoBuilder.java
  Log:
  Rename ComponentInfoBuilder to InfoBuilder and add support for building ServiceInfos.
  
  Revision  Changes    Path
  1.3       +4 -4      jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/ant/ComponentVerifierTask.java
  
  Index: ComponentVerifierTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/ant/ComponentVerifierTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ComponentVerifierTask.java	14 Sep 2002 06:30:49 -0000	1.2
  +++ ComponentVerifierTask.java	4 Oct 2002 00:42:48 -0000	1.3
  @@ -9,7 +9,7 @@
   
   import org.apache.avalon.framework.info.ComponentInfo;
   import org.apache.avalon.framework.logger.ConsoleLogger;
  -import org.apache.avalon.framework.tools.infobuilder.ComponentInfoBuilder;
  +import org.apache.avalon.framework.tools.infobuilder.InfoBuilder;
   import org.apache.avalon.framework.tools.verifier.InfoVerifier;
   import org.apache.tools.ant.AntClassLoader;
   import org.apache.tools.ant.BuildException;
  @@ -60,7 +60,7 @@
   
           final AntClassLoader classLoader = new AntClassLoader( getProject(), m_classpath
);
   
  -        final ComponentInfoBuilder builder = new ComponentInfoBuilder();
  +        final InfoBuilder builder = new InfoBuilder();
           builder.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_INFO ) );
   
           final InfoVerifier verifier = new InfoVerifier();
  @@ -71,7 +71,7 @@
               final Class implementation =
                   classLoader.loadClass( m_classname );
               final ComponentInfo componentInfo =
  -                builder.build( implementation );
  +                builder.buildComponentInfo( implementation );
               verifier.verifyType( "test",
                                    m_classname,
                                    componentInfo,
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/infobuilder/InfoBuilder.java
  
  Index: InfoBuilder.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 org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.ServiceInfo;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  
  /**
   * A InfoBuilder 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 at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/04 00:42:48 $
   */
  public final class InfoBuilder
      extends AbstractLogEnabled
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( InfoBuilder.class );
  
      private final InfoCreator m_xmlInfoCreator = createXMLInfoCreator();
      private final InfoCreator m_serialInfoCreator = new SerializedInfoCreator();
      private final InfoCreator m_legacyInfoCreator = createLegacyInfoCreator();
  
      /**
       * 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.
       *
       * @param clazz The class of Component
       * @return the created ComponentInfo
       * @throws ConfigurationException if an error occurs
       */
      public ComponentInfo buildComponentInfo( final Class clazz )
          throws Exception
      {
          return buildComponentInfo( clazz.getName(), clazz.getClassLoader() );
      }
  
      /**
       * Create a {@link ComponentInfo} object for specified
       * classname, in specified ClassLoader.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created ComponentInfo
       * @throws ConfigurationException if an error occurs
       */
      public ComponentInfo buildComponentInfo( final String classname,
                                               final ClassLoader classLoader )
          throws Exception
      {
          ComponentInfo info = buildComponentFromSer( classname, classLoader );
          if( null != info )
          {
              return info;
          }
  
          info = buildComponentFromLegacy( classname, classLoader );
          if( null != info )
          {
              return info;
          }
          else
          {
              return buildComponentFromXML( classname, classLoader );
          }
      }
  
      /**
       * Create a {@link ServiceInfo} object for specified Class.
       *
       * @param clazz The class of ServiceInfo
       * @return the created ServiceInfo
       * @throws ConfigurationException if an error occurs
       */
      public ServiceInfo buildServiceInfo( final Class clazz )
          throws Exception
      {
          return buildServiceInfo( clazz.getName(), clazz.getClassLoader() );
      }
  
      /**
       * Create a {@link ServiceInfo} object for specified
       * classname, in specified ClassLoader.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created ServiceInfo
       * @throws ConfigurationException if an error occurs
       */
      public ServiceInfo buildServiceInfo( final String classname,
                                           final ClassLoader classLoader )
          throws Exception
      {
          ServiceInfo info = buildServiceFromSer( classname, classLoader );
          if( null != info )
          {
              return info;
          }
  
          info = buildServiceFromLegacy( classname, classLoader );
          if( null != info )
          {
              return info;
          }
          else
          {
              return buildServiceFromXML( classname, classLoader );
          }
      }
  
      /**
       * Build {@link ComponentInfo} from the XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created {@link ComponentInfo}
       * @throws Exception if an error occurs
       */
      private ComponentInfo buildComponentFromSer( final String classname,
                                                   final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, "-info.ser" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              return null;
          }
  
          return m_serialInfoCreator.createComponentInfo( classname, inputStream );
      }
  
      /**
       * Build {@link ComponentInfo} from the legacy XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created {@link ComponentInfo}
       * @throws Exception if an error occurs
       */
      private ComponentInfo buildComponentFromLegacy( final String classname,
                                                      final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, ".xinfo" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              return null;
          }
  
          if( null != m_legacyInfoCreator )
          {
              return m_legacyInfoCreator.createComponentInfo( classname, inputStream );
          }
          else
          {
              return null;
          }
      }
  
      /**
       * Build ComponentInfo from the 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 buildComponentFromXML( final String classname,
                                                   final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, "-info.xml" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              final String message =
                  REZ.getString( "builder.missing-info.error",
                                 classname );
              throw new Exception( message );
          }
  
          final InfoCreator xmlInfoCreator = getXMLInfoCreator( classname );
          return xmlInfoCreator.createComponentInfo( classname, inputStream );
      }
  
      /**
       * Build {@link ServiceInfo} from the XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created {@link ServiceInfo}
       * @throws Exception if an error occurs
       */
      private ServiceInfo buildServiceFromSer( final String classname,
                                               final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, "-info.ser" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              return null;
          }
  
          return m_serialInfoCreator.createServiceInfo( classname, inputStream );
      }
  
      /**
       * Build {@link ServiceInfo} from the legacy XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created {@link ServiceInfo}
       * @throws Exception if an error occurs
       */
      private ServiceInfo buildServiceFromLegacy( final String classname,
                                                  final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, ".xinfo" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              return null;
          }
  
          if( null != m_legacyInfoCreator )
          {
              return m_legacyInfoCreator.createServiceInfo( classname, inputStream );
          }
          else
          {
              return null;
          }
      }
  
      /**
       * Build ServiceInfo from the XML descriptor format.
       *
       * @param classname The classname of Component
       * @param classLoader the ClassLoader to load info from
       * @return the created ServiceInfo
       * @throws Exception if an error occurs
       */
      private ServiceInfo buildServiceFromXML( final String classname,
                                               final ClassLoader classLoader )
          throws Exception
      {
          final String xinfo = deriveResourceName( classname, "-info.xml" );
          final InputStream inputStream = classLoader.getResourceAsStream( xinfo );
          if( null == inputStream )
          {
              final String message =
                  REZ.getString( "builder.missing-info.error",
                                 classname );
              throw new Exception( message );
          }
  
          final InfoCreator xmlInfoCreator = getXMLInfoCreator( classname );
          return xmlInfoCreator.createServiceInfo( classname, inputStream );
      }
  
      /**
       * Utility to get xml info builder, else throw
       * an exception if missing descriptor.
       *
       * @return the InfoCreator
       */
      private InfoCreator getXMLInfoCreator( final String classname )
          throws Exception
      {
          if( null != m_xmlInfoCreator )
          {
              return m_xmlInfoCreator;
          }
          else
          {
              final String message =
                  REZ.getString( "builder.missing-xml-creator.error",
                                 classname );
              throw new Exception( message );
          }
      }
  
      /**
       * Utility to get {@link XMLInfoCreator} if XML files are on
       * ClassPath.
       *
       * @return the XML {@link InfoCreator}
       */
      private static InfoCreator createXMLInfoCreator()
      {
          try
          {
              return new XMLInfoCreator();
          }
          catch( final Exception e )
          {
              //Ignore it if ClassNot found due to no
              //XML Classes on classpath
              return null;
          }
      }
  
      /**
       * Utility to get {@link LegacyBlockInfoCreator} if XML files are on
       * ClassPath.
       *
       * @return the Legacy {@link InfoCreator}
       */
      private static InfoCreator createLegacyInfoCreator()
      {
          try
          {
              return new LegacyBlockInfoCreator();
          }
          catch( final Exception e )
          {
              //Ignore it if ClassNot found due to no
              //XML Classes on classpath
              return null;
          }
      }
  
      /**
       * Derive the resourcename for specified class using specified postfix.
       *
       * @param classname the name of class
       * @param postfix the postfix to add to end of resource
       * @return the name of resource
       */
      private String deriveResourceName( final String classname,
                                         final String postfix )
      {
          return classname.replace( '.', '/' ) + postfix;
      }
  }
  
  
  

--
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