org.apache.tamaya.clsupport

## Class CLAwareServiceContext

- java.lang.Object
- - org.apache.tamaya.clsupport.AbstractClassloaderAwareItemLoader<org.apache.tamaya.clsupport.ServiceContainer>
  - - org.apache.tamaya.clsupport.CLAwareServiceContext

- All Implemented Interfaces:
    org.apache.tamaya.spi.ServiceContext

      @Priority(value=10)
      public class CLAwareServiceContext
      extends AbstractClassloaderAwareItemLoader<org.apache.tamaya.clsupport.ServiceContainer>
      implements org.apache.tamaya.spi.ServiceContext

  This class implements a `ServiceContext`, which basically provides a similar loading mechanism as used by the `ServiceLoader`. Whereas the `ServiceLoader` only loads configurations and instances from one classloader, this loader manages configs found and the related instances for each classloader along the classloader hierarchies individually. It ensures instances are loaded on the classloader level, where they first are visible. Additionally it ensures the same configuration resource (and its declared services) are loaded multiple times, when going up the classloader hierarchy.

  Finally classloaders are not stored by reference by this class, to ensure they still can be garbage collected. Refer also the inherited parent class for further details.

  This class uses an ordinal of `10`, so it overrides any default `ServiceContext` implementations provided with the Tamaya core modules.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CLAwareServiceContext()` Constructor, using the current default classloader as defined by `AbstractClassloaderAwareItemLoader.getDefaultClassLoader()`.
    `CLAwareServiceContext(ClassLoader classLoader)` Constructor, using the given classloader.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected org.apache.tamaya.clsupport.ServiceContainer` | `createItem(ClassLoader classLoader)` Implementation that creates a `ServiceContainer`, which manages all configs and instances loaded for a given classloader.
    `static int` | `getPriority(Object o)` Checks the given instance for a @Priority annotation.
    `<T> T` | `getService(Class<T> serviceType)` This method tries to evaluate the current singleton from the `ServiceContainer` attached to the current classloader.
    `<T> T` | `getService(Class<T> serviceType, ClassLoader classLoader)` Evaluates the current singleton instance using the given classloader context.
    `<T> List<T>` | `getServices(Class<T> serviceType)` Gets the services visible.
    `<T> List<T>` | `getServices(Class<T> serviceType, ClassLoader classLoader)` Gets the services visible.
    `int` | `ordinal()`
    `protected void` | `updateItem(org.apache.tamaya.clsupport.ServiceContainer currentContainer, ClassLoader classLoader)` Creates a new item for being stored linked with the given lassloader.

    - ### Methods inherited from class org.apache.tamaya.clsupport.AbstractClassloaderAwareItemLoader

      `getClassLoaderID, getClassLoaderID, getDefaultClassLoader, getItemNoParent, getItemNoParent, getItemNoParent, getItems, getItems, getParentItems, getParentItems, loadItems, loadItems`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CLAwareServiceContext

          public CLAwareServiceContext()

      Constructor, using the current default classloader as defined by `AbstractClassloaderAwareItemLoader.getDefaultClassLoader()`.

    - #### CLAwareServiceContext

          public CLAwareServiceContext(ClassLoader classLoader)

      Constructor, using the given classloader.
      Parameters:
        `classLoader` - the target classloader for initializing of services, not null.

  - ### Method Detail

    - #### createItem

          protected org.apache.tamaya.clsupport.ServiceContainer createItem(ClassLoader classLoader)

      Implementation that creates a `ServiceContainer`, which manages all configs and instances loaded for a given classloader.
      Specified by:
        `createItem` in class `AbstractClassloaderAwareItemLoader<org.apache.tamaya.clsupport.ServiceContainer>`
      Parameters:
        `classLoader` - the classloader, not null.
      Returns:
        a new empty, `ServiceContainer` instance.

    - #### updateItem

          protected void updateItem(org.apache.tamaya.clsupport.ServiceContainer currentContainer,
                        ClassLoader classLoader)

      Description copied from class: `AbstractClassloaderAwareItemLoader`
      Creates a new item for being stored linked with the given lassloader.
      Specified by:
        `updateItem` in class `AbstractClassloaderAwareItemLoader<org.apache.tamaya.clsupport.ServiceContainer>`
      Parameters:
        `currentContainer` - the current found ItemContainer instance to be updated.
        `classLoader` - the classloader, not null.

    - #### ordinal

          public int ordinal()

      Specified by:
        `ordinal` in interface `org.apache.tamaya.spi.ServiceContext`

    - #### getService

          public <T> T getService(Class<T> serviceType)

      This method tries to evaluate the current singleton from the `ServiceContainer` attached to the current classloader. If not found the singleton instance is evaluated based on the priorities assigned for all known providers. The resulting instance is then cached and always returned as singleton instance fomr this loader, when the same current classloader instance is active.
      Specified by:
        `getService` in interface `org.apache.tamaya.spi.ServiceContext`
      Type Parameters:
        `T` - the type
      Parameters:
        `serviceType` - the service type.
      Returns:
        the item found, or null.

    - #### getService

          public <T> T getService(Class<T> serviceType,
                         ClassLoader classLoader)

      Evaluates the current singleton instance using the given classloader context.
      Type Parameters:
        `T` - the type
      Parameters:
        `serviceType` - the service type.
        `classLoader` - the classloader, not null.
      Returns:
        the item found, or null.

    - #### getServices

          public <T> List<T> getServices(Class<T> serviceType)

      Gets the services visible.
      Specified by:
        `getServices` in interface `org.apache.tamaya.spi.ServiceContext`
      Type Parameters:
        `T` - the type param
      Parameters:
        `serviceType` - the service type.
      Returns:
        the services visible for the current classloader.

    - #### getServices

          public <T> List<T> getServices(Class<T> serviceType,
                                ClassLoader classLoader)

      Gets the services visible.
      Type Parameters:
        `T` - the type param
      Parameters:
        `serviceType` - the service type.
        `classLoader` - the classloader
      Returns:
        the services visible for the current classloader.

    - #### getPriority

          public static int getPriority(Object o)

      Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such annotation is present, a default priority is returned (1);
      Parameters:
        `o` - the instance, not null.
      Returns:
        a priority, by default 1.

Copyright © 2014–2016 Apache Software Foundation. All rights reserved.