public abstract class ServiceLocatorUtilities extends Object
ServiceLocator
.Constructor and Description |
---|
ServiceLocatorUtilities() |
Modifier and Type | Method and Description |
---|---|
static List<ActiveDescriptor<?>> |
addClasses(ServiceLocator locator,
boolean idempotent,
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.
|
static List<ActiveDescriptor<?>> |
addClasses(ServiceLocator locator,
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.
|
static List<FactoryDescriptors> |
addFactoryConstants(ServiceLocator locator,
Factory<?>... constants)
This method adds factory constants to the given locator.
|
static List<FactoryDescriptors> |
addFactoryDescriptors(ServiceLocator locator,
boolean requiresDeepCopy,
FactoryDescriptors... factories)
Adds the given factory descriptors to the service locator
|
static List<FactoryDescriptors> |
addFactoryDescriptors(ServiceLocator locator,
FactoryDescriptors... factories)
Adds the given factory descriptors to the service locator
|
static <T> ActiveDescriptor<T> |
addOneConstant(ServiceLocator locator,
Object constant)
This method adds one existing object to the given service locator.
|
static <T> ActiveDescriptor<T> |
addOneConstant(ServiceLocator locator,
Object constant,
String name,
Type... contracts)
This method adds one existing object to the given service locator.
|
static <T> ActiveDescriptor<T> |
addOneDescriptor(ServiceLocator locator,
Descriptor descriptor)
It is very often the case that one wishes to add a single descriptor to
a service locator.
|
static <T> ActiveDescriptor<T> |
addOneDescriptor(ServiceLocator locator,
Descriptor descriptor,
boolean requiresDeepCopy)
It is very often the case that one wishes to add a single descriptor to
a service locator.
|
static ServiceLocator |
bind(Binder... 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.
|
static void |
bind(ServiceLocator locator,
Binder... binders)
This method will bind all of the binders given together in a
single config transaction.
|
static ServiceLocator |
bind(String name,
Binder... 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.
|
static ServiceLocator |
createAndPopulateServiceLocator()
This method is often the first line of a stand-alone client that wishes to use HK2.
|
static ServiceLocator |
createAndPopulateServiceLocator(String name)
This method is often the first line of a stand-alone client that wishes to use HK2.
|
static DynamicConfiguration |
createDynamicConfiguration(ServiceLocator locator)
This method returns a
DynamicConfiguration for use with adding
and removing services to the given ServiceLocator . |
static void |
dumpAllDescriptors(ServiceLocator locator)
Dumps all descriptors in this ServiceLocator to stderr
|
static void |
dumpAllDescriptors(ServiceLocator locator,
PrintStream output)
Dumps all descriptors in this ServiceLocator to the given PrintStream
|
static void |
enableImmediateScope(ServiceLocator locator)
This method will add the ability to use the
Immediate scope to
the given locator. |
static ImmediateController |
enableImmediateScopeSuspended(ServiceLocator locator)
This method will add the ability to use the
Immediate scope to
the given locator. |
static void |
enableInheritableThreadScope(ServiceLocator locator)
This method will add the ability to use the
InheritableThread
scope to the given locator. |
static void |
enableLookupExceptions(ServiceLocator locator)
This method will cause lookup operations to throw exceptions when
exceptions are encountered in underlying operations such as
classloading.
|
static void |
enablePerThreadScope(ServiceLocator locator)
This method will add the ability to use the
PerThread scope to
the given locator. |
static void |
enableTopicDistribution(ServiceLocator locator)
Deprecated.
Use ExtrasUtilities.enableTopicDistribution. This method WILL BE REMOVED
in the next version of hk2
|
static <T> ActiveDescriptor<T> |
findOneDescriptor(ServiceLocator locator,
Descriptor descriptor)
Finds a descriptor in the given service locator.
|
static <T> T |
findOrCreateService(ServiceLocator locator,
Class<T> type,
Annotation... 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.
|
static Immediate |
getImmediateAnnotation()
Returns a
Immediate Annotation implementation |
static InheritableThread |
getInheritableThreadAnnotation()
Returns a
InheritableThread Annotation implementation |
static String |
getOneMetadataField(Descriptor d,
String field)
Gets one value from a metadata field from the given descriptor
|
static String |
getOneMetadataField(ServiceHandle<?> h,
String field)
Gets one value from a metadata field from the given service handle
|
static PerLookup |
getPerLookupAnnotation()
Returns a
PerLookup Annotation implementation |
static PerThread |
getPerThreadAnnotation()
Returns a
PerThread Annotation implementation |
static <T> T |
getService(ServiceLocator locator,
Descriptor descriptor)
Returns the service in this service locator given the current descriptor.
|
static <T> T |
getService(ServiceLocator locator,
String className)
Returns the best service matching the passed in fully qualified
class name of the service
|
static javax.inject.Singleton |
getSingletonAnnotation()
Returns a
Singleton Annotation implementation |
static void |
removeFilter(ServiceLocator locator,
Filter filter)
Removes all the descriptors from the given locator that match the
given filter
|
static void |
removeFilter(ServiceLocator locator,
Filter filter,
boolean includeAliasDescriptors)
Removes all the descriptors from the given locator that match the
given filter
|
static void |
removeOneDescriptor(ServiceLocator locator,
Descriptor descriptor)
This method will attempt to remove descriptors matching the passed in descriptor from
the given locator.
|
static void |
removeOneDescriptor(ServiceLocator locator,
Descriptor descriptor,
boolean includeAliasDescriptors)
This method will attempt to remove descriptors matching the passed in descriptor from
the given locator.
|
public static void enablePerThreadScope(ServiceLocator locator)
PerThread
scope to
the given locator. If the locator already has a Context
implementation
that handles the PerThread
scope this method does nothing.locator
- The non-null locator to enable the PerThread scope onMultiException
- if there were errors when committing the servicepublic static void enableInheritableThreadScope(ServiceLocator locator)
InheritableThread
scope to the given locator. If the locator already has a Context
implementation that handles the InheritableThread
scope this
method does nothing.locator
- The non-null locator to enable the PerThread scope onMultiException
- if there were errors when committing the servicepublic static void enableImmediateScope(ServiceLocator locator)
Immediate
scope to
the given locator. If the locator already has a Context
implementation
that handles the Immediate
scope this method does nothing.
This implementation of Immediate
scope will use a separate thread for
instantiating services. Any failures from Immediate
scoped services
will be given to the current set of ImmediateErrorHandler
implementations
locator
- The non-null locator to enable the Immediate scope onMultiException
- if there were errors when committing the servicepublic static ImmediateController enableImmediateScopeSuspended(ServiceLocator locator)
Immediate
scope to
the given locator. If the locator already has a Context
implementation
that handles the Immediate
scope this method does nothing. The Immediate
scope will start in the suspended state, allowing the caller to customize the
Immediate scope using the ImmediateController
locator
- The non-null locator to enable the Immediate scope onMultiException
- if there were errors when committing the servicepublic static void bind(ServiceLocator locator, Binder... binders)
locator
- The non-null locator to use for the configuration actionbinders
- The non-null list of binders to be added to the locatorMultiException
- if any error was encountered while binding servicespublic static ServiceLocator bind(String name, Binder... binders)
name
- The non-null name of the locator to use for the configuration actionbinders
- The non-null list of binders to be added to the locatorMultiException
- if any error was encountered while binding servicespublic static ServiceLocator bind(Binder... binders)
binders
- The non-null list of binders to be added to the locatorMultiException
- if any error was encountered while binding servicespublic static <T> ActiveDescriptor<T> addOneConstant(ServiceLocator locator, Object constant)
BuilderHelper.createConstantDescriptor(Object)
method.locator
- The non-null locator to add this descriptor toconstant
- The non-null constant to add to the service locatorpublic static List<FactoryDescriptors> addFactoryConstants(ServiceLocator locator, Factory<?>... constants)
locator
- The non-null locator to add these factory constants toconstants
- The constant factories to add to the locator. None of the constants
in this array may be nullpublic static <T> ActiveDescriptor<T> addOneConstant(ServiceLocator locator, Object constant, String name, Type... contracts)
BuilderHelper.createConstantDescriptor(Object)
method.locator
- The non-null locator to add this descriptor toconstant
- The non-null constant to add to the service locatorname
- The name this descriptor should take (may be null)contracts
- The full set of contracts this descriptor should take. If this
field is the empty set then the contracts will be automatically discoveredpublic static <T> ActiveDescriptor<T> addOneDescriptor(ServiceLocator locator, Descriptor descriptor)
ActiveDescriptor
and is reified, it will be added as an
ActiveDescriptor
. Otherwise it will be bound as a Descriptor
.
A deep copy will be made of the descriptor passed inlocator
- The non-null locator to add this descriptor todescriptor
- The non-null descriptor to add to this locatorMultiException
- On a commit failurepublic static <T> ActiveDescriptor<T> addOneDescriptor(ServiceLocator locator, Descriptor descriptor, boolean requiresDeepCopy)
ActiveDescriptor
and is reified, it will be added as an
ActiveDescriptor
. Otherwise it will be bound as a Descriptor
.locator
- The non-null locator to add this descriptor todescriptor
- The non-null descriptor to add to this locatorrequiresDeepCopy
- 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)MultiException
- On a commit failurepublic static List<FactoryDescriptors> addFactoryDescriptors(ServiceLocator locator, FactoryDescriptors... factories)
locator
- The locator to add the factories to. May not be nullfactories
- The list of factory descriptors to add to the system. May not be nullMultiException
- On a commit failurepublic static List<FactoryDescriptors> addFactoryDescriptors(ServiceLocator locator, boolean requiresDeepCopy, FactoryDescriptors... factories)
locator
- The locator to add the factories to. May not be nullrequiresDeepCopy
- This is false ONLY if every one of the factories given to this method can be used without a copyfactories
- The list of factory descriptors to add to the system. May not be nullMultiException
- On a commit failurepublic static List<ActiveDescriptor<?>> addClasses(ServiceLocator locator, boolean idempotent, Class<?>... toAdd)
If the class to add implements Factory
then two descriptors
will be added, one for the Factory
class itself, and one for
the Factory.provide()
method of the factory. In the output
list the descriptor for the Factory
will be added first, followed
by the descriptor for the Factory.provide()
method
locator
- The non-null locator to add this descriptor totoAdd
- The classes to add to the locator. If a class in this list implements
Factory
then two descriptors will be added for that classMultiException
- On a commit failure. If idempotent is true the commit failure
may be due to duplicate descriptors found in the locatorpublic static List<ActiveDescriptor<?>> addClasses(ServiceLocator locator, Class<?>... toAdd)
If the class to add implements Factory
then two descriptors
will be added, one for the Factory
class itself, and one for
the Factory.provide()
method of the factory. In the output
list the descriptor for the Factory
will be added first, followed
by the descriptor for the Factory.provide()
method
locator
- The non-null locator to add this descriptor totoAdd
- The classes to add to the locator. If a class in this list implements
Factory
then two descriptors will be added for that classMultiException
- On a commit failurepublic static <T> ActiveDescriptor<T> findOneDescriptor(ServiceLocator locator, Descriptor descriptor)
locator
- The non-null locator in which to find the descriptordescriptor
- The non-null descriptor to search forpublic static void removeOneDescriptor(ServiceLocator locator, Descriptor descriptor)
DescriptorImpl.equals(Object)
method
will be removed from the locator. Note that if more than one descriptor matches they
will all be removed. Hence more than one descriptor may be removed by this method.locator
- The non-null locator to remove the descriptor fromdescriptor
- The non-null descriptor to remove from the locatorpublic static void removeOneDescriptor(ServiceLocator locator, Descriptor descriptor, boolean includeAliasDescriptors)
DescriptorImpl.equals(Object)
method
will be removed from the locator. Note that if more than one descriptor matches they
will all be removed. Hence more than one descriptor may be removed by this method.locator
- The non-null locator to remove the descriptor fromdescriptor
- The non-null descriptor to remove from the locatorincludeAliasDescriptors
- If set to true all AliasDescriptor
s that point
to any descriptors found by filter will also be removedpublic static void removeFilter(ServiceLocator locator, Filter filter)
locator
- The non-null locator to remove the descriptors fromfilter
- The non-null filter which will determine what descriptors to removepublic static void removeFilter(ServiceLocator locator, Filter filter, boolean includeAliasDescriptors)
locator
- The non-null locator to remove the descriptors fromfilter
- The non-null filter which will determine what descriptors to removeincludeAliasDescriptors
- If set to true all AliasDescriptor
s that point
to any descriptors found by filter will also be removedpublic static <T> T getService(ServiceLocator locator, String className)
locator
- The locator to find the service inclassName
- The fully qualified class name of the servicepublic static <T> T getService(ServiceLocator locator, Descriptor descriptor)
locator
- The non-null locator in which to get the service associated with
this descriptordescriptor
- The non-null descriptor to find the corresponding service forpublic static DynamicConfiguration createDynamicConfiguration(ServiceLocator locator) throws IllegalStateException
DynamicConfiguration
for use with adding
and removing services to the given ServiceLocator
.locator
- A non-null locator to get a DynamicConfiguration forIllegalStateException
- If there was an error retrieving the
DynamicConfigurationService
for this locatorpublic static <T> T findOrCreateService(ServiceLocator locator, Class<T> type, Annotation... qualifiers) throws MultiException
ServiceLocator.createAndInitialize(Class)
on
the class (ignoring the qualifiers) in order to create an instance of the service.locator
- The service locator to search for the service withtype
- The non-null type of object to either find or createqualifiers
- The set of qualifiers that should be associated with the serviceMultiException
- On a failure from any of the underlying operationspublic static String getOneMetadataField(Descriptor d, String field)
d
- The non-null descriptor from which to get the first value in the given fieldfield
- The non-null field to get the first value ofpublic static String getOneMetadataField(ServiceHandle<?> h, String field)
h
- The non-null service handle from which to get the first value in the given fieldfield
- The non-null field to get the first value ofpublic static ServiceLocator createAndPopulateServiceLocator(String name) throws MultiException
name
- 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)
.MultiException
- If there was a failure when populating or creating the ServiceLocatorpublic static ServiceLocator createAndPopulateServiceLocator()
MultiException
- If there was a failure when populating or creating the ServiceLocatorpublic static void enableLookupExceptions(ServiceLocator locator)
RethrowErrorService
to the service registry
Do not use this methods in secure applications where callers to lookup should not be given the information that they do NOT have access to a service
locator
- The service locator to enable lookup exceptions on. May not be nullpublic static void enableTopicDistribution(ServiceLocator locator)
The default distribution service distributes messages on the
same thread as the caller of Topic.publish(Object)
and (TBD security policy). Objects to be distributed to will be
held with SoftReferences, and hence if they go out of scope they
will not be distributed to. Only services created AFTER the topic
distribution service is enabled will be distributed to.
This method is idempotent, so that if there is already a TopicDistributionService with the default name is available this method will do nothing
locator
- The service locator to enable topic distribution on. May not be nullpublic static void dumpAllDescriptors(ServiceLocator locator)
locator
- The non-null locator to dump to stderrpublic static void dumpAllDescriptors(ServiceLocator locator, PrintStream output)
locator
- The non-null locator to dumpoutput
- The non-null PrintStream to print the descriptors topublic static javax.inject.Singleton getSingletonAnnotation()
Singleton
Annotation
implementationSingleton
Annotation
implementationpublic static PerLookup getPerLookupAnnotation()
PerLookup
Annotation
implementationPerLookup
Annotation
implementationpublic static PerThread getPerThreadAnnotation()
PerThread
Annotation
implementationPerThread
Annotation
implementationpublic static InheritableThread getInheritableThreadAnnotation()
InheritableThread
Annotation
implementationInheritableThread
Annotation
implementationpublic static Immediate getImmediateAnnotation()
Immediate
Annotation
implementationImmediate
Annotation
implementationCopyright © 2009-2017, Oracle and/or its affiliates. All Rights Reserved.