Class AbstractConcurrentCustomScope<A extends Annotation>

java.lang.Object
io.micronaut.context.scope.AbstractConcurrentCustomScope<A>
Type Parameters:
A - The annotation type
All Implemented Interfaces:
LifeCycle<AbstractConcurrentCustomScope<A>>, CustomScope<A>, Closeable, AutoCloseable

public abstract class AbstractConcurrentCustomScope<A extends Annotation> extends Object implements CustomScope<A>, LifeCycle<AbstractConcurrentCustomScope<A>>, AutoCloseable
Abstract implementation of the custom scope interface that simplifies defining new scopes using the Map interface.

Note this implementation uses a singleReentrantReadWriteLock to lock the entire scope hence it is designed for scopes that will hold a small amount of beans. For implementations that hold many beans it is recommended to use a lock per BeanIdentifier.

Since:
3.0.0
Author:
graemerocher
  • Constructor Details

    • AbstractConcurrentCustomScope

      protected AbstractConcurrentCustomScope(Class<A> annotationType)
      A custom scope annotation.
      Parameters:
      annotationType - The annotation type
  • Method Details

    • getScopeMap

      protected abstract @NonNull Map<BeanIdentifier,CreatedBean<?>> getScopeMap(boolean forCreation)
      Parameters:
      forCreation - Whether it is for creation
      Returns:
      Obtains the scope map, never null
      Throws:
      IllegalStateException - if the scope map cannot be obtained in the current context
    • annotationType

      public final Class<A> annotationType()
      Specified by:
      annotationType in interface CustomScope<A extends Annotation>
      Returns:
      The scope annotation type
    • close

      public abstract void close()
      Implement the close logic for the scope.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface LifeCycle<A extends Annotation>
    • stop

      public final @NonNull AbstractConcurrentCustomScope<A> stop()
      Description copied from interface: LifeCycle
      Stops the life cycle component.
      Specified by:
      stop in interface LifeCycle<A extends Annotation>
      Returns:
      This lifecycle component
    • destroyScope

      protected void destroyScope(@Nullable Map<BeanIdentifier,CreatedBean<?>> scopeMap)
      Destroys the scope.
      Parameters:
      scopeMap - The scope map
    • getOrCreate

      public final <T> T getOrCreate(BeanCreationContext<T> creationContext)
      Description copied from interface: CustomScope
      Resolve an object from the given scope.
      Specified by:
      getOrCreate in interface CustomScope<A extends Annotation>
      Type Parameters:
      T - The bean generic type
      Parameters:
      creationContext - The creation context
      Returns:
      The bean instance
    • doCreate

      protected <T> @NonNull CreatedBean<T> doCreate(@NonNull BeanCreationContext<T> creationContext)
      Perform creation.
      Type Parameters:
      T - The generic type
      Parameters:
      creationContext - The creation context
      Returns:
      Created bean
    • remove

      public final <T> Optional<T> remove(BeanIdentifier identifier)
      Description copied from interface: CustomScope
      Remove a bean definition from the scope.
      Specified by:
      remove in interface CustomScope<A extends Annotation>
      Type Parameters:
      T - The generic type
      Parameters:
      identifier - The BeanIdentifier
      Returns:
      An Optional of the instance that was destroyed if it exists
    • handleDestructionException

      protected void handleDestructionException(BeanDestructionException e)
      Method that can be overridden to customize what happens on a shutdown error.
      Parameters:
      e - The exception
    • findBeanRegistration

      public final <T> Optional<BeanRegistration<T>> findBeanRegistration(T bean)
      Description copied from interface: CustomScope
      Get the BeanDefinition for the given bean.
      Specified by:
      findBeanRegistration in interface CustomScope<A extends Annotation>
      Type Parameters:
      T - The bean generic type
      Parameters:
      bean - The bean
      Returns:
      The bean definition if it can be resolved