You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/11/07 10:06:14 UTC

cvs commit: jakarta-avalon/src/java/org/apache/avalon/framework/service WrapperServiceManager.java WrapperServiceSelector.java

donaldp     2002/11/07 01:06:14

  Modified:    src/java/org/apache/avalon/framework/service
                        WrapperServiceManager.java
                        WrapperServiceSelector.java
  Added:       src/java/org/apache/avalon/framework/component
                        WrapperComponentManager.java
                        WrapperComponentSelector.java
  Log:
  Add in wrappers that wrap a ServiceManager in a ComponentManager. If one of the accessed services does not implement the Component interface then an exception is thrown. Other than that it acts in a way similar to the WrapperService*.
  
  Also updated the WrapperService* to use the messages throiwn by wrapped elements.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/component/WrapperComponentManager.java
  
  Index: WrapperComponentManager.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.component;
  
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceSelector;
  
  /**
   *  * This is a {@link ComponentManager} implementation that can wrap around a
   * {@link ServiceManager} object effectively adapting a {@link ServiceManager}
   * interface to a {@link ComponentManager} interface.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/07 09:06:13 $
   */
  public class WrapperComponentManager
      implements ComponentManager
  {
      /**
       * The service manager we are adapting.
       */
      private final ServiceManager m_manager;
  
      public WrapperComponentManager( final ServiceManager manager )
      {
          if( null == manager )
          {
              throw new NullPointerException( "manager" );
          }
  
          m_manager = manager;
      }
  
      /**
       * Retrieve a component via a key.
       *
       * @param key the key
       * @return the component
       * @throws ComponentException if unable to aquire component
       */
      public Component lookup( final String key )
          throws ComponentException
      {
          try
          {
              final Object object = m_manager.lookup( key );
              if( object instanceof ServiceSelector )
              {
                  return new WrapperComponentSelector( key, (ServiceSelector)object );
              }
              else if( object instanceof Component )
              {
                  return (Component)object;
              }
          }
          catch( final ServiceException se )
          {
              throw new ComponentException( se.getRole(), se.getMessage(), se.getCause() );
          }
  
          final String message = "Role does not implement the Component " +
              "interface and thus can not be accessed via ComponentManager";
          throw new ComponentException( key, message );
      }
  
      /**
       * Check to see if a <code>Component</code> exists for a key.
       *
       * @param key  a string identifying the key to check.
       * @return True if the component exists, False if it does not.
       */
      public boolean hasComponent( final String key )
      {
          return m_manager.hasService( key );
      }
  
      /**
       * Return the <code>Component</code> when you are finished with it.  This
       * allows the <code>ComponentManager</code> to handle the End-Of-Life Lifecycle
       * events associated with the Component.  Please note, that no Exceptions
       * should be thrown at this point.  This is to allow easy use of the
       * ComponentManager system without having to trap Exceptions on a release.
       *
       * @param component The Component we are releasing.
       */
      public void release( final Component component )
      {
          m_manager.release( component );
      }
  }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/component/WrapperComponentSelector.java
  
  Index: WrapperComponentSelector.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 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 "Jakarta", "Avalon", and "Apache Software Foundation"
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    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.avalon.framework.component;
  
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.service.ServiceSelector;
  import org.apache.avalon.framework.service.ServiceException;
  
  /**
   * This is a {@link ServiceSelector} implementation that can wrap around a legacy
   * {@link ComponentSelector} object effectively adapting a {@link ComponentSelector}
   * interface to a {@link ServiceSelector} interface.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/11/07 09:06:13 $
   */
  public class WrapperComponentSelector
      implements ComponentSelector
  {
      /**
       * The Selector we are wrapping.
       */
      private final ServiceSelector m_selector;
  
      /**
       * The role that this selector was aquired via.
       */
      private final String m_key;
  
      /**
       * This constructor is a constructor for a WrapperComponentSelector.
       *
       * @param key the key used to aquire this selector
       * @param selector the selector to wrap
       */
      public WrapperComponentSelector( final String key,
                                       final ServiceSelector selector )
      {
          if( null == key )
          {
              throw new NullPointerException( "key" );
          }
          if( null == selector )
          {
              throw new NullPointerException( "selector" );
          }
  
          m_key = key + "/";
          m_selector = selector;
      }
  
      /**
       * Select a Component based on a policy.
       *
       * @param policy the policy
       * @return the Component
       * @throws ComponentException if unable to select service
       */
      public Component select( final Object policy )
          throws ComponentException
      {
          try
          {
              final Object object = m_selector.select( policy );
              if( object instanceof Component )
              {
                  return (Component)object;
              }
          }
          catch( final ServiceException se )
          {
              throw new ComponentException( m_key + policy, se.getMessage(), se );
          }
  
          final String message = "Role does not implement the Component " +
              "interface and thus can not be accessed via ComponentSelector";
          throw new ComponentException( m_key + policy, message );
      }
  
      /**
       * Check to see if a {@link Component} exists relative to the supplied policy.
       *
       * @param policy a {@link Object} containing the selection criteria
       * @return True if the component is available, False if it not.
       */
      public boolean hasComponent( final Object policy )
      {
          return m_selector.isSelectable( policy );
      }
  
      /**
       * Return the {@link Object} when you are finished with it.  This
       * allows the {@link ServiceSelector} to handle the End-Of-Life Lifecycle
       * events associated with the {@link Object}.  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 {@link Object} we are releasing.
       */
      public void release( final Component object )
      {
          m_selector.release( object );
      }
  }
  
  
  
  1.2       +2 -3      jakarta-avalon/src/java/org/apache/avalon/framework/service/WrapperServiceManager.java
  
  Index: WrapperServiceManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/service/WrapperServiceManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WrapperServiceManager.java	7 Nov 2002 08:29:04 -0000	1.1
  +++ WrapperServiceManager.java	7 Nov 2002 09:06:13 -0000	1.2
  @@ -116,8 +116,7 @@
           }
           catch( final ComponentException ce )
           {
  -            final String message = "Could not return a reference to the Component";
  -            throw new ServiceException( key, message, ce );
  +            throw new ServiceException( key, ce.getMessage(), ce );
           }
       }
   
  
  
  
  1.2       +8 -9      jakarta-avalon/src/java/org/apache/avalon/framework/service/WrapperServiceSelector.java
  
  Index: WrapperServiceSelector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/service/WrapperServiceSelector.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WrapperServiceSelector.java	7 Nov 2002 08:29:04 -0000	1.1
  +++ WrapperServiceSelector.java	7 Nov 2002 09:06:13 -0000	1.2
  @@ -79,27 +79,27 @@
       /**
        * The role that this selector was aquired via.
        */
  -    private final String m_role;
  +    private final String m_key;
   
       /**
        * This constructor is a constructor for a ComponentServiceManager
        *
  -     * @param role the role used to aquire this selector
  +     * @param key the key used to aquire this selector
        * @param selector the selector to wrap
        */
  -    public WrapperServiceSelector( final String role,
  +    public WrapperServiceSelector( final String key,
                                      final ComponentSelector selector )
       {
  -        if( null == role )
  +        if( null == key )
           {
  -            throw new NullPointerException( "role" );
  +            throw new NullPointerException( "key" );
           }
           if( null == selector )
           {
               throw new NullPointerException( "selector" );
           }
   
  -        m_role = role + "/";
  +        m_key = key + "/";
           m_selector = selector;
       }
   
  @@ -119,8 +119,7 @@
           }
           catch( final ComponentException ce )
           {
  -            final String message = "Could not return a reference to the Component";
  -            throw new ServiceException( m_role + policy, message, ce );
  +            throw new ServiceException( m_key + policy, ce.getMessage(), ce );
           }
       }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>