mcconnell 2002/12/16 21:00:08 Added: assembly/src/java/org/apache/avalon/assembly/lifecycle/composition StandardServiceManager.java Removed: assembly/src/java/org/apache/avalon/assembly/lifecycle/composition DefaultServiceManager.java Log: DefaultServiceManager renamed to StandardServiceManager. Revision Changes Path 1.1 avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/lifecycle/composition/StandardServiceManager.java Index: StandardServiceManager.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 [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 (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.assembly.lifecycle.composition; import java.util.Hashtable; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.service.ServiceException; import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.assembly.appliance.Appliance; import org.apache.avalon.meta.info.DependencyDescriptor; /** * Helper class the implements the <code>ServiceManager</code> interface and * is supplied to componets during lifecyle processing. * @author Stephen McConnell <[EMAIL PROTECTED]> */ public class StandardServiceManager extends AbstractLogEnabled implements ServiceManager { //======================================================================== // state //======================================================================== /** * A map of resources keyed by lookup role. */ private Appliance m_appliance; /** * A table of appliance instances keyed by the object that was provided * by the appliance so that we can locate the correct appliance when * releasing the object. */ private Hashtable m_table = new Hashtable(); //======================================================================== // constructor //======================================================================== /** * Construct a new ServiceManager. * @param profile the component profile */ public StandardServiceManager( Appliance appliance ) { m_appliance = appliance; } //======================================================================== // ServiceManager //======================================================================== /** * Returns true if a provider exists for the supplied role. * @param role the service identifier * @return boolean TRUE if the service is available else FALSE */ public boolean hasService( String role ) { return ( m_appliance.getServiceProvider( role ) != null ); } /** * Retrieve Object by role from ComponentLocator. * @param role the role * @return the Object * @throws ServiceException if an error occurs */ public Object lookup( String role ) throws ServiceException { if( role == null ) { throw new NullPointerException( "role" ); } DependencyDescriptor dependency = m_appliance.getProfile().getType().getDependency( role ); Appliance provider = m_appliance.getServiceProvider( role ); if( provider == null ) { final String error = "Internal error - incorrect assembly in appliance: " + m_appliance + ". A null provider was retured for the dependency: " + dependency; throw new ServiceException( role, error ); } try { Object object = provider.access( dependency ); m_table.put( object, provider ); return object; } catch( Throwable e ) { final String error = "Unexpected internal error while accessing a service provider in appliance: " + m_appliance + " for the role: " + role; throw new ServiceException( role, error, e ); } } /** * Release a pooled object. * @param object a pooled object */ public void release( Object object ) { Appliance provider = (Appliance) m_table.get( object ); if( provider == null ) { if( getLogger().isWarnEnabled() ) { final String warning = "Inconsistent release request - the object: " + object.getClass().getName() + "/" + System.identityHashCode( object ) + " was not provided by this manager."; getLogger().warn( warning ); } } else { provider.release( object ); m_table.remove( object ); } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>