Interface ServiceFinder
- All Known Implementing Classes:
DefaultServiceFinder
Very similar to ServiceLoader but provides additional methods to
load configurations without being forced to load the classes.
Furthermore, it is not restricted to META-INF/services and the contents
of the configuration file. Supports dedicated classloaders for services as well.
The configuration files are read in the order determined by the ModuleSorter:
named modules according to their dependencies (the topmost module first), followed by the classpath
and automatic modules (unless the service is annotated with ClasspathFirst).
Throughout this interface, this is referred to as the "module order".
Applications should use the ServiceFactory to allow replacement of the
finder (see Service-annotation).
- Author:
- harald
-
Method Summary
Modifier and TypeMethodDescriptioncreateNameMap(String serviceName) Creates a map of names to mapped services.
The configurations are taken fromConstants.MAPPED_SERVICE_PATHand have the formprovider = name(orprovider : name), as generated forMappedServices.createNameMapToAll(String serviceName) Creates a map of names to all mapped services.
Same ascreateNameMap(String), but maps each name to all of its providers.findFirstServiceConfiguration(String serviceName) Finds the first service configuration by service name.
If similar configurations appear more than once, the first in module order is returned.<T> Class<T> findFirstServiceProvider(Class<T> service) Finds the first service provider by service class.
If similar configurations appear more than once, the first in module order is returned.findServiceConfigurations(String serviceName) Finds service configurations by service name.
Iterations over the returned map are ordered by discovery in module order.<T> Collection<Class<T>> findServiceProviders(Class<T> service) Finds the service providers by service class.
The providers are ordered by discovery in module order.findServiceURLs(String serviceName) Finds URLs of a service by name.
The URLs are sorted in module order.findUniqueServiceConfiguration(String serviceName) Finds a unique service configuration by service name.
It is checked that the service is defined exactly once.<T> Class<T> findUniqueServiceProvider(Class<T> service) Finds the unique service provider by service class.
It is checked that the service is defined exactly once.Gets the default classloader used by this finder.default ModuleLayerGets the module layer this finder looks up services in, if any.
A non-null layer meansgetClassLoader()is the layer's own classloader (seeModuleLayerBuilder), and module ordering for this finder's service lookups is based on that layer's own module graph rather than the one visible from the context classloader.Gets the finder's service path it is responsible for.
The path is normalized: it never starts with a slash and always ends with one, for example"META-INF/services/".
-
Method Details
-
getClassLoader
ClassLoader getClassLoader()Gets the default classloader used by this finder.- Returns:
- the classloader, never null
-
getModuleLayer
Gets the module layer this finder looks up services in, if any.
A non-null layer meansgetClassLoader()is the layer's own classloader (seeModuleLayerBuilder), and module ordering for this finder's service lookups is based on that layer's own module graph rather than the one visible from the context classloader.- Returns:
- the module layer, null if this finder is not layer-based
-
getServicePath
String getServicePath()Gets the finder's service path it is responsible for.
The path is normalized: it never starts with a slash and always ends with one, for example"META-INF/services/".- Returns:
- the service path
-
findFirstServiceConfiguration
Finds the first service configuration by service name.
If similar configurations appear more than once, the first in module order is returned. Useful to override service default implementations.- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service configuration entry, never null
- Throws:
TentackleRuntimeException- if no such service found
-
findFirstServiceProvider
Finds the first service provider by service class.
If similar configurations appear more than once, the first in module order is returned. Useful to override service default implementations.- Type Parameters:
T- the service type- Parameters:
service- the service class- Returns:
- the provider class
- Throws:
ClassNotFoundException- if some provider could not be loadedTentackleRuntimeException- if no such service found or the provider does not implement the service
-
findServiceConfigurations
Finds service configurations by service name.
Iterations over the returned map are ordered by discovery in module order. The keys are the configuration lines, the values the URLs of the files they were found in. The same configuration line in two different files is considered an error, since it usually indicates a misconfiguration.- Parameters:
serviceName- the service name, usually a classname- Returns:
- a map of the configured services and their corresponding URLs
- Throws:
TentackleRuntimeException- if a configuration could not be read or is defined more than once
-
findServiceProviders
Finds the service providers by service class.
The providers are ordered by discovery in module order.- Type Parameters:
T- the service type- Parameters:
service- the service class- Returns:
- the classes providing this service
- Throws:
ClassNotFoundException- if some provider could not be loadedTentackleRuntimeException- if some provider does not implement the service
-
findServiceURLs
Finds URLs of a service by name.
The URLs are sorted in module order.- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service's resource URLs
- Throws:
TentackleRuntimeException- if the resources could not be determined
-
findUniqueServiceConfiguration
Finds a unique service configuration by service name.
It is checked that the service is defined exactly once. Useful for factories, singletons and alike.- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service configuration entry
- Throws:
TentackleRuntimeException- if no such service found or the service is defined more than once
-
findUniqueServiceProvider
Finds the unique service provider by service class.
It is checked that the service is defined exactly once.- Type Parameters:
T- the service type- Parameters:
service- the service class- Returns:
- the provider class
- Throws:
ClassNotFoundException- if some provider could not be loadedTentackleRuntimeException- if no such service found, the service is defined more than once, or the provider does not implement the service
-
createNameMap
Creates a map of names to mapped services.
The configurations are taken fromConstants.MAPPED_SERVICE_PATHand have the formprovider = name(orprovider : name), as generated forMappedServices.The key of the map is the mapped name (the right side, unquoted if quoted). The value is the name of the provider (the left side) that comes first in module order.
- Parameters:
serviceName- the service name, usually the classname of the mapped service- Returns:
- the map
- Throws:
TentackleRuntimeException- if some configuration is malformed
-
createNameMapToAll
Creates a map of names to all mapped services.
Same ascreateNameMap(String), but maps each name to all of its providers.The key of the map is the mapped name (the right side). The value is the unordered set of names of all providers (the left side).
- Parameters:
serviceName- the service name, usually the classname of the mapped service- Returns:
- the map
- Throws:
TentackleRuntimeException- if some configuration is malformed
-