Top Description Methods
org.glassfish.hk2.api

public Interface ServiceLocator

Annotations
@Contract
Imports
org.jvnet.hk2.annotations.Contract, java.lang.annotation.Annotation, java.lang.reflect.Method, .Type, java.util.List

ServiceLocator is the registry for HK2 services

This service is the most fundamental service in an HK2 system. Every service locator starts with a ServiceLocator as a service, and hence ServiceLocators can be injected into every object managed by HK2.

A service locator can have a single parent. Services are looked up in the current service locator and in all the parents of the service locator. If multiple services exist that match the filter they will all be returned. Two services with the same priority are sorted first by service locator id and second by service id. This implies that services directly installed in a ServiceLocator have higher natural priority than those in the parents of the ServiceLocator. Services can also be marked as having visibility LOCAL, in which case they will only be available to the ServiceLocator performing the lookup, and will not leak out to children of that ServiceLocator.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

The return value of the method (null if the method is void type)
assistedInject
(Object
The non-null object to inject into
injectMe
,
Method
The non-null method to inject into
method
,
MethodParameter...
A list of parameter values known by the caller. The indexes in params may not repeat and must be in the valid range of parameters for the passed in method
params
)

This will invoke the given method on the given object.

public Object

Returns:

The return value of the method (null if the method is void type)
assistedInject
(Object
The non-null object to inject into
injectMe
,
Method
The non-null method to inject into
method
,
ServiceHandle<?>
A possibly null ServiceHandle that can be used after this call to destroy any hanging PerLookup instances created
root
,
MethodParameter...
A list of parameter values known by the caller. The indexes in params may not repeat and must be in the valid range of parameters for the passed in method
params
)

This will invoke the given method on the given object.

public <T> T

Returns:

An instance of the object
create
(Class<T>
The class to create, may not be null
createMe
)

This method will analyze the given class, and create it if can.

public <T> T

Returns:

An instance of the object
create
(Class<T>
The class to create, may not be null
createMe
,
String
The name of the ClassAnalyzer that should be used. If null the default analyzer will be used
strategy
)

This method will analyze the given class, and create it if can.

public <U> U

Returns:

An instance of the object that has been created, injected and post constructed
createAndInitialize
(Class<U>
The non-null class to create this object from
createMe
)

This method creates, injects and post-constructs an object with the given class.

public <U> U

Returns:

An instance of the object that has been created, injected and post constructed
createAndInitialize
(Class<U>
The non-null class to create this object from
createMe
,
String
The name of the ClassAnalyzer that should be used. If null the default analyzer will be used
strategy
)

This method creates, injects and post-constructs an object with the given class.

public <T> List<ServiceHandle<T>>

Returns:

A non-null but possibly empty list of service handles matching the given criteria
getAllServiceHandles
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a list of ServiceHandle that can be used to get and destroy services associated with descriptors that match the provided criteria

public List<ServiceHandle<?>>

Returns:

A non-null but possibly empty list of service handles matching the given criteria
getAllServiceHandles
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a list of ServiceHandle that can be used to get and destroy services associated with descriptors that match the provided criteria

public List<ServiceHandle<?>>

Returns:

A non-null but possibly empty list of service handles matching the given criteria
getAllServiceHandles
(Annotation
May not be null, and is a qualifier that must match the service definition
qualifier
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a list of ServiceHandle that can be used to get and destroy services associated with descriptors that match the provided criteria

public List<ServiceHandle<?>>

Returns:

A non-null but possibly empty list of service handles that match the given filter
getAllServiceHandles
(Filter
A filter to use when determining which descriptors should apply
searchCriteria
)

Gets a list of ServiceHandle whose ActiveDescriptors match the supplied filter.

public <T> List<T>

Returns:

A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
getAllServices
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets all services from this locator that implement this contract or have this implementation and have the provided qualifiers

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getAllServiceHandles(Class, Annotation...)

public <T> List<T>

Returns:

A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
getAllServices
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets all services from this locator that implement this contract or have this implementation and have the provided qualifiers

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getAllServiceHandles(Type, Annotation...)

public <T> List<T>

Returns:

A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
getAllServices
(Annotation
May not be null, and is a qualifier that must match the service definition
qualifier
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets all services from this locator that have the provided qualifiers

Use this method only if destroying the services is not important, otherwise use ServiceLocator#getAllServiceHandles(Annotation, Annotation...)

public List<?>

Returns:

A list of services matching this filter. May not return null, but may return an empty list
getAllServices
(Filter
The returned service will match the Filter (in other words, searchCriteria.matches returns true). May not be null
searchCriteria
)

Gets all services from this locator that match the provided Filter

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getAllServiceHandles(Filter)

This method should also be used with care to avoid classloading a large number of services

public ActiveDescriptor<?>

Returns:

The best descriptor matching the filter, or null if there is no descriptor that matches the filter
getBestDescriptor
(Filter
The non-null filter to use to retrieve the best descriptor
filter
)

Gets the descriptor that best matches this filter, taking ranking and service id into account

public String

Returns:

The name of the default class analyzer. Will not return null
getDefaultClassAnalyzerName
()

Gets the name of the default class analyzer service

public Unqualified

Returns:

The Unqualified annotation that will be (virtually) put on injection points that have no qualifiers and no other Unqualified annotation. If null then this ServiceLocator has no default Unqualified annotation to be applied
getDefaultUnqualified
()

This returns the default Unqualified annotation that will be put on all injection points that do not have any qualifiers.

public List<ActiveDescriptor<?>>

Returns:

A non-null but possibly empty list of descriptors in ranked order that match the given filter
getDescriptors
(Filter
A filter to use when determining which services should apply
filter
)

Gets the list of descriptors that match the given filter

public ActiveDescriptor<?>

Returns:

The active descriptor for this injection point
getInjecteeDescriptor
(Injectee
the injection point for whom to find the ActiveDescriptor
injectee
)

This method will first find a descriptor for this injectee, and then reify that descriptor.

public long

Returns:

The identifier for this service locator
getLocatorId
()

This returns the unique locator ID for this locator.

public String

Returns:

The name of this ServiceLocator, will not return null
getName
()

Returns the name of this ServiceLocator

public boolean

Returns:

If true hk2 will ensure that the context class loader cannot be changed by user code. If false hk2 will not modify the context class loader of the thread when user code has finished
getNeutralContextClassLoader
()

This returns the value of neutralContextClassLoader.

public ServiceLocator

Returns:

The parent service locator for this locator, or null if this service locator does not have a parent
getParent
()

Gets the parent service locator for this locator

public <T> T

Returns:

An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
getService
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets the best service from this locator that implements this contract or has this implementation

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getServiceHandle(Class, Annotation...)

public <T> T

Returns:

An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
getService
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets the best service from this locator that implements this contract or has this implementation

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getServiceHandle(Type, Annotation...)

public <T> T

Returns:

An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
getService
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
String
May be null (to indicate any name is ok), and is the name of the implementation to be returned
name
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets the best service from this locator that implements this contract or has this implementation and has the given name

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getServiceHandle(Class, String, Annotation...)

public <T> T

Returns:

An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
getService
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
String
May be null (to indicate any name is ok), and is the name of the implementation to be returned
name
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets the best service from this locator that implements this contract or has this implementation and has the given name

Use this method only if destroying the service is not important, otherwise use ServiceLocator#getServiceHandle(Type, String, Annotation...)

public <T> T

Returns:

The service matching this descriptor
getService
(ActiveDescriptor<T>
The descriptor whose service to create
activeDescriptor
,
ServiceHandle<?>
The ultimate parent of this service creation. May be null
root
)

This method should be called by code resolving injectee's on behalf of some root service, usually by an implementation of InjectionResolver#resolve(Injectee, ServiceHandle).

public <T> T

Returns:

The service matching this descriptor
getService
(ActiveDescriptor<T>
The descriptor whose service to create
activeDescriptor
,
ServiceHandle<?>
The ultimate parent of this service creation. May be null
root
,
Injectee
The injectee passed into the InjectionResolver#resolve(Injectee, ServiceHandle) if known, null otherwise
injectee
)

This method should be called by code resolving injectee's on behalf of some root service, usually by an implementation of InjectionResolver#resolve(Injectee, ServiceHandle).

public <T> ServiceHandle<T>

Returns:

Will return the service handle corresponding to the service or null if none were found
getServiceHandle
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a ServiceHandle that can be used to get and destroy the service that best matches the given criteria

public <T> ServiceHandle<T>

Returns:

Will return the service handle corresponding to the service or null if none were found
getServiceHandle
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a ServiceHandle that can be used to get and destroy the service that best matches the given criteria

public <T> ServiceHandle<T>

Returns:

Will the service handle for the best service matching the given criteria, or null if no matching service could be found
getServiceHandle
(Class<T>
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
String
The name to use to further qualify the search (may be null, indicating that any name will match)
name
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a ServiceHandle that can be used to get and destroy the service that best matches the given criteria

public <T> ServiceHandle<T>

Returns:

Will the service handle for the best service matching the given criteria, or null if no matching service could be found
getServiceHandle
(Type
May not be null, and is the contract or concrete implementation to get the best instance of
contractOrImpl
,
String
The name to use to further qualify the search (may be null, indicating that any name will match)
name
,
Annotation...
The set of qualifiers that must match this service definition
qualifiers
)

Gets a ServiceHandle that can be used to get and destroy the service that best matches the given criteria

public <T> ServiceHandle<T>

Returns:

A ServiceHandle that may be used to create or destroy the service associated with this ActiveDescriptor
getServiceHandle
(ActiveDescriptor<T>
The descriptor for which to create a ServiceHandle. May not be null
activeDescriptor
,
Injectee
The injectee on behalf of whom this service is being injected. May be null if the injectee is unknown
injectee
)

Gets a ServiceHandle that can be used to get and destroy the service described by the ActiveDescriptor.

public <T> ServiceHandle<T>

Returns:

A ServiceHandle that may be used to create or destroy the service associated with this ActiveDescriptor
getServiceHandle
(ActiveDescriptor<T>
The descriptor for which to create a ServiceHandle. May not be null
activeDescriptor
)

Gets a ServiceHandle that can be used to get and destroy the service described by the ActiveDescriptor.

public ServiceLocatorState

Returns:

The current state of the service locator
getState
()

Returns the current state of this service locator.

public void
inject(Object
The object to be analyzed and injected into
injectMe
)

This will analyze the given object and inject into its fields and methods.

public void
inject(Object
The object to be analyzed and injected into
injectMe
,
String
The name of the ClassAnalyzer that should be used. If null the default analyzer will be used
strategy
)

This will analyze the given object and inject into its fields and methods.

public boolean

Returns:

If the service locator has been shutdown.
isShutdown
()

Returns whether the state of this service locator is shutdown.

public void
postConstruct(Object
The object to postConstruct
postConstructMe
)

This will analyze the given object and call the postConstruct method.

public void
postConstruct(Object
The object to postConstruct
postConstructMe
,
String
The name of the ClassAnalyzer that should be used. If null the default analyzer will be used
strategy
)

This will analyze the given object and call the postConstruct method.

public void
preDestroy(Object
The object to preDestroy
preDestroyMe
)

This will analyze the given object and call the preDestroy method.

public void
preDestroy(Object
The object to preDestroy
preDestroyMe
,
String
The name of the ClassAnalyzer that should be used. If null the default analyzer will be used
strategy
)

This will analyze the given object and call the preDestroy method.

public ActiveDescriptor<?>

Returns:

The active descriptor as loaded with the first valid HK2Loader
reifyDescriptor
(Descriptor
The descriptor to convert, may not be null
descriptor
,
Injectee
The injectee on behalf of whom this descriptor is being injected. May be null if the injectee is unknown
injectee
)

Converts a descriptor to an ActiveDescriptor.

public ActiveDescriptor<?>

Returns:

The active descriptor as loaded with the first valid HK2Loader
reifyDescriptor
(Descriptor
The descriptor to convert, may not be null
descriptor
)

Converts a descriptor to an ActiveDescriptor.

public void
setDefaultClassAnalyzerName(String
The possibly null name of the default class analyzer (the class analyzer that will be used if a descriptor has not explicitly set the name of its class analyzer)
defaultClassAnalyzer
)

Sets the name of the default class analyzer that should be used for all Descriptors that return null as their class analyzer.

public void
setDefaultUnqualified(Unqualified
The Unqualified annotation that will be (virtually) put on injection points that have no qualifiers and no other Unqualified annotation. If null then this ServiceLocator has no default Unqualified annotation to be applied
unqualified
)

This sets the default Unqualified annotation that will be put on all injection points that do not have any qualifiers.

public void
setNeutralContextClassLoader(boolean
true if hk2 should ensure context class loader neutrality, false if hk2 should not change the context class loader on the thread around user code calls
neutralContextClassLoader
)

This sets the value of neutralContextClassLoader.

public void
shutdown()

This method will shutdown every service associated with this ServiceLocator.