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/lifecycle ResourceProvider.java LifecycleHelper.java ResourceAccessor.java
Date Tue, 18 Jun 2002 03:47:14 GMT
donaldp     2002/06/17 20:47:14

  Modified:    containerkit/src/java/org/apache/excalibur/containerkit/lifecycle
                        LifecycleHelper.java
  Added:       containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        KernelResourceProvider.java
               containerkit/src/java/org/apache/excalibur/containerkit/lifecycle
                        ResourceProvider.java
  Removed:     containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        KernelResourceAccessor.java
               containerkit/src/java/org/apache/excalibur/containerkit/lifecycle
                        ResourceAccessor.java
  Log:
  Rename accessor to provider because Stephen likes name better
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/KernelResourceProvider.java
  
  Index: KernelResourceProvider.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.kernel;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.DefaultComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.service.DefaultServiceManager;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.excalibur.containerkit.lifecycle.ResourceProvider;
  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.ContextDescriptor;
  import org.apache.excalibur.containerkit.metainfo.EntryDescriptor;
  
  /**
   * This is the bas object via which the kernel aquires
   * resources for each component. This base implementation
   * will aquire components and make sure that all required
   * components are present. It will also make sure that the types
   * of values returned from context are valid.
   *
   * <p>Note that this class assumes that the dependency graph
   * has been validated (presumably via
   * {@link org.apache.excalibur.containerkit.verifier.AssemblyVerifier}</p>
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/18 03:47:13 $
   */
  public abstract class KernelResourceProvider
      extends AbstractLogEnabled
      implements ResourceProvider
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( KernelResourceProvider.class );
  
      /**
       * Utility method via which the provider aquires services to place
       * in ServiceManager for a particular component.
       *
       * <p>Must be implemented in subclass.</p>
       *
       * @param name the name of service
       * @param entry the entry for component aquiring service
       * @return the service object for specified name
       */
      protected abstract Object getService( String name, Object entry );
  
      /**
       * Utility method via which the provider aquires a context value
       * to place in Context for a particular component.
       *
       * <p>Must be implemented in subclass.</p>
       *
       * @param name the name of service
       * @param entry the entry for component aquiring service
       * @return the context value for specified name
       */
      protected abstract Object getContextValue( String name, Object entry );
  
      /**
       * Create a Parameters object by Creating configuration object and converting that.
       *
       * @param entry the entry
       * @return a new Parameters object for component
       * @throws Exception if unable to create resource
       */
      public Parameters createParameters( Object entry )
          throws Exception
      {
          final Configuration configuration = createConfiguration( entry );
          return Parameters.fromConfiguration( configuration );
      }
  
      /**
       * Create a {@link Context} object that contains values specified in map.
       * The default implementation creates a basic Context object but different
       * containers may choose to overide this to provide their own subclass of context.
       *
       * @param contextData the data to place in context
       * @return the Context object
       */
      protected Context createContextImpl( final Map contextData )
      {
          final DefaultContext context = new DefaultContext( contextData );
          context.makeReadOnly();
          return context;
      }
  
      /**
       * Return the {@link ComponentMetaData} for specified component entry.
       * This implementation assumes that entry is instance of {@link ComponentMetaData}
       * but subclasses should overide this method if this assumption does not hold true.
       *
       * @param entry the component entry
       * @return the ComponentMetaData
       */
      protected ComponentMetaData getMetaData( final Object entry )
      {
          return (ComponentMetaData)entry;
      }
  
      /**
       * Create a context object for specified component.
       *
       * @param componentEntry the entry representing component
       * @return the created Context
       * @throws Exception if unable to create context or entrys in context
       */
      public final Context createContext( final Object componentEntry )
          throws Exception
      {
          final ComponentMetaData component = getMetaData( componentEntry );
          final String componentName = component.getName();
  
          final ContextDescriptor descriptor =
              component.getComponentInfo().getContextDescriptor();
  
          final Map contextData = new HashMap();
  
          final EntryDescriptor[] entrys = descriptor.getEntrys();
          for( int i = 0; i < entrys.length; i++ )
          {
              final EntryDescriptor entry = entrys[ i ];
              final String key = entry.getKey();
              final String type = entry.getType();
              final boolean optional = entry.isOptional();
              final Object value =
                  getContextValue( key, componentEntry );
  
              if( null == value )
              {
                  final String message =
                      REZ.getString( "resource.missing-context-value.error",
                                     optional ? "1" : "2",
                                     key,
                                     componentName );
                  if( !optional )
                  {
                      throw new Exception( message );
                  }
                  else
                  {
                      getLogger().warn( message );
                      continue;
                  }
              }
  
              final boolean typeValid = objectImplementsType( value, type );
              if( !typeValid )
              {
                  final String message =
                      REZ.getString( "resource.bad-value-type.error",
                                     optional ? "1" : "2",
                                     key,
                                     componentName,
                                     type,
                                     value.getClass().getName() );
                  if( !optional )
                  {
                      throw new Exception( message );
                  }
                  else
                  {
                      getLogger().warn( message );
                      continue;
                  }
              }
  
              contextData.put( key, value );
          }
  
          final Context context = createContextImpl( contextData );
          final String classname = descriptor.getClassname();
  
          final boolean validContextClass = objectImplementsType( context, classname );
          if( !validContextClass )
          {
              final String message =
                  REZ.getString( "resource.bad-context-type.error",
                                 classname,
                                 context.getClass().getName(),
                                 componentName );
              throw new Exception( message );
          }
  
          return context;
      }
  
      /**
       * Create a new ComponentManager for component.
       *
       * @param entry the entry
       * @return a new ComponentManager for component
       * @throws Exception if unable to create resource
       */
      public final ComponentManager createComponentManager( final Object entry )
          throws Exception
      {
          final Map services = createServiceMap( entry );
  
          final DefaultComponentManager componentManager = new DefaultComponentManager();
  
          final Iterator keys = services.keySet().iterator();
          while( keys.hasNext() )
          {
              final String key = (String)keys.next();
              final Object service = services.get( key );
              if( !Component.class.isInstance( service ) )
              {
                  final String message =
                      REZ.getString( "resource.service-not-a-component.error",
                                     key,
                                     service.getClass().getName() );
                  throw new Exception( message );
              }
              componentManager.put( key, (Component)service );
          }
  
          componentManager.makeReadOnly();
          return componentManager;
      }
  
      /**
       * Create a new ServiceManager for component.
       *
       * @param entry the entry
       * @return a new ServiceManager for component
       * @throws Exception if unable to create resource
       */
      public final ServiceManager createServiceManager( final Object entry )
          throws Exception
      {
          final Map services = createServiceMap( entry );
  
          final DefaultServiceManager serviceManager = new DefaultServiceManager();
  
          final Iterator keys = services.keySet().iterator();
          while( keys.hasNext() )
          {
              final String key = (String)keys.next();
              final Object service = services.get( key );
              serviceManager.put( key, service );
          }
  
          serviceManager.makeReadOnly();
          return serviceManager;
      }
  
      /**
       * Create a Map of services for specified component.
       * The map maps role name to service provider.
       *
       * @param componentEntry the component entry creating map for
       * @return the map
       * @throws Exception if error aquiring a service to place in map
       */
      private Map createServiceMap( final Object componentEntry )
          throws Exception
      {
          final ComponentMetaData component = getMetaData( componentEntry );
          final ComponentInfo info = component.getComponentInfo();
          final DependencyMetaData[] dependencies = component.getDependencies();
  
          final HashMap services = new HashMap();
  
          for( int i = 0; i < dependencies.length; i++ )
          {
              final DependencyMetaData dependency = dependencies[ i ];
              final String role = dependency.getRole();
              final String providerName = dependency.getProviderName();
              final boolean optional = info.getDependency( role ).isOptional();
  
              final Object service =
                  getService( providerName, componentEntry );
              if( null == service )
              {
                  final String message =
                      REZ.getString( "resource.missing-dependency.error",
                                     optional ? "1" : "2",
                                     role,
                                     component.getName() );
                  if( !optional )
                  {
                      throw new Exception( message );
                  }
                  else
                  {
                      getLogger().warn( message );
                      continue;
                  }
              }
  
              services.put( role, service );
          }
  
          return services;
      }
  
      /**
       * Check whether the specified value is compatible with specified type.
       *
       * @param value the value
       * @param type the desired type
       * @return true if value is compatible with type, false otherwise
       */
      private boolean objectImplementsType( final Object value, final String type )
      {
          try
          {
              final Class clazz = value.getClass();
              final ClassLoader classLoader = clazz.getClassLoader();
              final Class typeClass = classLoader.loadClass( type );
              if( typeClass.isAssignableFrom( clazz ) )
              {
                  return true;
              }
          }
          catch( final ClassNotFoundException cnfe )
          {
          }
          return false;
      }
  }
  
  
  
  1.8       +9 -9      jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/lifecycle/LifecycleHelper.java
  
  Index: LifecycleHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/lifecycle/LifecycleHelper.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- LifecycleHelper.java	6 Jun 2002 21:48:03 -0000	1.7
  +++ LifecycleHelper.java	18 Jun 2002 03:47:14 -0000	1.8
  @@ -64,13 +64,13 @@
        *
        * @param name the name of the component
        * @param entry the entry representing object
  -     * @param accessor the resource supplier 
  +     * @param provider the resource supplier
        * @throws LifecycleException if an error occurs when block passes
        *            through a specific lifecycle stage
        */
       public Object startup( final String name,
                              final Object entry,
  -                           final ResourceAccessor accessor )
  +                           final ResourceProvider provider )
           throws LifecycleException
       {
           int stage = 0;
  @@ -79,14 +79,14 @@
               //Creation stage
               stage = STAGE_CREATE;
               notice( name, stage );
  -            final Object object = accessor.createObject( entry );
  +            final Object object = provider.createObject( entry );
   
               //LogEnabled stage
               stage = STAGE_LOGGER;
               if( object instanceof LogEnabled )
               {
                   notice( name, stage );
  -                final Logger logger = accessor.createLogger( entry );
  +                final Logger logger = provider.createLogger( entry );
                   ContainerUtil.enableLogging( object, logger );
               }
   
  @@ -95,7 +95,7 @@
               if( object instanceof Contextualizable )
               {
                   notice( name, stage );
  -                final Context context = accessor.createContext( entry );
  +                final Context context = provider.createContext( entry );
                   ContainerUtil.contextualize( object, context );
               }
   
  @@ -105,14 +105,14 @@
               {
                   notice( name, stage );
                   final ServiceManager manager =
  -                    accessor.createServiceManager( entry );
  +                    provider.createServiceManager( entry );
                   ContainerUtil.service( object, manager );
               }
               else if( object instanceof Composable )
               {
                   notice( name, stage );
                   final ComponentManager componentManager =
  -                    accessor.createComponentManager( entry );
  +                    provider.createComponentManager( entry );
                   ContainerUtil.compose( object, componentManager );
               }
   
  @@ -122,7 +122,7 @@
               {
                   notice( name, stage );
                   final Configuration configuration =
  -                    accessor.createConfiguration( entry );
  +                    provider.createConfiguration( entry );
                   ContainerUtil.configure( object, configuration );
               }
   
  @@ -132,7 +132,7 @@
               {
                   notice( name, stage );
                   final Parameters parameters =
  -                    accessor.createParameters( entry );
  +                    provider.createParameters( entry );
                   ContainerUtil.parameterize( object, parameters );
               }
   
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/lifecycle/ResourceProvider.java
  
  Index: ResourceProvider.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.lifecycle;
  
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.logger.Logger;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.service.ServiceManager;
  
  /**
   * The interface via which resources required for a component
   * are aquired.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/18 03:47:14 $
   */
  public interface ResourceProvider
  {
      /**
       * Create the object specified by entry.
       *
       * @param entry the entry
       * @return the new object
       * @throws Exception if unable to create resource
       */
      Object createObject( Object entry )
          throws Exception;
  
      /**
       * Create a new Logger for component.
       *
       * @param entry the entry
       * @return a new Logger for component
       * @throws Exception if unable to create resource
       */
      Logger createLogger( Object entry )
          throws Exception;
  
      /**
       * Create a new Context for component.
       *
       * @param entry the entry
       * @return a new Context for component
       * @throws Exception if unable to create resource
       */
      Context createContext( Object entry )
          throws Exception;
  
      /**
       * Create a new ComponentManager for component.
       *
       * @param entry the entry
       * @return a new ComponentManager for component
       * @throws Exception if unable to create resource
       */
      ComponentManager createComponentManager( Object entry )
          throws Exception;
  
      /**
       * Create a new ServiceManager for component.
       *
       * @param entry the entry
       * @return a new ServiceManager for component
       * @throws Exception if unable to create resource
       */
      ServiceManager createServiceManager( Object entry )
          throws Exception;
  
      /**
       * Create a new Configuration object for component.
       *
       * @param entry the entry
       * @return a new Configuration object for component
       * @throws Exception if unable to create resource
       */
      Configuration createConfiguration( Object entry )
          throws Exception;
  
      /**
       * Create a new Parameters object for component.
       *
       * @param entry the entry
       * @return a new Parameters object for component
       * @throws Exception if unable to create resource
       */
      Parameters createParameters( Object entry )
          throws Exception;
  }
  
  
  

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