Class RegistryImpl
- java.lang.Object
-
- org.apache.tapestry5.ioc.internal.RegistryImpl
-
- All Implemented Interfaces:
ObjectLocator
,InternalRegistry
,ServiceProxyProvider
,OperationTracker
,Registry
,RegistryShutdownHub
public class RegistryImpl extends java.lang.Object implements Registry, InternalRegistry, ServiceProxyProvider
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.tapestry5.ioc.OperationTracker
OperationTracker.NonLoggableException
-
-
Constructor Summary
Constructors Constructor Description RegistryImpl(java.util.Collection<ModuleDef2> moduleDefs, PlasticProxyFactory proxyFactory, LoggerSource loggerSource, OperationTracker operationTracker)
Constructs the registry from a set of module definitions and other resources.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRegistryShutdownListener(java.lang.Runnable listener)
Adds a listener for eventual notification when the registry shuts down.void
addRegistryShutdownListener(RegistryShutdownListener listener)
Adds a listener for eventual notification.void
addRegistryWillShutdownListener(java.lang.Runnable listener)
Adds a listener for eventual notification.<T> T
autobuild(java.lang.Class<T> clazz)
Autobuilds a class by finding the public constructor with the most parameters.<T> T
autobuild(java.lang.String description, java.lang.Class<T> clazz)
Preferred version ofObjectLocator.autobuild(Class)
that tracks the operation usingOperationTracker.invoke(String, org.apache.tapestry5.ioc.Invokable)
.void
cleanupThread()
Invoked at the end of a request to discard any thread-specific information accumulated during the current request.java.lang.String
expandSymbols(java.lang.String input)
Given an input string that may contain symbols, returns the string with any and all symbols fully expanded.java.util.List<ServiceAdvisor>
findAdvisorsForService(ServiceDef3 serviceDef)
Searches for advisors for a particular service, returning them in order of application.java.util.List<ServiceDecorator>
findDecoratorsForService(ServiceDef3 serviceDef)
Searches for decorators for a particular service.<K,V>
java.util.Map<K,V>getMappedConfiguration(ServiceDef3 serviceDef, java.lang.Class<K> keyType, java.lang.Class<V> objectType)
Builds up a map of key/value pairs by invoking service contribution methods that target the service (from any module, unless the service is private).java.util.Set<java.lang.Class>
getMarkerAnnotations()
Returns a Set of Annotation classes that are used as service markers.<T> T
getObject(java.lang.Class<T> objectType, AnnotationProvider annotationProvider)
Obtains an object indirectly, using theMasterObjectProvider
service.<T> T
getObject(java.lang.Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator, Module localModule)
As withObjectLocator.getObject(Class, org.apache.tapestry5.commons.AnnotationProvider)
, but handles theLocal
annotation.<T> java.util.List<T>
getOrderedConfiguration(ServiceDef3 serviceDef, java.lang.Class<T> objectType)
Builds up an ordered collection by invoking service contributor methods that target the service (from any module, unless the service is private).<T> T
getService(java.lang.Class<T> serviceInterface)
Locates a service given a service interface and (optionally) some marker annotation types.<T> T
getService(java.lang.Class<T> serviceInterface, java.lang.Class<? extends java.lang.annotation.Annotation>... markerTypes)
Locates a service given a service interface and (optionally) some marker annotation types.<T> T
getService(java.lang.String serviceId, java.lang.Class<T> serviceInterface)
Obtains a service via its unique service id.ServiceLifecycle2
getServiceLifecycle(java.lang.String scope)
Returns a service lifecycle by service scope name.org.slf4j.Logger
getServiceLogger(java.lang.String serviceId)
Returns a logger for the service, which consists of the Module'slog name
suffixed with a period and the service id.<T> java.util.Collection<T>
getUnorderedConfiguration(ServiceDef3 serviceDef, java.lang.Class<T> objectType)
Builds up an unordered collection by invoking service contributor methods that target the service (from any module, unless the service is private).<T> T
invoke(java.lang.String description, Invokable<T> operation)
As withOperationTracker.run(String, Runnable)
, but the operation may return a value.<T> T
perform(java.lang.String description, IOOperation<T> operation)
As withOperationTracker.invoke(String, Invokable)
, but the operation may throw anIOException
.void
performRegistryStartup()
It's not unreasonable for an eagerly-loaded service to decide to start a thread, at which point we raise issues about improper publishing of the Registry instance from the RegistryImpl constructor.java.lang.Object
provideServiceProxy(java.lang.String serviceId)
Look up the service and return its proxy.<T> T
proxy(java.lang.Class<T> interfaceClass, java.lang.Class<? extends T> implementationClass)
Creates a proxy.<T> T
proxy(java.lang.Class<T> interfaceClass, java.lang.Class<? extends T> implementationClass, ObjectLocator locator)
Creates a just-in-time (and possibly, live reloading) proxy for the indicated class and interface, using the provided locator to autobuild the implementationClass (when necessary).void
run(java.lang.String description, java.lang.Runnable operation)
Executes the operation.void
shutdown()
Shuts down a Registry instance.
-
-
-
Constructor Detail
-
RegistryImpl
public RegistryImpl(java.util.Collection<ModuleDef2> moduleDefs, PlasticProxyFactory proxyFactory, LoggerSource loggerSource, OperationTracker operationTracker)
Constructs the registry from a set of module definitions and other resources.- Parameters:
moduleDefs
- defines the modules (and builders, decorators, etc., within)proxyFactory
- used to create new proxy objectsloggerSource
- used to obtain Logger instancesoperationTracker
-
-
-
Method Detail
-
performRegistryStartup
public void performRegistryStartup()
It's not unreasonable for an eagerly-loaded service to decide to start a thread, at which point we raise issues about improper publishing of the Registry instance from the RegistryImpl constructor. Moving eager loading of services out to its own method should ensure thread safety.- Specified by:
performRegistryStartup
in interfaceRegistry
-
getServiceLogger
public org.slf4j.Logger getServiceLogger(java.lang.String serviceId)
Description copied from interface:InternalRegistry
Returns a logger for the service, which consists of the Module'slog name
suffixed with a period and the service id.- Specified by:
getServiceLogger
in interfaceInternalRegistry
- Returns:
- the logger for the service
-
shutdown
public void shutdown()
Description copied from interface:Registry
Shuts down a Registry instance. Notifies all listeners that the registry has shutdown. Further method invocations on the Registry are no longer allowed, and the Registry instance should be discarded.- Specified by:
shutdown
in interfaceRegistry
- See Also:
RegistryShutdownHub
,RegistryShutdownListener
-
getService
public <T> T getService(java.lang.String serviceId, java.lang.Class<T> serviceInterface)
Description copied from interface:ObjectLocator
Obtains a service via its unique service id. Returns the service's proxy. The service proxy implements the same interface as the actual service, and is used to instantiate the actual service only as needed (this is transparent to the application).- Specified by:
getService
in interfaceObjectLocator
- Parameters:
serviceId
- unique Service id used to locate the service object (may contain symbols, which will be expanded), case is ignoredserviceInterface
- the interface implemented by the service (or an interface extended by the service interface)- Returns:
- the service instance
-
cleanupThread
public void cleanupThread()
Description copied from interface:Registry
Invoked at the end of a request to discard any thread-specific information accumulated during the current request.- Specified by:
cleanupThread
in interfaceRegistry
- See Also:
PerthreadManager
,ThreadCleanupListener
-
getUnorderedConfiguration
public <T> java.util.Collection<T> getUnorderedConfiguration(ServiceDef3 serviceDef, java.lang.Class<T> objectType)
Description copied from interface:InternalRegistry
Builds up an unordered collection by invoking service contributor methods that target the service (from any module, unless the service is private).- Specified by:
getUnorderedConfiguration
in interfaceInternalRegistry
- Parameters:
serviceDef
- defines the service for which configuration data is being assembledobjectType
- identifies the type of object allowed into the collection- Returns:
- the final collection
-
getOrderedConfiguration
public <T> java.util.List<T> getOrderedConfiguration(ServiceDef3 serviceDef, java.lang.Class<T> objectType)
Description copied from interface:InternalRegistry
Builds up an ordered collection by invoking service contributor methods that target the service (from any module, unless the service is private). Once all values have been added (each with an id, and pre/post constraints), the values are ordered, null values dropped, and the final sorted list is returned.- Specified by:
getOrderedConfiguration
in interfaceInternalRegistry
- Parameters:
serviceDef
- defines the service for which configuration data is being assembledobjectType
- identifies the type of object allowed into the collection- Returns:
- the final ordered list
-
getMappedConfiguration
public <K,V> java.util.Map<K,V> getMappedConfiguration(ServiceDef3 serviceDef, java.lang.Class<K> keyType, java.lang.Class<V> objectType)
Description copied from interface:InternalRegistry
Builds up a map of key/value pairs by invoking service contribution methods that target the service (from any module, unless the service is private). Values and keys may not be null. Invalid values (keys or values that are the wrong type, or duplicate keys) result in warnings and are ignored.- Specified by:
getMappedConfiguration
in interfaceInternalRegistry
- Parameters:
serviceDef
- defines the service for which configuration data is being assembledkeyType
- identifies the type of key object allowed into the mapobjectType
- identifies the type of value object allowed into the map- Returns:
- the final ordered list
-
getService
public <T> T getService(java.lang.Class<T> serviceInterface)
Description copied from interface:ObjectLocator
Locates a service given a service interface and (optionally) some marker annotation types. A single service must implement the service interface (which * can be hard to guarantee) and by marked by all the marker types. The search takes into account inheritance of the service interface (not the service implementation), which may result in a failure due to extra matches.- Specified by:
getService
in interfaceObjectLocator
- Parameters:
serviceInterface
- the interface the service implements- Returns:
- the service's proxy
- See Also:
Marker
-
getService
public <T> T getService(java.lang.Class<T> serviceInterface, java.lang.Class<? extends java.lang.annotation.Annotation>... markerTypes)
Description copied from interface:ObjectLocator
Locates a service given a service interface and (optionally) some marker annotation types. A single service must implement the service interface (which * can be hard to guarantee) and by marked by all the marker types. The search takes into account inheritance of the service interface (not the service implementation), which may result in a failure due to extra matches. The ability to specify marker annotation types was added in 5.3- Specified by:
getService
in interfaceObjectLocator
- Parameters:
serviceInterface
- the interface the service implementsmarkerTypes
- Markers used to select a specific service that implements the interface- Returns:
- the service's proxy
- See Also:
Marker
-
getServiceLifecycle
public ServiceLifecycle2 getServiceLifecycle(java.lang.String scope)
Description copied from interface:InternalRegistry
Returns a service lifecycle by service scope name.- Specified by:
getServiceLifecycle
in interfaceInternalRegistry
- Parameters:
scope
- the name of the service scope (case insensitive)- Returns:
- the lifecycle corresponding to the scope
-
findDecoratorsForService
public java.util.List<ServiceDecorator> findDecoratorsForService(ServiceDef3 serviceDef)
Description copied from interface:InternalRegistry
Searches for decorators for a particular service. The resultingDecoratorDef
s are ordered, then converted intoServiceDecorator
s.- Specified by:
findDecoratorsForService
in interfaceInternalRegistry
-
findAdvisorsForService
public java.util.List<ServiceAdvisor> findAdvisorsForService(ServiceDef3 serviceDef)
Description copied from interface:InternalRegistry
Searches for advisors for a particular service, returning them in order of application.- Specified by:
findAdvisorsForService
in interfaceInternalRegistry
-
getObject
public <T> T getObject(java.lang.Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator, Module localModule)
Description copied from interface:InternalRegistry
As withObjectLocator.getObject(Class, org.apache.tapestry5.commons.AnnotationProvider)
, but handles theLocal
annotation.- Specified by:
getObject
in interfaceInternalRegistry
- Parameters:
objectType
- type of object o be injectedannotationProvider
- access to annotations at point of injectionlocator
- used to resolve any subsequent injectionslocalModule
- module to limit services to, if Local annotaton present- Returns:
- the service or object
-
getObject
public <T> T getObject(java.lang.Class<T> objectType, AnnotationProvider annotationProvider)
Description copied from interface:ObjectLocator
Obtains an object indirectly, using theMasterObjectProvider
service.- Specified by:
getObject
in interfaceObjectLocator
- Parameters:
objectType
- the type of object to be returnedannotationProvider
- provides access to annotations on the field or parameter for which a value is to be obtained, which may be utilized in selecting an appropriate object, use null when annotations are not available (in which case, selection will be based only on the object type)- Returns:
- the requested object
- See Also:
ObjectProvider
-
addRegistryShutdownListener
public void addRegistryShutdownListener(RegistryShutdownListener listener)
Description copied from interface:RegistryShutdownHub
Adds a listener for eventual notification.- Specified by:
addRegistryShutdownListener
in interfaceRegistryShutdownHub
-
addRegistryShutdownListener
public void addRegistryShutdownListener(java.lang.Runnable listener)
Description copied from interface:RegistryShutdownHub
Adds a listener for eventual notification when the registry shuts down. Runtime exceptions thrown by the listener will be logged and ignored.- Specified by:
addRegistryShutdownListener
in interfaceRegistryShutdownHub
-
addRegistryWillShutdownListener
public void addRegistryWillShutdownListener(java.lang.Runnable listener)
Description copied from interface:RegistryShutdownHub
Adds a listener for eventual notification. RegistryWillShutdownListeners are notified before any standard listeners, and before service proxies and other parts of the Registry are disabled. Runtime exceptions thrown by the listener will be logged and ignored.- Specified by:
addRegistryWillShutdownListener
in interfaceRegistryShutdownHub
-
expandSymbols
public java.lang.String expandSymbols(java.lang.String input)
Description copied from interface:InternalRegistry
Given an input string that may contain symbols, returns the string with any and all symbols fully expanded.- Specified by:
expandSymbols
in interfaceInternalRegistry
- Returns:
- expanded input
-
autobuild
public <T> T autobuild(java.lang.String description, java.lang.Class<T> clazz)
Description copied from interface:ObjectLocator
Preferred version ofObjectLocator.autobuild(Class)
that tracks the operation usingOperationTracker.invoke(String, org.apache.tapestry5.ioc.Invokable)
.- Specified by:
autobuild
in interfaceObjectLocator
- Parameters:
description
- description used withOperationTracker
clazz
- the type of object to instantiate- Returns:
- the instantiated instance
-
autobuild
public <T> T autobuild(java.lang.Class<T> clazz)
Description copied from interface:ObjectLocator
Autobuilds a class by finding the public constructor with the most parameters. Services and other resources or dependencies will be injected into the parameters of the constructor and into private fields marked with theInject
annotation. There are two cases: constructing a service implementation, and constructing an arbitrary object. In the former case, many service resources are also available for injection, not just dependencies or objects provided via the MasterObjectProvider service.- Specified by:
autobuild
in interfaceObjectLocator
- Parameters:
clazz
- the type of object to instantiate- Returns:
- the instantiated instance
-
proxy
public <T> T proxy(java.lang.Class<T> interfaceClass, java.lang.Class<? extends T> implementationClass)
Description copied from interface:ObjectLocator
Creates a proxy. The proxy will defer invocation ofObjectLocator.autobuild(Class)
until just-in-time (that is, first method invocation). In a limited number of cases, it is necessary to use such a proxy to prevent service construction cycles, particularly when contributing (directly or indirectly) to theMasterObjectProvider
(which is itself at the heart of autobuilding). If the class file for the class is a file on the file system (not a file packaged in a JAR), then the proxy will autoreload: changing the class file will result in the new class being reloaded and re-instantiated (with dependencies).- Specified by:
proxy
in interfaceObjectLocator
- Parameters:
interfaceClass
- the interface implemented by the proxyimplementationClass
- a concrete class that implements the interface- Returns:
- a proxy
- See Also:
ObjectLocator.autobuild(Class)
-
proxy
public <T> T proxy(java.lang.Class<T> interfaceClass, java.lang.Class<? extends T> implementationClass, ObjectLocator locator)
Description copied from interface:InternalRegistry
Creates a just-in-time (and possibly, live reloading) proxy for the indicated class and interface, using the provided locator to autobuild the implementationClass (when necessary).- Specified by:
proxy
in interfaceInternalRegistry
-
provideServiceProxy
public java.lang.Object provideServiceProxy(java.lang.String serviceId)
Description copied from interface:ServiceProxyProvider
Look up the service and return its proxy.- Specified by:
provideServiceProxy
in interfaceServiceProxyProvider
- Parameters:
serviceId
- the id of the service to obtain- Returns:
- the service proxy
-
run
public void run(java.lang.String description, java.lang.Runnable operation)
Description copied from interface:OperationTracker
Executes the operation. If the operation throws aRuntimeException
it will be logged and rethrown wrapped as aOperationException
.- Specified by:
run
in interfaceOperationTracker
- Parameters:
description
- used if there is an exceptionoperation
- to execute
-
invoke
public <T> T invoke(java.lang.String description, Invokable<T> operation)
Description copied from interface:OperationTracker
As withOperationTracker.run(String, Runnable)
, but the operation may return a value.- Specified by:
invoke
in interfaceOperationTracker
- Parameters:
description
- used if there is an exceptionoperation
- to invoke- Returns:
- result of operation
-
perform
public <T> T perform(java.lang.String description, IOOperation<T> operation) throws java.io.IOException
Description copied from interface:OperationTracker
As withOperationTracker.invoke(String, Invokable)
, but the operation may throw anIOException
.- Specified by:
perform
in interfaceOperationTracker
- Parameters:
description
- used if there is an exception (outside of IOException)operation
- to perform- Returns:
- result of operation
- Throws:
java.io.IOException
-
getMarkerAnnotations
public java.util.Set<java.lang.Class> getMarkerAnnotations()
Description copied from interface:InternalRegistry
Returns a Set of Annotation classes that are used as service markers.- Specified by:
getMarkerAnnotations
in interfaceInternalRegistry
-
-