java.lang.Object
io.helidon.inject.runtime.AbstractServiceProvider<T>
- Type Parameters:
T
- the type of the service this provider manages
- All Implemented Interfaces:
Weighted
,ActivationPhaseReceiver
,Activator
,DeActivator
,InjectionPointProvider<T>
,Resettable
,ServiceProvider<T>
,ServiceProviderBindable<T>
,Provider<T>
,Comparable<Weighted>
- Direct Known Subclasses:
ConfigDrivenServiceProviderBase
,ReflectionBasedSingletonServiceProvider
public abstract class AbstractServiceProvider<T>
extends Object
implements ServiceProviderBindable<T>, Activator, DeActivator, ActivationPhaseReceiver, Resettable
Abstract base implementation for
ServiceProviderBindable
, which represents the basics for regular
Singleton, ApplicationScoped, Provider, and ServiceProvider based managed services. All code-generated services will
extend from this abstract base class.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
Represents a result of a phase transition. -
Field Summary
Fields inherited from interface io.helidon.common.Weighted
DEFAULT_WEIGHT
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
The default constructor.protected
AbstractServiceProvider
(T instance, Phase phase, io.helidon.inject.api.ServiceInfo serviceInfo, InjectionServices injectionServices) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionio.helidon.inject.api.ActivationResult
activate
(io.helidon.inject.api.ActivationRequest req) Activate a managed service/provider.protected Optional
<ActivationLog> Returns the activation log.The agent responsible for activation - this will be non-null for build-time activators.createLogEntryAndResult
(Phase targetPhase) Creates a log entry result based upon the target phase provided.protected T
createServiceProvider
(Map<String, Object> resolvedDeps) Creates the service with the supplied resolved dependencies, key'ed by each injection point id.The current activation phase for this service provider.io.helidon.inject.api.ActivationResult
deactivate
(io.helidon.inject.api.DeActivationRequest req) Deactivate a managed service.The agent responsible for deactivation - this will be non-null for build-time activators.io.helidon.inject.api.DependenciesInfo
Provides the dependencies for this service provider if known, or null if not known or not available.protected void
dependencies
(io.helidon.inject.api.DependenciesInfo dependencies) Used to set the dependencies from this service provider.Describe the service provider.protected void
doConstructing
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult) Called during construction phase.protected void
doDestroying
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult) Called after thePreDestroyMethod
process.protected void
doInjectingFields
(Object target, Map<String, Object> deps, Set<String> injections, TypeName forServiceType) Called during the injection of fields.protected void
doInjectingMethods
(Object target, Map<String, Object> deps, Set<String> injections, TypeName forServiceType) Called during the injection of methods.protected void
doPostConstructing
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult) Called during thePostConstructMethod
process.protected void
doPreDestroying
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult) Called during thePreDestroyMethod
process.boolean
protected ServiceProviderInjectionException
expectedQualifiedServiceError
(io.helidon.inject.api.ContextualServiceQuery ctx) Creates an injection exception appropriate when there are no matching qualified services for the context provided.first
(io.helidon.inject.api.ContextualServiceQuery ctx) Get (or create) an instance of this service type for the given injection point context.get()
Get (or create) an instance of this service type using default/empty criteria and context.protected <T> T
Called by the generated code when it is attempting to resolve a specific injection point dependency by id.getOrCreateInjectionPlan
(boolean resolveIps) Get or Create the injection plan.int
hashCode()
id()
Identifies the service provider physically and uniquely.protected String
The identity prefix, or empty string if there is no prefix.protected String
The identity suffix, or empty string if there is no suffix.Called at startup to establish the injection plan as an alternative to gathering it dynamically.Returns the previously assignedInjectionServices
instance.void
injectionServices
(Optional<InjectionServices> injectionServices) Assigns the services instance this provider is bound to.Returns the service provider that intercepts this provider.void
interceptor
(ServiceProvider<?> interceptor) Sets the interceptor for this service provider.protected boolean
isAlreadyAtTargetPhase
(Phase targetPhase) Returns true if the current activation phase has reached the given target phase.boolean
isCustom()
Identifies whether the implementation was custom written and not code generated.boolean
Returns true if this service provider instance is anInterceptor
.boolean
Does the service provide singletons, does it always produce the same result for every call toInjectionPointProvider.get()
.list
(io.helidon.inject.api.ContextualServiceQuery ctx) Get (or create) a list of instances matching the criteria for the given injection point context.protected System.Logger
logger()
The logger.maybeActivate
(io.helidon.inject.api.ContextualServiceQuery ctx) Will trigger an activation if the managed service is not yet active.void
moduleName
(String moduleName) Called to inform a service provider the module name it is bound to.name
(boolean simple) The name assigned to this provider.protected void
onFailedFinish
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult, Throwable t, boolean throwOnError) Called on a failed finish of activation.protected void
Called on the final leg of the shutdown sequence.protected void
onFinished
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult) Called on a successful finish of activation.void
onPhaseEvent
(Event event, Phase phase) Called when there is an event transition within the service registry.The optional method handling PreDestroy.The optional method handling PostConstruct.boolean
reset
(boolean deep) Resets the state of this object.io.helidon.inject.api.ServiceInfo
The meta information that describes the service.protected void
serviceInfo
(io.helidon.inject.api.ServiceInfo serviceInfo) Sets the service info that describes the managed service that is assigned.The agent/instance to be used for binding this service provider to the injectable application that was code generated.Returns the managed service this provider has (or is in the process of) activating.Used to control the order of injection.protected void
startTransitionCurrentActivationPhase
(AbstractServiceProvider.LogEntryAndResult logEntryAndResult, Phase newPhase) Starts transitioning to a new phase.static <T> Optional
<AbstractServiceProvider<T>> toAbstractServiceProvider
(ServiceProvider<?> sp, boolean expected) Will test and downcast the passed service provider to an instance ofAbstractServiceProvider
.toString()
double
weight()
Weight of this class (maybe because it is defined dynamically, so it cannot be defined by an annotation).Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.helidon.inject.api.ServiceProvider
serviceType
Methods inherited from interface io.helidon.inject.api.ServiceProviderBindable
isIntercepted, isRootProvider, rootProvider, rootProvider
-
Constructor Details
-
AbstractServiceProvider
protected AbstractServiceProvider()The default constructor. -
AbstractServiceProvider
protected AbstractServiceProvider(T instance, Phase phase, io.helidon.inject.api.ServiceInfo serviceInfo, InjectionServices injectionServices) Constructor.- Parameters:
instance
- the managed service instancephase
- the current phaseserviceInfo
- the service infoinjectionServices
- the services instance
-
-
Method Details
-
toAbstractServiceProvider
public static <T> Optional<AbstractServiceProvider<T>> toAbstractServiceProvider(ServiceProvider<?> sp, boolean expected) Will test and downcast the passed service provider to an instance ofAbstractServiceProvider
.- Type Parameters:
T
- the managed service type- Parameters:
sp
- the service providerexpected
- is the result expected to be present- Returns:
- the abstract service provider
-
activator
Description copied from interface:ServiceProvider
The agent responsible for activation - this will be non-null for build-time activators. If not present then anInjector
must be used to reflectively activate.- Specified by:
activator
in interfaceServiceProvider<T>
- Returns:
- the activator
-
deActivator
Description copied from interface:ServiceProvider
The agent responsible for deactivation - this will be non-null for build-time activators. If not present then anInjector
must be used to reflectively deactivate.- Specified by:
deActivator
in interfaceServiceProvider<T>
- Returns:
- the deactivator to use or null if the service is not interested in deactivation
-
serviceProviderBindable
Description copied from interface:ServiceProvider
The agent/instance to be used for binding this service provider to the injectable application that was code generated.- Specified by:
serviceProviderBindable
in interfaceServiceProvider<T>
- Returns:
- the service provider that should be used for binding, or empty if this provider does not support binding
- See Also:
-
isProvider
public boolean isProvider()Description copied from interface:ServiceProvider
Does the service provide singletons, does it always produce the same result for every call toInjectionPointProvider.get()
. I.e., if the managed service implements Provider orInjectionPointProvider
then this typically is considered not a singleton provider. I.e., If the managed services is NOTSingleton
, then it will be treated as per request / dependent scope. Note that this is similar in nature to RequestScope, except the "official" request scope is bound to the web request. Here, we are speaking about contextually any caller asking for a new instance of the service in question. The requester in question will ideally be able to identify itself to this provider viaInjectionPointProvider.first(ContextualServiceQuery)
so that this provider can properly service the "provide" request.- Specified by:
isProvider
in interfaceServiceProvider<T>
- Returns:
- true if the service provider provides per-request instances for each caller
-
isCustom
public boolean isCustom()Identifies whether the implementation was custom written and not code generated. We assume by default this is part of code-generation, and the default is to return false.- Returns:
- true if a custom, user-supplied implementation (rare)
-
serviceInfo
public io.helidon.inject.api.ServiceInfo serviceInfo()Description copied from interface:ServiceProvider
The meta information that describes the service. Must remain immutable for the lifetime of the JVM post binding - ie., afterServiceBinder.bind(ServiceProvider)
is called.- Specified by:
serviceInfo
in interfaceServiceProvider<T>
- Returns:
- the meta information describing the service
-
dependencies
public io.helidon.inject.api.DependenciesInfo dependencies()Description copied from interface:ServiceProvider
Provides the dependencies for this service provider if known, or null if not known or not available.- Specified by:
dependencies
in interfaceServiceProvider<T>
- Returns:
- the dependencies this service provider has or null if unknown or unavailable
-
weight
public double weight()Description copied from interface:Weighted
Weight of this class (maybe because it is defined dynamically, so it cannot be defined by an annotation). If not dynamic, you can use theWeight
annotation rather than implementing this interface as long as it is supported by the library using thisWeighted
. -
currentActivationPhase
Description copied from interface:ServiceProvider
The current activation phase for this service provider.- Specified by:
currentActivationPhase
in interfaceServiceProvider<T>
- Returns:
- the activation phase
-
injectionServices
Description copied from interface:ServiceProviderBindable
Returns the previously assignedInjectionServices
instance.- Specified by:
injectionServices
in interfaceServiceProviderBindable<T>
- Returns:
- the previously assigned injection services instance, or empty if never assigned
- See Also:
-
injectionServices
Description copied from interface:ServiceProviderBindable
Assigns the services instance this provider is bound to. A service provider can be associated with 0..1 services instance. If not set, the service provider should useInjectionServices.injectionServices()
to ascertain the instance.- Specified by:
injectionServices
in interfaceServiceProviderBindable<T>
- Parameters:
injectionServices
- the injection services instance, or empty to clear any active binding
-
moduleName
Description copied from interface:ServiceProviderBindable
Called to inform a service provider the module name it is bound to. Will only be called when there is a non-null module name associated for the givenModuleComponent
. A service provider can be associated with 0..1 modules.- Specified by:
moduleName
in interfaceServiceProviderBindable<T>
- Parameters:
moduleName
- the non-null module name
-
isInterceptor
public boolean isInterceptor()Description copied from interface:ServiceProviderBindable
Returns true if this service provider instance is anInterceptor
.- Specified by:
isInterceptor
in interfaceServiceProviderBindable<T>
- Returns:
- true if this service provider is an interceptor
-
interceptor
Description copied from interface:ServiceProviderBindable
Returns the service provider that intercepts this provider.- Specified by:
interceptor
in interfaceServiceProviderBindable<T>
- Returns:
- the service provider that intercepts this provider
-
interceptor
Description copied from interface:ServiceProviderBindable
Sets the interceptor for this service provider.- Specified by:
interceptor
in interfaceServiceProviderBindable<T>
- Parameters:
interceptor
- the interceptor for this provider
-
hashCode
public int hashCode() -
equals
-
toString
-
description
Description copied from interface:ServiceProvider
Describe the service provider. This will change based upon activation state.- Specified by:
description
in interfaceServiceProvider<T>
- Returns:
- the logical and immutable description
-
id
Description copied from interface:ServiceProvider
Identifies the service provider physically and uniquely.- Specified by:
id
in interfaceServiceProvider<T>
- Returns:
- the unique identity of the service provider
-
name
The name assigned to this provider. Simple names are not unique.- Parameters:
simple
- flag to indicate simple name usage- Returns:
- this name assigned to this provider
-
get
Description copied from interface:InjectionPointProvider
Get (or create) an instance of this service type using default/empty criteria and context. -
first
Description copied from interface:InjectionPointProvider
Get (or create) an instance of this service type for the given injection point context. This is logically the same as using the first element of the result from callingInjectionPointProvider.list(ContextualServiceQuery)
.- Specified by:
first
in interfaceInjectionPointProvider<T>
- Parameters:
ctx
- the service query- Returns:
- the best service provider matching the criteria
-
list
Description copied from interface:InjectionPointProvider
Get (or create) a list of instances matching the criteria for the given injection point context.- Specified by:
list
in interfaceInjectionPointProvider<T>
- Parameters:
ctx
- the service query- Returns:
- the resolved services matching criteria for the injection point in order of weight, or null if the context is not supported
-
activate
public io.helidon.inject.api.ActivationResult activate(io.helidon.inject.api.ActivationRequest req) Description copied from interface:Activator
Activate a managed service/provider. -
onPhaseEvent
Description copied from interface:ActivationPhaseReceiver
Called when there is an event transition within the service registry.- Specified by:
onPhaseEvent
in interfaceActivationPhaseReceiver
- Parameters:
event
- the eventphase
- the phase
-
injectionPlanBinder
Called at startup to establish the injection plan as an alternative to gathering it dynamically.- Specified by:
injectionPlanBinder
in interfaceServiceProviderBindable<T>
- Returns:
- binder used for this service provider, or empty if not capable or ineligible of being bound
-
getOrCreateInjectionPlan
public Map<String,io.helidon.inject.runtime.HelidonInjectionPlan> getOrCreateInjectionPlan(boolean resolveIps) Get or Create the injection plan.- Parameters:
resolveIps
- true if the injection points should also be activated/resolved.- Returns:
- the injection plan
-
reset
public boolean reset(boolean deep) Description copied from interface:Resettable
Resets the state of this object.- Specified by:
reset
in interfaceResettable
- Parameters:
deep
- true to iterate over any contained objects, to reflect the reset into the retained object- Returns:
- returns true if the state was changed
-
postConstructMethod
Description copied from interface:ServiceProvider
The optional method handling PreDestroy.- Specified by:
postConstructMethod
in interfaceServiceProvider<T>
- Returns:
- the post-construct method or empty if there is none
-
preDestroyMethod
Description copied from interface:ServiceProvider
The optional method handling PostConstruct.- Specified by:
preDestroyMethod
in interfaceServiceProvider<T>
- Returns:
- the pre-destroy method or empty if there is none
-
deactivate
public io.helidon.inject.api.ActivationResult deactivate(io.helidon.inject.api.DeActivationRequest req) Description copied from interface:DeActivator
Deactivate a managed service. This will trigger anyPreDestroy
method on the underlying service type instance.- Specified by:
deactivate
in interfaceDeActivator
- Parameters:
req
- deactivation request- Returns:
- the result
-
onFinalShutdown
protected void onFinalShutdown()Called on the final leg of the shutdown sequence. -
onFailedFinish
protected void onFailedFinish(AbstractServiceProvider.LogEntryAndResult logEntryAndResult, Throwable t, boolean throwOnError) Called on a failed finish of activation.- Parameters:
logEntryAndResult
- the log entry holding the resultt
- the error that was observedthrowOnError
- the flag indicating whether we should throw on error- See Also:
-
logger
The logger.- Returns:
- the logger
-
serviceInfo
protected void serviceInfo(io.helidon.inject.api.ServiceInfo serviceInfo) Sets the service info that describes the managed service that is assigned.- Parameters:
serviceInfo
- the service info
-
dependencies
protected void dependencies(io.helidon.inject.api.DependenciesInfo dependencies) Used to set the dependencies from this service provider.- Parameters:
dependencies
- the dependencies from this service provider
-
isAlreadyAtTargetPhase
Returns true if the current activation phase has reached the given target phase.- Parameters:
targetPhase
- the target phase- Returns:
- true if the targetPhase has been reached
-
identityPrefix
The identity prefix, or empty string if there is no prefix.- Returns:
- the identity prefix
-
identitySuffix
The identity suffix, or empty string if there is no suffix.- Returns:
- the identity suffix
-
serviceRef
Returns the managed service this provider has (or is in the process of) activating.- Returns:
- the service we are managing lifecycle for
-
activationLog
Returns the activation log.- Returns:
- the activation log
-
get
Called by the generated code when it is attempting to resolve a specific injection point dependency by id.- Type Parameters:
T
- the type of the dependency- Parameters:
deps
- the entire map of resolved dependenciesid
- the id of the dependency to lookup- Returns:
- the resolved object
-
maybeActivate
Will trigger an activation if the managed service is not yet active.- Parameters:
ctx
- the context that triggered the activation- Returns:
- the result of the activation
-
onFinished
Called on a successful finish of activation.- Parameters:
logEntryAndResult
- the record holding the result- See Also:
-
doConstructing
Called during construction phase.- Parameters:
logEntryAndResult
- the record that holds the results
-
createServiceProvider
Creates the service with the supplied resolved dependencies, key'ed by each injection point id.- Parameters:
resolvedDeps
- the resolved dependencies- Returns:
- the newly created managed service
- Throws:
ServiceProviderInjectionException
- since this is a base method for what is expected to be a code-generated derivedActivator
then this method will throw an exception if the derived class does not implement this method as it normally should
-
serviceTypeInjectionOrder
Used to control the order of injection. Jsr-330 is particular about this.- Returns:
- the order of injection
-
doInjectingFields
protected void doInjectingFields(Object target, Map<String, Object> deps, Set<String> injections, TypeName forServiceType) Called during the injection of fields.- Parameters:
target
- the targetdeps
- the dependenciesinjections
- the injectionsforServiceType
- the service type
-
doInjectingMethods
protected void doInjectingMethods(Object target, Map<String, Object> deps, Set<String> injections, TypeName forServiceType) Called during the injection of methods.- Parameters:
target
- the targetdeps
- the dependenciesinjections
- the injectionsforServiceType
- the service type
-
doPostConstructing
Called during thePostConstructMethod
process.- Parameters:
logEntryAndResult
- the entry holding the result
-
doPreDestroying
Called during thePreDestroyMethod
process.- Parameters:
logEntryAndResult
- the entry holding the result
-
doDestroying
Called after thePreDestroyMethod
process.- Parameters:
logEntryAndResult
- the entry holding the result
-
expectedQualifiedServiceError
protected ServiceProviderInjectionException expectedQualifiedServiceError(io.helidon.inject.api.ContextualServiceQuery ctx) Creates an injection exception appropriate when there are no matching qualified services for the context provided.- Parameters:
ctx
- the context- Returns:
- the injection exception
-
createLogEntryAndResult
Creates a log entry result based upon the target phase provided.- Parameters:
targetPhase
- the target phase- Returns:
- a new log entry and result record
-
startTransitionCurrentActivationPhase
protected void startTransitionCurrentActivationPhase(AbstractServiceProvider.LogEntryAndResult logEntryAndResult, Phase newPhase) Starts transitioning to a new phase.- Parameters:
logEntryAndResult
- the record that will hold the state of the transitionnewPhase
- the target new phase
-