Class ComponentContainer

java.lang.Object
com.atlassian.jira.component.pico.ComponentContainer

@ParametersAreNonnullByDefault public class ComponentContainer extends Object
Wraps a Pico container and a Spring container, adding various convenience methods and awareness of OSGi.

The components contained herein could be any combination of:

  • core Jira components (published to OSGi or not), and
  • Plugins 1 components.
  • Constructor Details

    • ComponentContainer

      public ComponentContainer(org.picocontainer.MutablePicoContainer picoContainer, OsgiServiceRegistry osgiServiceRegistry, ComponentContainer.ContainerLevel level, org.springframework.context.ConfigurableApplicationContext springContainer)
      Constructor.
      Parameters:
      picoContainer - the parent Pico container
      osgiServiceRegistry - the registry of components to publish to OSGi
      level - the level of this container
      springContainer - the child Spring container
  • Method Details

    • loadComponent

      @Nullable public static <T> T loadComponent(Class<T> componentClass, Collection<Object> dependencies, @Nullable ComponentContainer existingContainer)
      Instantiates a component from its class and dependent classes/instances.
      Type Parameters:
      T - the type of component
      Parameters:
      componentClass - the class of component to instantiate
      dependencies - the component's dependencies (classes and/or instances)
      existingContainer - the existing container, if any
      Returns:
      the component, or null if the component class is the Void class
      Since:
      8.0
    • refresh

      public void refresh()
      Refreshes this container; allows components to be looked up. Call this after registering all Pico components.
      Since:
      9.11
    • loadComponent

      public <T> T loadComponent(Class<T> componentClass, Collection<Object> dependencies)
      Instantiates a component from its class and dependent classes/instances, found in this container and/or among the given dependencies.
      Type Parameters:
      T - the type of component
      Parameters:
      componentClass - the class of component to instantiate
      dependencies - the component's dependencies (classes and/or instances)
      Returns:
      the component, or null if the component class is the Void class
      Since:
      8.0
    • registerComponent

      public void registerComponent(@Nullable Class<?> interfaceClass, Class<?> componentClass)
      Adds the given component to this container.
      Parameters:
      interfaceClass - the implemented interface, if any (e.g. none in plugins 1)
      componentClass - the implementation class
    • implementationBasedOnAppProperty

      public <T> void implementationBasedOnAppProperty(OsgiServiceScope scope, Class<T> interfaceClass, Class<? extends T> enabledClass, Class<? extends T> disabledClass, String appPropertiesKey)
      Registers one of two possible implementations of a given interface, based on the boolean value of the given application property. The instances of the two implementation classes must each be provided separately to this call, e.g. via internalInstanceOf(java.lang.Class).
      Type Parameters:
      T - the component type
      Parameters:
      scope - the scope of the component
      interfaceClass - the implemented interface
      enabledClass - the implementation if the property is set
      disabledClass - the implementation if the property is not set
      appPropertiesKey - the key of the property to check
      Since:
      8.0
    • getComponents

      public <T> List<T> getComponents(Class<T> type)
      Returns any components of the given type.
      Type Parameters:
      T - the component type
      Parameters:
      type - the component type
      Returns:
      see above
      Since:
      8.0
      See Also:
      • PicoContainer.getComponents(Class)
    • getComponentAdapter

      @Nullable public org.picocontainer.ComponentAdapter getComponentAdapter(Class<?> key)
      Returns the component adapter with the given key.
      Parameters:
      key - the key to look up
      Returns:
      see PicoContainer.getComponentAdapter(Object)
    • getComponentInstance

      @Nullable public <T> T getComponentInstance(Class<T> key)
      Returns the component whose key is the given class.
      Type Parameters:
      T - the type of key
      Parameters:
      key - the component key, typically an interface or implementation
      Returns:
      null if there is no component with that key
    • getComponentInstanceOfType

      @Nullable public <T> T getComponentInstanceOfType(Class<T> keyOrType)
      Retrieves and returns a component which is an instance of given class. This is the same as calling getComponentInstance(Class), except that if the given Class is not a known key, it will try to find a unique component that implements or extends that Class.
      Parameters:
      keyOrType - the key or class of component to find
      Returns:
      the found component, or null if it doesn't exist
    • getHostComponentProvider

      public com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider getHostComponentProvider()
    • internalInstance

      public void internalInstance(Object instance)
      Adds the given internal component (not published to OSGi), using the object's class as the key.
      Parameters:
      instance - the instance to add
    • internalInstance

      public void internalInstance(String key, Object instance)
      Adds the given internal component (not published to OSGi), using the given key.
      Parameters:
      key - the key under which to add the component
      instance - the component instance to add
    • instance

      public <T, S extends T> void instance(OsgiServiceScope scope, Class<T> key, S instance)
      Adds the given component to this container.
      Type Parameters:
      T - the interface being implemented
      S - the class that implements it
      Parameters:
      scope - the visibility of the component to OSGi
      key - the component's key in the container, usually the interface being implemented
      instance - the component instance to add
    • internalInstanceOf

      public void internalInstanceOf(Class<?> componentClass)
      Adds an internal (non-OSGi) instance of the given class to this container.
      Parameters:
      componentClass - the class that implements the component
    • implementation

      public <T> void implementation(OsgiServiceScope scope, Class<? super T> key, Class<T> implementation)
    • implementation

      public <T> void implementation(OsgiServiceScope scope, Class<? super T> key, Class<T> implementation, boolean trackBundle)
    • implementation

      public <T> void implementation(OsgiServiceScope scope, Class<? super T> key, Class<T> implementation, Object... parametersOrKeys)
      Registers the given component using the given keys to look up its constructor parameters. Any of the given keys that are already a Parameter are used as-is rather than being used for lookup.
      Parameters:
      scope - the visibility of the component to OSGi
      key - the interface that the component implements
      implementation - the component's implementation class
      parametersOrKeys - the component's constructor parameters (or the keys thereof, in any combination)
      See Also:
    • implementation

      public <T> void implementation(OsgiServiceScope scope, Class<? super T> key, Class<T> implementation, org.picocontainer.Parameter[] parameters)
      Registers the given component by explicitly providing its constructor arguments.
      Type Parameters:
      T - the type of the implementation class
      Parameters:
      scope - the visibility of the component to OSGi
      key - the interface that the component implements
      implementation - the component's implementation class
      parameters - the component's constructor arguments
      See Also:
      • MutablePicoContainer.addComponent(java.lang.Object, java.lang.Object, org.picocontainer.Parameter...)
    • implementationViaNoArgConstructor

      public <T> void implementationViaNoArgConstructor(OsgiServiceScope scope, Class<T> key, Class<? extends T> implementation)
      Registers a component via its no-arg constructor, to work around PICO-201.
      Type Parameters:
      T - the type of component
      Parameters:
      scope - the scope of the component
      key - the component key (usually the interface being implemented)
      implementation - the class that implements the component; must have a no-arg constructor
    • provideViaFactory

      public <T> void provideViaFactory(Class<T> serviceInterface, Class<? extends org.osgi.framework.ServiceFactory<T>> osgiFactoryClass)
      Registers an OSGi ServiceFactory for the given type of service.

      There are two ways to provide a service to OSGi: via a singleton object that implements the service interface, or via a ServiceFactory that creates a separate instance of the service for each calling bundle. This method does the latter.

      Type Parameters:
      T - the interface of the service being provided
      Parameters:
      serviceInterface - the interface of the OSGi service
      osgiFactoryClass - the service factory class. The factory implementation will be created and injected by the Pico container, but will not be registered in the container.
    • component

      public void component(OsgiServiceScope scope, org.picocontainer.ComponentAdapter componentAdapter)
      Adds the given component adapter to this container, with the given scope.
      Parameters:
      scope - the scope of the component
      componentAdapter - the adapter to add
    • getLevel

      @Nonnull public ComponentContainer.ContainerLevel getLevel()
      Returns the level of this container.
      Returns:
      see above
      Since:
      8.0
    • setLevel

      public void setLevel(ComponentContainer.ContainerLevel newLevel)
      Sets the level of this container.
      Parameters:
      newLevel - the level to set
      Since:
      8.0
    • getComponent

      @Nullable public <T> T getComponent(Class<T> componentClass)
      Returns the component with the given class as its key or type.
      Type Parameters:
      T - the component type
      Parameters:
      componentClass - the key or type to look up
      Returns:
      null if it can't be found
      Since:
      8.0
      See Also:
      • PicoContainer.getComponent(Class)