org.jboss.cache.factories
Class ComponentRegistry

java.lang.Object
  extended by org.jboss.cache.factories.ComponentRegistry
All Implemented Interfaces:
Lifecycle

public class ComponentRegistry
extends Object
implements Lifecycle

A registry where components which have been created are stored. Components are stored as singletons, registered under a specific name.

Components can be retrieved from the registry using getComponent(Class).

Components can be registered using registerComponent(Object, Class), which will cause any dependencies to be wired in as well. Components that need to be created as a result of wiring will be done using getOrCreateComponent(Class), which will look up the default factory for the component type (factories annotated with the appropriate DefaultFactoryFor annotation.

Default factories are treated as components too and will need to be wired before being used.

The registry can exist in one of several states, as defined by the CacheStatus enumeration. In terms of the cache, state changes in the following manner:

Cache configuration can only be changed and will only be reinjected if the cache is not in the CacheStatus.STARTED state.

Since:
2.1.0
Author:
Manik Surtani (manik AT jboss DOT org)

Nested Class Summary
 class ComponentRegistry.Component
          A wrapper representing a component in the registry
 
Field Summary
protected static Object NULL_COMPONENT
           
 
Constructor Summary
ComponentRegistry(Configuration configuration, CacheSPI cache)
          Creates an instance of the component registry.
 
Method Summary
protected  void addComponentDependencies(ComponentRegistry.Component c)
          Adds component dependencies for a given component, by populating ComponentRegistry.Component.injectionMethods.
 void create()
          Creates the components needed by a cache instance and sets the cache status to CacheStatus.CREATED when it is done.
 void destroy()
          Destroys the cache and frees up any resources.
<T> T
getComponent(Class<T> type)
          Retrieves a component of a specified type from the registry, or null if it cannot be found.
protected  Configuration getConfiguration()
          Retrieves the configuration component.
protected  ComponentFactory getFactory(Class componentClass)
          Retrieves a component factory instance capable of constructing components of a specified type.
protected
<T> T
getFromConfiguration(Class<T> componentClass)
          Retrieves a component from the Configuration or RuntimeConfig.
protected
<T> T
getOrCreateComponent(Class<T> componentClass)
          Retrieves a component if one exists, and if not, attempts to find a factory capable of constructing the component (factories annotated with the DefaultFactoryFor annotation that is capable of creating the component class).
 Set<ComponentRegistry.Component> getRegiteredComponents()
          Returns an immutable set contating all the components that exists in the reporsitory at this moment.
 CacheStatus getState()
          Retrieves the state of the registry
 boolean invocationsAllowed(boolean originLocal)
          Asserts whether invocations are allowed on the cache or not.
protected  void invokeInjectionMethod(Object o, Method m)
           
 void registerComponent(Object component, Class type)
          Registers a component in the registry under the given type, and injects any dependencies needed.
 void registerDefaultClassLoader(ClassLoader loader)
          Registers the default class loader.
 void resetNonVolatile()
          Removes any components not annotated as @NonVolatile.
 void rewire()
          Rewires components.
 void setBlockInStarting(boolean blockInStarting)
           
 void start()
          This starts the components in the cache, connecting to channels, starting service threads, etc.
 void stop()
          Stops the cache and sets the cache status to CacheStatus.STOPPED once it is done.
 void wireDependencies(Object target)
          Wires an object instance with dependencies annotated with the Inject annotation, creating more components as needed based on the Configuration passed in if these additional components don't exist in the ComponentRegistry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL_COMPONENT

protected static final Object NULL_COMPONENT
Constructor Detail

ComponentRegistry

public ComponentRegistry(Configuration configuration,
                         CacheSPI cache)
Creates an instance of the component registry. The configuration passed in is automatically registered.

Parameters:
configuration - configuration with which this is created
Method Detail

getState

public CacheStatus getState()
Retrieves the state of the registry

Returns:
state of the registry

wireDependencies

public void wireDependencies(Object target)
                      throws ConfigurationException
Wires an object instance with dependencies annotated with the Inject annotation, creating more components as needed based on the Configuration passed in if these additional components don't exist in the ComponentRegistry. Strictly for components that don't otherwise live in the registry and have a lifecycle, such as Nodes.

Parameters:
target - object to wire
Throws:
ConfigurationException - if there is a problem wiring the instance

registerDefaultClassLoader

public void registerDefaultClassLoader(ClassLoader loader)
Registers the default class loader. This method *must* be called before any other components are registered, typically called by bootstrap code. Defensively, it is called in the constructor of ComponentRegistry with a null parameter.

Parameters:
loader - a class loader to use by default. If this is null, the class loader used to load this instance of ComponentRegistry is used.

registerComponent

public void registerComponent(Object component,
                              Class type)
Registers a component in the registry under the given type, and injects any dependencies needed. If a component of this type already exists, it is overwritten.

Parameters:
component - component to register
type - type of component

addComponentDependencies

protected void addComponentDependencies(ComponentRegistry.Component c)
Adds component dependencies for a given component, by populating ComponentRegistry.Component.injectionMethods.

Parameters:
c - component to add dependencies to

invokeInjectionMethod

protected void invokeInjectionMethod(Object o,
                                     Method m)

getOrCreateComponent

protected <T> T getOrCreateComponent(Class<T> componentClass)
Retrieves a component if one exists, and if not, attempts to find a factory capable of constructing the component (factories annotated with the DefaultFactoryFor annotation that is capable of creating the component class).

If an instance needs to be constructed, dependencies are then automatically wired into the instance, based on methods on the component type annotated with Inject.

Summing it up, component retrieval happens in the following order:
1. Look for a component that has already been created and registered. 2. Look for an appropriate component that exists in the Configuration that may be injected from an external system. 3. Look for a class definition passed in to the Configuration - such as an EvictionPolicy implementation 4. Attempt to create it by looking for an appropriate factory (annotated with DefaultFactoryFor)

Parameters:
componentClass - type of component to be retrieved. Should not be null.
Returns:
a fully wired component instance, or null if one cannot be found or constructed.
Throws:
ConfigurationException - if there is a problem with consructing or wiring the instance.

getFactory

protected ComponentFactory getFactory(Class componentClass)
Retrieves a component factory instance capable of constructing components of a specified type. If the factory doesn't exist in the registry, one is created.

Parameters:
componentClass - type of component to construct
Returns:
component factory capable of constructing such components

getFromConfiguration

protected <T> T getFromConfiguration(Class<T> componentClass)
Retrieves a component from the Configuration or RuntimeConfig.

Parameters:
componentClass - component type
Returns:
component, or null if it cannot be found

getConfiguration

protected Configuration getConfiguration()
Retrieves the configuration component.

Returns:
a Configuration object

getComponent

public <T> T getComponent(Class<T> type)
Retrieves a component of a specified type from the registry, or null if it cannot be found.

Parameters:
type - type to find
Returns:
component, or null

rewire

public void rewire()
Rewires components. Can only be called if the current state is WIRED or STARTED.


resetNonVolatile

public void resetNonVolatile()
Removes any components not annotated as @NonVolatile.


create

public void create()
Creates the components needed by a cache instance and sets the cache status to CacheStatus.CREATED when it is done.

Specified by:
create in interface Lifecycle

start

public void start()
This starts the components in the cache, connecting to channels, starting service threads, etc. If the cache is not in the CacheStatus.CREATED state, create() will be invoked first.

Specified by:
start in interface Lifecycle

stop

public void stop()
Stops the cache and sets the cache status to CacheStatus.STOPPED once it is done. If the cache is not in the CacheStatus.STARTED state, this is a no-op.

Specified by:
stop in interface Lifecycle

destroy

public void destroy()
Destroys the cache and frees up any resources. Sets the cache status to CacheStatus.DESTROYED when it is done.

If the cache is in CacheStatus.STARTED when this method is called, it will first call stop() to stop the cache.

Specified by:
destroy in interface Lifecycle

invocationsAllowed

public boolean invocationsAllowed(boolean originLocal)
Asserts whether invocations are allowed on the cache or not. Returns true if invocations are to be allowed, false otherwise. If the origin of the call is remote and the cache status is CacheStatus.STARTING, this method will block for up to Configuration.getStateRetrievalTimeout() millis, checking for a valid state.

Parameters:
originLocal - true if the call originates locally (i.e., from the CacheInvocationDelegate or false if it originates remotely, i.e., from the CommandAwareRpcDispatcher.
Returns:
true if invocations are allowed, false otherwise.

getRegiteredComponents

public Set<ComponentRegistry.Component> getRegiteredComponents()
Returns an immutable set contating all the components that exists in the reporsitory at this moment.


setBlockInStarting

public void setBlockInStarting(boolean blockInStarting)


Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.