java.lang.Object
org.tentackle.common.DefaultServiceFinder
- All Implemented Interfaces:
ServiceFinder
@Service(ServiceFinder.class)
public class DefaultServiceFinder
extends Object
implements ServiceFinder
Default implementation of a
ServiceFinder.- Author:
- harald
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader().
The servicepath is"META_INF/services/".DefaultServiceFinder(ClassLoader loader, String servicePath) Creates a service finder.DefaultServiceFinder(String servicePath) Creates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader(). -
Method Summary
Modifier and TypeMethodDescriptioncreateNameMap(String serviceName) Creates a map of classnames to mapped services.createNameMapToAll(String serviceName) Creates a map of names to all mapped services.findFirstServiceConfiguration(String serviceName) Finds the first service configurations by service name.
If similar configurations appear more than once on the classpath, the first is returned.<T> Class<T>findFirstServiceProvider(Class<T> service) Finds the first service provider by service name.
If similar configurations appear more than once on the classpath, the first is returned.findServiceConfigurations(String serviceName) Finds service configurations by service name.
Iterations over the returned map are ordered by discovery along the classpath.<T> Collection<Class<T>>findServiceProviders(Class<T> service) Finds the service providers by service names.findServiceURLs(String serviceName) Finds URLs of a service by name.findUniqueServiceConfiguration(String serviceName) Finds a unique service configurations 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.Gets the classloader used by this finder.protected ClassLoadergetClassLoader(String serviceName) Gets the classloader for the given service name.Gets the service path the finder is responsible for.protected StringnormalizeLine(String line) Normalizes a line.
Trailing and leading spaces are ignored.protected voidParses a service URL.
The implementation assumes the provider configuration file in property-file format.protected voidParses an enumeration of URLs.
-
Constructor Details
-
DefaultServiceFinder
Creates a service finder.- Parameters:
loader- optional classloader, null if useClassLoader.getSystemClassLoader()servicePath- optional service path prefix, null if"META_INF/services/"
-
DefaultServiceFinder
Creates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader().- Parameters:
servicePath- optional service path prefix, null if"META_INF/services/"
-
DefaultServiceFinder
public DefaultServiceFinder()Creates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader().
The servicepath is"META_INF/services/".
-
-
Method Details
-
getClassLoader
Gets the classloader used by this finder.- Specified by:
getClassLoaderin interfaceServiceFinder- Returns:
- the classloader, never null
-
getServicePath
Gets the service path the finder is responsible for.- Specified by:
getServicePathin interfaceServiceFinder- Returns:
- the service path
-
findServiceURLs
Finds URLs of a service by name.- Specified by:
findServiceURLsin interfaceServiceFinder- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service's resource urls
-
findServiceConfigurations
Finds service configurations by service name.
Iterations over the returned map are ordered by discovery along the classpath.- Specified by:
findServiceConfigurationsin interfaceServiceFinder- Parameters:
serviceName- the service name, usually a classname- Returns:
- a map of the configured services and their corresponding URLs
-
findFirstServiceConfiguration
Finds the first service configurations by service name.
If similar configurations appear more than once on the classpath, the first is returned. Useful to override service default implementations.- Specified by:
findFirstServiceConfigurationin interfaceServiceFinder- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service configuration entry
-
findUniqueServiceConfiguration
Finds a unique service configurations by service name.
It is checked that the service is defined exactly once. Useful for factories, singletons and alike.- Specified by:
findUniqueServiceConfigurationin interfaceServiceFinder- Parameters:
serviceName- the service name, usually a classname- Returns:
- the service configuration entry
-
findServiceProviders
public <T> Collection<Class<T>> findServiceProviders(Class<T> service) throws ClassNotFoundException Finds the service providers by service names.- Specified by:
findServiceProvidersin interfaceServiceFinder- 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 loaded
-
findFirstServiceProvider
Finds the first service provider by service name.
If similar configurations appear more than once on the classpath, the first is returned. Useful to override service default implementations.- Specified by:
findFirstServiceProviderin interfaceServiceFinder- Type Parameters:
T- the service type- Parameters:
service- the service class- Returns:
- the provider class
- Throws:
ClassNotFoundException- if some provider could not be loaded
-
findUniqueServiceProvider
Finds the unique service provider by service class.- Specified by:
findUniqueServiceProviderin interfaceServiceFinder- Type Parameters:
T- the service type- Parameters:
service- the service class- Returns:
- the provider class
- Throws:
ClassNotFoundException- if some provider could not be loaded
-
createNameMap
Creates a map of classnames to mapped services.They key of the map is the service name. The value is the name providing the service which comes first in the classpath.
- Specified by:
createNameMapin interfaceServiceFinder- Parameters:
serviceName- the service class- Returns:
- the map
-
createNameMapToAll
Creates a map of names to all mapped services.They key of the map is the service name. The value is a list of names providing the service ordered by discovery along the classpath.
- Specified by:
createNameMapToAllin interfaceServiceFinder- Parameters:
serviceName- the service class- Returns:
- the map
-
getClassLoader
Gets the classloader for the given service name.- Parameters:
serviceName- the service name- Returns:
- the loader, never null
-
parseURL
Parses a service URL.
The implementation assumes the provider configuration file in property-file format. This includes the special case of key-only properties which is equivalent to the format used inServiceLoader.- Parameters:
url- the url of the provider definitionserviceName- the name of the serviceservices- the implementing services map
-
parseURLs
Parses an enumeration of URLs.- Parameters:
urls- the URLsserviceName- the service nameservices- the destination map
-
normalizeLine
Normalizes a line.
Trailing and leading spaces are ignored. Comments starting with a hash # are stripped. A hash can be escaped by a backslash.- Parameters:
line- the source line- Returns:
- the normalized line, null if line contains no information
-