Package org.eclipse.aether.impl
Class DefaultServiceLocator
- java.lang.Object
-
- org.eclipse.aether.impl.DefaultServiceLocator
-
- All Implemented Interfaces:
ServiceLocator
public final class DefaultServiceLocator extends java.lang.Object implements ServiceLocator
A simple service locator that is already setup with all components from this library. To acquire a complete repository system, clients need to add an artifact descriptor reader, a version resolver, a version range resolver and optionally some repository connector and transporter factories to access remote repositories. Once the locator is fully populated, the repository system can be created like this: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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classDefaultServiceLocator.Entry<T>static classDefaultServiceLocator.ErrorHandlerA hook to customize the handling of errors encountered while locating a service implementation.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Class<?>,DefaultServiceLocator.Entry<?>>entriesprivate DefaultServiceLocator.ErrorHandlererrorHandler
-
Constructor Summary
Constructors Constructor Description DefaultServiceLocator()Creates a new service locator that already knows about all service implementations included this library.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> DefaultServiceLocatoraddService(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> TgetService(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 voidserviceCreationFailed(java.lang.Class<?> type, java.lang.Class<?> impl, java.lang.Throwable exception)voidsetErrorHandler(DefaultServiceLocator.ErrorHandler errorHandler)Sets the error handler to use.<T> DefaultServiceLocatorsetService(java.lang.Class<T> type, java.lang.Class<? extends T> impl)Sets the implementation class for a service.<T> DefaultServiceLocatorsetServices(java.lang.Class<T> type, T... services)Sets the instances for a service.
-
-
-
Field Detail
-
entries
private final java.util.Map<java.lang.Class<?>,DefaultServiceLocator.Entry<?>> entries
-
errorHandler
private DefaultServiceLocator.ErrorHandler errorHandler
-
-
Method Detail
-
getEntry
private <T> DefaultServiceLocator.Entry<T> getEntry(java.lang.Class<T> type, boolean create)
-
setService
public <T> DefaultServiceLocator setService(java.lang.Class<T> type, java.lang.Class<? extends T> impl)
Sets the implementation class for a service. The specified class must have a no-arg constructor (of any visibility). If the service implementation itself requires other services for its operation, it should implementServiceto gain access to this service locator.- Type Parameters:
T- The service type.- Parameters:
type- The interface describing the service, must not benull.impl- The implementation class of the service, must not benull.- Returns:
- This locator for chaining, never
null.
-
addService
public <T> DefaultServiceLocator addService(java.lang.Class<T> type, java.lang.Class<? extends T> impl)
Adds an implementation class for a service. The specified class must have a no-arg constructor (of any visibility). If the service implementation itself requires other services for its operation, it should implementServiceto gain access to this service locator.- Type Parameters:
T- The service type.- Parameters:
type- The interface describing the service, must not benull.impl- The implementation class of the service, must not benull.- Returns:
- This locator for chaining, never
null.
-
setServices
public <T> DefaultServiceLocator setServices(java.lang.Class<T> type, T... services)
Sets the instances for a service.- Type Parameters:
T- The service type.- Parameters:
type- The interface describing the service, must not benull.services- The instances of the service, may benullbut must not containnullelements.- Returns:
- This locator for chaining, never
null.
-
getService
public <T> T getService(java.lang.Class<T> type)
Description copied from interface:ServiceLocatorGets an instance of the specified service.- Specified by:
getServicein interfaceServiceLocator- Type Parameters:
T- The service type.- Parameters:
type- The interface describing the service, must not benull.- Returns:
- The service instance or
nullif the service could not be located/initialized.
-
getServices
public <T> java.util.List<T> getServices(java.lang.Class<T> type)
Description copied from interface:ServiceLocatorGets all available instances of the specified service.- Specified by:
getServicesin interfaceServiceLocator- Type Parameters:
T- The service type.- Parameters:
type- The interface describing the service, must not benull.- Returns:
- The (read-only) list of available service instances, never
null.
-
serviceCreationFailed
private void serviceCreationFailed(java.lang.Class<?> type, java.lang.Class<?> impl, java.lang.Throwable exception)
-
setErrorHandler
public void setErrorHandler(DefaultServiceLocator.ErrorHandler errorHandler)
Sets the error handler to use.- Parameters:
errorHandler- The error handler to use, may benullto ignore/swallow errors.
-
-