- java.lang.Object
-
- org.tentackle.common.DefaultServiceFinder
-
- All Implemented Interfaces:
ServiceFinder
public class DefaultServiceFinder extends java.lang.Object implements ServiceFinder
Default implementation of aServiceFinder.- Author:
- harald
-
-
Constructor Summary
Constructors Constructor Description DefaultServiceFinder()Creates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader().
The servicepath is"META_INF/services/".DefaultServiceFinder(java.lang.ClassLoader loader, java.lang.String servicePath)Creates a service finder.DefaultServiceFinder(java.lang.String servicePath)Creates a default service finder.
The classloader used isThread.currentThread().getContextClassLoader().
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Map<java.lang.String,java.lang.String>createNameMap(java.lang.String serviceName)Creates a map of classnames to mapped services.java.util.Map<java.lang.String,java.util.Set<java.lang.String>>createNameMapToAll(java.lang.String serviceName)Creates a map of names to all mapped services.java.util.Map.Entry<java.lang.String,java.net.URL>findFirstServiceConfiguration(java.lang.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> java.lang.Class<T>findFirstServiceProvider(java.lang.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.java.util.Map<java.lang.String,java.net.URL>findServiceConfigurations(java.lang.String serviceName)Finds service configurations by service name.
Iterations over the returned map are ordered by discovery along the classpath.<T> java.util.Collection<java.lang.Class<T>>findServiceProviders(java.lang.Class<T> service)Finds the service providers by service names.java.util.Collection<java.net.URL>findServiceURLs(java.lang.String serviceName)Finds URLs of a service by name.java.util.Map.Entry<java.lang.String,java.net.URL>findUniqueServiceConfiguration(java.lang.String serviceName)Finds a unique service configurations by service name.
It is checked that the service is defined exactly once.<T> java.lang.Class<T>findUniqueServiceProvider(java.lang.Class<T> service)Finds the unique service provider by service class.java.lang.ClassLoadergetClassLoader()Gets the classloader used by this finder.protected java.lang.ClassLoadergetClassLoader(java.lang.String serviceName)Gets the classloader for the given service name.java.lang.StringgetServicePath()Gets the finder' service path it is responsible for.protected java.lang.StringnormalizeLine(java.lang.String line)Normalizes a line.
Trailing and leading spaces are ignored.protected voidparseURL(java.net.URL url, java.lang.String serviceName, java.util.Map<java.lang.String,java.net.URL> services)Parses a service URL.
The implementation assumes the provider configuration file in property-file format.protected voidparseURLs(java.util.Collection<java.net.URL> urls, java.lang.String serviceName, java.util.Map<java.lang.String,java.net.URL> services)Parses an enumeration of URLs.
-
-
-
Constructor Detail
-
DefaultServiceFinder
public DefaultServiceFinder(java.lang.ClassLoader loader, java.lang.String servicePath)Creates a service finder.- Parameters:
loader- optional classloader, null if useClassLoader.getSystemClassLoader()servicePath- optional service path prefix, null if"META_INF/services/"
-
DefaultServiceFinder
public DefaultServiceFinder(java.lang.String servicePath)
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 Detail
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
Gets the classloader used by this finder.- Specified by:
getClassLoaderin interfaceServiceFinder- Returns:
- the classloader, never null
-
getServicePath
public java.lang.String getServicePath()
Gets the finder' service path it is responsible for.- Specified by:
getServicePathin interfaceServiceFinder- Returns:
- the service path
-
findServiceURLs
public java.util.Collection<java.net.URL> findServiceURLs(java.lang.String serviceName)
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
public java.util.Map<java.lang.String,java.net.URL> findServiceConfigurations(java.lang.String serviceName)
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
public java.util.Map.Entry<java.lang.String,java.net.URL> findFirstServiceConfiguration(java.lang.String serviceName)
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 configuation entry
-
findUniqueServiceConfiguration
public java.util.Map.Entry<java.lang.String,java.net.URL> findUniqueServiceConfiguration(java.lang.String serviceName)
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 configuation entry
-
findServiceProviders
public <T> java.util.Collection<java.lang.Class<T>> findServiceProviders(java.lang.Class<T> service) throws java.lang.ClassNotFoundExceptionFinds 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:
java.lang.ClassNotFoundException- if some provider could not be loaded
-
findFirstServiceProvider
public <T> java.lang.Class<T> findFirstServiceProvider(java.lang.Class<T> service) throws java.lang.ClassNotFoundExceptionFinds 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:
java.lang.ClassNotFoundException- if some provider could not be loaded
-
findUniqueServiceProvider
public <T> java.lang.Class<T> findUniqueServiceProvider(java.lang.Class<T> service) throws java.lang.ClassNotFoundExceptionFinds 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:
java.lang.ClassNotFoundException- if some provider could not be loaded
-
createNameMap
public java.util.Map<java.lang.String,java.lang.String> createNameMap(java.lang.String serviceName)
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
public java.util.Map<java.lang.String,java.util.Set<java.lang.String>> createNameMapToAll(java.lang.String serviceName)
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
protected java.lang.ClassLoader getClassLoader(java.lang.String serviceName)
Gets the classloader for the given service name.- Parameters:
serviceName- the service name- Returns:
- the loader, never null
-
parseURL
protected void parseURL(java.net.URL url, java.lang.String serviceName, java.util.Map<java.lang.String,java.net.URL> services)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
protected void parseURLs(java.util.Collection<java.net.URL> urls, java.lang.String serviceName, java.util.Map<java.lang.String,java.net.URL> services)Parses an enumeration of URLs.- Parameters:
urls- the URLsserviceName- the service nameservices- the destination map
-
normalizeLine
protected java.lang.String normalizeLine(java.lang.String line)
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
-
-