FileDocCategorySizeDatePackage
BeanContextServicesSupport.javaAPI DocJava SE 5 API35717Fri Aug 26 14:56:58 BST 2005java.beans.beancontext

BeanContextServicesSupport

public class BeanContextServicesSupport extends BeanContextSupport implements BeanContextServices

This helper class provides a utility implementation of the java.beans.beancontext.BeanContextServices interface.

Since this class directly implements the BeanContextServices interface, the class can, and is intended to be used either by subclassing this implementation, or via delegation of an instance of this class from another through the BeanContextProxy interface.

author
Laurence P. G. Cable
version
1.23, 04/15/04
since
1.2

Fields Summary
protected transient HashMap
services
all accesses to the protected transient HashMap services field should be synchronized on that object
protected transient int
serializable
The number of instances of a serializable BeanContextServceProvider.
protected transient BCSSProxyServiceProvider
proxy
Delegate for the BeanContextServiceProvider.
protected transient ArrayList
bcsListeners
List of BeanContextServicesListener objects.
Constructors Summary
public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dTime, boolean visible)

Construct a BeanContextServicesSupport instance

param
peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
param
lcle The current Locale for this BeanContext.
param
dtime The initial state, true if in design mode, false if runtime.
param
visible The initial visibility.

	super(peer, lcle, dTime, visible);
    
public BeanContextServicesSupport(BeanContextServices peer, Locale lcle, boolean dtime)
Create an instance using the specified Locale and design mode.

param
peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
param
lcle The current Locale for this BeanContext.
param
dtime The initial state, true if in design mode, false if runtime.

	this (peer, lcle, dtime, true);
    
public BeanContextServicesSupport(BeanContextServices peer, Locale lcle)
Create an instance using the specified locale

param
peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer
param
lcle The current Locale for this BeanContext.

	this (peer, lcle, false, true);
    
public BeanContextServicesSupport(BeanContextServices peer)
Create an instance with a peer

param
peer The peer BeanContext we are supplying an implementation for, if null the this object is its own peer

	this (peer, null, false, true);
    
public BeanContextServicesSupport()
Create an instance that is not a delegate of another object

	this (null, null, false, true);
    
Methods Summary
public voidaddBeanContextServicesListener(java.beans.beancontext.BeanContextServicesListener bcsl)
/ /** add a BeanContextServicesListener

throw
new NullPointerException

	if (bcsl == null) throw new NullPointerException("bcsl");

	synchronized(bcsListeners) {
	    if (bcsListeners.contains(bcsl))
		return;
	    else
	        bcsListeners.add(bcsl);
	}
    
public booleanaddService(java.lang.Class serviceClass, java.beans.beancontext.BeanContextServiceProvider bcsp)
add a service

	return addService(serviceClass, bcsp, true);
    
protected booleanaddService(java.lang.Class serviceClass, java.beans.beancontext.BeanContextServiceProvider bcsp, boolean fireEvent)
add a service


	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (bcsp         == null) throw new NullPointerException("bcsp");

	synchronized(BeanContext.globalHierarchyLock) {
            if (services.containsKey(serviceClass))
    	        return false;
    	    else {
    	        services.put(serviceClass,  createBCSSServiceProvider(serviceClass, bcsp));
    
    	        if (bcsp instanceof Serializable) serializable++;
    
    	        if (!fireEvent) return true;
    
    
    	        BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent(getBeanContextServicesPeer(), serviceClass);
    
    	        fireServiceAdded(bcssae);
    
    	        synchronized(children) {
    	            Iterator i = children.keySet().iterator();
    
    	            while (i.hasNext()) {
    	                Object c = i.next();
    
    	                if (c instanceof BeanContextServices) {
    		            ((BeanContextServicesListener)c).serviceAvailable(bcssae);
    	                }
    	            }
    	        }
    
    	        return true;
    	    }
	}
    
protected synchronized voidbcsPreDeserializationHook(java.io.ObjectInputStream ois)
called from BeanContextSupport readObject before it deserializes the children ... This class will deserialize any Serializable BeanContextServiceProviders serialized earlier thus making them available to the children when they deserialized. subclasses may envelope this method to insert their own serialization processing that has to occur prior to serialization of the children


	serializable = ois.readInt();

	int count = serializable;

	while (count > 0) {
	    services.put(ois.readObject(), ois.readObject());
	    count--;
	} 
    
protected synchronized voidbcsPreSerializationHook(java.io.ObjectOutputStream oos)
called from BeanContextSupport writeObject before it serializes the children ... This class will serialize any Serializable BeanContextServiceProviders herein. subclasses may envelope this method to insert their own serialization processing that has to occur prior to serialization of the children


	oos.writeInt(serializable);

	if (serializable <= 0) return;

	int count = 0;

	Iterator i = services.entrySet().iterator();

	while (i.hasNext() && count < serializable) {
	    Map.Entry 		entry = (Map.Entry)i.next();
	    BCSSServiceProvider bcsp  = null;

	     try {
		bcsp = (BCSSServiceProvider)entry.getValue();
	     } catch (ClassCastException cce) {
		continue;
	     }

	     if (bcsp.getServiceProvider() instanceof Serializable) {
		oos.writeObject(entry.getKey());
		oos.writeObject(bcsp);
		count++;
	     }
	}

	if (count != serializable) 
	    throw new IOException("wrote different number of service providers than expected");
    
protected voidchildJustRemovedHook(java.lang.Object child, BCSChild bcsc)
called from superclass child removal operations after a child has been successfully removed. called with child synchronized. This subclass uses this hook to immediately revoke any services being used by this child if it is a BeanContextChild. subclasses may envelope this method in order to implement their own child removal side-effects.

	BCSSChild bcssc = (BCSSChild)bcsc;

	bcssc.cleanupReferences();
    
protected BCSChildcreateBCSChild(java.lang.Object targetChild, java.lang.Object peer)

Subclasses can override this method to insert their own subclass of Child without having to override add() or the other Collection methods that add children to the set.

param
targetChild the child to create the Child on behalf of
param
peer the peer if the targetChild and peer are related by BeanContextProxy

	return new BCSSChild(targetChild, peer);
    
protected java.beans.beancontext.BeanContextServicesSupport$BCSSServiceProvidercreateBCSSServiceProvider(java.lang.Class sc, java.beans.beancontext.BeanContextServiceProvider bcsp)
subclasses can override this method to create new subclasses of BCSSServiceProvider without having to overrride addService() in order to instantiate.

	    return new BCSSServiceProvider(sc, bcsp);
	
protected final voidfireServiceAdded(java.lang.Class serviceClass)
Fires a BeanContextServiceEvent notifying of a new service.

	BeanContextServiceAvailableEvent bcssae = new BeanContextServiceAvailableEvent(getBeanContextServicesPeer(), serviceClass); 

	fireServiceAdded(bcssae);
    
protected final voidfireServiceAdded(java.beans.beancontext.BeanContextServiceAvailableEvent bcssae)
Fires a BeanContextServiceAvailableEvent indicating that a new service has become available.

param
bcssae the BeanContextServiceAvailableEvent

	Object[]		         copy;

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServicesListener)copy[i]).serviceAvailable(bcssae);
	}
    
protected final voidfireServiceRevoked(java.beans.beancontext.BeanContextServiceRevokedEvent bcsre)
Fires a BeanContextServiceEvent notifying of a service being revoked.

param
bcsre the BeanContextServiceRevokedEvent

	Object[]		         copy;

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServiceRevokedListener)copy[i]).serviceRevoked(bcsre);
	}
    
protected final voidfireServiceRevoked(java.lang.Class serviceClass, boolean revokeNow)
Fires a BeanContextServiceRevokedEvent indicating that a particular service is no longer available.

	Object[]		       copy;
	BeanContextServiceRevokedEvent bcsre = new BeanContextServiceRevokedEvent(getBeanContextServicesPeer(), serviceClass, revokeNow); 

	synchronized (bcsListeners) { copy = bcsListeners.toArray(); }

	for (int i = 0; i < copy.length; i++) {
	    ((BeanContextServicesListener)copy[i]).serviceRevoked(bcsre);
	}
   
public java.beans.beancontext.BeanContextServicesgetBeanContextServicesPeer()
Gets the BeanContextServices associated with this BeanContextServicesSupport.

return
the instance of BeanContext this object is providing the implementation for.

 
	return (BeanContextServices)getBeanContextChildPeer();
    
protected static final java.beans.beancontext.BeanContextServicesListenergetChildBeanContextServicesListener(java.lang.Object child)
Gets the BeanContextServicesListener (if any) of the specified child.

param
child the specified child
return
the BeanContextServicesListener (if any) of the specified child

	try {
	    return (BeanContextServicesListener)child;
	} catch (ClassCastException cce) {
	    return null;
	}
    
public java.util.IteratorgetCurrentServiceClasses()

return
an iterator for all the currently registered service classes.

	return new BCSIterator(services.keySet().iterator());
    
public java.util.IteratorgetCurrentServiceSelectors(java.lang.Class serviceClass)

return
an iterator for all the currently available service selectors (if any) available for the specified service.

	
	BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);
	
	return bcsssp != null ? new BCSIterator(bcsssp.getServiceProvider().getCurrentServiceSelectors(getBeanContextServicesPeer(), serviceClass)) : null;
    
public java.lang.ObjectgetService(java.beans.beancontext.BeanContextChild child, java.lang.Object requestor, java.lang.Class serviceClass, java.lang.Object serviceSelector, java.beans.beancontext.BeanContextServiceRevokedListener bcsrl)
/ /** obtain a service which may be delegated

	if (child        == null) throw new NullPointerException("child");
	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (requestor    == null) throw new NullPointerException("requestor");
	if (bcsrl        == null) throw new NullPointerException("bcsrl");

	Object              service = null;
	BCSSChild           bcsc;
	BeanContextServices bcssp   = getBeanContextServicesPeer();

	synchronized(BeanContext.globalHierarchyLock) {
	    synchronized(children) { bcsc = (BCSSChild)children.get(child); }

	    if (bcsc == null) throw new IllegalArgumentException("not a child of this context"); // not a child ...

	    BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);

	    if (bcsssp != null) {
	        BeanContextServiceProvider bcsp = bcsssp.getServiceProvider();
	        service = bcsp.getService(bcssp, requestor, serviceClass, serviceSelector);
	        if (service != null) { // do bookkeeping ...
		    try {
	                bcsc.usingService(requestor, service, serviceClass, bcsp, false, bcsrl);
		    } catch (TooManyListenersException tmle) {
		        bcsp.releaseService(bcssp, requestor, service);
		        throw tmle;
		    } catch (UnsupportedOperationException uope) {
		        bcsp.releaseService(bcssp, requestor, service);
		        throw uope; // unchecked rt exception
		    }

		    return service;
	        }
	    }


	    if (proxy != null) {

	        // try to delegate ...

	        service = proxy.getService(bcssp, requestor, serviceClass, serviceSelector);
	
	        if (service != null) { // do bookkeeping ...
		    try {
		        bcsc.usingService(requestor, service, serviceClass, proxy, true, bcsrl);
		    } catch (TooManyListenersException tmle) {
		        proxy.releaseService(bcssp, requestor, service);
		        throw tmle;
		    } catch (UnsupportedOperationException uope) {
		        proxy.releaseService(bcssp, requestor, service);
		        throw uope; // unchecked rt exception
		    }

		    return service;
	        }
	    }
	}

       	return null;
    
public synchronized booleanhasService(java.lang.Class serviceClass)
has a service, which may be delegated

	if (serviceClass == null) throw new NullPointerException("serviceClass");

	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(serviceClass)) return true;

	    BeanContextServices bcs = null;

	    try {
	        bcs = (BeanContextServices)getBeanContext();
	    } catch (ClassCastException cce) {
	        return false;
	    }

	    return bcs == null ? false : bcs.hasService(serviceClass);
	}
    
public voidinitialize()
called by BeanContextSupport superclass during construction and deserialization to initialize subclass transient state. subclasses may envelope this method, but should not override it or call it directly.

	super.initialize();

	services     = new HashMap(serializable + 1);
	bcsListeners = new ArrayList(1);
    
protected synchronized voidinitializeBeanContextResources()
called from setBeanContext to notify a BeanContextChild to allocate resources obtained from the nesting BeanContext. subclasses may envelope this method to implement their own semantics.

	super.initializeBeanContextResources();

	BeanContext nbc = getBeanContext();

	if (nbc == null) return;

	try {
	    BeanContextServices bcs = (BeanContextServices)nbc;

	    proxy = new BCSSProxyServiceProvider(bcs);
	} catch (ClassCastException cce) {
	    // do nothing ...
	}
    
private synchronized voidreadObject(java.io.ObjectInputStream ois)
deserialize the instance


	ois.defaultReadObject();

	deserialize(ois, (Collection)bcsListeners);
    
protected synchronized voidreleaseBeanContextResources()
called from setBeanContext to notify a BeanContextChild to release resources obtained from the nesting BeanContext. This method revokes any services obtained from its parent. subclasses may envelope this method to implement their own semantics.

	Object[] bcssc;

	super.releaseBeanContextResources();

	synchronized(children) {
	    if (children.isEmpty()) return;

	    bcssc = children.values().toArray();
	}


	for (int i = 0; i < bcssc.length; i++) {
	    ((BCSSChild)bcssc[i]).revokeAllDelegatedServicesNow();
	}

	proxy = null;
    
public voidreleaseService(java.beans.beancontext.BeanContextChild child, java.lang.Object requestor, java.lang.Object service)
release a service

	if (child     == null) throw new NullPointerException("child");
	if (requestor == null) throw new NullPointerException("requestor");
	if (service   == null) throw new NullPointerException("service");

	BCSSChild bcsc;

	synchronized(BeanContext.globalHierarchyLock) {
	        synchronized(children) { bcsc = (BCSSChild)children.get(child); }

	        if (bcsc != null)
		    bcsc.releaseService(requestor, service);
		else
		   throw new IllegalArgumentException("child actual is not a child of this BeanContext");
        }
    
public voidremoveBeanContextServicesListener(java.beans.beancontext.BeanContextServicesListener bcsl)
remove a BeanContextServicesListener

	if (bcsl == null) throw new NullPointerException("bcsl");

	synchronized(bcsListeners) {
	    if (!bcsListeners.contains(bcsl))
		return;
	    else
	        bcsListeners.remove(bcsl);
	}
    
public voidrevokeService(java.lang.Class serviceClass, java.beans.beancontext.BeanContextServiceProvider bcsp, boolean revokeCurrentServicesNow)
remove a service


	if (serviceClass == null) throw new NullPointerException("serviceClass");
	if (bcsp         == null) throw new NullPointerException("bcsp");

	synchronized(BeanContext.globalHierarchyLock) {
	    if (!services.containsKey(serviceClass)) return;

	    BCSSServiceProvider bcsssp = (BCSSServiceProvider)services.get(serviceClass);
	
	    if (!bcsssp.getServiceProvider().equals(bcsp)) 
	        throw new IllegalArgumentException("service provider mismatch");

	    services.remove(serviceClass);

            if (bcsp instanceof Serializable) serializable--;

	    Iterator i = bcsChildren(); // get the BCSChild values.

	    while (i.hasNext()) {
	        ((BCSSChild)i.next()).revokeService(serviceClass, false, revokeCurrentServicesNow);
	    }

	    fireServiceRevoked(serviceClass, revokeCurrentServicesNow);
	}
    
public voidserviceAvailable(java.beans.beancontext.BeanContextServiceAvailableEvent bcssae)
BeanContextServicesListener callback, propagates event to all currently registered listeners and BeanContextServices children, if this BeanContextService does not already implement this service itself. subclasses may override or envelope this method to implement their own propagation semantics.

	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(bcssae.getServiceClass())) return;

	    fireServiceAdded(bcssae);

	    Iterator i;

	    synchronized(children) {
	        i = children.keySet().iterator();
	    }

	    while (i.hasNext()) {
	        Object c = i.next();

	        if (c instanceof BeanContextServices) {
	            ((BeanContextServicesListener)c).serviceAvailable(bcssae);
	        }
	    }
	}
     
public voidserviceRevoked(java.beans.beancontext.BeanContextServiceRevokedEvent bcssre)
BeanContextServicesListener callback, propagates event to all currently registered listeners and BeanContextServices children, if this BeanContextService does not already implement this service itself. subclasses may override or envelope this method to implement their own propagation semantics.

	synchronized(BeanContext.globalHierarchyLock) {
	    if (services.containsKey(bcssre.getServiceClass())) return;

	    fireServiceRevoked(bcssre);

	    Iterator i;

	    synchronized(children) {
	        i = children.keySet().iterator();
	    }

	    while (i.hasNext()) {
	        Object c = i.next();

	        if (c instanceof BeanContextServices) {
	            ((BeanContextServicesListener)c).serviceRevoked(bcssre);
	        }
	    }
	}
    
private synchronized voidwriteObject(java.io.ObjectOutputStream oos)
serialize the instance

	oos.defaultWriteObject();

	serialize(oos, (Collection)bcsListeners);