Package org.infinispan.factories
Class AbstractComponentRegistry
- java.lang.Object
-
- org.infinispan.factories.AbstractComponentRegistry
-
- All Implemented Interfaces:
Cloneable
,org.infinispan.commons.api.Lifecycle
- Direct Known Subclasses:
ComponentRegistry
,GlobalComponentRegistry
public abstract class AbstractComponentRegistry extends Object implements org.infinispan.commons.api.Lifecycle, Cloneable
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 usinggetComponent(Class)
. Components can be registered usingregisterComponent(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 usinggetOrCreateComponent(Class)
, which will look up the default factory for the component type (factories annotated with the appropriateDefaultFactoryFor
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 theComponentStatus
enumeration. In terms of the cache, state changes in the following manner:- INSTANTIATED - when first constructed
- CONSTRUCTED - when created using the DefaultCacheFactory
- STARTED - when
Lifecycle.start()
is called - STOPPED - when
Cache.stop()
is called
ComponentStatus.RUNNING
state. Thread Safety: instances ofGlobalComponentRegistry
can be concurrently updated so all the write operations are serialized through class intrinsic lock.- Since:
- 4.0
- Author:
- Manik Surtani, Galder ZamarreƱo
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AbstractComponentRegistry.Component
A wrapper representing a component in the registry
-
Field Summary
Fields Modifier and Type Field Description static boolean
DEBUG_DEPENDENCIES
Set the system property infinispan.debugDependencies to true to enable some extra information to errors generated by the component factory.protected ComponentStatus
state
-
Constructor Summary
Constructors Constructor Description AbstractComponentRegistry()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addShutdownHook()
AbstractComponentRegistry
clone()
protected AbstractComponentFactory
createComponentFactoryInternal(Class<?> componentClass, String cfClass)
protected abstract ClassLoader
getClassLoader()
<T> T
getComponent(Class<T> type)
Retrieves a component of a specified type from the registry, or null if it cannot be found.<T> T
getComponent(Class<T> componentClass, String name)
<T> T
getComponent(String componentClassName)
<T> T
getComponent(String componentClassName, String name)
<T> T
getComponent(String componentClassName, String name, boolean nameIsFQCN)
abstract ComponentMetadataRepo
getComponentMetadataRepo()
protected Configuration
getConfiguration()
Retrieves the configuration component.protected AbstractComponentFactory
getFactory(Class<?> componentClass)
Retrieves a component factory instance capable of constructing components of a specified type.protected abstract Log
getLog()
<T> Optional<T>
getOptionalComponent(Class<T> type)
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 theDefaultFactoryFor
annotation that is capable of creating the component class).protected <T> T
getOrCreateComponent(Class<T> componentClass, String name)
protected <T> T
getOrCreateComponent(Class<T> componentClass, String name, boolean nameIsFQCN)
Set<AbstractComponentRegistry.Component>
getRegisteredComponents()
Returns an immutable set containing all the components that exists in the repository at this moment.ComponentStatus
getStatus()
Retrieves the state of the registryabstract TimeService
getTimeService()
boolean
invocationsAllowed(boolean originLocal)
Asserts whether invocations are allowed on the cache or not.protected AbstractComponentRegistry.Component
lookupComponent(String componentClassName, String componentName, boolean nameIsFQCN)
protected void
postStart()
void
registerComponent(Object component, Class<?> type)
Registers a component in the registry under the given type, and injects any dependencies needed.void
registerComponent(Object component, String name)
void
registerComponent(Object component, String name, boolean nameIsFQCN)
protected void
registerComponentInternal(Object component, String name, boolean nameIsFQCN)
protected ClassLoader
registerDefaultClassLoader(ClassLoader loader)
Registers the default class loader.protected void
registerNonVolatileComponent(Object component, Class<?> type)
protected void
registerNonVolatileComponent(Object component, String name)
protected void
registerNullComponent(String name)
registers a special "null" component that has no dependencies.protected void
removeShutdownHook()
void
resetVolatileComponents()
Removes any components not annotated as @SurvivesRestarts.void
rewire()
Rewires components.void
start()
This starts the components in the registry, connecting to channels, starting service threads, etc.void
stop()
Stops the component and sets its status toComponentStatus.TERMINATED
once it is done.protected void
throwStackAwareConfigurationException(String message)
void
wireDependencies(Object target)
Wires an object instance with dependencies annotated with theInject
annotation, creating more components as needed based on the Configuration passed in if these additional components don't exist in theComponentRegistry
.
-
-
-
Field Detail
-
DEBUG_DEPENDENCIES
public static final boolean DEBUG_DEPENDENCIES
Set the system property- infinispan.debugDependencies
to- true
to enable some extra information to errors generated by the component factory.
-
state
protected volatile ComponentStatus state
-
-
Method Detail
-
getStatus
public ComponentStatus getStatus()
Retrieves the state of the registry- Returns:
- state of the registry
-
getClassLoader
protected abstract ClassLoader getClassLoader()
-
getLog
protected abstract Log getLog()
-
getComponentMetadataRepo
public abstract ComponentMetadataRepo getComponentMetadataRepo()
-
wireDependencies
public void wireDependencies(Object target) throws org.infinispan.commons.CacheConfigurationException
Wires an object instance with dependencies annotated with theInject
annotation, creating more components as needed based on the Configuration passed in if these additional components don't exist in theComponentRegistry
. Strictly for components that don't otherwise live in the registry and have a lifecycle, such as Commands.- Parameters:
target
- object to wire- Throws:
org.infinispan.commons.CacheConfigurationException
- if there is a problem wiring the instance
-
registerComponent
public final 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 registertype
- type of component
-
registerComponent
public final void registerComponent(Object component, String name, boolean nameIsFQCN)
-
registerNonVolatileComponent
protected final void registerNonVolatileComponent(Object component, String name)
-
registerNonVolatileComponent
protected final void registerNonVolatileComponent(Object component, Class<?> type)
-
registerComponentInternal
protected void registerComponentInternal(Object component, String name, boolean nameIsFQCN)
-
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 theDefaultFactoryFor
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 withInject
. 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 theConfiguration
that may be injected from an external system. 3. Look for a class definition passed in to theConfiguration
- such as an EvictionPolicy implementation 4. Attempt to create it by looking for an appropriate factory (annotated withDefaultFactoryFor
)- 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:
org.infinispan.commons.CacheConfigurationException
- if there is a problem with constructing or wiring the instance.
-
getOrCreateComponent
protected <T> T getOrCreateComponent(Class<T> componentClass, String name, boolean nameIsFQCN)
-
getFactory
protected AbstractComponentFactory 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
-
createComponentFactoryInternal
protected AbstractComponentFactory createComponentFactoryInternal(Class<?> componentClass, String cfClass)
-
lookupComponent
protected AbstractComponentRegistry.Component lookupComponent(String componentClassName, String componentName, boolean nameIsFQCN)
-
registerNullComponent
protected final void registerNullComponent(String name)
registers a special "null" component that has no dependencies.- Parameters:
name
- name of component to register as a null
-
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
-
getComponent
public <T> T getComponent(String componentClassName)
-
registerDefaultClassLoader
protected ClassLoader 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.
-
rewire
public void rewire()
Rewires components. Used to rewire components in the CR if a cache has been stopped (moved to state TERMINATED), which would (almost) empty the registry of components. Rewiring will re-inject all dependencies so that the cache can be started again.
-
resetVolatileComponents
public void resetVolatileComponents()
Removes any components not annotated as @SurvivesRestarts.
-
start
public void start()
This starts the components in the registry, connecting to channels, starting service threads, etc. If the component is not in theComponentStatus.INITIALIZING
state, it will be initialized first.- Specified by:
start
in interfaceorg.infinispan.commons.api.Lifecycle
-
postStart
protected void postStart()
-
stop
public void stop()
Stops the component and sets its status toComponentStatus.TERMINATED
once it is done. If the component is not in theComponentStatus.RUNNING
state, this is a no-op.- Specified by:
stop
in interfaceorg.infinispan.commons.api.Lifecycle
-
addShutdownHook
protected void addShutdownHook()
-
removeShutdownHook
protected void removeShutdownHook()
-
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 isComponentStatus.INITIALIZING
, this method will block for up toStateTransferConfiguration.timeout()
millis, checking for a valid state.- Parameters:
originLocal
- true if the call originates locally (i.e., from theCacheImpl
or false if it originates remotely, i.e., from theInboundInvocationHandler
.- Returns:
- true if invocations are allowed, false otherwise.
-
getRegisteredComponents
public Set<AbstractComponentRegistry.Component> getRegisteredComponents()
Returns an immutable set containing all the components that exists in the repository at this moment.- Returns:
- a set of components
-
clone
public AbstractComponentRegistry clone() throws CloneNotSupportedException
- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
-
getTimeService
public abstract TimeService getTimeService()
-
throwStackAwareConfigurationException
protected void throwStackAwareConfigurationException(String message)
-
-