janb        2003/08/15 07:11:27

  Modified:    modules/core/src/java/org/apache/geronimo/common
                        AbstractComponent.java AbstractContainer.java
                        AbstractStateManageable.java Component.java
                        Container.java
  Added:       modules/core/src/java/org/apache/geronimo/common
                        AbstractRPCContainer.java RPCContainer.java
  Log:
  Refactored Container to RPCContainer and AbstractRPCContainer. Many classes 
in ejb package required modification
  to use RPCContainer instead of Container - for now this is done with class 
casts, which look messy, maybe theres
  a better way.
  
  Revision  Changes    Path
  1.5       +69 -6     
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractComponent.java
  
  Index: AbstractComponent.java
  ===================================================================
  RCS file: 
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractComponent.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbstractComponent.java    14 Aug 2003 07:14:33 -0000      1.4
  +++ AbstractComponent.java    15 Aug 2003 14:11:26 -0000      1.5
  @@ -65,14 +65,34 @@
       extends AbstractStateManageable
       implements Component
   {
  +    //the Container this Component belongs to
       private Container container;
   
  -    public final Container getContainer()
  +    //the identity of this Component
  +    private String name;
  +
  +
  +    /**
  +     * The Container that this Component belongs to
  +     *
  +     * @return a <code>Container</code> value
  +     */
  +    public Container getContainer()
       {
           return container;
       }
   
  -    public final void setContainer(Container container)
  +    /**
  +     * Sets the container which ownes this component.
  +     * The contianer can only be set before create() or to null after the 
destroy().
  +     *
  +     * @param container which owns this component
  +     * @throws java.lang.IllegalStateException if this component is not in 
the not-created or destroyed state
  +     * @throws java.lang.IllegalArgumentException if this comonent has not 
been created and the container
  +     * parameter is null, or the component has been destroyed and the 
container parameter is NOT null
  +     */
  +    public void setContainer(Container container)
  +        throws IllegalStateException, IllegalArgumentException
       {
           if (getStateInstance() != State.STOPPED)
           {
  @@ -82,21 +102,21 @@
           this.container= container;
       }
   
  -    /* (non-Javadoc)
  +    /* Start the Component
        * @see org.apache.geronimo.common.AbstractStateManageable#doStart()
        */
       public void doStart() throws Exception
       {
       }
   
  -    /* (non-Javadoc)
  +    /* Stop the Component
        * @see org.apache.geronimo.common.AbstractStateManageable#doStop()
        */
       public void doStop() throws Exception
       {
       }
   
  -    /* (non-Javadoc)
  +    /* 
        * @see 
org.apache.geronimo.common.AbstractStateManageable#doNotification(java.lang.String)
        */
       public void doNotification(String eventTypeValue)
  @@ -104,4 +124,47 @@
           log.debug("notification: "+eventTypeValue+" from "+this);
       }
       
  +
  +    /**
  +     * Get the unique identity of this Component
  +     *
  +     * @return the name (formatted according to JSR 77)
  +     */
  +    public String getObjectName ()
  +    {
  +        return name;
  +    }
  +
  +    /**
  +     * Two Components are equal if they have the same name;
  +     *
  +     * @param component to test
  +     * @return true if the names are the same, false otherwise
  +     */
  +    public boolean equals (Component component)
  +    {
  +        if (component == null)
  +            return false;
  +
  +        if (component.getObjectName().equals(name))
  +            return true;
  +
  +        return false;
  +    }
  +
  +
  +    /**
  +     * Get a hash value for this Component.
  +     * This is the hash of the unique name of
  +     * the Component.
  +     *
  +     * @return hash of Component name
  +     */
  +    public int hashCode ()
  +    {
  +        if (name == null)
  +            return 0;
  +
  +        return name.hashCode();
  +    }
   }
  
  
  
  1.2       +133 -4    
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractContainer.java
  
  Index: AbstractContainer.java
  ===================================================================
  RCS file: 
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractContainer.java    14 Aug 2003 07:14:33 -0000      1.1
  +++ AbstractContainer.java    15 Aug 2003 14:11:26 -0000      1.2
  @@ -55,15 +55,144 @@
    */
   package org.apache.geronimo.common;
   
  +import java.util.ArrayList;
  +import java.util.Collections;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.ListIterator;
  +
   /**
    * Abstract implementation of Container interface.
  - * There is no behaviour here yet, as this is just a place holder 
  - * for a class that should exist
    * 
  - * */
  -public abstract class AbstractContainer 
  + * @version $Revision$ $Date$ 
  + * 
  +*/
  +public abstract class AbstractContainer
       extends AbstractComponent
       implements Container
   {
  +    private ArrayList components = new ArrayList();
  +
  +    /**
  +     * A Container cannot be itself contained in a Container. 
  +     *
  +     * NB. Do we agree this is the model?
  +     * @return always null
  +     */
  +    public Container getContainer()
  +    {
  +        return null;
  +    }
  +
  +    /**
  +     * A Container cannot be itself contained in a Container.
  +     *     
  +     * NB. Do we agree this is the model?
  +     * @throws java.lang.UnsupportedOperationException
  +     */
  +    public void setContainer()
  +    {
  +        throw new UnsupportedOperationException("Cannot call setContainer on 
a Container");
  +    }
  +
  +    /**
  +     * Add a component to the set for a Container.
  +     * Subclasses might like to override this in order
  +     * to check their state before allowing the addition.
  +     *
  +     * @param component 
  +     */
  +    public void addComponent(Component component)
  +    {
  +        if (component == null)
  +            return;
  +
  +        components.add(component);
  +    }
  +
  +    /**
  +     * Get all the Components known to the Container
  +     *
  +     * @return an immutable List of Components
  +     */
  +    public List getComponents()
  +    {
  +        return Collections.unmodifiableList(components);
  +    }
  +
  +    /**
  +     * Remove a Component from the Container.
  +     * If the Component is not in the Container,
  +     * an Exception is thrown.
  +     *
  +     * Subclasses might want to override this, for
  +     * example to disallow Component addition/removal
  +     * after the Container is started.
  +     *
  +     * @param component the Component to remove
  +     */
  +    public void removeComponent(Component component) throws Exception
  +    {
  +        if (component == null)
  +            return;
  +
  +        components.remove(component);
  +    }
  +
  +    /**
  +     * Start the Container, and all of its Components.
  +     *
  +     * @exception Exception if an error occurs
  +     */
  +    public void startRecursive() throws Exception
  +    {
  +        //start the container itself
  +        start();
  +
  +        //start the Components
  +        Iterator itor = components.iterator();
  +        while (itor.hasNext())
  +        {
  +            //only start stopped or failed Components as per JSR77
  +            Component c = (Component) itor.next();
  +            if ((c.getStateInstance() == State.STOPPED)
  +                || 
  +                (c.getStateInstance() == State.FAILED))
  +                c.startRecursive();
  +        }
  +
  +        //NOTE: it is perfectly possible that some Components will be
  +        //in the RUNNING state and some of them in the STOPPED or FAILED 
state
  +    }
  +
  +
  +    /* 
--------------------------------------------------------------------------------------
 */
  +    /** Do a recursive stop.
  +     * 
  +     * @see org.apache.geronimo.common.StateManageable#stop()
  +     */
  +    public void stop()
  +    {
  +        // Stop all the Components in reverse insertion order
  +        try
  +        {
  +            setState(State.STOPPING);
  +
  +            for (ListIterator iterator =
  +                components.listIterator(components.size());
  +                iterator.hasPrevious();
  +                )
  +            {
  +                Interceptor interceptor = (Interceptor) iterator.previous();
  +                interceptor.stop();
  +            }
  +            setState(State.STOPPED);
  +        }
  +        finally
  +        {
  +            if (getStateInstance() != State.STOPPED)
  +                setState(State.FAILED);
  +        }
  +    }
   
   }
  
  
  
  1.2       +2 -2      
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractStateManageable.java
  
  Index: AbstractStateManageable.java
  ===================================================================
  RCS file: 
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractStateManageable.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractStateManageable.java      14 Aug 2003 07:14:33 -0000      1.1
  +++ AbstractStateManageable.java      15 Aug 2003 14:11:26 -0000      1.2
  @@ -266,5 +266,5 @@
           }
       }
   
  -    
  +
   }
  
  
  
  1.5       +8 -3      
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/Component.java
  
  Index: Component.java
  ===================================================================
  RCS file: 
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/Component.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Component.java    14 Aug 2003 07:14:33 -0000      1.4
  +++ Component.java    15 Aug 2003 14:11:26 -0000      1.5
  @@ -62,7 +62,7 @@
    *
    * @version $Revision$ $Date$
    */
  -public interface Component  extends StateManageable
  +public interface Component extends StateManageable
   {
       /**
        * Gets the container to which this component belongs.
  @@ -82,5 +82,10 @@
       void setContainer(Container container) throws IllegalStateException, 
IllegalArgumentException;
   
   
  -
  +    /**
  +     * Get the unique id of this Component
  +     *
  +     * @return name formatted according to JSR77
  +     */
  +    String getObjectName();
   }
  
  
  
  1.4       +26 -28    
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/Container.java
  
  Index: Container.java
  ===================================================================
  RCS file: 
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/Container.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Container.java    11 Aug 2003 17:59:10 -0000      1.3
  +++ Container.java    15 Aug 2003 14:11:26 -0000      1.4
  @@ -55,47 +55,45 @@
    */
   package org.apache.geronimo.common;
   
  -import javax.management.ObjectName;
  +
  +import java.util.List;
  +
   
   /**
  - *
  + * Container
    *
    *
    * @version $Revision$ $Date$
    */
  -public interface Container extends Component {
  -    //
  -    //  Main entry point
  -    //
  -    InvocationResult invoke(Invocation invocation) throws Exception;
  +public interface Container extends Component{
   
       /**
  -     * Get the JMX object name of the logical plugin.
  -     * @param logicalPluginName the logical name of the desired plugin
  -     * @return the JMX object name associated with the logical plugin, or 
null if a name is not found
  +     * Add a component to the set for a Container
  +     *
  +     * @param component
  +     * 
        */
  -    ObjectName getPlugin(String logicalPluginName);
  +    public void addComponent(Component component);
  +
  +
   
  -    /**
  -     * Puts the objectName in the container.
  -     * @param logicalPluginName the logical name of the plugin to set
  -     * @param objectName the JMX object name to set
  -     */
  -    void putPlugin(String logicalPluginName, ObjectName objectName);
   
       /**
  -     * Gets the named plugin as an Object.
  -     * @deprecated Switch plugin to a JMX object an use 'ObjectName 
getPlugin(String name)' instead
  -     * @param logicalPluginName the name of the plugin to get
  -     * @return the actual plugin object
  +     * Get all the Components known to the Container
  +     *
  +     * @return an immutable List of Components
        */
  -    Object getPluginObject(String logicalPluginName);
  +    public List getComponents ();
  +
  +
   
       /**
  -     * Puts the named plugin Object in the container.
  -     * @deprecated Switch plugin to a JMX object an use 'void 
putPlugin(String name, ObjectName objectName)' instead
  -     * @param logicalPluginName the name of the plugin to get
  -     * @param plugin the plugin obect or null to remove an existing plugin
  +     * Remove a Component from the Container.
  +     * If the Component is not in the Container,
  +     * an Exception is thrown.
  +     *
  +     * @param component the Component to remove
  +     * @exception Exception if the Component cannot be removed
        */
  -    void putPluginObject(String logicalPluginName, Object plugin);
  +    public void removeComponent (Component component) throws Exception;
   }
  
  
  
  1.1                  
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/AbstractRPCContainer.java
  
  Index: AbstractRPCContainer.java
  ===================================================================
  package org.apache.geronimo.common;
  
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.LinkedList;
  import java.util.ListIterator;
  import java.util.Map;
  import javax.management.ObjectName;
  /**
   * AbstractRPCContainer.java
   *
   * Base class for a Container that can be remotely invoked.
   * 
   *
   * 
   * @version $Revision: 1.1 $ $Date: 2003/08/15 14:11:26 $
   */
  public class AbstractRPCContainer
        extends AbstractContainer
        implements RPCContainer {
  
        protected final Map plugins = new LinkedHashMap();
        protected final Map pluginObjects = new LinkedHashMap();
        protected final LinkedList interceptors = new LinkedList();
        // for efficency keep a reference to the first interceptor
        protected Interceptor firstInterceptor;
  
        /**
         * Begin the invocation chain.
         *
         * @todo Check that invoke() is illegal unless Container has started
         * @param invocation 
         * @return InvocationResult
         * @exception Exception if an error occurs
         */
        public InvocationResult invoke(Invocation invocation) throws Exception {
  
                if (getStateInstance() != State.RUNNING)
                        throw new IllegalStateException("invoke can only be 
called after the Container has started");
  
                return firstInterceptor.invoke(invocation);
        }
  
        /**
         * Add a Component to this Container.
         *
         * @param component a <code>Component</code> value
         */
        public void addComponent(Component component) {
                if (component == null)
                        return;
  
                if (component instanceof Interceptor) {
                        addInterceptor((Interceptor) component);
                        return;
                }
  
                //Is there a type for Plugins?
  
                throw new IllegalStateException(
                        "Cannot add component of type "
                                + component.getClass()
                                + " to an RPCContainer");
        }
  
        /**
         * Add an Interceptor to the end of the Interceptor list.
         * 
         * @todo Can interceptors be added after the Container is started?
         * @param interceptor 
         */
        public void addInterceptor(Interceptor interceptor) {
  
                if (getStateInstance() != State.STOPPED)
                        throw new IllegalStateException("Interceptors cannot be 
added unless the Container is stopped");
  
                if (firstInterceptor == null) {
                        firstInterceptor = interceptor;
                        interceptors.addLast(interceptor);
                } else {
                        Interceptor lastInterceptor = (Interceptor) 
interceptors.getLast();
                        lastInterceptor.setNext(interceptor);
                        interceptors.addLast(interceptor);
                }
  
                super.addComponent(interceptor);
        }
  
        public ObjectName getPlugin(String logicalPluginName) {
                return (ObjectName) plugins.get(logicalPluginName);
        }
  
        public void putPlugin(String logicalPluginName, ObjectName objectName) {
                if (getStateInstance() != State.STOPPED) {
                        throw new IllegalStateException(
                                "putPluginObject can only be called while in 
the stopped state: state="
                                        + getState());
                }
                plugins.put(logicalPluginName, objectName);
        }
  
        public Object getPluginObject(String logicalPluginName) {
                return pluginObjects.get(logicalPluginName);
        }
  
        public void putPluginObject(String logicalPluginName, Object plugin) {
                if (getStateInstance() != State.STOPPED) {
                        throw new IllegalStateException(
                                "putPluginObject can only be called while in 
the not-created or destroyed states: state="
                                        + getState());
                }
                pluginObjects.put(logicalPluginName, plugin);
        }
  
  
  
        /**
         * Start the Container
         * The Interceptors will be handled by 
         * @exception Exception if an error occurs
         */
        public void doStart() throws Exception {
                // Start all the Plugins in forward insertion order
                for (Iterator iterator = 
pluginObjects.values().iterator();iterator.hasNext();) {
                        Object object = iterator.next();
                        // TODO Start the plugin - are these Components also 
maybe they should just be StateManageable
                }
        }
  
        /**
         * Stop the container
         *
         */
        public void doStop() {
                // Stop all the plugins in reverse insertion order
                LinkedList list = new LinkedList();
                for (Iterator iterator = 
pluginObjects.values().iterator();iterator.hasNext();) {
                        Object object = iterator.next();
                        //TODO work out what has to be done to stop a Plugin
                }
        }
  
  } // AbstractRPCContainer
  
  
  
  1.1                  
incubator-geronimo/modules/core/src/java/org/apache/geronimo/common/RPCContainer.java
  
  Index: RPCContainer.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  package org.apache.geronimo.common;
  
  
  import org.apache.geronimo.common.Interceptor;
  import org.apache.geronimo.common.Invocation;
  import org.apache.geronimo.common.InvocationResult;
  import javax.management.ObjectName;
  
  /**
   *
   *
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/15 14:11:26 $
   */
  public interface RPCContainer extends Container {
      //
      //  Main entry point
      //
      InvocationResult invoke(Invocation invocation) throws Exception;
  
  
      /**
       * Add an interceptor to the interceptor stack
       * @param Interceptor
       */
      void addInterceptor (Interceptor interceptor);
  
      /**
       * Get the JMX object name of the logical plugin.
       * @param logicalPluginName the logical name of the desired plugin
       * @return the JMX object name associated with the logical plugin, or 
null if a name is not found
       */
      ObjectName getPlugin(String logicalPluginName);
  
      /**
       * Puts the objectName in the container.
       * @param logicalPluginName the logical name of the plugin to set
       * @param objectName the JMX object name to set
       */
      void putPlugin(String logicalPluginName, ObjectName objectName);
  
      /**
       * Gets the named plugin as an Object.
       * @deprecated Switch plugin to a JMX object an use 'ObjectName 
getPlugin(String name)' instead
       * @param logicalPluginName the name of the plugin to get
       * @return the actual plugin object
       */
      Object getPluginObject(String logicalPluginName);
  
      /**
       * Puts the named plugin Object in the container.
       * @deprecated Switch plugin to a JMX object an use 'void 
putPlugin(String name, ObjectName objectName)' instead
       * @param logicalPluginName the name of the plugin to get
       * @param plugin the plugin obect or null to remove an existing plugin
       */
      void putPluginObject(String logicalPluginName, Object plugin);
  }
  
  
  

Reply via email to