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/generator InfoBuilder.java
Date Tue, 12 Nov 2002 00:05:17 GMT
donaldp     2002/11/11 16:05:17

  Added:       info/src/java/org/apache/avalon/framework/tools/generator
                        InfoBuilder.java
  Log:
  Add a utility class that is used to build a ComponentInfo object
  from QDoxs JavaClass object model. This essentially involves interpreting
  all of the javadoc tags present in JavaClass object model.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/info/src/java/org/apache/avalon/framework/tools/generator/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.generator;
  
  import com.thoughtworks.qdox.model.DocletTag;
  import com.thoughtworks.qdox.model.JavaClass;
  import com.thoughtworks.qdox.model.JavaMethod;
  import com.thoughtworks.qdox.model.Type;
  import java.util.ArrayList;
  import org.apache.avalon.framework.info.Attribute;
  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;
  
  /**
   * This is a utility class that is used to build a ComponentInfo object
   * from QDoxs JavaClass object model. This essentially involves interpreting
   * all of the javadoc tags present in JavaClass object model.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/12 00:05:17 $
   */
  class InfoBuilder
  {
      private static final String CONTEXT_CLASS = "org.apache.avalon.framework.context.Context";
      private static final String LOGGER_CLASS = "org.apache.avalon.framework.logger.Logger";
      private static final String COMPONENT_MANAGER_CLASS =
          "org.apache.avalon.framework.component.ComponentManager";
      private static final String SERVICE_MANAGER_CLASS =
          "org.apache.avalon.framework.component.ServiceManager";
  
      private static final Attribute[] EMPTY_ATTRIBUTES = new Attribute[ 0 ];
  
      /**
       * Build a ComponentInfo object for specified class.
       *
       * @param javaClass the class
       * @return the ComponentInfo object
       */
      static ComponentInfo buildComponentInfo( final JavaClass javaClass )
      {
          final ComponentDescriptor component = buildComponentDescriptor( javaClass );
          final ContextDescriptor context = buildContextDescriptor( javaClass );
          final LoggerDescriptor[] loggers = buildLoggers( javaClass );
          final ServiceDescriptor[] services = buildServices( javaClass );
          final DependencyDescriptor[] dependencies = buildDependencies( javaClass );
  
          return new ComponentInfo( component, loggers, context, services, dependencies );
      }
  
      /**
       * Build the set of dependency descriptors for specified class.
       *
       * @param javaClass the class
       * @return the set of dependency descriptors
       */
      private static DependencyDescriptor[] buildDependencies( final JavaClass javaClass )
      {
          JavaMethod method =
              getLifecycleMethod( javaClass, "compose", COMPONENT_MANAGER_CLASS );
  
          //If no compose then try for a service method ...
          if( null == method )
          {
              method =
                  getLifecycleMethod( javaClass, "service", SERVICE_MANAGER_CLASS );
          }
  
          if( null == method )
          {
              return new DependencyDescriptor[ 0 ];
          }
          else
          {
              final ArrayList deps = new ArrayList();
              final DocletTag[] tags = method.getTagsByName( "avalon.dependency" );
              for( int i = 0; i < tags.length; i++ )
              {
                  final DocletTag tag = tags[ i ];
                  final String unresolvedType = getNamedParameter( tag, "interface" );
                  final String type = resolveType( javaClass, unresolvedType );
                  final String key = getNamedParameter( tag, "key", type );
                  final String optional = getNamedParameter( tag, "optional", "false" );
                  final boolean isOptional = "true".equals( optional );
                  final DependencyDescriptor dependency =
                      new DependencyDescriptor( key, type, isOptional, EMPTY_ATTRIBUTES );
                  deps.add( dependency );
              }
              return (DependencyDescriptor[])deps.toArray( new DependencyDescriptor[ deps.size()
] );
          }
      }
  
      /**
       * Build the set of service descriptors for specified class.
       *
       * @param javaClass the class
       * @return the set of service descriptors
       */
      private static ServiceDescriptor[] buildServices( final JavaClass javaClass )
      {
          final ArrayList services = new ArrayList();
          final DocletTag[] tags = javaClass.getTagsByName( "avalon.service" );
          for( int i = 0; i < tags.length; i++ )
          {
              final DocletTag tag = tags[ i ];
              final String unresolvedType = getNamedParameter( tag, "interface" );
              final String type = resolveType( javaClass, unresolvedType );
              final ServiceDescriptor service = new ServiceDescriptor( type, EMPTY_ATTRIBUTES
);
              services.add( service );
          }
          return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ services.size()
] );
      }
  
      /**
       * Build the set of logger descriptors for specified class.
       *
       * @param javaClass the class
       * @return the set of logger descriptors
       */
      private static LoggerDescriptor[] buildLoggers( final JavaClass javaClass )
      {
          final JavaMethod method =
              getLifecycleMethod( javaClass, "enableLogging", LOGGER_CLASS );
          if( null == method )
          {
              return new LoggerDescriptor[ 0 ];
          }
          else
          {
              final ArrayList loggers = new ArrayList();
              final DocletTag[] tags = method.getTagsByName( "avalon.loggers" );
              for( int i = 0; i < tags.length; i++ )
              {
                  final String name =
                      getNamedParameter( tags[ i ], "name", "" );
                  final LoggerDescriptor logger =
                      new LoggerDescriptor( name, EMPTY_ATTRIBUTES );
                  loggers.add( logger );
              }
              return (LoggerDescriptor[])loggers.toArray( new LoggerDescriptor[ loggers.size()
] );
          }
      }
  
      /**
       * Build the context descriptor for specified class.
       *
       * @param javaClass the class
       * @return the context descriptor
       */
      private static ContextDescriptor buildContextDescriptor( final JavaClass javaClass )
      {
          final JavaMethod method =
              getLifecycleMethod( javaClass, "contextualize", CONTEXT_CLASS );
          if( null == method )
          {
              return new ContextDescriptor( CONTEXT_CLASS,
                                            new EntryDescriptor[ 0 ],
                                            EMPTY_ATTRIBUTES );
          }
          else
          {
              String type = CONTEXT_CLASS;
              final DocletTag tag = method.getTagByName( "avalon.context" );
              if( null != tag && null != tag.getNamedParameter( "type" ) )
              {
                  type = resolveType( javaClass, tag.getNamedParameter( "type" ) );
              }
  
              final ArrayList entrySet = new ArrayList();
              final DocletTag[] tags = method.getTagsByName( "avalon.entry" );
              for( int i = 0; i < tags.length; i++ )
              {
                  final String key = getNamedParameter( tags[ i ], "key" );
                  final String entryType = getNamedParameter( tags[ i ], "type" );
                  final String optional = getNamedParameter( tags[ i ], "optional", "false"
);
                  final boolean isOptional = "true".equals( optional );
                  final EntryDescriptor entry =
                      new EntryDescriptor( key, entryType, isOptional, EMPTY_ATTRIBUTES );
                  entrySet.add( entry );
              }
              final EntryDescriptor[] entrys =
                  (EntryDescriptor[])entrySet.toArray( new EntryDescriptor[ entrySet.size()
] );
  
              return new ContextDescriptor( type, entrys, EMPTY_ATTRIBUTES );
          }
      }
  
      /**
       * Build the component descriptor for specified class.
       *
       * @param javaClass the class
       * @return the component descriptor
       */
      private static ComponentDescriptor buildComponentDescriptor( final JavaClass javaClass
)
      {
          final String type = javaClass.getFullyQualifiedName();
          return new ComponentDescriptor( type, EMPTY_ATTRIBUTES );
      }
  
      /**
       * Resolve the specified type.
       * Resolving essentially means finding the fully qualified name of
       * a class from just it's short name.
       *
       * @param javaClass the java class relative to which the type must be resolved
       * @param type the unresolved type
       * @return the resolved type
       */
      private static String resolveType( final JavaClass javaClass, final String type )
      {
          return javaClass.getParentSource().resolveType( type );
      }
  
      /**
       * Retrieve a method with specified name and one parameter of specified
       * type. The method must also return void.
       *
       * @param javaClass the java class to retrieve method for
       * @param methodName the name of the method
       * @param parameterType the class name of parameter
       * @return the method if such a method exists
       */
      private static JavaMethod getLifecycleMethod( final JavaClass javaClass,
                                                    final String methodName,
                                                    final String parameterType )
      {
          final JavaMethod[] methods = javaClass.getMethods();
          for( int i = 0; i < methods.length; i++ )
          {
              final JavaMethod method = methods[ i ];
              if( methodName.equals( method.getName() ) &&
                  method.getReturns().equals( new Type( "void", 0 ) ) &&
                  method.getParameters().length == 1 &&
                  method.getParameters()[ 0 ].getType().getValue().equals( parameterType )
)
              {
                  return method;
              }
          }
          return null;
      }
  
      /**
       * Retrieve specified named parameter from tag. If the parameter
       * does not exist then return specified default value.
       *
       * @param tag the tag
       * @param name the name of parameter
       * @return the value of named parameter
       */
      private static String getNamedParameter( final DocletTag tag,
                                               final String name,
                                               final String defaultValue )
      {
          final String value = tag.getNamedParameter( name );
          if( null == value )
          {
              return defaultValue;
          }
          return value;
      }
  
      /**
       * Retrieve specified named parameter from tag. If the parameter
       * does not exist then throw an exception.
       *
       * @param tag the tag
       * @param name the name of parameter
       * @return the value of named parameter
       */
      private static String getNamedParameter( final DocletTag tag, final String name )
      {
          final String value = tag.getNamedParameter( name );
          if( null == value )
          {
              final String message =
                  "Malformed tag '" + tag.getName() + "'. " +
                  "Missing required parameter '" + name + "'";
              throw new IllegalArgumentException( message );
          }
          return value;
      }
  }
  
  
  

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