com.googlecode.openbeans.beancontext
Class BeanContextServicesSupport

java.lang.Object
  extended by com.googlecode.openbeans.beancontext.BeanContextChildSupport
      extended by com.googlecode.openbeans.beancontext.BeanContextSupport
          extended by com.googlecode.openbeans.beancontext.BeanContextServicesSupport
All Implemented Interfaces:
BeanContext, BeanContextChild, BeanContextServiceRevokedListener, BeanContextServices, BeanContextServicesListener, DesignMode, PropertyChangeListener, VetoableChangeListener, Visibility, Serializable, Iterable, Collection, EventListener

public class BeanContextServicesSupport
extends BeanContextSupport
implements BeanContextServices, Serializable

This support class implements BeanContextServices interface. This class can be used directly, or be a super class of your class, or be a delegate of your implementation that needs to support BeanContextServices interface.

See Also:
Serialized Form

Nested Class Summary
protected  class BeanContextServicesSupport.BCSSChild
          Every child of context is companied with a BCSSChild instance.
protected  class BeanContextServicesSupport.BCSSProxyServiceProvider
          This class implements the BeanContextServiceProvider interface by wrapping a BeanContextServices.
protected static class BeanContextServicesSupport.BCSSServiceProvider
          Every servie registered in this context is companied with a BCSSServiceProvider instance.
 
Nested classes/interfaces inherited from class com.googlecode.openbeans.beancontext.BeanContextSupport
BeanContextSupport.BCSChild, BeanContextSupport.BCSIterator
 
Field Summary
protected  ArrayList bcsListeners
          A list of registered BeanContextServicesListeners.
protected  BeanContextServicesSupport.BCSSProxyServiceProvider proxy
          A proxy service provider that delegates service requests to the parent context.
protected  int serializable
          The number of serializable service providers currently registered.
protected  HashMap services
          A map of all registered services - key is service class, value is BCSSServiceProvider object.
 
Fields inherited from class com.googlecode.openbeans.beancontext.BeanContextSupport
bcmListeners, children, designTime, locale, okToUseGui
 
Fields inherited from class com.googlecode.openbeans.beancontext.BeanContextChildSupport
beanContext, beanContextChildPeer, pcSupport, rejectedSetBCOnce, vcSupport
 
Fields inherited from interface com.googlecode.openbeans.beancontext.BeanContext
globalHierarchyLock
 
Fields inherited from interface com.googlecode.openbeans.DesignMode
PROPERTYNAME
 
Constructor Summary
BeanContextServicesSupport()
          Constructs a standard BeanContextServicesSupport.
BeanContextServicesSupport(BeanContextServices peer)
          Constructs a BeanContextServicesSupport which is a delegate of the given peer.
BeanContextServicesSupport(BeanContextServices peer, Locale locale)
          Constructs a BeanContextServicesSupport which is a delegate of the given peer.
BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean designTime)
          Constructs a BeanContextServicesSupport which is a delegate of the given peer.
BeanContextServicesSupport(BeanContextServices peer, Locale locale, boolean designTime, boolean okToUseGui)
          Constructs a BeanContextServicesSupport which is a delegate of the given peer.
 
Method Summary
 void addBeanContextServicesListener(BeanContextServicesListener listener)
           
 boolean addService(Class serviceClass, BeanContextServiceProvider provider)
          Add a service to this context.
protected  boolean addService(Class serviceClass, BeanContextServiceProvider provider, boolean fireEvent)
          Add a service to this context.
protected  void bcsPreDeserializationHook(ObjectInputStream ois)
          Deserializes all serializable services and their providers before the children of this context is deserialized.
protected  void bcsPreSerializationHook(ObjectOutputStream oos)
          Serializes all serializable services and their providers before the children of this context is serialized.
protected  void childJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsChild)
          This method is called everytime a child is removed from this context.
protected  BeanContextSupport.BCSChild createBCSChild(Object child, Object proxyPeer)
          Creates a BCSSChild object to company the given child.
protected  BeanContextServicesSupport.BCSSServiceProvider createBCSSServiceProvider(Class serviceClass, BeanContextServiceProvider provider)
          Creates a BCSSServiceProvider to company the given service.
protected  void fireServiceAdded(BeanContextServiceAvailableEvent event)
          Fires a BeanContextServiceAvailableEvent to registered BeanContextServicesListeners.
protected  void fireServiceAdded(Class serviceClass)
          Fires a BeanContextServiceAvailableEvent to registered BeanContextServicesListeners.
protected  void fireServiceRevoked(BeanContextServiceRevokedEvent event)
          Fires a BeanContextServiceRevokedEvent to registered BeanContextServicesListeners.
protected  void fireServiceRevoked(Class serviceClass, boolean revokeNow)
          Fires a BeanContextServiceRevokedEvent to registered BeanContextServicesListeners.
 BeanContextServices getBeanContextServicesPeer()
          Returns the peer of this context casted as BeanContextServices.
protected static BeanContextServicesListener getChildBeanContextServicesListener(Object child)
          Returns the given child casted to BeanContextServicesListener, or null if it does not implements the interface.
 Iterator getCurrentServiceClasses()
          Returns an iterator of all registered service classes, with removed() disabled.
 Iterator getCurrentServiceSelectors(Class serviceClass)
          Returns the service selectors of the specified service.
 Object getService(BeanContextChild child, Object requestor, Class serviceClass, Object serviceSelector, BeanContextServiceRevokedListener bcsrl)
          Get a service instance on behalf of the specified child of this context, by calling the registered service provider, or by delegating to the parent context.
 boolean hasService(Class serviceClass)
          Checks whether a service is registed in this context or the parent context.
 void initialize()
          Initializes all transient fields of this instance, called by constructors and readObject().
protected  void initializeBeanContextResources()
          Called after the parent context is updated.
protected  void releaseBeanContextResources()
          Called before the parent context is updated.
 void releaseService(BeanContextChild child, Object requestor, Object service)
          Release a service which has been requested previously.
 void removeBeanContextServicesListener(BeanContextServicesListener listener)
           
 void revokeService(Class serviceClass, BeanContextServiceProvider serviceProvider, boolean revokeCurrentServicesNow)
          Revokes a service in this bean context.
 void serviceAvailable(BeanContextServiceAvailableEvent event)
          Notify all listeners and children that implements BeanContextServices of the event.
 void serviceRevoked(BeanContextServiceRevokedEvent event)
          Notify all listeners and children that implements BeanContextServices of the event.
 
Methods inherited from class com.googlecode.openbeans.beancontext.BeanContextSupport
add, addAll, addBeanContextMembershipListener, avoidingGui, bcsChildren, childDeserializedHook, childJustAddedHook, classEquals, clear, contains, containsAll, containsKey, copyChildren, deserialize, dontUseGui, fireChildrenAdded, fireChildrenRemoved, getBeanContextPeer, getChildBeanContextChild, getChildBeanContextMembershipListener, getChildPropertyChangeListener, getChildSerializable, getChildVetoableChangeListener, getChildVisibility, getLocale, getResource, getResourceAsStream, instantiateChild, isDesignTime, isEmpty, isSerializing, iterator, needsGui, okToUseGui, propertyChange, readChildren, remove, remove, removeAll, removeBeanContextMembershipListener, retainAll, serialize, setDesignTime, setLocale, size, toArray, toArray, validatePendingAdd, validatePendingRemove, vetoableChange, writeChildren
 
Methods inherited from class com.googlecode.openbeans.beancontext.BeanContextChildSupport
addPropertyChangeListener, addVetoableChangeListener, firePropertyChange, fireVetoableChange, getBeanContext, getBeanContextChildPeer, isDelegated, removePropertyChangeListener, removeVetoableChangeListener, setBeanContext, validatePendingSetBeanContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.googlecode.openbeans.beancontext.BeanContext
addBeanContextMembershipListener, getResource, getResourceAsStream, instantiateChild, removeBeanContextMembershipListener
 
Methods inherited from interface com.googlecode.openbeans.beancontext.BeanContextChild
addPropertyChangeListener, addVetoableChangeListener, getBeanContext, removePropertyChangeListener, removeVetoableChangeListener, setBeanContext
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from interface com.googlecode.openbeans.DesignMode
isDesignTime, setDesignTime
 
Methods inherited from interface com.googlecode.openbeans.Visibility
avoidingGui, dontUseGui, needsGui, okToUseGui
 

Field Detail

services

protected transient HashMap services
A map of all registered services - key is service class, value is BCSSServiceProvider object. All access to this object should be synchronized on itself.


serializable

protected transient int serializable
The number of serializable service providers currently registered.


proxy

protected transient BeanContextServicesSupport.BCSSProxyServiceProvider proxy
A proxy service provider that delegates service requests to the parent context.


bcsListeners

protected transient ArrayList bcsListeners
A list of registered BeanContextServicesListeners. All access to this object should be synchronized on itself.

Constructor Detail

BeanContextServicesSupport

public BeanContextServicesSupport()
Constructs a standard BeanContextServicesSupport.


BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer)
Constructs a BeanContextServicesSupport which is a delegate of the given peer.

Parameters:
peer - the peer of this context

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer,
                                  Locale locale)
Constructs a BeanContextServicesSupport which is a delegate of the given peer.

Parameters:
peer - the peer of this context
locale - the locale of this context

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer,
                                  Locale locale,
                                  boolean designTime)
Constructs a BeanContextServicesSupport which is a delegate of the given peer.

Parameters:
peer - the peer of this context
locale - the locale of this context
designTime - whether in design mode or not

BeanContextServicesSupport

public BeanContextServicesSupport(BeanContextServices peer,
                                  Locale locale,
                                  boolean designTime,
                                  boolean okToUseGui)
Constructs a BeanContextServicesSupport which is a delegate of the given peer.

Parameters:
peer - the peer of this context
locale - the locale of this context
designTime - whether in design mode or not
okToUseGui - whether GUI is usable or not
Method Detail

addBeanContextServicesListener

public void addBeanContextServicesListener(BeanContextServicesListener listener)
Specified by:
addBeanContextServicesListener in interface BeanContextServices

addService

public boolean addService(Class serviceClass,
                          BeanContextServiceProvider provider)
Add a service to this context.

Delegate to addService(serviceClass, provider, true).

Specified by:
addService in interface BeanContextServices
See Also:
BeanContextServices.addService(java.lang.Class, com.googlecode.openbeans.beancontext.BeanContextServiceProvider)

addService

protected boolean addService(Class serviceClass,
                             BeanContextServiceProvider provider,
                             boolean fireEvent)
Add a service to this context.

If the service already exists in the context, simply return false. Otherwise, the service is added and event is fired if required.

Parameters:
serviceClass - the service class
provider - the provider of the service
fireEvent - the flag indicating to fire event or not
Returns:
true if the service is added; or false if the context already has this service

bcsPreDeserializationHook

protected void bcsPreDeserializationHook(ObjectInputStream ois)
                                  throws IOException,
                                         ClassNotFoundException
Deserializes all serializable services and their providers before the children of this context is deserialized.

First a int is read, indicating the number of services to read. Then pairs of service class and service provider are read one by one.

Overrides:
bcsPreDeserializationHook in class BeanContextSupport
Parameters:
ois - the object input stream
Throws:
IOException
ClassNotFoundException
See Also:
BeanContextSupport.bcsPreDeserializationHook(java.io.ObjectInputStream)

bcsPreSerializationHook

protected void bcsPreSerializationHook(ObjectOutputStream oos)
                                throws IOException
Serializes all serializable services and their providers before the children of this context is serialized.

First a int is writtern, indicating the number of serializable services. Then pairs of service class and service provider are writtern one by one.

Overrides:
bcsPreSerializationHook in class BeanContextSupport
Parameters:
oos - the object output stream
Throws:
IOException
See Also:
BeanContextSupport.bcsPreSerializationHook(java.io.ObjectOutputStream)

childJustRemovedHook

protected void childJustRemovedHook(Object child,
                                    BeanContextSupport.BCSChild bcsChild)
This method is called everytime a child is removed from this context.

The implementation releases all services requested by the child.

Overrides:
childJustRemovedHook in class BeanContextSupport
Parameters:
child - the child just removed
bcsChild - the BCSChild just removed
See Also:
BeanContextSupport.childJustRemovedHook(java.lang.Object, com.googlecode.openbeans.beancontext.BeanContextSupport.BCSChild)

createBCSChild

protected BeanContextSupport.BCSChild createBCSChild(Object child,
                                                     Object proxyPeer)
Creates a BCSSChild object to company the given child.

Overrides:
createBCSChild in class BeanContextSupport
Parameters:
child - the child
proxyPeer - the proxy peer of the child if there is one
Returns:
a BCSChild object to company the given child
See Also:
BeanContextSupport.createBCSChild(java.lang.Object, java.lang.Object)

createBCSSServiceProvider

protected BeanContextServicesSupport.BCSSServiceProvider createBCSSServiceProvider(Class serviceClass,
                                                                                   BeanContextServiceProvider provider)
Creates a BCSSServiceProvider to company the given service.

Parameters:
serviceClass - the service class
provider - the service provider
Returns:
a BCSSServiceProvider to company the given service

fireServiceAdded

protected final void fireServiceAdded(Class serviceClass)
Fires a BeanContextServiceAvailableEvent to registered BeanContextServicesListeners.

Parameters:
serviceClass - the service that has been added

fireServiceAdded

protected final void fireServiceAdded(BeanContextServiceAvailableEvent event)
Fires a BeanContextServiceAvailableEvent to registered BeanContextServicesListeners.

Parameters:
event - the event

fireServiceRevoked

protected final void fireServiceRevoked(Class serviceClass,
                                        boolean revokeNow)
Fires a BeanContextServiceRevokedEvent to registered BeanContextServicesListeners.

Parameters:
serviceClass - the service that has been revoked
revokeNow - whether to terminate service immediately

fireServiceRevoked

protected final void fireServiceRevoked(BeanContextServiceRevokedEvent event)
Fires a BeanContextServiceRevokedEvent to registered BeanContextServicesListeners.

Parameters:
event - the event

getBeanContextServicesPeer

public BeanContextServices getBeanContextServicesPeer()
Returns the peer of this context casted as BeanContextServices.

Returns:
the peer of this context casted as BeanContextServices

getChildBeanContextServicesListener

protected static final BeanContextServicesListener getChildBeanContextServicesListener(Object child)
Returns the given child casted to BeanContextServicesListener, or null if it does not implements the interface.

Parameters:
child - a child
Returns:
the given child casted to BeanContextServicesListener, or null if it does not implements the interface

getCurrentServiceClasses

public Iterator getCurrentServiceClasses()
Returns an iterator of all registered service classes, with removed() disabled.

Specified by:
getCurrentServiceClasses in interface BeanContextServices
Returns:
an iterator of all registered service classes
See Also:
BeanContextServices.getCurrentServiceClasses()

getCurrentServiceSelectors

public Iterator getCurrentServiceSelectors(Class serviceClass)
Returns the service selectors of the specified service. The iterator's remove() operation is disabled.

Specified by:
getCurrentServiceSelectors in interface BeanContextServices
See Also:
BeanContextServices.getCurrentServiceSelectors(java.lang.Class)

getService

public Object getService(BeanContextChild child,
                         Object requestor,
                         Class serviceClass,
                         Object serviceSelector,
                         BeanContextServiceRevokedListener bcsrl)
                  throws TooManyListenersException
Get a service instance on behalf of the specified child of this context, by calling the registered service provider, or by delegating to the parent context.

Specified by:
getService in interface BeanContextServices
Parameters:
child - the child that request service
requestor - the requestor object
serviceClass - the service class
serviceSelector - the service selectors
bcsrl - the BeanContextServiceRevokedListener
Returns:
a service instance on behalf of the specified child of this context
Throws:
IllegalArgumentException - if child is not a child of this context
TooManyListenersException
See Also:
BeanContextServices.getService(com.googlecode.openbeans.beancontext.BeanContextChild, java.lang.Object, java.lang.Class, java.lang.Object, com.googlecode.openbeans.beancontext.BeanContextServiceRevokedListener)

hasService

public boolean hasService(Class serviceClass)
Checks whether a service is registed in this context or the parent context.

Specified by:
hasService in interface BeanContextServices
Parameters:
serviceClass - the service class
Returns:
true if the service is registered
See Also:
BeanContextServices.hasService(java.lang.Class)

initialize

public void initialize()
Description copied from class: BeanContextSupport
Initializes all transient fields of this instance, called by constructors and readObject().

Overrides:
initialize in class BeanContextSupport

initializeBeanContextResources

protected void initializeBeanContextResources()
Called after the parent context is updated. The implementation checks if the parent context is a BeanContextServices. If it is, then a BCSSProxyServiceProvider is created to delegate service requests to the parent context.

Overrides:
initializeBeanContextResources in class BeanContextChildSupport
See Also:
BeanContextChildSupport.initializeBeanContextResources()

releaseBeanContextResources

protected void releaseBeanContextResources()
Called before the parent context is updated. The implementation releases any service that is currently provided by the parent context.

Overrides:
releaseBeanContextResources in class BeanContextChildSupport
See Also:
BeanContextChildSupport.releaseBeanContextResources()

releaseService

public void releaseService(BeanContextChild child,
                           Object requestor,
                           Object service)
Release a service which has been requested previously.

Specified by:
releaseService in interface BeanContextServices
Parameters:
child - the child that request the service
requestor - the requestor object
service - the service instance
Throws:
IllegalArgumentException - if child is not a child of this context

removeBeanContextServicesListener

public void removeBeanContextServicesListener(BeanContextServicesListener listener)
Specified by:
removeBeanContextServicesListener in interface BeanContextServices

revokeService

public void revokeService(Class serviceClass,
                          BeanContextServiceProvider serviceProvider,
                          boolean revokeCurrentServicesNow)
Revokes a service in this bean context.

The given service provider is unregistered and a BeanContextServiceRevokedEvent is fired. All registered service listeners and current service users get notified.

Specified by:
revokeService in interface BeanContextServices
Parameters:
serviceClass - the service class
serviceProvider - the service provider
revokeCurrentServicesNow - true if service should be terminated immediantly
See Also:
BeanContextServices.revokeService(java.lang.Class, com.googlecode.openbeans.beancontext.BeanContextServiceProvider, boolean)

serviceAvailable

public void serviceAvailable(BeanContextServiceAvailableEvent event)
Notify all listeners and children that implements BeanContextServices of the event.

Specified by:
serviceAvailable in interface BeanContextServicesListener
Overrides:
serviceAvailable in class BeanContextChildSupport
See Also:
BeanContextServicesListener.serviceAvailable(com.googlecode.openbeans.beancontext.BeanContextServiceAvailableEvent)

serviceRevoked

public void serviceRevoked(BeanContextServiceRevokedEvent event)
Notify all listeners and children that implements BeanContextServices of the event.

Specified by:
serviceRevoked in interface BeanContextServiceRevokedListener
Overrides:
serviceRevoked in class BeanContextChildSupport
See Also:
BeanContextServiceRevokedListener.serviceRevoked(com.googlecode.openbeans.beancontext.BeanContextServiceRevokedEvent)


Copyright © 2013. All Rights Reserved.