org.beanfabrics.context
Interface Context

All Known Implementing Classes:
BnParentContext, DefaultContext, ModelContext

public interface Context

A Context is an hierarchical environment for service objects.

Call addService(Class, Object) to put a service object into this context. Call getService(Class) or findService(Class) to receive a reference to a service.

A context can be part of Zero-to-Many parents, and can itself be parent of Zero-to-Many children. Any service made available for a parent is also available for all children but not vice-versa.


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.
 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.
 

Method Detail

addParent

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

Parameters:
parent -

removeParent

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

Parameters:
parent -

getParents

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

Returns:
a list of all parents

addContextListener

void addContextListener(ContextListener l)
Add the given ContextListener.

Parameters:
l -

removeContextListener

void removeContextListener(ContextListener l)
Remove the given ContextListener.

Parameters:
l -

addService

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

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

removeService

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

Parameters:
type -
Returns:
the service that has been removed, or null otherwise.

getServiceEntries

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.

Returns:
a list of all service entries

findService

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.

Parameters:
type -
Returns:
the first service entry that matches the given type

getService

<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 findService(Class). It returns the same result as ctx.findService(aType).getService().

Type Parameters:
T -
Parameters:
type -
Returns:
the first service implementation that matches the given type