public final class DefaultServiceLocator extends java.lang.Object implements ServiceLocator
RepositorySystem repoSystem = serviceLocator.getService( RepositorySystem.class );Note: This class is not thread-safe. Clients are expected to create the service locator and the repository system on a single thread.
| Modifier and Type | Class and Description |
|---|---|
private class |
DefaultServiceLocator.Entry<T> |
static class |
DefaultServiceLocator.ErrorHandler
A hook to customize the handling of errors encountered while locating a service implementation.
|
| Modifier and Type | Field and Description |
|---|---|
private java.util.Map<java.lang.Class<?>,DefaultServiceLocator.Entry<?>> |
entries |
private DefaultServiceLocator.ErrorHandler |
errorHandler |
| Constructor and Description |
|---|
DefaultServiceLocator()
Creates a new service locator that already knows about all service implementations included this library.
|
| Modifier and Type | Method and Description |
|---|---|
<T> DefaultServiceLocator |
addService(java.lang.Class<T> type,
java.lang.Class<? extends T> impl)
Adds an implementation class for a service.
|
private <T> DefaultServiceLocator.Entry<T> |
getEntry(java.lang.Class<T> type,
boolean create) |
<T> T |
getService(java.lang.Class<T> type)
Gets an instance of the specified service.
|
<T> java.util.List<T> |
getServices(java.lang.Class<T> type)
Gets all available instances of the specified service.
|
private void |
serviceCreationFailed(java.lang.Class<?> type,
java.lang.Class<?> impl,
java.lang.Throwable exception) |
void |
setErrorHandler(DefaultServiceLocator.ErrorHandler errorHandler)
Sets the error handler to use.
|
<T> DefaultServiceLocator |
setService(java.lang.Class<T> type,
java.lang.Class<? extends T> impl)
Sets the implementation class for a service.
|
<T> DefaultServiceLocator |
setServices(java.lang.Class<T> type,
T... services)
Sets the instances for a service.
|
private final java.util.Map<java.lang.Class<?>,DefaultServiceLocator.Entry<?>> entries
private DefaultServiceLocator.ErrorHandler errorHandler
public DefaultServiceLocator()
private <T> DefaultServiceLocator.Entry<T> getEntry(java.lang.Class<T> type, boolean create)
public <T> DefaultServiceLocator setService(java.lang.Class<T> type, java.lang.Class<? extends T> impl)
Service to gain access to this service locator.T - The service type.type - The interface describing the service, must not be null.impl - The implementation class of the service, must not be null.null.public <T> DefaultServiceLocator addService(java.lang.Class<T> type, java.lang.Class<? extends T> impl)
Service to gain access to this service locator.T - The service type.type - The interface describing the service, must not be null.impl - The implementation class of the service, must not be null.null.public <T> DefaultServiceLocator setServices(java.lang.Class<T> type, T... services)
T - The service type.type - The interface describing the service, must not be null.services - The instances of the service, may be null but must not contain null elements.null.public <T> T getService(java.lang.Class<T> type)
ServiceLocatorgetService in interface ServiceLocatorT - The service type.type - The interface describing the service, must not be null.null if the service could not be located/initialized.public <T> java.util.List<T> getServices(java.lang.Class<T> type)
ServiceLocatorgetServices in interface ServiceLocatorT - The service type.type - The interface describing the service, must not be null.null.private void serviceCreationFailed(java.lang.Class<?> type,
java.lang.Class<?> impl,
java.lang.Throwable exception)
public void setErrorHandler(DefaultServiceLocator.ErrorHandler errorHandler)
errorHandler - The error handler to use, may be null to ignore/swallow errors.