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/kernel AbstractServiceKernel.java DependencyGraph.java
Date Sun, 23 Jun 2002 05:59:18 GMT
donaldp     2002/06/22 22:59:18

  Modified:    containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        AbstractServiceKernel.java
  Added:       containerkit/src/java/org/apache/excalibur/containerkit/dependency
                        DependencyMap.java
  Removed:     containerkit/src/java/org/apache/excalibur/containerkit/dependency
                        DependencyGraph.java
               containerkit/src/java/org/apache/excalibur/containerkit/kernel
                        DependencyGraph.java
  Log:
  Rework the Dependeny graph stuff to be reusable in other contexts easily.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/containerkit/src/java/org/apache/excalibur/containerkit/dependency/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.dependency;
  
  import java.util.ArrayList;
  import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
  import org.apache.excalibur.containerkit.metadata.DependencyMetaData;
  import org.apache.excalibur.containerkit.metainfo.DependencyDescriptor;
  
  /**
   * Utility class to help aquire a ordered graph of
   * consumers and providers for specific components.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/23 05:59:17 $
   */
  public class DependencyMap
  {
      private final ComponentMetaData[] m_components;
  
      public DependencyMap( final ComponentMetaData[] components )
      {
          m_components = components;
      }
  
      /**
       * Get the serilized graph of {@link ComponentMetaData} 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 ComponentMetaData[] getStartupGraph()
      {
          return walkGraph( true );
      }
  
      /**
       * Get the serilized graph of {@link ComponentMetaData} 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 ComponentMetaData[] getShutdownGraph()
      {
          return walkGraph( false );
      }
  
      /**
       * Get the serilized graph of {@link ComponentMetaData} objects
       * that use services of specified component.
       *
       * @param component the component
       * @return the ordered list of consumers
       */
      public ComponentMetaData[] getConsumerGraph( final ComponentMetaData component )
      {
          return getComponentGraph( component, false );
      }
  
      /**
       * Get the serilized graph of {@link ComponentMetaData} objects
       * that provide specified component with services.
       *
       * @param component the component
       * @return the ordered list of providers
       */
      public ComponentMetaData[] getProviderGraph( final ComponentMetaData component )
      {
          return getComponentGraph( component, true );
      }
  
      /**
       * Get the graph of a single component.
       *
       * @param component the component
       * @param provider true if traversing providers, false if consumers
       * @return the list of components in graph
       */
      private ComponentMetaData[] getComponentGraph( final ComponentMetaData component, final
boolean provider )
      {
          final ArrayList result = new ArrayList();
          visitcomponent( component,
                          provider,
                          new ArrayList(),
                          result );
  
          final ComponentMetaData[] returnValue = new ComponentMetaData[ result.size() ];
          return (ComponentMetaData[])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 forward true if forward dependencys traced, false if dependencies reversed
       * @return the ordered node names
       */
      private ComponentMetaData[] walkGraph( final boolean forward )
      {
          final ArrayList result = new ArrayList();
  
          for( int i = 0; i < m_components.length; i++ )
          {
              visitcomponent( m_components[ i ],
                              forward,
                              new ArrayList(),
                              result );
          }
  
          final ComponentMetaData[] returnValue = new ComponentMetaData[ result.size() ];
          return (ComponentMetaData[])result.toArray( returnValue );
      }
  
      /**
       * Visit a component when traversing dependencies.
       *
       * @param component the component
       * @param forward true if walking down tree, else false
       * @param done those nodes already traversed
       * @param order the order in which nodes have already been
       *             traversed
       */
      private void visitcomponent( final ComponentMetaData component,
                                   final boolean forward,
                                   final ArrayList done,
                                   final ArrayList order )
      {
          //If already visited this component then bug out early
          if( done.contains( component ) )
          {
              return;
          }
          done.add( component );
  
          if( forward )
          {
              visitDependencies( component, done, order );
          }
          else
          {
              visitReverseDependencies( component, done, order );
          }
  
          order.add( component );
      }
  
      /**
       * Traverse dependencies of specified component.
       *
       * @param component the ComponentMetaData
       */
      private void visitDependencies( final ComponentMetaData component,
                                      final ArrayList done,
                                      final ArrayList order )
      {
          final DependencyDescriptor[] descriptors =
              component.getComponentInfo().getDependencies();
  
          for( int i = 0; i < descriptors.length; i++ )
          {
              final DependencyMetaData dependency =
                  component.getDependency( descriptors[ i ].getRole() );
              final ComponentMetaData other =
                  getComponent( dependency.getProviderName() );
              visitcomponent( other, true, done, order );
          }
      }
  
      /**
       * Traverse all reverse dependencies of specified component.
       * A reverse dependency are those that dependend on component.
       *
       * @param component the ComponentMetaData
       */
      private void visitReverseDependencies( final ComponentMetaData component,
                                             final ArrayList done,
                                             final ArrayList order )
      {
          final String name = component.getName();
  
          for( int i = 0; i < m_components.length; i++ )
          {
              final ComponentMetaData other = m_components[ i ];
              final DependencyMetaData[] roles = other.getDependencies();
              if( null == roles )
              {
                  continue;
              }
  
              for( int j = 0; j < roles.length; j++ )
              {
                  final String depends = roles[ j ].getProviderName();
                  if( depends.equals( name ) )
                  {
                      visitcomponent( other, false, done, order );
                  }
              }
          }
      }
  
      /**
       * Utility method to get component with specified name from specified array.
       *
       * @param name the name of component
       * @return the component
       */
      private ComponentMetaData getComponent( final String name )
      {
          for( int i = 0; i < m_components.length; i++ )
          {
              if( m_components[ i ].getName().equals( name ) )
              {
                  return m_components[ i ];
              }
          }
  
          //Should never happen if Verifier passed checks
          throw new IllegalStateException();
      }
  }
  
  
  
  1.5       +38 -20    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbstractServiceKernel.java	23 Jun 2002 04:44:15 -0000	1.4
  +++ AbstractServiceKernel.java	23 Jun 2002 05:59:18 -0000	1.5
  @@ -7,17 +7,18 @@
    */
   package org.apache.excalibur.containerkit.kernel;
   
  -import java.util.Arrays;
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.HashMap;
  -import java.util.List;
   import java.util.Iterator;
  +import java.util.List;
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.activity.Startable;
   import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.excalibur.containerkit.dependency.DependencyMap;
   import org.apache.excalibur.containerkit.lifecycle.LifecycleHelper;
   import org.apache.excalibur.containerkit.lifecycle.ResourceProvider;
   import org.apache.excalibur.containerkit.metadata.ComponentMetaData;
  @@ -52,11 +53,18 @@
        */
       private final HashMap m_entrys = new HashMap();
   
  +    /**
  +     * The {@link DependencyMap} via which dependency graph is
  +     * produced.
  +     */
  +    private DependencyMap m_dependencyMap;
  +
       public void initialize()
           throws Exception
       {
           m_resourceProvider = prepareResourceProvider();
           m_lifecycleHelper = prepareLifecycleHelper();
  +        m_dependencyMap = new DependencyMap( getCompnentMetaDatas() );
       }
   
       protected abstract ResourceProvider prepareResourceProvider();
  @@ -71,29 +79,33 @@
       protected final void startupAllComponents()
           throws Exception
       {
  -        final String[] order =
  -            DependencyGraph.walkGraph( true, getCompnentMetaDatas() );
  -        processComponents( true, order );
  +        final ComponentMetaData[] components = m_dependencyMap.getStartupGraph();
  +        processComponents( true, components );
       }
   
       protected final void shutdownAllComponents()
           throws Exception
       {
  -        final String[] order =
  -            DependencyGraph.walkGraph( false, getCompnentMetaDatas() );
  -        processComponents( false, order );
  +        final ComponentMetaData[] components = m_dependencyMap.getStartupGraph();
  +        processComponents( false, components );
       }
   
       protected final void startupComponent( final String name )
           throws Exception
       {
  -        //        final String[] order = DependencyGraph.walkGraph( false, components );
  -        //        processComponents( false, order );
  +        final ComponentEntry entry = (ComponentEntry)m_entrys.get( name );
  +        final ComponentMetaData[] components =
  +            m_dependencyMap.getProviderGraph( entry.getMetaData() );
  +        processComponents( true, components );
       }
   
       protected final void shutdownComponent( final String name )
           throws Exception
       {
  +        final ComponentEntry entry = (ComponentEntry)m_entrys.get( name );
  +        final ComponentMetaData[] components =
  +            m_dependencyMap.getConsumerGraph( entry.getMetaData() );
  +        processComponents( false, components );
       }
   
       /**
  @@ -124,19 +136,18 @@
        * in order specified by the dependency graph.
        *
        * @param startup true if application startup phase, false if shutdown phase
  -     * @param order
        * @throws Exception if there is error processing any of the components
        *         through the phases
        */
       private void processComponents( final boolean startup,
  -                                    final String[] order )
  +                                    final ComponentMetaData[] components )
           throws Exception
       {
  -        processComponentsNotice( order, startup );
  +        processComponentsNotice( components, startup );
   
  -        for( int i = 0; i < order.length; i++ )
  +        for( int i = 0; i < components.length; i++ )
           {
  -            processComponent( order[ i ], startup );
  +            processComponent( components[ i ], startup );
           }
       }
   
  @@ -148,15 +159,16 @@
        * it is expected that all of the consumers of services provided
        * by this component have already been shutdown.
        *
  -     * @param name the name of the component
  +     * @param component the component
        * @param startup true if application startup phase, false if shutdown phase
        * @throws Exception if there is error processing any of the components
        *         through the phases
        */
  -    private void processComponent( final String name,
  +    private void processComponent( final ComponentMetaData component,
                                      final boolean startup )
           throws Exception
       {
  +        final String name = component.getName();
           final ComponentEntry entry =
               (ComponentEntry)m_entrys.get( name );
   
  @@ -209,14 +221,20 @@
        * @param order the order the components will be processed in
        * @param startup true if application startup phase, false if shutdown phase
        */
  -    private void processComponentsNotice( final String[] order,
  +    private void processComponentsNotice( final ComponentMetaData[] order,
                                             final boolean startup )
       {
   
           if( getLogger().isInfoEnabled() )
           {
               final Integer count = new Integer( order.length );
  -            final List pathList = Arrays.asList( order );
  +
  +            final List pathList = new ArrayList();
  +            for( int i = 0; i < order.length; i++ )
  +            {
  +                pathList.add( order[ i ].getName() );
  +            }
  +
               final String message =
                   REZ.getString( "components-processing",
                                  count,
  
  
  

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