Class AbstractServiceProvider<T>

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.
  • 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 instance
      phase - the current phase
      serviceInfo - the service info
      injectionServices - 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 of AbstractServiceProvider.
      Type Parameters:
      T - the managed service type
      Parameters:
      sp - the service provider
      expected - is the result expected to be present
      Returns:
      the abstract service provider
    • activator

      public Optional<Activator> activator()
      Description copied from interface: ServiceProvider
      The agent responsible for activation - this will be non-null for build-time activators. If not present then an Injector must be used to reflectively activate.
      Specified by:
      activator in interface ServiceProvider<T>
      Returns:
      the activator
    • deActivator

      public Optional<DeActivator> deActivator()
      Description copied from interface: ServiceProvider
      The agent responsible for deactivation - this will be non-null for build-time activators. If not present then an Injector must be used to reflectively deactivate.
      Specified by:
      deActivator in interface ServiceProvider<T>
      Returns:
      the deactivator to use or null if the service is not interested in deactivation
    • serviceProviderBindable

      public Optional<ServiceProviderBindable<T>> 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 interface ServiceProvider<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 to InjectionPointProvider.get(). I.e., if the managed service implements Provider or InjectionPointProvider then this typically is considered not a singleton provider. I.e., If the managed services is NOT Singleton, 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 via InjectionPointProvider.first(ContextualServiceQuery) so that this provider can properly service the "provide" request.
      Specified by:
      isProvider in interface ServiceProvider<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., after ServiceBinder.bind(ServiceProvider) is called.
      Specified by:
      serviceInfo in interface ServiceProvider<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 interface ServiceProvider<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 the Weight annotation rather than implementing this interface as long as it is supported by the library using this Weighted.
      Specified by:
      weight in interface Weighted
      Returns:
      the weight of this service, must be a non-negative number
    • currentActivationPhase

      public Phase currentActivationPhase()
      Description copied from interface: ServiceProvider
      The current activation phase for this service provider.
      Specified by:
      currentActivationPhase in interface ServiceProvider<T>
      Returns:
      the activation phase
    • injectionServices

      public Optional<InjectionServices> injectionServices()
      Description copied from interface: ServiceProviderBindable
      Returns the previously assigned InjectionServices instance.
      Specified by:
      injectionServices in interface ServiceProviderBindable<T>
      Returns:
      the previously assigned injection services instance, or empty if never assigned
      See Also:
    • injectionServices

      public void injectionServices(Optional<InjectionServices> 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 use InjectionServices.injectionServices() to ascertain the instance.
      Specified by:
      injectionServices in interface ServiceProviderBindable<T>
      Parameters:
      injectionServices - the injection services instance, or empty to clear any active binding
    • moduleName

      public void moduleName(String 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 given ModuleComponent. A service provider can be associated with 0..1 modules.
      Specified by:
      moduleName in interface ServiceProviderBindable<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 an Interceptor.
      Specified by:
      isInterceptor in interface ServiceProviderBindable<T>
      Returns:
      true if this service provider is an interceptor
    • interceptor

      public Optional<ServiceProvider<?>> interceptor()
      Description copied from interface: ServiceProviderBindable
      Returns the service provider that intercepts this provider.
      Specified by:
      interceptor in interface ServiceProviderBindable<T>
      Returns:
      the service provider that intercepts this provider
    • interceptor

      public void interceptor(ServiceProvider<?> interceptor)
      Description copied from interface: ServiceProviderBindable
      Sets the interceptor for this service provider.
      Specified by:
      interceptor in interface ServiceProviderBindable<T>
      Parameters:
      interceptor - the interceptor for this provider
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object another)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • description

      public String description()
      Description copied from interface: ServiceProvider
      Describe the service provider. This will change based upon activation state.
      Specified by:
      description in interface ServiceProvider<T>
      Returns:
      the logical and immutable description
    • id

      public String id()
      Description copied from interface: ServiceProvider
      Identifies the service provider physically and uniquely.
      Specified by:
      id in interface ServiceProvider<T>
      Returns:
      the unique identity of the service provider
    • name

      public String name(boolean simple)
      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

      public T get()
      Description copied from interface: InjectionPointProvider
      Get (or create) an instance of this service type using default/empty criteria and context.
      Specified by:
      get in interface InjectionPointProvider<T>
      Specified by:
      get in interface Provider<T>
      Returns:
      the best service provider matching the criteria
    • first

      public Optional<T> first(io.helidon.inject.api.ContextualServiceQuery ctx)
      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 calling InjectionPointProvider.list(ContextualServiceQuery).
      Specified by:
      first in interface InjectionPointProvider<T>
      Parameters:
      ctx - the service query
      Returns:
      the best service provider matching the criteria
    • list

      public List<T> list(io.helidon.inject.api.ContextualServiceQuery ctx)
      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 interface InjectionPointProvider<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.
      Specified by:
      activate in interface Activator
      Parameters:
      req - activation request
      Returns:
      the result of the activation
    • onPhaseEvent

      public void onPhaseEvent(Event event, Phase phase)
      Description copied from interface: ActivationPhaseReceiver
      Called when there is an event transition within the service registry.
      Specified by:
      onPhaseEvent in interface ActivationPhaseReceiver
      Parameters:
      event - the event
      phase - the phase
    • injectionPlanBinder

      public Optional<ServiceInjectionPlanBinder.Binder> injectionPlanBinder()
      Called at startup to establish the injection plan as an alternative to gathering it dynamically.
      Specified by:
      injectionPlanBinder in interface ServiceProviderBindable<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 interface Resettable
      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

      public Optional<PostConstructMethod> postConstructMethod()
      Description copied from interface: ServiceProvider
      The optional method handling PreDestroy.
      Specified by:
      postConstructMethod in interface ServiceProvider<T>
      Returns:
      the post-construct method or empty if there is none
    • preDestroyMethod

      public Optional<PreDestroyMethod> preDestroyMethod()
      Description copied from interface: ServiceProvider
      The optional method handling PostConstruct.
      Specified by:
      preDestroyMethod in interface ServiceProvider<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 any PreDestroy method on the underlying service type instance.
      Specified by:
      deactivate in interface DeActivator
      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 result
      t - the error that was observed
      throwOnError - the flag indicating whether we should throw on error
      See Also:
    • logger

      protected System.Logger 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

      protected boolean isAlreadyAtTargetPhase(Phase targetPhase)
      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

      protected String identityPrefix()
      The identity prefix, or empty string if there is no prefix.
      Returns:
      the identity prefix
    • identitySuffix

      protected String identitySuffix()
      The identity suffix, or empty string if there is no suffix.
      Returns:
      the identity suffix
    • serviceRef

      protected Optional<T> serviceRef()
      Returns the managed service this provider has (or is in the process of) activating.
      Returns:
      the service we are managing lifecycle for
    • activationLog

      protected Optional<ActivationLog> activationLog()
      Returns the activation log.
      Returns:
      the activation log
    • get

      protected <T> T get(Map<String,T> deps, String id)
      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 dependencies
      id - the id of the dependency to lookup
      Returns:
      the resolved object
    • maybeActivate

      protected Optional<T> maybeActivate(io.helidon.inject.api.ContextualServiceQuery ctx)
      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

      protected void onFinished(AbstractServiceProvider.LogEntryAndResult logEntryAndResult)
      Called on a successful finish of activation.
      Parameters:
      logEntryAndResult - the record holding the result
      See Also:
    • doConstructing

      protected void doConstructing(AbstractServiceProvider.LogEntryAndResult logEntryAndResult)
      Called during construction phase.
      Parameters:
      logEntryAndResult - the record that holds the results
    • createServiceProvider

      protected T createServiceProvider(Map<String,Object> resolvedDeps)
      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 derived Activator then this method will throw an exception if the derived class does not implement this method as it normally should
    • serviceTypeInjectionOrder

      protected List<TypeName> 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 target
      deps - the dependencies
      injections - the injections
      forServiceType - 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 target
      deps - the dependencies
      injections - the injections
      forServiceType - the service type
    • doPostConstructing

      protected void doPostConstructing(AbstractServiceProvider.LogEntryAndResult logEntryAndResult)
      Called during the PostConstructMethod process.
      Parameters:
      logEntryAndResult - the entry holding the result
    • doPreDestroying

      protected void doPreDestroying(AbstractServiceProvider.LogEntryAndResult logEntryAndResult)
      Called during the PreDestroyMethod process.
      Parameters:
      logEntryAndResult - the entry holding the result
    • doDestroying

      protected void doDestroying(AbstractServiceProvider.LogEntryAndResult logEntryAndResult)
      Called after the PreDestroyMethod 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

      protected AbstractServiceProvider.LogEntryAndResult createLogEntryAndResult(Phase targetPhase)
      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 transition
      newPhase - the target new phase