bloritsch    2003/02/27 12:04:54

  Modified:    fortress/src/java/org/apache/avalon/fortress/impl/lookup
                        FortressServiceManager.java
                        FortressServiceSelector.java
  Added:       fortress/src/java/org/apache/avalon/fortress/impl/lookup
                        ComponentKey.java
  Log:
  make sure the component key hack is properly applied to both selector and 
manager
  
  Revision  Changes    Path
  1.7       +223 -242  
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/lookup/FortressServiceManager.java
  
  Index: FortressServiceManager.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/lookup/FortressServiceManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FortressServiceManager.java       25 Feb 2003 16:28:31 -0000      1.6
  +++ FortressServiceManager.java       27 Feb 2003 20:04:53 -0000      1.7
  @@ -1,242 +1,223 @@
  -/*
  -
  - ============================================================================
  -                   The Apache Software License, Version 1.1
  - ============================================================================
  -
  - Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without modifica-
  - tion, 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", "Excalibur" 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
  -    [EMAIL PROTECTED]
  -
  - 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 (INCLU-
  - DING, 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.fortress.impl.lookup;
  -
  -import java.util.Map;
  -
  -import org.apache.avalon.fortress.Container;
  -import org.apache.avalon.fortress.impl.AbstractContainer;
  -import org.apache.avalon.fortress.impl.handler.ComponentHandler;
  -import org.apache.avalon.framework.component.ComponentSelector;
  -import org.apache.avalon.framework.service.ServiceException;
  -import org.apache.avalon.framework.service.ServiceManager;
  -import org.apache.avalon.framework.service.ServiceSelector;
  -import org.apache.avalon.framework.service.WrapperServiceSelector;
  -import org.apache.commons.collections.StaticBucketMap;
  -
  -/**
  - * This is the Default ServiceManager for the Container.  It provides
  - * a very simple abstraction, and makes it easy for the Container to manage
  - * the references.
  - *
  - * @author <a href="mailto:dev@avalon.apache.org";>Avalon Development Team</a>
  - * @version CVS $Revision$ $Date$
  - */
  -public class FortressServiceManager
  -    implements ServiceManager
  -{
  -    private final Container m_container;
  -    private final Map m_used;
  -    private final ServiceManager m_parent;
  -
  -    /**
  -     * This constructor is for a ContainerComponentManager with a parent
  -     * ComponentLocator
  -     * @param container the impl
  -     * @param parent the parent service manager
  -     * @exception NullPointerException if the supplied impl is null
  -     * @exception NullPointerException if the supplied parent is null
  -     */
  -    public FortressServiceManager( final Container container,
  -                                   final ServiceManager parent ) throws 
NullPointerException
  -    {
  -        if( null == container )
  -        {
  -            throw new NullPointerException( "impl" );
  -        }
  -        if( null == parent )
  -        {
  -            throw new NullPointerException( "parent" );
  -        }
  -
  -        m_parent = parent;
  -        m_container = container;
  -        m_used = new StaticBucketMap();
  -    }
  -
  -    public Object lookup( final String role )
  -        throws ServiceException
  -    {
  -        Lookup lookup = parseRole(role);
  -
  -        if( !m_container.has( lookup.role, lookup.hint ) )
  -        {
  -            return m_parent.lookup( role );
  -        }
  -
  -        final Object result = m_container.get( lookup.role, lookup.hint );
  -        if( result instanceof ServiceSelector )
  -        {
  -            return result;
  -        }
  -
  -        if( result instanceof ComponentSelector )
  -        {
  -            return new WrapperServiceSelector( lookup.role, 
(ComponentSelector)result );
  -        }
  -
  -        if( !( result instanceof ComponentHandler ) )
  -        {
  -            final String message = "Invalid entry in component manager";
  -            throw new ServiceException( role, message );
  -        }
  -
  -        try
  -        {
  -            final ComponentHandler handler = (ComponentHandler)result;
  -            final Object component = handler.get();
  -            
  -            m_used.put( new ComponentKey(component), handler );
  -            return component;
  -        }
  -        catch( final ServiceException ce )
  -        {
  -            throw ce; // rethrow
  -        }
  -        catch( final Exception e )
  -        {
  -            final String message =
  -                "Could not return a reference to the Component";
  -            throw new ServiceException( role, message, e );
  -        }
  -    }
  -
  -    public boolean hasService( final String role )
  -    {
  -        Lookup lookup = parseRole( role );
  -
  -        if( m_container.has( lookup.role, lookup.hint ) )
  -        {
  -            return true;
  -        }
  -        else
  -        {
  -            return null != m_parent ? m_parent.hasService( role ) : false;
  -        }
  -    }
  -
  -    public void release( final Object component )
  -    {
  -        final ComponentHandler handler = (ComponentHandler)m_used.remove( 
new ComponentKey(component) );
  -        if( null == handler )
  -        {
  -            if( null == m_parent )
  -            {
  -                /* This is a purplexing problem.  SOmetimes the m_used hash
  -                 * returns null for the component--usually a ThreadSafe
  -                 * component.  When there is no handler and no parent, that
  -                 * is an error condition--but if the component is usually
  -                 * ThreadSafe, the impact is essentially nill.
  -                 */
  -                //Pete: This occurs when objects are released more often than
  -                //when they are aquired
  -                //Pete: It also happens when a release of a 
ComponentSelector occurs
  -            }
  -            else
  -            {
  -                m_parent.release( component );
  -            }
  -        }
  -        else
  -        {
  -            handler.put( component );
  -        }
  -    }
  -
  -    private Lookup parseRole( String role )
  -    {
  -        Lookup lookup = new Lookup();
  -        lookup.role = role;
  -        lookup.hint = AbstractContainer.DEFAULT_ENTRY;
  -
  -        if ( role.endsWith("Selector") )
  -        {
  -            lookup.role = role.substring(0, role.length() - 
"Selector".length());
  -            lookup.hint = AbstractContainer.SELECTOR_ENTRY;
  -        }
  -
  -        int index = role.lastIndexOf("/");
  -
  -        // needs to be further than the first character
  -        if ( index > 0 )
  -        {
  -            lookup.role = role.substring(0, index);
  -            lookup.hint = role.substring(index + 1);
  -        }
  -
  -        return lookup;
  -    }
  -
  -    private final static class Lookup
  -    {
  -        public String role;
  -        public String hint;
  -    }
  -    
  -    private final static class ComponentKey
  -    {
  -        private final Object component;
  -        
  -        public ComponentKey( Object component )
  -        {
  -            this.component = component;
  -        }
  -        
  -        public boolean equals( Object other )
  -        {
  -            return (other instanceof ComponentKey) && ((ComponentKey) 
other).component == this.component;
  -        }
  -        
  -        public int hashCode()
  -        {
  -            return component.hashCode();
  -        }
  -    }
  -}
  +/*
  +
  + ============================================================================
  +                   The Apache Software License, Version 1.1
  + ============================================================================
  +
  + Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  +
  + Redistribution and use in source and binary forms, with or without modifica-
  + tion, 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", "Excalibur" 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
  +    [EMAIL PROTECTED]
  +
  + 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 (INCLU-
  + DING, 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.fortress.impl.lookup;
  +
  +import java.util.Map;
  +
  +import org.apache.avalon.fortress.Container;
  +import org.apache.avalon.fortress.impl.AbstractContainer;
  +import org.apache.avalon.fortress.impl.handler.ComponentHandler;
  +import org.apache.avalon.framework.component.ComponentSelector;
  +import org.apache.avalon.framework.service.ServiceException;
  +import org.apache.avalon.framework.service.ServiceManager;
  +import org.apache.avalon.framework.service.ServiceSelector;
  +import org.apache.avalon.framework.service.WrapperServiceSelector;
  +import org.apache.commons.collections.StaticBucketMap;
  +
  +/**
  + * This is the Default ServiceManager for the Container.  It provides
  + * a very simple abstraction, and makes it easy for the Container to manage
  + * the references.
  + *
  + * @author <a href="mailto:dev@avalon.apache.org";>Avalon Development Team</a>
  + * @version CVS $Revision$ $Date$
  + */
  +public class FortressServiceManager
  +    implements ServiceManager
  +{
  +    private final Container m_container;
  +    private final Map m_used;
  +    private final ServiceManager m_parent;
  +
  +    /**
  +     * This constructor is for a ContainerComponentManager with a parent
  +     * ComponentLocator
  +     * @param container the impl
  +     * @param parent the parent service manager
  +     * @exception NullPointerException if the supplied impl is null
  +     * @exception NullPointerException if the supplied parent is null
  +     */
  +    public FortressServiceManager( final Container container,
  +                                   final ServiceManager parent ) throws 
NullPointerException
  +    {
  +        if( null == container )
  +        {
  +            throw new NullPointerException( "impl" );
  +        }
  +        if( null == parent )
  +        {
  +            throw new NullPointerException( "parent" );
  +        }
  +
  +        m_parent = parent;
  +        m_container = container;
  +        m_used = new StaticBucketMap();
  +    }
  +
  +    public Object lookup( final String role )
  +        throws ServiceException
  +    {
  +        Lookup lookup = parseRole(role);
  +
  +        if( !m_container.has( lookup.role, lookup.hint ) )
  +        {
  +            return m_parent.lookup( role );
  +        }
  +
  +        final Object result = m_container.get( lookup.role, lookup.hint );
  +        if( result instanceof ServiceSelector )
  +        {
  +            return result;
  +        }
  +
  +        if( result instanceof ComponentSelector )
  +        {
  +            return new WrapperServiceSelector( lookup.role, 
(ComponentSelector)result );
  +        }
  +
  +        if( !( result instanceof ComponentHandler ) )
  +        {
  +            final String message = "Invalid entry in component manager";
  +            throw new ServiceException( role, message );
  +        }
  +
  +        try
  +        {
  +            final ComponentHandler handler = (ComponentHandler)result;
  +            final Object component = handler.get();
  +            
  +            m_used.put( new ComponentKey(component), handler );
  +            return component;
  +        }
  +        catch( final ServiceException ce )
  +        {
  +            throw ce; // rethrow
  +        }
  +        catch( final Exception e )
  +        {
  +            final String message =
  +                "Could not return a reference to the Component";
  +            throw new ServiceException( role, message, e );
  +        }
  +    }
  +
  +    public boolean hasService( final String role )
  +    {
  +        Lookup lookup = parseRole( role );
  +
  +        if( m_container.has( lookup.role, lookup.hint ) )
  +        {
  +            return true;
  +        }
  +        else
  +        {
  +            return null != m_parent ? m_parent.hasService( role ) : false;
  +        }
  +    }
  +
  +    public void release( final Object component )
  +    {
  +        final ComponentHandler handler = (ComponentHandler)m_used.remove( 
new ComponentKey(component) );
  +        if( null == handler )
  +        {
  +            if( null == m_parent )
  +            {
  +                /* This is a purplexing problem.  SOmetimes the m_used hash
  +                 * returns null for the component--usually a ThreadSafe
  +                 * component.  When there is no handler and no parent, that
  +                 * is an error condition--but if the component is usually
  +                 * ThreadSafe, the impact is essentially nill.
  +                 */
  +                //Pete: This occurs when objects are released more often than
  +                //when they are aquired
  +                //Pete: It also happens when a release of a 
ComponentSelector occurs
  +            }
  +            else
  +            {
  +                m_parent.release( component );
  +            }
  +        }
  +        else
  +        {
  +            handler.put( component );
  +        }
  +    }
  +
  +    private Lookup parseRole( String role )
  +    {
  +        Lookup lookup = new Lookup();
  +        lookup.role = role;
  +        lookup.hint = AbstractContainer.DEFAULT_ENTRY;
  +
  +        if ( role.endsWith("Selector") )
  +        {
  +            lookup.role = role.substring(0, role.length() - 
"Selector".length());
  +            lookup.hint = AbstractContainer.SELECTOR_ENTRY;
  +        }
  +
  +        int index = role.lastIndexOf("/");
  +
  +        // needs to be further than the first character
  +        if ( index > 0 )
  +        {
  +            lookup.role = role.substring(0, index);
  +            lookup.hint = role.substring(index + 1);
  +        }
  +
  +        return lookup;
  +    }
  +
  +    private final static class Lookup
  +    {
  +        public String role;
  +        public String hint;
  +    }
  +    
  +}
  
  
  
  1.5       +154 -154  
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/lookup/FortressServiceSelector.java
  
  Index: FortressServiceSelector.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/lookup/FortressServiceSelector.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FortressServiceSelector.java      25 Feb 2003 16:28:31 -0000      1.4
  +++ FortressServiceSelector.java      27 Feb 2003 20:04:53 -0000      1.5
  @@ -1,154 +1,154 @@
  -/*
  -
  - ============================================================================
  -                   The Apache Software License, Version 1.1
  - ============================================================================
  -
  - Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without modifica-
  - tion, 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", "Excalibur" 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
  -    [EMAIL PROTECTED]
  -
  - 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 (INCLU-
  - DING, 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.fortress.impl.lookup;
  -
  -import java.util.Map;
  -
  -import org.apache.avalon.fortress.Container;
  -import org.apache.avalon.fortress.impl.handler.ComponentHandler;
  -import org.apache.avalon.framework.service.ServiceException;
  -import org.apache.avalon.framework.service.ServiceSelector;
  -import org.apache.commons.collections.StaticBucketMap;
  -
  -/**
  - * This is the Default ServiceSelector for the Container.  It provides
  - * a very simple abstraction, and makes it easy for the Container to manage
  - * the references.
  - *
  - * @author <a href="mailto:dev@avalon.apache.org";>Avalon Development Team</a>
  - * @version CVS $Revision$ $Date$
  - */
  -public class FortressServiceSelector
  -    implements ServiceSelector
  -{
  -    private final String m_key;
  -    private final Container m_container;
  -    private final Map m_used;
  -
  -   /**
  -    * Creation of  new service selector.
  -    * @param container the impl
  -    * @param key a key
  -    */
  -    public FortressServiceSelector( final Container container,
  -                                    final String key )
  -    {
  -        if( null == container )
  -        {
  -            throw new NullPointerException( "impl" );
  -        }
  -        if( null == key )
  -        {
  -            throw new NullPointerException( "key" );
  -        }
  -
  -        m_key = key;
  -        m_container = container;
  -        m_used = new StaticBucketMap();
  -    }
  -
  -    public Object select( final Object hint )
  -        throws ServiceException
  -    {
  -        try
  -        {
  -            final ComponentHandler handler = getHandler( hint );
  -            final Object component = handler.get();
  -            m_used.put( component.toString(), handler );
  -            return component;
  -        }
  -        catch( final ServiceException ce )
  -        {
  -            throw ce; // rethrow
  -        }
  -        catch( final Exception e )
  -        {
  -            final String name = m_key + "/" + hint.toString();
  -            final String message = "Could not return a reference to the 
Component";
  -            throw new ServiceException( name, message, e );
  -        }
  -    }
  -
  -    public boolean isSelectable( final Object hint )
  -    {
  -        return m_container.has( m_key, hint );
  -    }
  -
  -    public void release( Object component )
  -    {
  -        final ComponentHandler handler =
  -            (ComponentHandler)m_used.remove( component.toString() );
  -        if( null != handler )
  -        {
  -            handler.put( component );
  -        }
  -    }
  -
  -    private ComponentHandler getHandler( final Object hint )
  -        throws ServiceException
  -    {
  -        if( null == hint )
  -        {
  -            final String message = "hint cannot be null";
  -            throw new IllegalArgumentException( message );
  -        }
  -
  -        final ComponentHandler handler =
  -            (ComponentHandler)m_container.get( m_key, hint );
  -        if( null == handler )
  -        {
  -            final String message =
  -                "The hint does not exist in the ComponentSelector";
  -            throw new ServiceException( m_key + "/" + hint.toString(),
  -                                        message );
  -        }
  -        return handler;
  -    }
  -}
  +/*
  +
  + ============================================================================
  +                   The Apache Software License, Version 1.1
  + ============================================================================
  +
  + Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  +
  + Redistribution and use in source and binary forms, with or without modifica-
  + tion, 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", "Excalibur" 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
  +    [EMAIL PROTECTED]
  +
  + 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 (INCLU-
  + DING, 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.fortress.impl.lookup;
  +
  +import java.util.Map;
  +
  +import org.apache.avalon.fortress.Container;
  +import org.apache.avalon.fortress.impl.handler.ComponentHandler;
  +import org.apache.avalon.framework.service.ServiceException;
  +import org.apache.avalon.framework.service.ServiceSelector;
  +import org.apache.commons.collections.StaticBucketMap;
  +
  +/**
  + * This is the Default ServiceSelector for the Container.  It provides
  + * a very simple abstraction, and makes it easy for the Container to manage
  + * the references.
  + *
  + * @author <a href="mailto:dev@avalon.apache.org";>Avalon Development Team</a>
  + * @version CVS $Revision$ $Date$
  + */
  +public class FortressServiceSelector
  +    implements ServiceSelector
  +{
  +    private final String m_key;
  +    private final Container m_container;
  +    private final Map m_used;
  +
  +   /**
  +    * Creation of  new service selector.
  +    * @param container the impl
  +    * @param key a key
  +    */
  +    public FortressServiceSelector( final Container container,
  +                                    final String key )
  +    {
  +        if( null == container )
  +        {
  +            throw new NullPointerException( "impl" );
  +        }
  +        if( null == key )
  +        {
  +            throw new NullPointerException( "key" );
  +        }
  +
  +        m_key = key;
  +        m_container = container;
  +        m_used = new StaticBucketMap();
  +    }
  +
  +    public Object select( final Object hint )
  +        throws ServiceException
  +    {
  +        try
  +        {
  +            final ComponentHandler handler = getHandler( hint );
  +            final Object component = handler.get();
  +            m_used.put( new ComponentKey(component), handler );
  +            return component;
  +        }
  +        catch( final ServiceException ce )
  +        {
  +            throw ce; // rethrow
  +        }
  +        catch( final Exception e )
  +        {
  +            final String name = m_key + "/" + hint.toString();
  +            final String message = "Could not return a reference to the 
Component";
  +            throw new ServiceException( name, message, e );
  +        }
  +    }
  +
  +    public boolean isSelectable( final Object hint )
  +    {
  +        return m_container.has( m_key, hint );
  +    }
  +
  +    public void release( Object component )
  +    {
  +        final ComponentHandler handler =
  +            (ComponentHandler)m_used.remove( new ComponentKey(component) );
  +        if( null != handler )
  +        {
  +            handler.put( component );
  +        }
  +    }
  +
  +    private ComponentHandler getHandler( final Object hint )
  +        throws ServiceException
  +    {
  +        if( null == hint )
  +        {
  +            final String message = "hint cannot be null";
  +            throw new IllegalArgumentException( message );
  +        }
  +
  +        final ComponentHandler handler =
  +            (ComponentHandler)m_container.get( m_key, hint );
  +        if( null == handler )
  +        {
  +            final String message =
  +                "The hint does not exist in the ComponentSelector";
  +            throw new ServiceException( m_key + "/" + hint.toString(),
  +                                        message );
  +        }
  +        return handler;
  +    }
  +}
  
  
  
  1.1                  
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/lookup/ComponentKey.java
  
  Index: ComponentKey.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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", "Excalibur" 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
      [EMAIL PROTECTED]
  
   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 (INCLU-
   DING, 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.fortress.impl.lookup;
  
  /**
   * Hack that is temporarily necessary until the next version of
   * Commons Collections is released.
   */
  final class ComponentKey
  {
      private final Object component;
      
      public ComponentKey( Object component )
      {
          this.component = component;
      }
      
      public boolean equals( Object other )
      {
          return (other instanceof ComponentKey) && ((ComponentKey) 
other).component == this.component;
      }
      
      public int hashCode()
      {
          return component.hashCode();
      }
  }
  
  

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

Reply via email to