org.jvnet.hk2.internal
Class ServiceLocatorImpl

java.lang.Object
  extended by org.jvnet.hk2.internal.ServiceLocatorImpl
All Implemented Interfaces:
ServiceLocator

public class ServiceLocatorImpl
extends Object
implements ServiceLocator

Author:
jwells

Constructor Summary
ServiceLocatorImpl(String name, ServiceLocator parent)
          Called by the Generator, and hence must be a public method
 
Method Summary
<T> T
create(Class<T> createMe)
          This method will analyze the given class, and create it if can.
<U> U
createAndInitialize(Class<U> createMe)
          Creates, injects and postConstructs, all in one
 List<ServiceHandle<?>> getAllServiceHandles(Annotation qualifier, Annotation... qualifiers)
          Gets service handles that can be used to get and destroy the returned services
 List<ServiceHandle<?>> getAllServiceHandles(Filter searchCriteria)
          Gets a service handle that can be used to get and destroy the returned service.
 List<ServiceHandle<?>> getAllServiceHandles(Type contractOrImpl, Annotation... qualifiers)
          Gets service handles that can be used to get and destroy the returned services
<T> List<T>
getAllServices(Annotation qualifier, Annotation... qualifiers)
          Gets the all the services from this locator that has the given qualifier or qualifiers
 List<?> getAllServices(Filter searchCriteria)
          Gets the all the services from this locator that implements this contract or has this implementation
<T> List<T>
getAllServices(Type contractOrImpl, Annotation... qualifiers)
          Gets the all the services from this locator that implements this contract or has this implementation
 ActiveDescriptor<?> getBestDescriptor(Filter filter)
          Gets the descriptor that best matches this filter, taking ranking and service id into account
 List<ActiveDescriptor<?>> getDescriptors(Filter filter)
          Gets the list of descriptors that match the given filter
 ActiveDescriptor<?> getInjecteeDescriptor(Injectee injectee)
          This method will first find a descriptor for this injectee, and then reify that descriptor.
 long getLocatorId()
          This returns the unique locator ID for this locator.
 String getName()
          Returns the name of this ServiceLocator
<T> T
getService(ActiveDescriptor<T> activeDescriptor, ServiceHandle<?> root)
          This method should be called by code getting injectee's on behalf of some root object.
<T> T
getService(Type contractOrImpl, Annotation... qualifiers)
          Gets the best service from this locator that implements this contract or has this implementation
<T> T
getService(Type contractOrImpl, String name, Annotation... qualifiers)
          Gets the best service from this locator that implements this contract or has this implementation and has the given name
<T> ServiceHandle<T>
getServiceHandle(ActiveDescriptor<T> activeDescriptor)
          Gets a service handle that can be used to get and destroy the returned service.
<T> ServiceHandle<T>
getServiceHandle(ActiveDescriptor<T> activeDescriptor, Injectee injectee)
          Gets a service handle that can be used to get and destroy the returned service.
<T> ServiceHandle<T>
getServiceHandle(Type contractOrImpl, Annotation... qualifiers)
          Gets a service handle that can be used to get and destroy the returned service.
<T> ServiceHandle<T>
getServiceHandle(Type contractOrImpl, String name, Annotation... qualifiers)
          Gets a service handle that can be used to get and destroy the returned service
 void inject(Object injectMe)
          This will analyze the given object and inject into its fields and methods.
 void postConstruct(Object postConstructMe)
          This will analyze the given object and call the postConstruct method.
 void preDestroy(Object preDestroyMe)
          This will analyze the given object and call the preDestroy method.
 ActiveDescriptor<?> reifyDescriptor(Descriptor descriptor)
          Converts a descriptor to an ActiveDescriptor.
 ActiveDescriptor<?> reifyDescriptor(Descriptor descriptor, Injectee injectee)
          Converts a descriptor to an ActiveDescriptor.
 void shutdown()
          This method will shutdown every service associated with this ServiceLocator.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ServiceLocatorImpl

public ServiceLocatorImpl(String name,
                          ServiceLocator parent)
Called by the Generator, and hence must be a public method

Parameters:
name - The name of this locator
parent - The parent of this locator (may be null)
Method Detail

getDescriptors

public List<ActiveDescriptor<?>> getDescriptors(Filter filter)
Description copied from interface: ServiceLocator
Gets the list of descriptors that match the given filter

Specified by:
getDescriptors in interface ServiceLocator
Parameters:
filter - A filter to use when determining which services should apply
Returns:
A list of descriptors in ranked order that match the given filter

getBestDescriptor

public ActiveDescriptor<?> getBestDescriptor(Filter filter)
Description copied from interface: ServiceLocator
Gets the descriptor that best matches this filter, taking ranking and service id into account

Specified by:
getBestDescriptor in interface ServiceLocator
Parameters:
filter - The filter to use to retrieve the set of descriptors
Returns:
The best descriptor matching the filter, or null if there is no descriptor that matches the filter

reifyDescriptor

public ActiveDescriptor<?> reifyDescriptor(Descriptor descriptor,
                                           Injectee injectee)
                                    throws MultiException
Description copied from interface: ServiceLocator
Converts a descriptor to an ActiveDescriptor. Will use the registered HK2Loaders to perform this action

Specified by:
reifyDescriptor in interface ServiceLocator
Parameters:
descriptor - The descriptor to convert, may not be null
injectee - The injectee on behalf of whom this descriptor is being injected. May be null if the injectee is unknown
Returns:
The active descriptor as loaded with the first valid HK2Loader
Throws:
MultiException - if there were errors when loading or analyzing the class

reifyDescriptor

public ActiveDescriptor<?> reifyDescriptor(Descriptor descriptor)
                                    throws MultiException
Description copied from interface: ServiceLocator
Converts a descriptor to an ActiveDescriptor. Will use the registered HK2Loaders to perform this action

Specified by:
reifyDescriptor in interface ServiceLocator
Parameters:
descriptor - The descriptor to convert, may not be null
Returns:
The active descriptor as loaded with the first valid HK2Loader
Throws:
MultiException - if there were errors when loading or analyzing the class

getInjecteeDescriptor

public ActiveDescriptor<?> getInjecteeDescriptor(Injectee injectee)
                                          throws MultiException
Description copied from interface: ServiceLocator
This method will first find a descriptor for this injectee, and then reify that descriptor. If multiple descriptors are found, they will be reified in ranking order until an ActiveDescriptor matching the Injectee is found.

Specified by:
getInjecteeDescriptor in interface ServiceLocator
Parameters:
injectee - the injection point for whom to find the ActiveDescriptor
Returns:
The active descriptor for this injection point
Throws:
MultiException - if there were errors when loading or analyzing the class

getServiceHandle

public <T> ServiceHandle<T> getServiceHandle(ActiveDescriptor<T> activeDescriptor,
                                             Injectee injectee)
                                  throws MultiException
Description copied from interface: ServiceLocator
Gets a service handle that can be used to get and destroy the returned service. If a service, and all per lookup services must be destroyed then this method should be used to destroy the object

It is assumed that this method is called by the top level code. All injection points created because of this invocation must use the getServiceHandle(ActiveDescriptor, ServiceHandle) method to retrieve objects, so that they can be destroyed in the proper sequence

Specified by:
getServiceHandle in interface ServiceLocator
Parameters:
activeDescriptor - The service handle that can be used to get and destroy this service
injectee - The injectee on behalf of whom this descriptor is being injected. May be null if the injectee is unknown
Returns:
Will return root as a convenience
Throws:
MultiException - if there was an error during service creation

getServiceHandle

public <T> ServiceHandle<T> getServiceHandle(ActiveDescriptor<T> activeDescriptor)
                                  throws MultiException
Description copied from interface: ServiceLocator
Gets a service handle that can be used to get and destroy the returned service. If a service, and all per lookup services must be destroyed then this method should be used to destroy the object

It is assumed that this method is called by the top level code. All injection points created because of this invocation must use the getServiceHandle(ActiveDescriptor, ServiceHandle) method to retrieve objects, so that they can be destroyed in the proper sequence

Specified by:
getServiceHandle in interface ServiceLocator
Parameters:
activeDescriptor - The service handle that can be used to get and destroy this service
Returns:
Will return root as a convenience
Throws:
MultiException - if there was an error during service creation

getService

public <T> T getService(ActiveDescriptor<T> activeDescriptor,
                        ServiceHandle<?> root)
             throws MultiException
Description copied from interface: ServiceLocator
This method should be called by code getting injectee's on behalf of some root object. In this way the objects associated with the root object can be destroyed in the proper sequence

Specified by:
getService in interface ServiceLocator
Parameters:
activeDescriptor - The descriptor whose service to create
root - The ultimate parent of this service creation. May be null
Returns:
The service matching this descriptor
Throws:
MultiException - if there was an error during service creation

getService

public <T> T getService(Type contractOrImpl,
                        Annotation... qualifiers)
             throws MultiException
Description copied from interface: ServiceLocator
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

Specified by:
getService in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
qualifiers - The set of qualifiers that must match this service definition
Returns:
An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
Throws:
MultiException - if there was an error during service creation

getAllServices

public <T> List<T> getAllServices(Type contractOrImpl,
                                  Annotation... qualifiers)
                       throws MultiException
Description copied from interface: ServiceLocator
Gets the all the services from this locator that implements this contract or has this implementation

Use this method only if destroying the service is not important

Specified by:
getAllServices in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
qualifiers - The set of qualifiers that must match this service definition
Returns:
A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
Throws:
MultiException - if there was an error during service creation

getAllServices

public <T> List<T> getAllServices(Annotation qualifier,
                                  Annotation... qualifiers)
                       throws MultiException
Description copied from interface: ServiceLocator
Gets the all the services from this locator that has the given qualifier or qualifiers

Use this method only if destroying the services is not important

Specified by:
getAllServices in interface ServiceLocator
Parameters:
qualifier - May not be null, and is a qualifier that must match the service definition
qualifiers - The set of qualifiers that must match this service definition
Returns:
A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
Throws:
MultiException - if there was an error during service creation

getService

public <T> T getService(Type contractOrImpl,
                        String name,
                        Annotation... qualifiers)
             throws MultiException
Description copied from interface: ServiceLocator
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

Specified by:
getService in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
name - May not be null, and is the name of the implementation to be returned
qualifiers - The set of qualifiers that must match this service definition
Returns:
An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract
Throws:
MultiException - if there was an error during service creation

getAllServices

public List<?> getAllServices(Filter searchCriteria)
                       throws MultiException
Description copied from interface: ServiceLocator
Gets the all the services from this locator that implements this contract or has this implementation

Use this method only if destroying the service is not important

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

Specified by:
getAllServices in interface ServiceLocator
Parameters:
searchCriteria - The returned service will match the Filter (in other words, searchCriteria.matches returns true). May not be null
Returns:
A list of services matching this filter. May not return null, but may return an empty list
Throws:
MultiException - if there was an error during service creation

getName

public String getName()
Description copied from interface: ServiceLocator
Returns the name of this ServiceLocator

Specified by:
getName in interface ServiceLocator
Returns:
The name of this ServiceLocator, will not return null

shutdown

public void shutdown()
Description copied from interface: ServiceLocator
This method will shutdown every service associated with this ServiceLocator. Those services that have a preDestroy shall have their preDestroy called

Specified by:
shutdown in interface ServiceLocator

create

public <T> T create(Class<T> createMe)
Description copied from interface: ServiceLocator
This method will analyze the given class, and create it if can. The object created in this way will not be managed by HK2. It is the responsibility of the caller to ensure that any lifecycle this object has is honored

Specified by:
create in interface ServiceLocator
Parameters:
createMe - The class to create, may not be null
Returns:
An instance of the object

inject

public void inject(Object injectMe)
Description copied from interface: ServiceLocator
This will analyze the given object and inject into its fields and methods. The object injected in this way will not be managed by HK2

Specified by:
inject in interface ServiceLocator
Parameters:
injectMe - The object to be analyzed and injected into

postConstruct

public void postConstruct(Object postConstructMe)
Description copied from interface: ServiceLocator
This will analyze the given object and call the postConstruct method. The object given will not be managed by HK2

Specified by:
postConstruct in interface ServiceLocator
Parameters:
postConstructMe - The object to postConstruct

preDestroy

public void preDestroy(Object preDestroyMe)
Description copied from interface: ServiceLocator
This will analyze the given object and call the preDestroy method. The object given will not be managed by HK2

Specified by:
preDestroy in interface ServiceLocator
Parameters:
preDestroyMe - The object to preDestroy

createAndInitialize

public <U> U createAndInitialize(Class<U> createMe)
Creates, injects and postConstructs, all in one

Specified by:
createAndInitialize in interface ServiceLocator
Parameters:
createMe - The non-null class to create this object from
Returns:
An instance of the object that has been created, injected and post constructed

getServiceHandle

public <T> ServiceHandle<T> getServiceHandle(Type contractOrImpl,
                                             Annotation... qualifiers)
                                  throws MultiException
Description copied from interface: ServiceLocator
Gets a service handle that can be used to get and destroy the returned service. If a service, and all per lookup services must be destroyed then this method should be used to destroy the object

It is assumed that this method is called by the top level code. All injection points created because of this invocation must use the getServiceHandle(ActiveDescriptor, ServiceHandle) method to retrieve objects, so that they can be destroyed in the proper sequence

Specified by:
getServiceHandle in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
qualifiers - The set of qualifiers that must match this service definition
Returns:
Will return root as a convenience
Throws:
MultiException - if there was an error during service creation

getAllServiceHandles

public List<ServiceHandle<?>> getAllServiceHandles(Type contractOrImpl,
                                                   Annotation... qualifiers)
                                            throws MultiException
Description copied from interface: ServiceLocator
Gets service handles that can be used to get and destroy the returned services

Specified by:
getAllServiceHandles in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
qualifiers - The set of qualifiers that must match this service definition
Returns:
A non-null but possibly empty list of service handles matching the given criteria
Throws:
MultiException - if there was an error during service creation

getServiceHandle

public <T> ServiceHandle<T> getServiceHandle(Type contractOrImpl,
                                             String name,
                                             Annotation... qualifiers)
                                  throws MultiException
Description copied from interface: ServiceLocator
Gets a service handle that can be used to get and destroy the returned service

Specified by:
getServiceHandle in interface ServiceLocator
Parameters:
contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of
name - The name to use to further qualify the search (may be null)
qualifiers - The set of qualifiers that must match this service definition
Returns:
Will the service handle for the best service matching the given criteria, or null if no matching service could be found
Throws:
MultiException - if there was an error during service creation

getAllServiceHandles

public List<ServiceHandle<?>> getAllServiceHandles(Filter searchCriteria)
                                            throws MultiException
Description copied from interface: ServiceLocator
Gets a service handle that can be used to get and destroy the returned service. If a service, and all per lookup services must be destroyed then this method should be used to destroy the object

It is assumed that this method is called by the top level code. All injection points created because of this invocation must use the getServiceHandle(ActiveDescriptor, ServiceHandle) method to retrieve objects, so that they can be destroyed in the proper sequence

Specified by:
getAllServiceHandles in interface ServiceLocator
Parameters:
searchCriteria - A filter to use when determining which services should apply
Returns:
A list of handles in ranked order that match the given filter
Throws:
MultiException - if there was an error during service creation

getAllServiceHandles

public List<ServiceHandle<?>> getAllServiceHandles(Annotation qualifier,
                                                   Annotation... qualifiers)
                                            throws MultiException
Description copied from interface: ServiceLocator
Gets service handles that can be used to get and destroy the returned services

Specified by:
getAllServiceHandles in interface ServiceLocator
Parameters:
qualifier - May not be null, and is a qualifier that must match the service definition
qualifiers - The set of qualifiers that must match this service definition
Returns:
A non-null but possibly empty list of service handles matching the given criteria
Throws:
MultiException - if there was an error during service creation

getLocatorId

public long getLocatorId()
Description copied from interface: ServiceLocator
This returns the unique locator ID for this locator. The locator ID will be assigned at the time of creation and must be a monotonacally increasing number (starting at zero)

Specified by:
getLocatorId in interface ServiceLocator
Returns:
The identifier for this service locator

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013 Oracle Corporation. All Rights Reserved.