public class ComponentRegistry extends Object implements Lifecycle
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.create()
is called, the components are rewired.Cache.start()
is calledCache.stop()
is calledCache.destroy()
is called.CacheStatus.STARTED
state.Modifier and Type | Class and Description |
---|---|
class |
ComponentRegistry.Component
A wrapper representing a component in the registry
|
Modifier and Type | Field and Description |
---|---|
protected static Object |
NULL_COMPONENT |
Constructor and Description |
---|
ComponentRegistry(Configuration configuration,
CacheSPI cache)
Creates an instance of the component registry.
|
Modifier and Type | Method and Description |
---|---|
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> |
getRegisteredComponents()
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 SkipBlockUntilStart)
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 |
setStatusCheckNecessary(boolean statusCheckNecessary) |
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 . |
protected static final Object NULL_COMPONENT
public ComponentRegistry(Configuration configuration, CacheSPI cache)
configuration
- configuration with which this is createdpublic CacheStatus getState()
public void wireDependencies(Object target) throws ConfigurationException
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.target
- object to wireConfigurationException
- if there is a problem wiring the instancepublic void registerDefaultClassLoader(ClassLoader loader)
loader
- a class loader to use by default. If this is null, the class loader used to load this instance of ComponentRegistry is used.public void registerComponent(Object component, Class type)
component
- component to registertype
- type of componentprotected void addComponentDependencies(ComponentRegistry.Component c)
ComponentRegistry.Component.injectionMethods
.c
- component to add dependencies toprotected <T> T getOrCreateComponent(Class<T> componentClass)
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: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
)
componentClass
- type of component to be retrieved. Should not be null.ConfigurationException
- if there is a problem with consructing or wiring the instance.protected ComponentFactory getFactory(Class componentClass)
componentClass
- type of component to constructprotected <T> T getFromConfiguration(Class<T> componentClass)
Configuration
or RuntimeConfig
.componentClass
- component typeprotected Configuration getConfiguration()
public <T> T getComponent(Class<T> type)
type
- type to findpublic void rewire()
public void resetNonVolatile()
public void create()
CacheStatus.CREATED
when it is done.public void start()
CacheStatus.CREATED
state, create()
will be invoked first.public void stop()
CacheStatus.STOPPED
once it is done. If the cache is not in
the CacheStatus.STARTED
state, this is a no-op.public void destroy()
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.public boolean invocationsAllowed(boolean SkipBlockUntilStart)
CacheStatus.STARTING
,
this method will block for up to Configuration.getStateRetrievalTimeout()
millis, checking
for a valid state.SkipBlockUntilStart
- true if the call should not wait for the cache to start (typical local use) and false if we should try and wait (typical remote lookup use)public Set<ComponentRegistry.Component> getRegisteredComponents()
public void setStatusCheckNecessary(boolean statusCheckNecessary)
Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.