- java.lang.Object
-
- org.tentackle.common.DefaultServiceFinder
-
- All Implemented Interfaces:
ServiceFinder
public class DefaultServiceFinder extends 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(ClassLoader loader, String servicePath)Creates a service finder.DefaultServiceFinder(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 Map<String,String>createNameMap(String serviceName)Creates a map of classnames to mapped services.Map<String,Set<String>>createNameMapToAll(String serviceName)Creates a map of names to all mapped services.Map.Entry<String,URL>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.Map<String,URL>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.Collection<URL>findServiceURLs(String serviceName)Finds URLs of a service by name.Map.Entry<String,URL>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.ClassLoadergetClassLoader()Gets the classloader used by this finder.protected ClassLoadergetClassLoader(String serviceName)Gets the classloader for the given service name.StringgetServicePath()Gets the finder' service path it is responsible for.protected StringnormalizeLine(String line)Normalizes a line.
Trailing and leading spaces are ignored.protected voidparseURL(URL url, String serviceName, Map<String,URL> services)Parses a service URL.
The implementation assumes the provider configuration file in property-file format.protected voidparseURLs(Collection<URL> urls, String serviceName, Map<String,URL> services)Parses an enumeration of URLs.
-
-
-
Constructor Detail
-
DefaultServiceFinder
public DefaultServiceFinder(ClassLoader loader, 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(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 ClassLoader getClassLoader()
Gets the classloader used by this finder.- Specified by:
getClassLoaderin interfaceServiceFinder- Returns:
- the classloader, never null
-
getServicePath
public String getServicePath()
Gets the finder' service path it is responsible for.- Specified by:
getServicePathin interfaceServiceFinder- Returns:
- the service path
-
findServiceURLs
public Collection<URL> findServiceURLs(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 Map<String,URL> findServiceConfigurations(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 Map.Entry<String,URL> 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. 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 Map.Entry<String,URL> findUniqueServiceConfiguration(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> 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
public <T> Class<T> findFirstServiceProvider(Class<T> service) throws ClassNotFoundException
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
public <T> Class<T> findUniqueServiceProvider(Class<T> service) throws ClassNotFoundException
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
public Map<String,String> createNameMap(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 Map<String,Set<String>> createNameMapToAll(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 ClassLoader getClassLoader(String serviceName)
Gets the classloader for the given service name.- Parameters:
serviceName- the service name- Returns:
- the loader, never null
-
parseURL
protected void parseURL(URL url, String serviceName, Map<String,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(Collection<URL> urls, String serviceName, Map<String,URL> services)
Parses an enumeration of URLs.- Parameters:
urls- the URLsserviceName- the service nameservices- the destination map
-
normalizeLine
protected String normalizeLine(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
-
-