org.beanfabrics.context
Class DefaultContext

java.lang.Object
  extended by org.beanfabrics.context.DefaultContext
All Implemented Interfaces:
Context
Direct Known Subclasses:
BnParentContext, ModelContext

public class DefaultContext
extends java.lang.Object
implements Context

The DefaultContext is the default implementation of the Context interface.


Constructor Summary
DefaultContext()
          Constructs a DefaultContext.
 
Method Summary
 void addContextListener(ContextListener l)
          Add the given ContextListener.
 void addParent(Context parent)
          Adds the given Context as a parent to the this context.
<T> boolean
addService(java.lang.Class<? super T> type, T service)
          Place the given service into this Context and make it available for clients.
 ServiceEntry findService(java.lang.Class<?> type)
          Finds and returns the first service entry in this context that matches the given type.
protected  void fireParentAdded(Context parent)
          Fires a parentAdded-event to each registered listener.
protected  void fireParentRemoved(Context parent)
          Fires a parentRemoved-event to each registered listener.
protected  void fireServiceAdded(ServiceEntry entry)
          Fires a service-added-event to each registered listener.
protected  void fireServiceRemoved(ServiceEntry entry)
          Fires a service-removed-event to each registered listener.
 java.util.List<Context> getParents()
          Returns a list of all parents.
<T> T
getService(java.lang.Class<? extends T> type)
          Finds and returns the first service implementation in this context that matches the given type.
 java.util.List<ServiceEntry> getServiceEntries()
          Returns a list of all service entries that are available in this context.
 void removeContextListener(ContextListener l)
          Remove the given ContextListener.
 void removeParent(Context parent)
          Removes the given parent Context from this context.
<T> T
removeService(java.lang.Class<? extends T> type)
          Removes the first service of the given type.
protected  ServiceEntry removeServiceEntry(Context origin, java.lang.Class<?> type)
          Removes the first service entry from this context that matches the given type and origially was placed into the given "origin" context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultContext

public DefaultContext()
Constructs a DefaultContext.

Method Detail

getParents

public java.util.List<Context> getParents()
Returns a list of all parents.

Specified by:
getParents in interface Context
Returns:
a list of all parents

addParent

public void addParent(Context parent)
Adds the given Context as a parent to the this context.

Specified by:
addParent in interface Context

removeParent

public void removeParent(Context parent)
Removes the given parent Context from this context.

Specified by:
removeParent in interface Context

addContextListener

public void addContextListener(ContextListener l)
Add the given ContextListener.

Specified by:
addContextListener in interface Context

removeContextListener

public void removeContextListener(ContextListener l)
Remove the given ContextListener.

Specified by:
removeContextListener in interface Context

fireParentAdded

protected void fireParentAdded(Context parent)
Fires a parentAdded-event to each registered listener.

Parameters:
parent - the parent this context has been added to

fireParentRemoved

protected void fireParentRemoved(Context parent)
Fires a parentRemoved-event to each registered listener.

Parameters:
parent - the parent this context has been removed from.

fireServiceAdded

protected void fireServiceAdded(ServiceEntry entry)
Fires a service-added-event to each registered listener.

Parameters:
entry - the service entry that has been added to this context

fireServiceRemoved

protected void fireServiceRemoved(ServiceEntry entry)
Fires a service-removed-event to each registered listener.

Parameters:
entry - the service entry that has been removed from this context

addService

public <T> boolean addService(java.lang.Class<? super T> type,
                              T service)
Place the given service into this Context and make it available for clients.

Specified by:
addService in interface Context
Type Parameters:
T - the generic type of the service
Parameters:
type - the type of the service. Clients will find the service by using this type.
service - the service implementation
Returns:
true, if the service has been added sucessfully

removeServiceEntry

protected ServiceEntry removeServiceEntry(Context origin,
                                          java.lang.Class<?> type)
Removes the first service entry from this context that matches the given type and origially was placed into the given "origin" context.

Parameters:
origin -
type -
Returns:
the removed entry

removeService

public <T> T removeService(java.lang.Class<? extends T> type)
Removes the first service of the given type.

Specified by:
removeService in interface Context
Returns:
the service that has been removed, or null otherwise.

getServiceEntries

public java.util.List<ServiceEntry> getServiceEntries()
Returns a list of all service entries that are available in this context. This includes services that are placed into this service directly as well as services that are placed into any of its parents.

Specified by:
getServiceEntries in interface Context
Returns:
a list of all service entries

findService

public ServiceEntry findService(java.lang.Class<?> type)
Finds and returns the first service entry in this context that matches the given type. If more than one service is available, this method returns the nearest entry whereby distance is defined as the number of hierarchical steps between this context and the context the service was originally placed into. If more services have the same distance this method returns that entry that was added first.

Specified by:
findService in interface Context
Returns:
the first service entry that matches the given type

getService

public <T> T getService(java.lang.Class<? extends T> type)
Finds and returns the first service implementation in this context that matches the given type. This method is a shortcut of Context.findService(Class). It returns the same result as ctx.findService(aType).getService().

Specified by:
getService in interface Context
Returns:
the first service implementation that matches the given type