avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: jakarta-avalon/src/java/org/apache/avalon/framework/service DefaultServiceManager.java ServiceException.java ServiceManager.java ServiceSelector.java Serviceable.java package.html
Date Thu, 21 Feb 2002 04:36:35 GMT
mcconnell    02/02/20 20:36:35

  Added:       src/java/org/apache/avalon/framework/service
                        DefaultServiceManager.java ServiceException.java
                        ServiceManager.java ServiceSelector.java
                        Serviceable.java package.html
  Log:
  initial entry of Service package
  
  Revision  Changes    Path
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/DefaultServiceManager.java
  
  Index: DefaultServiceManager.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.avalon.framework.service;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  /**
   * This class is a static implementation of a ServiceManager. Allow ineritance
   * and extension so you can generate a tree of ServiceManager each defining
   * Object scope.
   *
   * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  public class DefaultServiceManager
      implements ServiceManager
  {
      private final HashMap               m_components = new HashMap();
      private final ServiceManager        m_parent;
      private boolean                     m_readOnly;
  
      /**
       * Construct ServiceManager with no parent.
       *
       */
      public DefaultServiceManager()
      {
          this( null );
      }
  
      /**
       * Construct ServiceManager with specified parent.
       *
       * @param parent the ServiceManager parent
       */
      public DefaultServiceManager( final ServiceManager parent )
      {
          m_parent = parent;
      }
  
      /**
       * Retrieve Object by role from ServiceManager.
       *
       * @param role the role
       * @return the Object
       * @exception ServiceException if an error occurs
       */
      public Object lookup( final String role )
          throws ServiceException
      {
          final Object component = m_components.get( role );
  
          if( null != component )
          {
              return component;
          }
          else if( null != m_parent )
          {
              return m_parent.lookup( role );
          }
          else
          {
              throw new ServiceException( "Unable to provide implementation for " + role );
          }
      }
  
      public boolean hasService( final String role ) {
          boolean componentExists = false;
  
          try
          {
              this.lookup(role);
              componentExists = true;
          }
          catch (Throwable t)
          {
              // Ignore all throwables--we want a yes or no answer.
          }
          return componentExists;
      }
  
      /**
       * Place Component into ComponentManager.
       *
       * @param role the components role
       * @param component the component
       */
      public void put( final String role, final Object object )
      {
          checkWriteable();
          m_components.put( role, object );
      }
  
      /**
       * Build a human readable representation of ComponentManager2.
       *
       * @return the description of ComponentManager2
       */
      public String toString()
      {
          final StringBuffer buffer = new StringBuffer();
          final Iterator components = m_components.keySet().iterator();
          buffer.append( "Services:" );
  
          while( components.hasNext() )
          {
              buffer.append( "[" );
              buffer.append( components.next() );
              buffer.append( "]" );
          }
  
          return buffer.toString();
      }
  
      /**
       * Helper method for subclasses to retrieve parent.
       *
       * @return the parent ServiceManager
       */
      protected final ServiceManager getParent()
      {
          return m_parent;
      }
  
      /**
       * Helper method for subclasses to retrieve component map.
       *
       * @return the component map
       */
      protected final Map getComponentMap()
      {
          return m_components;
      }
  
      public void makeReadOnly()
      {
          m_readOnly = true;
      }
  
      protected final void checkWriteable()
          throws IllegalStateException
      {
          if( m_readOnly )
          {
              throw new IllegalStateException( "ServiceManager is read only and can not be
modified" );
          }
      }
  
      /**
       * Release the object.
       * @param object The <code>Object</code> to release.
       */
      public void release( Object object ){}
  
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/ServiceException.java
  
  Index: ServiceException.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.avalon.framework.service;
  
  import org.apache.avalon.framework.CascadingException;
  
  /**
   * The exception thrown to indicate a problem with service.
   * It is usually thrown by ServiceManager or ServiceSelector.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  public class ServiceException
      extends CascadingException
  {
      /**
       * Construct a new <code>ServiceException</code> instance.
       *
       * @param message the exception message
       * @param throwable the throwable
       */
      public ServiceException( final String message, final Throwable throwable )
      {
          super( message, throwable );
      }
  
      /**
       * Construct a new <code>ServiceException</code> instance.
       *
       * @param message the exception message
       */
      public ServiceException( final String message )
      {
          super( message, null );
      }
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/ServiceManager.java
  
  Index: ServiceManager.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.avalon.framework.service;
  
  /**
   * A <code>ServiceManager</code> selects <code>Object</code>s based
on a
   * role.  The contract is that all the <code>Object</code>s implement the
   * differing roles and there is one <code>Object</code> per role.  If you
   * need to select on of many <code>Object</code>s that implement the same
   * role, then you need to use a <code>ServiceSelector</code>.  Roles are
   * usually the full interface name.
   *
   * A role is better understood by the analogy of a play.  There are many
   * different roles in a script.  Any actor or actress can play any given part
   * and you get the same results (phrases said, movements made, etc.).  The exact
   * nuances of the performance is different.
   *
   * Below is a list of things that might be considered the different roles:
   *
   * <ul>
   *   <li> InputAdaptor and OutputAdaptor</li>
   *   <li> Store and Spool</li>
   * </ul>
   *
   * The <code>ServiceManager</code> does not specify the methodology of
   * getting the <code>Object</code>, merely the interface used to get it.
   * Therefore the <code>ServiceManager</code> can be implemented with a
   * factory pattern, an object pool, or a simple Hashtable.
   *
   * @see org.apache.avalon.framework.service.Serviceable
   * @see org.apache.avalon.framework.service.ServiceSelector
   *
   * @author <a href="mailto:scoobie@betaversion.org">Federico Barbieri</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  public interface ServiceManager
  {
      /**
       * Get the <code>Object</code> associated with the given role.  For
       * instance, If the <code>ServiceManager</code> had a
       * <code>LoggerComponent</code> stored and referenced by role, I would use
       * the following call:
       * <pre>
       * try
       * {
       *     MyComponent log;
       *     myComponent = (MyComponent) manager.lookup(MyComponent.ROLE);
       * }
       * catch (...)
       * {
       *     ...
       * }
       * </pre>
       *
       * @param name The role name of the <code>Object</code> to retrieve.
       * @exception ServiceException if an error occurs
       */
      Object lookup( String role )
          throws ServiceException;
  
      /**
       * Check to see if a <code>Object</code> exists for a role.
       *
       * @param role  a string identifying the role to check.
       * @return True if the object exists, False if it does not.
       */
      boolean hasService( String role );
  
      /**
       * Return the <code>Object</code> when you are finished with it.  This
       * allows the <code>ServiceManager</code> to handle the End-Of-Life Lifecycle
       * events associated with the <code>Object</code>.  Please note, that no

       * Exception should be thrown at this point.  This is to allow easy use of the
       * ServiceManager system without having to trap Exceptions on a release.
       *
       * @param object The <code>Object</code> we are releasing.
       */
      void release( Object object );
  
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/ServiceSelector.java
  
  Index: ServiceSelector.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.avalon.framework.service;
  
  /**
   * A <code>ServiceSelector</code> selects <code>Object</code>s based
on a
   * supplied policy.  The contract is that all the <code>Object</code>s implement
the
   * same role.
   *
   * @see org.apache.avalon.framework.service.Serviceable
   * @see org.apache.avalon.framework.service.ServiceManager
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  public interface ServiceSelector
  {
      /**
       * Select the <code>Object</code> associated with the given policy.
       * For instance, If the <code>ServiceSelector</code> has a
       * <code>Generator</code> stored and referenced by a URL, I would use the
       * following call:
       *
       * <pre>
       * try
       * {
       *     Generator input;
       *     input = (Generator)selector.select( new URL("foo://demo/url") );
       * }
       * catch (...)
       * {
       *     ...
       * }
       * </pre>
       *
       * @param policy A criteria against which a <code>Object</code> is selected.
       *
       * @exception ServiceSelector If the requested <code>Object</code> cannot
be supplied
       */
      Object select( Object policy )
          throws ServiceException;
  
      /**
       * Check to see if a <code>Object</code> exists relative to the supplied
policy.
       *
       * @param policy a <code>Object</code> containing the selection criteria
       * @return True if the component is available, False if it not.
       */
      boolean isSelectable( Object policy );
  
      /**
       * Return the <code>Object</code> when you are finished with it.  This
       * allows the <code>ServiceSelector</code> to handle the End-Of-Life Lifecycle
       * events associated with the <code>Object</code>.  Please note, that no

       * Exception should be thrown at this point.  This is to allow easy use of the
       * ServiceSelector system without having to trap Exceptions on a release.
       *
       * @param object The <code>Object</code> we are releasing.
       */
      void release( Object object );
  
  
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/Serviceable.java
  
  Index: Serviceable.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.avalon.framework.service;
  
  /**
   * A servicable is a class that need to connect to software components using
   * a "role" abstraction, thus not depending on particular implementations
   * but on behavioral interfaces.
   * <br />
   *
   * The contract surrounding a <code>Serviceable</code> is that it is a user.
   * The <code>Serviceable</code> is able to use <code>Object</code>s
managed
   * by the <code>ServiceManager</code> it was initialized with.  As part
   * of the contract with the system, the instantiating entity must call
   * the <code>compose</code> method before the <code>Serviceable</code>

   * can be considered valid. 
   *
   * @see org.apache.avalon.framework.service.ServiceManager
   * @see org.apache.avalon.framework.service.ServiceResolver
   *
   * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
   * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   */
  public interface Serviceable
  {
      /**
       * Pass the <code>ServiceManager</code> to the <code>servicable</code>.
       * The <code>Servicable</code> implementation should use the specified
       * <code>ServiceManager</code> to acquire the components it needs for
       * execution.
       *
       * @param manager The <code>ServiceManager</code> which this
       *                <code>Servicable</code> uses.
       */
      void service( ServiceManager manager )
          throws ServiceException;
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/service/package.html
  
  Index: package.html
  ===================================================================
  <body>
  Interfaces and default implementation of a service management framework supporting container
based service lookup and decommissioning. 
  </body>
  
  
  

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