package textbender.g.beans.beancontext; // Copyright 2003, 2005-2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.beans.beancontext.*; import java.util.TooManyListenersException; import textbender.g.util.logging.LoggerX; /** A simplified registry for childless, irrevocable and unique services. * Service clients are not registered as BeanContextChildren, * so services are 'childless'. Once registered, they may never be revoked. * And service classes are unique; there is at most one instance of each. *

* Providers register each service by calling * {@linkplain #addSimpleService(Class,Object) addSimpleService}(). * Clients listen for these registrations via * {@linkplain #addBeanContextServicesListener addBeanContextServicesListener}(), * and subsequently fetch the services they need via * {@linkplain #getService(Class) getService}(). *

*

* Thread safe, though individual services are not (unless you know better). *

*/ public final class ServiceRegistryCIU extends BeanContextServicesSupport { /** The common instance of ServiceRegistryCIU. Other instances may be created, * but one is enough for most purposes. */ public static ServiceRegistryCIU i() { return instance; } // ------------------------------------------------------------------------------------ /** Registers a new bean context services listener. * * @param listener to register * @param fireUp if true, * listener is immediately {@linkplain BeanContextServicesX#fireUp fired up} * to the current state; otherwise it is not */ public void addBeanContextServicesListener( BeanContextServicesListener listener, boolean fireUp ) { BeanContextServicesX.addBeanContextServicesListener ( /*serviceSource*/ServiceRegistryCIU.this, listener, fireUp ); } /** Adds a new service to the registry. * Once added, the service cannot be removed or revoked. * * @param serviceClass specifying the Java class of the service, * or one of its parent classes/interfaces (for which the service * will be the sole registrant) * @param service instance that actually implements the service class * (of which the service must be an instance) * * @throws IllegalStateException if a service was already added for that serviceClass */ public void addSimpleService( Class serviceClass, Object service ) { assert serviceClass.isInstance( service ); LoggerX.i(getClass()).fine( "registering " + serviceClass.getName() ); boolean isAdded = super.addService( serviceClass, new SimpleServiceProvider( service )); if( !isAdded ) throw new IllegalStateException( "was already added: " + serviceClass ); } /** Returns a service previously {@linkplain #addSimpleService(Class,Object) added} * to the registry. * * @param serviceClass per {@linkplain #addSimpleService(Class,Object) addSimpleService}() * * @return service; or null if none yet added to the registry */ public Object getService( Class serviceClass ) { try { return getService ( /*child*/dummyBean, /*requestor*/dummyBean, serviceClass, /*selector*/null, BeanContextServiceRevokedListener0.i() ); } catch( TooManyListenersException x ) { throw new RuntimeException( x ); } // must not occur } // - B e a n - C o n t e x t - S e r v i c e s ---------------------------------------- public void revokeService( Class serviceClass, BeanContextServiceProvider serviceProvider, boolean revokeCurrentServicesNow ){ throw new UnsupportedOperationException( "services are irrevocable" ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** A dummy bean to act on behalf of all service clients. * There is currently no need for this complexity. */ private final BeanContextChild dummyBean = new BeanContextChildSupport(); { add( dummyBean ); // as a child of this context } ////////////////////////////////////////////////////////////////////////////////////////// // Last, so static fields above initialize, and are available during instantiation below private static final ServiceRegistryCIU instance = new ServiceRegistryCIU(); }