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/containerkit/src/java/org/apache/excalibur/containerkit/verifier MetaDataVerifier.java AssemblyVerifier.java Resources.properties
Date Tue, 18 Jun 2002 07:38:48 GMT
donaldp     2002/06/18 00:38:48

  Modified:    containerkit/src/java/org/apache/excalibur/containerkit/verifier
                        AssemblyVerifier.java Resources.properties
  Added:       containerkit/src/java/org/apache/excalibur/containerkit/verifier
                        MetaDataVerifier.java
  Log:
  Extract out MetaDataVerifier that verifies a particular ComponentMetaData matches its implementation
class
  
  Revision  Changes    Path
  1.22      +27 -157   jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/verifier/AssemblyVerifier.java
  
  Index: AssemblyVerifier.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/verifier/AssemblyVerifier.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- AssemblyVerifier.java	18 Jun 2002 05:42:54 -0000	1.21
  +++ AssemblyVerifier.java	18 Jun 2002 07:38:47 -0000	1.22
  @@ -11,18 +11,14 @@
   import java.util.Stack;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
  -import org.apache.avalon.framework.component.Composable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.Logger;
  -import org.apache.avalon.framework.service.Serviceable;
  -import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
   import org.apache.excalibur.containerkit.metadata.DependencyMetaData;
   import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
   import org.apache.excalibur.containerkit.metainfo.DependencyDescriptor;
   import org.apache.excalibur.containerkit.metainfo.ServiceDescriptor;
   import org.apache.excalibur.containerkit.metainfo.ServiceDesignator;
  -import org.apache.excalibur.containerkit.metainfo.ContextDescriptor;
   
   /**
    * This Class verifies that Sars are valid. It performs a number
  @@ -60,20 +56,20 @@
       /**
        * The verifier for components in assembly.
        */
  -    private final ComponentVerifier m_verifier;
  +    private final MetaDataVerifier m_verifier;
   
       /**
  -     * Create an AssemblyVerifier using base Componern ComponentVerifier.
  +     * Create an AssemblyVerifier using base Componernt MetaDataVerifier.
        */
       public AssemblyVerifier()
       {
  -        this( new ComponentVerifier() );
  +        this( new MetaDataVerifier() );
       }
   
       /**
  -     * Create an AssemblyVerifier using specified Component ComponentVerifier.
  +     * Create an AssemblyVerifier using specified MetaDataVerifier.
        */
  -    public AssemblyVerifier( final ComponentVerifier verifier )
  +    public AssemblyVerifier( final MetaDataVerifier verifier )
       {
           if( null == verifier )
           {
  @@ -99,6 +95,7 @@
        *                    to verify that specified Class objects exist and
        *                    implement the correct interfaces.
        * @throws VerifyException if an error occurs
  +     * @todo Rework method so that it does not need to accept a ClassLoader object
        */
       public void verifyAssembly( final ComponentMetaData[] components,
                                   final ClassLoader classLoader )
  @@ -128,7 +125,15 @@
   
           message = REZ.getString( "assembly.component-type.notice" );
           getLogger().info( message );
  -        verifyTypes( components, classLoader );
  +
  +        //Need to move the next chunk of code to a different class
  +        //MetaDataVerifier??
  +        for( int i = 0; i < components.length; i++ )
  +        {
  +            final ComponentMetaData component = components[ i ];
  +            final Class clazz = getClass( classLoader, component );
  +            m_verifier.verifyType( component, clazz );
  +        }
       }
   
       /**
  @@ -347,128 +352,32 @@
       }
   
       /**
  -     * Verfiy that all Components specify classes that implement the
  -     * advertised interfaces.
  -     *
  -     * @param components the ComponentMetaData objects for the components
  -     * @throws VerifyException if an error occurs
  -     */
  -    protected void verifyTypes( final ComponentMetaData[] components,
  -                                final ClassLoader classLoader )
  -        throws VerifyException
  -    {
  -        for( int i = 0; i < components.length; i++ )
  -        {
  -            verifyType( components[ i ], classLoader );
  -        }
  -    }
  -
  -    /**
  -     * Verfiy that specified components designate classes that implement the
  -     * advertised interfaces.
  +     * Load class object for specified ComponentMetaData.
        *
  -     * @param component the ComponentMetaData object for the components
  -     * @throws VerifyException if an error occurs
  +     * @param classLoader the ClassLoader to use
  +     * @param component the meta data associate with component
  +     * @return the Class object
  +     * @throws VerifyException if unable to aquire class object
        */
  -    protected void verifyType( final ComponentMetaData component,
  -                               final ClassLoader classLoader )
  +    private Class getClass( final ClassLoader classLoader,
  +                            final ComponentMetaData component )
           throws VerifyException
       {
  -        final String name = component.getName();
  -        final String classname = component.getClassname();
           Class clazz = null;
           try
           {
  -            clazz = classLoader.loadClass( classname );
  +            clazz = classLoader.loadClass( component.getClassname() );
           }
           catch( final Exception e )
           {
               final String message =
                   REZ.getString( "assembly.bad-class.error",
  -                               name,
  -                               classname,
  +                               component.getName(),
  +                               component.getClassname(),
                                  e.toString() );
               throw new VerifyException( message );
           }
  -
  -        final Class[] interfaces =
  -            getServiceClasses( name,
  -                               component.getComponentInfo().getServices(),
  -                               classLoader );
  -
  -        m_verifier.verifyComponent( name, clazz, interfaces );
  -
  -        verifyDependencyPresence( component, clazz );
  -        verifyContextPresence( component, clazz );
  -    }
  -
  -    /**
  -     * Verify that the if  the component is not Contextualizable that it
  -     * does not declare Context Entrys.
  -     *
  -     * @param component the component metadata
  -     * @param clazz the class implementing component
  -     * @throws VerifyException if fails verification check
  -     */
  -    protected void verifyContextPresence( final ComponentMetaData component,
  -                                          final Class clazz )
  -        throws VerifyException
  -    {
  -        final ComponentInfo info = component.getComponentInfo();
  -        final ContextDescriptor context = info.getContextDescriptor();
  -        final int count = context.getEntrys().length;
  -
  -        if( !Contextualizable.class.isAssignableFrom( clazz ) )
  -        {
  -            if( 0 != count )
  -            {
  -                final String message =
  -                    REZ.getString( "assembly.declare-uneeded-entrys.error",
  -                                   component.getName(),
  -                                   component.getClassname() );
  -                throw new VerifyException( message );
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Verify that the component either;
  -     * <p>Is not Composable/Serviceable and does not declare dependencys</p>
  -     * <p>or</p>
  -     * <p>Is Composable/Serviceable and does declare dependencys</p>
  -     *
  -     * @param component the component metadata
  -     * @param clazz the class implementing component
  -     * @throws VerifyException if fails verification check
  -     */
  -    protected void verifyDependencyPresence( final ComponentMetaData component,
  -                                             final Class clazz )
  -        throws VerifyException
  -    {
  -        final int count = component.getDependencies().length;
  -        if( Composable.class.isAssignableFrom( clazz ) ||
  -            Serviceable.class.isAssignableFrom( clazz ) )
  -        {
  -            if( 0 == count )
  -            {
  -                final String message =
  -                    REZ.getString( "assembly.nodeclare-deps.error",
  -                                   component.getName(),
  -                                   component.getClassname() );
  -                throw new VerifyException( message );
  -            }
  -        }
  -        else
  -        {
  -            if( 0 != count )
  -            {
  -                final String message =
  -                    REZ.getString( "assembly.declare-uneeded-deps.error",
  -                                   component.getName(),
  -                                   component.getClassname() );
  -                throw new VerifyException( message );
  -            }
  -        }
  +        return clazz;
       }
   
       /**
  @@ -607,45 +516,6 @@
                   throw new VerifyException( message );
               }
           }
  -    }
  -
  -    /**
  -     * Retrieve an array of Classes for all the services that a Component
  -     * offers. This method also makes sure all services offered are
  -     * interfaces.
  -     *
  -     * @param name the name of component
  -     * @param services the services the component offers
  -     * @param classLoader the classLoader
  -     * @return an array of Classes for all the services
  -     * @throws VerifyException if an error occurs
  -     */
  -    protected Class[] getServiceClasses( final String name,
  -                                         final ServiceDescriptor[] services,
  -                                         final ClassLoader classLoader )
  -        throws VerifyException
  -    {
  -        final Class[] classes = new Class[ services.length ];
  -        for( int i = 0; i < services.length; i++ )
  -        {
  -            final ServiceDesignator service = services[ i ].getServiceDesignator();
  -            final String classname = service.getClassname();
  -            try
  -            {
  -                classes[ i ] = classLoader.loadClass( classname );
  -            }
  -            catch( final Throwable t )
  -            {
  -                final String message =
  -                    REZ.getString( "assembly.bad-service-class.error",
  -                                   name,
  -                                   classname,
  -                                   t.toString() );
  -                throw new VerifyException( message, t );
  -            }
  -        }
  -
  -        return classes;
       }
   
       /**
  
  
  
  1.9       +6 -4      jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/verifier/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/verifier/Resources.properties,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Resources.properties	18 Jun 2002 05:42:54 -0000	1.8
  +++ Resources.properties	18 Jun 2002 07:38:47 -0000	1.9
  @@ -27,7 +27,9 @@
   assembly.duplicate-name.error=The name "{0}" is used by multiple Components in assembly.
   assembly.unknown-dependency.error=Unknown dependency named "{0}" with role "{1}" declared
for Component {2}.
   assembly.unspecified-dependency.error=Dependency for role "{0}" not specified for the Component
named "{1}".
  -assembly.bad-service-class.error=Unable to load service class "{1}" for Component named
"{0}". (Reason: {2}).
  -assembly.nodeclare-deps.error=Component named "{0}" of type "{1}" is Composable or Serviceable
but does not declare any dependencies.
  -assembly.declare-uneeded-deps.error=Component named "{0}" of type "{1}" is not Composable
or Serviceable but declares dependencies.
  -assembly.declare-uneeded-entrys.error=Component named "{0}" of type "{1}" is not Contextualizable
but declares Context Entrys.
  \ No newline at end of file
  +
  +#MetaData Verifier
  +metadata.bad-service-class.error=Unable to load service class "{1}" for Component named
"{0}". (Reason: {2}).
  +metadata.nodeclare-deps.error=Component named "{0}" of type "{1}" is Composable or Serviceable
but does not declare any dependencies.
  +metadata.declare-uneeded-deps.error=Component named "{0}" of type "{1}" is not Composable
or Serviceable but declares dependencies.
  +metadata.declare-uneeded-entrys.error=Component named "{0}" of type "{1}" is not Contextualizable
but declares Context Entrys.
  \ No newline at end of file
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/verifier/MetaDataVerifier.java
  
  Index: MetaDataVerifier.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.verifier;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
  import org.apache.excalibur.containerkit.metainfo.ComponentInfo;
  import org.apache.excalibur.containerkit.metainfo.ContextDescriptor;
  import org.apache.excalibur.containerkit.metainfo.ServiceDescriptor;
  import org.apache.excalibur.containerkit.metainfo.ServiceDesignator;
  
  /**
   * This Class verifies that an implementation is valid wrt the
   * ComponentMetaData. It performs a number of checks to make sure
   * that the implementation class is consistent with MetaData.
   * Some of the checks it performs include;
   *
   * <ul>
   *   <li>Verify that the Class objects for Component implement the
   *       service interfaces.</li>
   *   <li>Verify that the Class is a valid Avalon Component as per the
   *       rules in {@link ComponentVerifier} object.</li>
   *   <li>Verify that the Class is Composable/Serviceable if and only if
   *       dependencies are declared.</li>
   *   <li>Verify that the Class is Contextualizable if and context
   *       entrys are declared.</li>
   * </ul>
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/18 07:38:47 $
   */
  public class MetaDataVerifier
      extends AbstractLogEnabled
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( MetaDataVerifier.class );
  
      /**
       * The verifier for components in assembly.
       */
      private final ComponentVerifier m_verifier;
  
      /**
       * Create an MetaDataVerifier using base Componern ComponentVerifier.
       */
      public MetaDataVerifier()
      {
          this( new ComponentVerifier() );
      }
  
      /**
       * Create an AssemblyVerifier using specified Component ComponentVerifier.
       */
      public MetaDataVerifier( final ComponentVerifier verifier )
      {
          if( null == verifier )
          {
              throw new NullPointerException( "verifier" );
          }
  
          m_verifier = verifier;
      }
  
      public void enableLogging( final Logger logger )
      {
          super.enableLogging( logger );
          setupLogger( m_verifier );
      }
  
      /**
       * Verfiy that specified components designate classes that implement the
       * advertised interfaces. And confrorm to expectations of MetaData.
       *
       * @param component the ComponentMetaData object for the components
       * @throws VerifyException if an error occurs
       */
      protected void verifyType( final ComponentMetaData component,
                                 final Class clazz )
          throws VerifyException
      {
          final String name = component.getName();
          final Class[] interfaces =
              getServiceClasses( name,
                                 component.getComponentInfo().getServices(),
                                 clazz.getClassLoader() );
  
          m_verifier.verifyComponent( name, clazz, interfaces );
  
          verifyDependencyPresence( component, clazz );
          verifyContextPresence( component, clazz );
      }
  
      /**
       * Verify that the if  the component is not Contextualizable that it
       * does not declare Context Entrys.
       *
       * @param component the component metadata
       * @param clazz the class implementing component
       * @throws VerifyException if fails verification check
       */
      protected void verifyContextPresence( final ComponentMetaData component,
                                            final Class clazz )
          throws VerifyException
      {
          final ComponentInfo info = component.getComponentInfo();
          final ContextDescriptor context = info.getContextDescriptor();
          final int count = context.getEntrys().length;
  
          if( !Contextualizable.class.isAssignableFrom( clazz ) )
          {
              if( 0 != count )
              {
                  final String message =
                      REZ.getString( "metadata.declare-uneeded-entrys.error",
                                     component.getName(),
                                     component.getClassname() );
                  throw new VerifyException( message );
              }
          }
      }
  
      /**
       * Verify that the component either;
       * <p>Is not Composable/Serviceable and does not declare dependencys</p>
       * <p>or</p>
       * <p>Is Composable/Serviceable and does declare dependencys</p>
       *
       * @param component the component metadata
       * @param clazz the class implementing component
       * @throws VerifyException if fails verification check
       */
      protected void verifyDependencyPresence( final ComponentMetaData component,
                                               final Class clazz )
          throws VerifyException
      {
          final int count = component.getDependencies().length;
          if( Composable.class.isAssignableFrom( clazz ) ||
              Serviceable.class.isAssignableFrom( clazz ) )
          {
              if( 0 == count )
              {
                  final String message =
                      REZ.getString( "metadata.nodeclare-deps.error",
                                     component.getName(),
                                     component.getClassname() );
                  throw new VerifyException( message );
              }
          }
          else
          {
              if( 0 != count )
              {
                  final String message =
                      REZ.getString( "metadata.declare-uneeded-deps.error",
                                     component.getName(),
                                     component.getClassname() );
                  throw new VerifyException( message );
              }
          }
      }
  
      /**
       * Retrieve an array of Classes for all the services that a Component
       * offers. This method also makes sure all services offered are
       * interfaces.
       *
       * @param name the name of component
       * @param services the services the component offers
       * @param classLoader the classLoader
       * @return an array of Classes for all the services
       * @throws VerifyException if an error occurs
       */
      protected Class[] getServiceClasses( final String name,
                                           final ServiceDescriptor[] services,
                                           final ClassLoader classLoader )
          throws VerifyException
      {
          final Class[] classes = new Class[ services.length ];
          for( int i = 0; i < services.length; i++ )
          {
              final ServiceDesignator service = services[ i ].getServiceDesignator();
              final String classname = service.getClassname();
              try
              {
                  classes[ i ] = classLoader.loadClass( classname );
              }
              catch( final Throwable t )
              {
                  final String message =
                      REZ.getString( "metadata.bad-service-class.error",
                                     name,
                                     classname,
                                     t.toString() );
                  throw new VerifyException( message, t );
              }
          }
  
          return classes;
      }
  }
  
  

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