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/store ComponentStore.java
Date Mon, 09 Sep 2002 12:07:46 GMT
donaldp     2002/09/09 05:07:46

  Modified:    containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        AbstractServiceKernel.java
  Added:       containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        ComponentReference.java ComponentStore.java
               containerkit/src/java/org/apache/excalibur/containerkit/kernel/processor
                        DependencyMap.java Phase.java PhaseEntry.java
                        PhaseProcessor.java
  Removed:     containerkit/src/java/org/apache/excalibur/containerkit/directory
                        ComponentReference.java
               containerkit/src/java/org/apache/excalibur/containerkit/processor
                        DependencyMap.java Phase.java PhaseEntry.java
               containerkit/src/java/org/apache/excalibur/containerkit/store
                        ComponentStore.java
  Log:
  Start moving all the unstable stuff that is NK partition specific under kernel package.
Can differentiate again when stable or remove if Spice superscedes it.
  
  Revision  Changes    Path
  1.30      +3 -4      jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/AbstractServiceKernel.java
  
  Index: AbstractServiceKernel.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/AbstractServiceKernel.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- AbstractServiceKernel.java	31 Aug 2002 10:57:59 -0000	1.29
  +++ AbstractServiceKernel.java	9 Sep 2002 12:07:45 -0000	1.30
  @@ -17,12 +17,11 @@
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.container.ContainerUtil;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  -import org.apache.excalibur.containerkit.processor.DependencyMap;
  +import org.apache.excalibur.containerkit.kernel.processor.DependencyMap;
   import org.apache.excalibur.containerkit.factory.ComponentFactory;
   import org.apache.excalibur.containerkit.lifecycle.LifecycleHelper;
   import org.apache.excalibur.containerkit.lifecycle.ResourceProvider;
   import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
  -import org.apache.excalibur.containerkit.store.ComponentStore;
   import org.apache.excalibur.containerkit.registry.ComponentProfile;
   import org.apache.avalon.framework.info.ComponentInfo;
   import org.apache.excalibur.containerkit.factory.ComponentBundle;
  @@ -61,7 +60,7 @@
        */
       private final ComponentStore m_store = new ComponentStore();
       /**
  -     * The {@link org.apache.excalibur.containerkit.processor.DependencyMap} via which
dependency graph is
  +     * The {@link org.apache.excalibur.containerkit.kernel.processor.DependencyMap} via
which dependency graph is
        * produced.
        */
       private final DependencyMap m_dependencyMap = new DependencyMap();
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/ComponentReference.java
  
  Index: ComponentReference.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 org.apache.avalon.framework.info.ComponentInfo;
  
  /**
   * This interface defines a resource returned from the
   * {@link ComponentDirectory}.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/09 12:07:45 $
   */
  public interface ComponentReference
  {
      /**
       * Return the underlying object representing the Component.
       *
       * @return the underlying object representing the Component
       */
      Object getComponent();
  
      /**
       * @todo Determine if this is reallly needed. It is only
       * returned as  a result of Directory lookup, hence should
       * know the type.
       * @todo Determine if this could be replace by getInfo()
       */
      ComponentInfo getInfo();
  
      /**
       * Invalidate the component reference. After the reference has been
       * invalidated, any calls made on the Compnent will have undefined
       * results.
       */
      void invalidate();
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/ComponentStore.java
  
  Index: ComponentStore.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.ArrayList;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import org.apache.excalibur.containerkit.registry.ComponentProfile;
  
  /**
   *
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/09 12:07:45 $
   */
  public class ComponentStore
  {
      /**
       * Parent {@link org.apache.excalibur.containerkit.kernel.ComponentStore}. Components
in parent
       * {@link org.apache.excalibur.containerkit.kernel.ComponentStore} are potential Providers
for services
       * if no component in current {@link org.apache.excalibur.containerkit.kernel.ComponentStore}
satisfies
       * dependency.
       */
      private final ComponentStore m_parent;
      /**
       * The child {@link org.apache.excalibur.containerkit.kernel.ComponentStore} objects.
       * Possible consumers of services in this assembly.
       */
      private final ArrayList m_children = new ArrayList();
      /**
       * The set of components in assembly.
       * Used when searching for providers/consumers.
       */
      private final Map m_components = new HashMap();
  
      /**
       * Create a root ComponentStore without any parent
       * ComponentStore.
       */
      public ComponentStore()
      {
          this( null );
      }
  
      /**
       * Return the parent ComponentStore (may be null).
       */
      public ComponentStore getParent()
      {
          return m_parent;
      }
  
      /**
       * Create a root ComponentStore with specified parent
       * ComponentStore.
       */
      public ComponentStore( final ComponentStore parent )
      {
          m_parent = parent;
      }
  
      /**
       * Add child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}.
       *
       * @param child the child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}.
       */
      public void addChildStore( final ComponentStore child )
      {
          m_children.add( child );
      }
  
      /**
       * Return the list of child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}s.
       *
       * @return the list of child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}s.
       */
      public List getChildStores()
      {
          return m_children;
      }
  
      /**
       * Remove child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}.
       *
       * @param child the child {@link org.apache.excalibur.containerkit.kernel.ComponentStore}.
       */
      public void removeChildStore( final ComponentStore child )
      {
          m_children.remove( child );
      }
  
      /**
       * Add a component to store.
       *
       * @param component the component
       */
      public void addComponent( final ComponentProfile component )
      {
          final String name =
              component.getMetaData().getName();
          m_components.put( name, component );
      }
  
      /**
       * Remove a component from the store.
       *
       * @param component the component
       */
      public void removeComponent( final ComponentProfile component )
      {
          final String name =
              component.getMetaData().getName();
          m_components.remove( name );
      }
  
      /**
       * Return a component with specified name.
       *
       * @return a component with specified name
       */
      public ComponentProfile getComponent( final String name )
      {
          return (ComponentProfile)m_components.get( name );
      }
  
      /**
       * Return a collection containing all the
       * names of components in store. No ordering of
       * components is guarenteed or mandated.
       *
       * @return the collection containing all component names
       */
      public Collection getComponentNames()
      {
          final Collection collection = m_components.keySet();
          final ArrayList components = new ArrayList();
          components.addAll( collection );
          return components;
      }
  
      /**
       * Return a collection containing all the
       * components in store. Noordering of
       * components is guarenteed or mandated.
       *
       * @return the collection containing all components
       */
      public Collection getComponents()
      {
          final Collection collection = m_components.values();
          final ArrayList components = new ArrayList();
          components.addAll( collection );
          return components;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/processor/DependencyMap.java
  
  Index: DependencyMap.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.processor;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
  import org.apache.excalibur.containerkit.metadata.DependencyMetaData;
  import org.apache.excalibur.containerkit.registry.ComponentProfile;
  import org.apache.excalibur.containerkit.kernel.ComponentStore;
  
  /**
   * Utility class to help aquire a ordered graph of
   * consumers and providers for specific components.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/09 12:07:46 $
   */
  public class DependencyMap
  {
      /**
       * Get the serilized graph of {@link ComponentProfile} objects
       * required when starting up all the components. This makes sure
       * that all providers occur before their coresponding
       * consumers in graph.
       *
       * @return the ordered list of components
       */
      public ComponentProfile[] getStartupGraph( final ComponentStore store )
      {
          return walkGraph( true, store );
      }
  
      /**
       * Get the serilized graph of {@link ComponentProfile} objects
       * required when shutting down all the components. This makes
       * sure that all consumers occur before their coresponding
       * providers in graph.
       *
       * @return the ordered list of components
       */
      public ComponentProfile[] getShutdownGraph( final ComponentStore store )
      {
          return walkGraph( false, store );
      }
  
      /**
       * Get the serilized graph of {@link ComponentProfile} objects
       * that use services of specified component.
       *
       * @param component the component
       * @return the ordered list of consumers
       */
      public ComponentProfile[] getConsumerGraph( final ComponentProfile component,
                                                  final ComponentStore store )
      {
          return getComponentGraph( component, false, store );
      }
  
      /**
       * Get the serilized graph of {@link ComponentProfile} objects
       * that provide specified component with services.
       *
       * @param component the component
       * @return the ordered list of providers
       */
      public ComponentProfile[] getProviderGraph( final ComponentProfile component,
                                                  final ComponentStore store )
      {
          return getComponentGraph( component, true, store );
      }
  
      /**
       * Get the graph of a single component.
       *
       * @param component the component
       * @param providers true if traversing providers, false if consumers
       * @return the list of components in graph
       */
      private ComponentProfile[] getComponentGraph( final ComponentProfile component,
                                                    final boolean providers,
                                                    final ComponentStore store )
      {
          final ArrayList result = new ArrayList();
          visitcomponent( component,
                          providers,
                          new ArrayList(),
                          result,
                          store );
  
          final ComponentProfile[] returnValue = new ComponentProfile[ result.size() ];
          return (ComponentProfile[])result.toArray( returnValue );
      }
  
      /**
       * Method to generate an ordering of nodes to traverse.
       * It is expected that the specified components have passed
       * verification tests and are well formed.
       *
       * @param providers true if forward dependencys traced, false if dependencies reversed
       * @return the ordered node names
       */
      private ComponentProfile[] walkGraph( final boolean providers,
                                            final ComponentStore store )
      {
          final ArrayList result = new ArrayList();
          final ArrayList done = new ArrayList();
  
          final Collection components = store.getComponents();
          final ComponentProfile[] entrySet =
              (ComponentProfile[])components.toArray( new ComponentProfile[ components.size()
] );
          for( int i = 0; i < entrySet.length; i++ )
          {
              final ComponentProfile component = entrySet[ i ];
              visitcomponent( component,
                              providers,
                              done,
                              result,
                              store );
          }
  
          final ComponentProfile[] returnValue = new ComponentProfile[ result.size() ];
          return (ComponentProfile[])result.toArray( returnValue );
      }
  
      /**
       * Visit a component when traversing dependencies.
       *
       * @param component the component
       * @param providers true if walking tree looking for providers, else false
       * @param done those nodes already traversed
       * @param order the order in which nodes have already been
       *             traversed
       */
      private void visitcomponent( final ComponentProfile component,
                                   final boolean providers,
                                   final ArrayList done,
                                   final ArrayList order,
                                   final ComponentStore store )
      {
          //If already visited this component then bug out early
          if( done.contains( component ) )
          {
              return;
          }
          done.add( component );
  
          if( providers )
          {
              visitProviders( component, done, order, store );
          }
          else
          {
              visitConsumers( component, done, order, store );
          }
  
          order.add( component );
      }
  
      /**
       * Traverse graph of components that provide services to
       * the specified component.
       *
       * @param component the ComponentProfile
       */
      private void visitProviders( final ComponentProfile component,
                                   final ArrayList done,
                                   final ArrayList order,
                                   final ComponentStore store )
      {
          final DependencyDescriptor[] descriptors =
              component.getInfo().getDependencies();
          final ComponentMetaData metaData = component.getMetaData();
  
          for( int i = 0; i < descriptors.length; i++ )
          {
              final DependencyMetaData dependency =
                  metaData.getDependency( descriptors[ i ].getKey() );
  
              // added != null clause to catch cases where an optional
              // dependency exists and the dependecy has not been bound
              // to a provider
  
              if( dependency != null )
              {
                  final ComponentProfile other =
                      getComponent( dependency.getProviderName(), store );
                  visitcomponent( other, true, done, order, store );
              }
          }
      }
  
      /**
       * Traverse all Consumers of component. ie Anyone that uses
       * service provided by component.
       *
       * @param component the ComponentProfile
       */
      private void visitConsumers( final ComponentProfile component,
                                   final ArrayList done,
                                   final ArrayList order,
                                   final ComponentStore store )
      {
          final String name = component.getMetaData().getName();
  
          final Collection components = store.getComponents();
          final ComponentProfile[] entrySet =
              (ComponentProfile[])components.toArray( new ComponentProfile[ components.size()
] );
          for( int i = 0; i < entrySet.length; i++ )
          {
              final ComponentProfile other = entrySet[ i ];
              final DependencyMetaData[] roles =
                  other.getMetaData().getDependencies();
  
              for( int j = 0; j < roles.length; j++ )
              {
                  final String depends = roles[ j ].getProviderName();
                  if( depends.equals( name ) )
                  {
                      visitcomponent( other, false, done, order, store );
                  }
              }
          }
  
          final List childStores = store.getChildStores();
          final int childCount = childStores.size();
          for( int i = 0; i < childCount; i++ )
          {
              final ComponentStore child = (ComponentStore)childStores.get( i );
              visitConsumers( component, done, order, child );
          }
      }
  
      /**
       * Utility method to get component with specified name from specified array.
       *
       * @param name the name of component
       * @return the component
       */
      private ComponentProfile getComponent( final String name,
                                             final ComponentStore store )
      {
          final ComponentProfile component = store.getComponent( name );
          if( null != component )
          {
              return component;
          }
  
          final ComponentStore parent = store.getParent();
          if( null != parent )
          {
              return parent.getComponent( name );
          }
  
          //Should never happen if Verifier passed checks
          throw new IllegalStateException();
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/processor/Phase.java
  
  Index: Phase.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.processor;
  
  /**
   * The phase is a set of Stages that a component must pass
   * through.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/09 12:07:46 $
   */
  public interface Phase
  {
      void processPhase( String name )
          throws Exception;
  }
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/processor/PhaseEntry.java
  
  Index: PhaseEntry.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.processor;
  
  /**
   * The PhaseEntry class defines a particular Phase that
   * components can pass through. It defines the traversal
   * strategy, the phase and whether or not failure of
   * a particular component passing through the Phase
   * will cause all processing to be stopped.
   */
  public class PhaseEntry
  {
      public final static int TRAVERSE_DEPS_FORWARD = 0;
      public final static int TRAVERSE_DEPS_REVERSE = 1;
      public final static int NO_TRAVERSE_DEPS = 2;
  
      private final int m_traversalStrategy;
      private final boolean m_haltOnError;
      private final Phase m_processor;
  
      public PhaseEntry( final int traversalStrategy,
                         final boolean haltOnError,
                         final Phase processor )
      {
          m_traversalStrategy = traversalStrategy;
          m_haltOnError = haltOnError;
          m_processor = processor;
      }
  
      public int getTraversalStrategy()
      {
          return m_traversalStrategy;
      }
  
      public boolean shouldHaltOnError()
      {
          return m_haltOnError;
      }
  
      public Phase getPhase()
      {
          return m_processor;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/kernel/processor/PhaseProcessor.java
  
  Index: PhaseProcessor.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.processor;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.excalibur.containerkit.registry.ComponentProfile;
  
  /**
   *
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/09 12:07:46 $
   */
  public class PhaseProcessor
      extends AbstractLogEnabled
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( PhaseProcessor.class );
  
      /**
       * The {@link DependencyMap} via which dependency graph is
       * produced.
       */
      private DependencyMap m_dependencyMap = new DependencyMap();
  
      public void processPhase( final PhaseEntry entry )
          throws Throwable
      {
          final Phase phase = entry.getPhase();
          final boolean shouldHaltOnError = entry.shouldHaltOnError();
          final int strategy = entry.getTraversalStrategy();
          final ComponentProfile[] graph = getOrderedComponents( strategy );
          for( int i = 0; i < graph.length; i++ )
          {
              final ComponentProfile component = graph[ i ];
              final String name = component.getMetaData().getName();
              try
              {
                  phase.processPhase( name );
              }
              catch( final Throwable t )
              {
                  if( shouldHaltOnError )
                  {
                      throw t;
                  }
                  else
                  {
                      final String message =
                          REZ.getString( "error.phase." );
                      getLogger().warn( message, t );
                  }
              }
          }
      }
  
      private ComponentProfile[] getOrderedComponents( final int strategy )
      {
          ComponentProfile[] graph;
          if( PhaseEntry.TRAVERSE_DEPS_FORWARD == strategy )
          {
              graph = m_dependencyMap.getStartupGraph( null );
          }
          else if( PhaseEntry.TRAVERSE_DEPS_REVERSE == strategy )
          {
              graph = m_dependencyMap.getShutdownGraph( null );
          }
          else
          {
              //Should really be processing blocks in
              //definition order
              graph = m_dependencyMap.getShutdownGraph( null );
          }
          return graph;
      }
  }
  
  
  

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