Top Description Inners Fields Constructors Methods
org.glassfish.hk2.utilities

public abstract Class ServiceLocatorUtilities

extends Object
Class Inheritance
Imports
java.io.IOException, .PrintStream, java.lang.annotation.Annotation, java.lang.reflect.Type, java.util.ArrayList, .Arrays, .HashSet, .LinkedList, .List, .Map, .Set, jakarta.inject.Singleton, org.glassfish.hk2.api.ActiveDescriptor, .AnnotationLiteral, .Context, .Descriptor, .DuplicateServiceException, .DynamicConfiguration, .DynamicConfigurationService, .Factory, .FactoryDescriptors, .Filter, .Immediate, .ImmediateController, .IndexedFilter, .InheritableThread, .MultiException, .PerLookup, .PerThread, .Populator, .ServiceHandle, .ServiceLocator, .ServiceLocatorFactory, .ImmediateController.ImmediateServiceState, org.glassfish.hk2.internal.ImmediateHelper, .InheritableThreadContext, .PerThreadContext

This is a set of useful utilities for working with ServiceLocator.
Author
jwells

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final String
private static final Immediate
private static final InheritableThread
private static final PerLookup
private static final PerThread
private static final Singleton

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static List<ActiveDescriptor<?>>

Returns:

The list of descriptors added to the system. Will not return null but may return an empty list
addClasses
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
boolean idempotent, Class<?>...
The classes to add to the locator. If a class in this list implements Factory then two descriptors will be added for that class
toAdd
)

It is very often the case that one wishes to add classes that hk2 will automatically analyze for contracts and qualifiers to a service locator.

public static List<ActiveDescriptor<?>>

Returns:

The list of descriptors added to the system. Will not return null but may return an empty list
addClasses
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
Class<?>...
The classes to add to the locator. If a class in this list implements Factory then two descriptors will be added for that class
toAdd
)

It is very often the case that one wishes to add classes that hk2 will automatically analyze for contracts and qualifiers to a service locator.

public static List<FactoryDescriptors>

Returns:

The descriptors that were added to the service locator. Will not return null, but may return an empty list (if the constants array was zero length)
addFactoryConstants
(ServiceLocator
The non-null locator to add these factory constants to
locator
,
Factory<?>...
The constant factories to add to the locator. None of the constants in this array may be null
constants
)

This method adds factory constants to the given locator.

public static List<FactoryDescriptors>

Returns:

a list of the FactoryDescriptor descriptors that were added to the service locator
addFactoryDescriptors
(ServiceLocator
The locator to add the factories to. May not be null
locator
,
FactoryDescriptors...
The list of factory descriptors to add to the system. May not be null
factories
)

Adds the given factory descriptors to the service locator

public static List<FactoryDescriptors>

Returns:

A list of the FactoryDescriptor descriptors that were added to the service locator
addFactoryDescriptors
(ServiceLocator
The locator to add the factories to. May not be null
locator
,
boolean
This is false ONLY if every one of the factories given to this method can be used without a copy
requiresDeepCopy
,
FactoryDescriptors...
The list of factory descriptors to add to the system. May not be null
factories
)

Adds the given factory descriptors to the service locator

public static <T> ActiveDescriptor<T>

Returns:

The descriptor that was added to the service locator
addOneConstant
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
Object
The non-null constant to add to the service locator
constant
)

This method adds one existing object to the given service locator.

public static <T> ActiveDescriptor<T>

Returns:

The descriptor that was added to the service locator
addOneConstant
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
Object
The non-null constant to add to the service locator
constant
,
String
The name this descriptor should take (may be null)
name
,
Type...
The full set of contracts this descriptor should take. If this field is the empty set then the contracts will be automatically discovered
contracts
)

This method adds one existing object to the given service locator.

public static <T> ActiveDescriptor<T>

Returns:

The descriptor that was added to the system
addOneDescriptor
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
Descriptor
The non-null descriptor to add to this locator
descriptor
)

It is very often the case that one wishes to add a single descriptor to a service locator.

public static <T> ActiveDescriptor<T>

Returns:

The descriptor that was added to the system
addOneDescriptor
(ServiceLocator
The non-null locator to add this descriptor to
locator
,
Descriptor
The non-null descriptor to add to this locator
descriptor
,
boolean
If true a deep copy will be made of the key. If false then the Descriptor will be used as is, and it is the responsibility of the caller to ensure that the fields of the Descriptor never change (with the exception of any writeable fields, such as ranking)
requiresDeepCopy
)

It is very often the case that one wishes to add a single descriptor to a service locator.

public static void
bind(ServiceLocator
The non-null locator to use for the configuration action
locator
,
Binder...
The non-null list of binders to be added to the locator
binders
)

This method will bind all of the binders given together in a single config transaction.

public static ServiceLocator

Returns:

The service locator that was either found or created
bind
(String
The non-null name of the locator to use for the configuration action
name
,
Binder...
The non-null list of binders to be added to the locator
binders
)

This method will create or find a ServiceLocator with the given name and bind all of the binders given together in a single config transaction.

public static ServiceLocator

Returns:

The service locator that was either found or created
bind
(Binder...
The non-null list of binders to be added to the locator
binders
)

This method will create or find a ServiceLocator with the name "default" and bind all of the binders given together in a single config transaction.

public static ServiceLocator

Returns:

A service locator that has been populated with services
createAndPopulateServiceLocator
(String
The name of the service locator to create. If there is already a service locator of this name this method will use that one to populate. If this is null a randomly assigned name will be given to the service locator, and it will not be tracked by the system. If this is NOT null then this service locator can be found with ServiceLocatorFactory#find(String).
name
)

This method is often the first line of a stand-alone client that wishes to use HK2.

public static ServiceLocator

Returns:

A service locator that has been populated with services
createAndPopulateServiceLocator
()

This method is often the first line of a stand-alone client that wishes to use HK2.

public static DynamicConfiguration

Returns:

A non-null DynamicConfiguration object that can be used to add or remove services to the passed in locator
createDynamicConfiguration
(ServiceLocator
A non-null locator to get a DynamicConfiguration for
locator
)

This method returns a DynamicConfiguration for use with adding and removing services to the given ServiceLocator.

public static void
dumpAllDescriptors(ServiceLocator
The non-null locator to dump to stderr
locator
)

Dumps all descriptors in this ServiceLocator to stderr

public static void
dumpAllDescriptors(ServiceLocator
The non-null locator to dump
locator
,
PrintStream
The non-null PrintStream to print the descriptors to
output
)

Dumps all descriptors in this ServiceLocator to the given PrintStream

public static void
enableGreedyResolution(ServiceLocator
The locator to enable for greedy resolution
locator
)

Enables greedy service resolution in this service locator by adding the GreedyResolver into the service locator.

public static void
enableImmediateScope(ServiceLocator
The non-null locator to enable the Immediate scope on
locator
)

This method will add the ability to use the Immediate scope to the given locator.

public static ImmediateController

Returns:

The ImmediateController that can be used to further configure the Immediate scope
enableImmediateScopeSuspended
(ServiceLocator
The non-null locator to enable the Immediate scope on
locator
)

This method will add the ability to use the Immediate scope to the given locator.

public static void
enableInheritableThreadScope(ServiceLocator
The non-null locator to enable the PerThread scope on
locator
)

This method will add the ability to use the InheritableThread scope to the given locator.

public static void
enableLookupExceptions(ServiceLocator
The service locator to enable lookup exceptions on. May not be null
locator
)

This method will cause lookup operations to throw exceptions when exceptions are encountered in underlying operations such as classloading.

public static void
enablePerThreadScope(ServiceLocator
The non-null locator to enable the PerThread scope on
locator
)

This method will add the ability to use the PerThread scope to the given locator.

public static <T> ActiveDescriptor<T>

Returns:

The descriptor as found in the locator, or null if no such descriptor could be found
findOneDescriptor
(ServiceLocator
The non-null locator in which to find the descriptor
locator
,
Descriptor
The non-null descriptor to search for
descriptor
)

Finds a descriptor in the given service locator.

public static <T> T

Returns:

An instance of type as either found in the locator or automatically created, injected and post-constructed. Note that the return value CAN be null IF the service was found in the service locator and the context in which the service is in allows for null services.
findOrCreateService
(ServiceLocator
The service locator to search for the service with
locator
,
Class<T>
The non-null type of object to either find or create
type
,
Annotation...
The set of qualifiers that should be associated with the service
qualifiers
)

This method will first attempt to find a service corresponding to the type and qualifiers passed in to the method, and if one is found simply returns it.

pack-priv static String
public static Immediate

Returns:

a Immediate Annotation implementation
getImmediateAnnotation
()

Returns a Immediate Annotation implementation

public static InheritableThread

Returns:

a InheritableThread Annotation implementation
getInheritableThreadAnnotation
()

Returns a InheritableThread Annotation implementation

public static String

Returns:

The first value in the given field, or null if no such field exists in the descriptor
getOneMetadataField
(Descriptor
The non-null descriptor from which to get the first value in the given field
d
,
String
The non-null field to get the first value of
field
)

Gets one value from a metadata field from the given descriptor

public static String

Returns:

The first value in the given field, or null if no such field exists in the descriptor
getOneMetadataField
(ServiceHandle<?>
The non-null service handle from which to get the first value in the given field
h
,
String
The non-null field to get the first value of
field
)

Gets one value from a metadata field from the given service handle

public static PerLookup

Returns:

a PerLookup Annotation implementation
getPerLookupAnnotation
()

Returns a PerLookup Annotation implementation

public static PerThread

Returns:

a PerThread Annotation implementation
getPerThreadAnnotation
()

Returns a PerThread Annotation implementation

public static <T> T

Returns:

The found service, or null if there is no service with this class name
getService
(ServiceLocator
The locator to find the service in
locator
,
String
The fully qualified class name of the service
className
)

Returns the best service matching the passed in fully qualified class name of the service

public static <T> T

Returns:

The service object
getService
(ServiceLocator
The non-null locator in which to get the service associated with this descriptor
locator
,
Descriptor
The non-null descriptor to find the corresponding service for
descriptor
)

Returns the service in this service locator given the current descriptor.

public static Singleton

Returns:

a Singleton Annotation implementation
getSingletonAnnotation
()

Returns a Singleton Annotation implementation

private static boolean
public static void
removeFilter(ServiceLocator
The non-null locator to remove the descriptors from
locator
,
Filter
The non-null filter which will determine what descriptors to remove
filter
)

Removes all the descriptors from the given locator that match the given filter

public static void
removeFilter(ServiceLocator
The non-null locator to remove the descriptors from
locator
,
Filter
The non-null filter which will determine what descriptors to remove
filter
,
boolean
If set to true all AliasDescriptors that point to any descriptors found by filter will also be removed
includeAliasDescriptors
)

Removes all the descriptors from the given locator that match the given filter

public static void
removeOneDescriptor(ServiceLocator
The non-null locator to remove the descriptor from
locator
,
Descriptor
The non-null descriptor to remove from the locator
descriptor
)

This method will attempt to remove descriptors matching the passed in descriptor from the given locator.

public static void
removeOneDescriptor(ServiceLocator
The non-null locator to remove the descriptor from
locator
,
Descriptor
The non-null descriptor to remove from the locator
descriptor
,
boolean
If set to true all AliasDescriptors that point to any descriptors found by filter will also be removed
includeAliasDescriptors
)

This method will attempt to remove descriptors matching the passed in descriptor from the given locator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait