java.beans.beancontext

Class BeanContextServicesSupport

public class BeanContextServicesSupport extends BeanContextSupport implements BeanContextServices

This is a helper class for implementing a bean context which supplies services. It is intended to be used either by subclassing or by calling methods of this implementation from another.

Since: 1.2

Nested Class Summary
protected classBeanContextServicesSupport.BCSSChild
protected classBeanContextServicesSupport.BCSSProxyServiceProvider
protected static classBeanContextServicesSupport.BCSSServiceProvider
Field Summary
protected ArrayListbcsListeners
A collection of listeners who receive availability and revocation notifications.
protected BeanContextServicesSupport.BCSSProxyServiceProviderproxy
protected intserializable
The number of serializable service providers.
protected HashMapservices
A map of registered services, linking the service class to its associated {@link BCSSServiceProvider}.
Constructor Summary
BeanContextServicesSupport()
Construct a {@link BeanContextServicesSupport} instance.
BeanContextServicesSupport(BeanContextServices peer)
Construct a {@link BeanContextServicesSupport} instance.
BeanContextServicesSupport(BeanContextServices peer, Locale locale)
Construct a {@link BeanContextServicesSupport} instance.
BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean dtime)
Construct a {@link BeanContextServicesSupport} instance.
BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean dtime, boolean visible)
Construct a {@link BeanContextServicesSupport} instance.
Method Summary
voidaddBeanContextServicesListener(BeanContextServicesListener listener)
Adds a new listener for service availability and revocation events.
booleanaddService(Class serviceClass, BeanContextServiceProvider bcsp)
Registers a new service from the specified service provider.
protected booleanaddService(Class serviceClass, BeanContextServiceProvider bcsp, boolean fireEvent)
Registers a new service from the specified service provider.
protected voidbcsPreDeserializationHook(ObjectInputStream ois)
Deserializes any service providers which are serializable.
protected voidbcsPreSerializationHook(ObjectOutputStream oos)
Serializes any service providers which are serializable.
protected voidchildJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsc)
Revokes any services used by a child that has just been removed.
protected BeanContextSupport.BCSChildcreateBCSChild(Object targetChild, Object peer)
Overrides the {@link BeanContextSupport#createBCSChild} method so as to use a {@link BCSSChild} instead.
protected BeanContextServicesSupport.BCSSServiceProvidercreateBCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp)
Provides a hook so that subclasses can replace the {@link BCSSServiceProvider} class, used to store registered service providers, with a subclass without replacing the {@link #addService(Class, BeanContextServiceProvider)} method.
protected voidfireServiceAdded(BeanContextServiceAvailableEvent bcssae)
Sends a BeanContextServiceAvailableEvent to all registered listeners.
protected voidfireServiceAdded(Class serviceClass)
Sends a BeanContextServiceAvailableEvent to all registered listeners.
protected voidfireServiceRevoked(BeanContextServiceRevokedEvent event)
Sends a BeanContextServiceRevokedEvent to all registered listeners.
protected voidfireServiceRevoked(Class serviceClass, boolean revokeNow)
Sends a BeanContextServiceRevokedEvent to all registered listeners.
BeanContextServicesgetBeanContextServicesPeer()
Returns the services peer given at construction time, or null if no peer was given.
protected static BeanContextServicesListenergetChildBeanContextServicesListener(Object child)
Returns child as an instance of {@link BeanContextServicesListener}, or null if child does not implement that interface.
IteratorgetCurrentServiceClasses()
Returns an iterator over the currently available services.
IteratorgetCurrentServiceSelectors(Class serviceClass)
Returns an iterator over the service selectors of the service provider for the given service.
ObjectgetService(BeanContextChild child, Object requestor, Class serviceClass, Object serviceSelector, BeanContextServiceRevokedListener bcsrl)
Retrieves the specified service.
booleanhasService(Class serviceClass)
Returns true if the specified service is available.
voidinitialize()
protected voidinitializeBeanContextResources()
Subclasses may override this method to allocate resources from the nesting bean context.
protected voidreleaseBeanContextResources()
Relinquishes any resources obtained from the parent context.
voidreleaseService(BeanContextChild child, Object requestor, Object service)
Releases the reference to a service held by a {@link BeanContextChild} (or an arbitrary object associated with it).
voidremoveBeanContextServicesListener(BeanContextServicesListener listener)
voidrevokeService(Class serviceClass, BeanContextServiceProvider bcsp, boolean revokeCurrentServicesNow)
Revokes the given service.
voidserviceAvailable(BeanContextServiceAvailableEvent bcssae)
voidserviceRevoked(BeanContextServiceRevokedEvent bcssre)

Field Detail

bcsListeners

protected transient ArrayList bcsListeners
A collection of listeners who receive availability and revocation notifications.

proxy

protected transient BeanContextServicesSupport.BCSSProxyServiceProvider proxy

serializable

protected transient int serializable
The number of serializable service providers.

services

protected transient HashMap services
A map of registered services, linking the service class to its associated {@link BCSSServiceProvider}.

Constructor Detail

BeanContextServicesSupport

public BeanContextServicesSupport()
Construct a {@link BeanContextServicesSupport} instance.

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer)
Construct a {@link BeanContextServicesSupport} instance.

Parameters: peer the bean context services peer (null permitted).

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer, Locale locale)
Construct a {@link BeanContextServicesSupport} instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale).

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean dtime)
Construct a {@link BeanContextServicesSupport} instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale). dtime a flag indicating whether or not the bean context is in design time mode.

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean dtime, boolean visible)
Construct a {@link BeanContextServicesSupport} instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale). dtime a flag indicating whether or not the bean context is in design time mode. visible initial value of the okToUseGui flag.

Method Detail

addBeanContextServicesListener

public void addBeanContextServicesListener(BeanContextServicesListener listener)
Adds a new listener for service availability and revocation events.

Parameters: listener the listener to add.

addService

public boolean addService(Class serviceClass, BeanContextServiceProvider bcsp)
Registers a new service from the specified service provider. The service is internally associated with the service provider and a BeanContextServiceAvailableEvent is fired. If the service is already registered, then this method instead returns false. This is equivalent to calling addService(serviceClass, bcsp, true).

Parameters: serviceClass the class of the service to be registered. bcsp the provider of the given service.

Returns: true if the service was registered successfully.

See Also: BeanContextServicesSupport

addService

protected boolean addService(Class serviceClass, BeanContextServiceProvider bcsp, boolean fireEvent)
Registers a new service from the specified service provider. The service is internally associated with the service provider and (if fireEvent is true) a BeanContextServiceAvailableEvent is fired. If the service is already registered, then this method instead returns false.

Parameters: serviceClass the class of the service to be registered. bcsp the provider of the given service. fireEvent true if a service availability event should be fired.

Returns: true if the service was registered successfully.

bcsPreDeserializationHook

protected void bcsPreDeserializationHook(ObjectInputStream ois)
Deserializes any service providers which are serializable. This method is called by the readObject method of {@link BeanContextSupport} prior to deserialization of the children. Subclasses may envelope its behaviour in order to read further serialized data to the stream.

Parameters: oos the stream from which data is being deserialized.

Throws: IOException if an I/O error occurs. ClassNotFoundException if the class of a deserialized object can not be found.

bcsPreSerializationHook

protected void bcsPreSerializationHook(ObjectOutputStream oos)
Serializes any service providers which are serializable. This method is called by the writeObject method of {@link BeanContextSupport} prior to serialization of the children. Subclasses may envelope its behaviour in order to add further serialized data to the stream.

Parameters: oos the stream to which data is being serialized.

Throws: IOException if an I/O error occurs.

childJustRemovedHook

protected void childJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsc)
Revokes any services used by a child that has just been removed. The superclass ({@link BeanContextSupport}) calls this method when a child has just been successfully removed. Subclasses can extend this method in order to perform additional operations on child removal.

Parameters: child the child being removed. bcsc the support object for the child.

createBCSChild

protected BeanContextSupport.BCSChild createBCSChild(Object targetChild, Object peer)
Overrides the {@link BeanContextSupport#createBCSChild} method so as to use a {@link BCSSChild} instead.

Parameters: targetChild the child to create the child for. peer the peer which relates to the child if a proxy is used.

Returns: a new instance of {@link BCSSChild}.

createBCSSServiceProvider

protected BeanContextServicesSupport.BCSSServiceProvider createBCSSServiceProvider(Class sc, BeanContextServiceProvider bcsp)
Provides a hook so that subclasses can replace the {@link BCSSServiceProvider} class, used to store registered service providers, with a subclass without replacing the {@link #addService(Class, BeanContextServiceProvider)} method.

Parameters: sc the class of service being registered. bcsp the provider of the service.

Returns: a instance of {@link BCSSServiceProvider} wrapping the provider.

fireServiceAdded

protected final void fireServiceAdded(BeanContextServiceAvailableEvent bcssae)
Sends a BeanContextServiceAvailableEvent to all registered listeners.

Parameters: bcssae the event to send.

fireServiceAdded

protected final void fireServiceAdded(Class serviceClass)
Sends a BeanContextServiceAvailableEvent to all registered listeners.

Parameters: serviceClass the service that is now available.

See Also: fireServiceAdded

fireServiceRevoked

protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
Sends a BeanContextServiceRevokedEvent to all registered listeners.

Parameters: event the event to send.

fireServiceRevoked

protected final void fireServiceRevoked(Class serviceClass, boolean revokeNow)
Sends a BeanContextServiceRevokedEvent to all registered listeners.

Parameters: serviceClass the service that has been revoked.

See Also: fireServiceRevoked

getBeanContextServicesPeer

public BeanContextServices getBeanContextServicesPeer()
Returns the services peer given at construction time, or null if no peer was given.

Returns: the {@link BeanContextServices} peer.

getChildBeanContextServicesListener

protected static final BeanContextServicesListener getChildBeanContextServicesListener(Object child)
Returns child as an instance of {@link BeanContextServicesListener}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link BeanContextServicesListener}.

getCurrentServiceClasses

public Iterator getCurrentServiceClasses()
Returns an iterator over the currently available services.

Returns: an iterator over the currently available services.

getCurrentServiceSelectors

public Iterator getCurrentServiceSelectors(Class serviceClass)
Returns an iterator over the service selectors of the service provider for the given service. The iterator is actually obtained by calling the {@link BeanContextServiceProvider#getCurrentServiceSelectors} of the provider itself. If the specified service is not available, null is returned.

Parameters: serviceClass the service whose provider's selectors should be iterated over.

Returns: an {@link Iterator} over the service selectors of the provider of the given service.

getService

public Object getService(BeanContextChild child, Object requestor, Class serviceClass, Object serviceSelector, BeanContextServiceRevokedListener bcsrl)
Retrieves the specified service. If a provider for the service is registered in this context, then the request is passed on to the provider and the service returned. Otherwise, the request is delegated to a parent {@link BeanContextServices}, if possible. If the service can not be found at all, then null is returned.

Parameters: child the child obtaining the reference. requestor the requestor of the service, which may be the child itself. serviceClass the service being requested. serviceSelector an additional service-dependent parameter (may be null if not appropriate). bcsrl a listener used to notify the requestor that the service has since been revoked.

Returns: a reference to the service requested, or null.

Throws: TooManyListenersException according to Sun's documentation.

hasService

public boolean hasService(Class serviceClass)
Returns true if the specified service is available.

Parameters: serviceClass the service to check for.

Returns: true if the service is available.

initialize

public void initialize()

initializeBeanContextResources

protected void initializeBeanContextResources()
Subclasses may override this method to allocate resources from the nesting bean context.

releaseBeanContextResources

protected void releaseBeanContextResources()
Relinquishes any resources obtained from the parent context. Specifically, those services obtained from the parent are revoked. Subclasses may override this method to deallocate resources from the nesting bean context.

releaseService

public void releaseService(BeanContextChild child, Object requestor, Object service)
Releases the reference to a service held by a {@link BeanContextChild} (or an arbitrary object associated with it). It simply calls the appropriate method on the underlying provider.

Parameters: child the child who holds the reference. requestor the object that requested the reference. service the service being released.

removeBeanContextServicesListener

public void removeBeanContextServicesListener(BeanContextServicesListener listener)

revokeService

public void revokeService(Class serviceClass, BeanContextServiceProvider bcsp, boolean revokeCurrentServicesNow)
Revokes the given service. A {@link BeanContextServiceRevokedEvent} is emitted to all registered {@link BeanContextServiceRevokedListener}s and {@link BeanContextServiceListener}s. If revokeCurrentServicesNow is true, termination of the service is immediate. Otherwise, prior acquisitions of the service by requestors remain valid.

Parameters: serviceClass the service to revoke. bcsp the provider of the revoked service. revokeCurrentServicesNow true if this is an exceptional circumstance where service should be immediately revoked.

serviceAvailable

public void serviceAvailable(BeanContextServiceAvailableEvent bcssae)

serviceRevoked

public void serviceRevoked(BeanContextServiceRevokedEvent bcssre)