bloritsch    2002/09/09 08:25:22

  Added:       container/src/java/org/apache/excalibur/container/lookup
                        AbstractServiceManager.java
  Log:
  add the abstract ServiceManager that allows resolution against a public JNDI bound 
service
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/container/src/java/org/apache/excalibur/container/lookup/AbstractServiceManager.java
  
  Index: AbstractServiceManager.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.container.lookup;
  
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  /**
   * Abstract implementation of a <code>ServiceManager</code>.  This will
   * make it easier to implement ServiceManagers that use exposed management
   * services, as well as respect the hierarchical nature of ServiceManagers.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Berin Loritsch</a>
   */
  public abstract class AbstractServiceManager implements ServiceManager
  {
      private final ServiceManager m_parent;
      private final Context        m_context;
  
      public AbstractServiceManager()
      {
          this( null, null );
      }
  
      public AbstractServiceManager( ServiceManager parent )
      {
          this( parent, null );
      }
  
      public AbstractServiceManager( ServiceManager parent, Context initContext )
      {
          m_parent = parent;
  
          Context checkContext = initContext;
  
          if ( null == checkContext )
          {
              // If there is a public context for services use it.
              try
              {
                  checkContext = new InitialContext();
              }
              catch ( NamingException ne )
              {
                  checkContext = null;
              }
          }
  
          m_context = checkContext;
      }
  
      protected abstract boolean serviceExists( String role );
      protected abstract Object obtainService( String role );
      public abstract void release( Object service );
  
      /**
       * The logic for testing if a service exists.  First we test ourselves,
       * second we test the parent ServiceManager if it exists, and lastly
       * we test the JNDI context if it exists.
       */
      public boolean hasService( String role )
      {
          boolean exists = serviceExists( role );
  
          if ( ! exists && null != m_parent )
          {
              exists = m_parent.hasService( role );
          }
  
          if ( ! exists && null != m_context )
          {
              exists = ( null == obtainServiceFromJNDI( role ) );
          }
  
          return exists;
      }
  
      /**
       * The logic for obtaining a service from a ServiceManager.  First we
       * try to get it from this ServiceManager, then we try to get it from the
       * parent manager.  If it exists, we will lastly try the JNDI context.
       */
      public Object lookup( String role )
          throws ServiceException
      {
          Object service = obtainService( role );
  
          if ( null == service && null != m_parent )
          {
              service = obtainServiceFromParent( role );
          }
  
          if ( null == service && null != m_context )
          {
              service = obtainServiceFromJNDI( role );
          }
  
          if ( null == service )
          {
              throw new ServiceException( role, "Service does not exist." );
          }
  
          return service;
      }
  
      /**
       * Grabs the service from the parent.  If it does not exist, we
       * don't even try.
       */
      protected Object obtainServiceFromParent( String role )
      {
          Object service = null;
  
          if ( m_parent.hasService( role ) )
          {
              try
              {
                  service = m_parent.lookup( role );
              }
              catch ( ServiceException se )
              {
                  // ignore, it is already null
              }
          }
  
          return service;
      }
  
      /**
       * Grabs the service from the JNDI role.  Unfortunately, there is
       * no way to test the context before we grab it.  As a result we
       * have to incur the overhead of a lookup/exception if it is not
       * bound.
       */
      protected Object obtainServiceFromJNDI( String role )
      {
          Object service = null;
  
          try
          {
              service = m_context.lookup( role );
          }
          catch ( NamingException se )
          {
              // ignore, it is already null
          }
  
          return service;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to