public class ServiceFactory extends Object
ServiceFinder.
The factory itself is loaded via the standard JDK ServiceLoader and can
be replaced via META-INF/services. If no such configuration is found, for whatever reason,
an instance of ServiceFactory is used.
As a consequence, the fallback and default implementation is ServiceFactory itself.
However, applications can replace it, if necessary.
| Modifier and Type | Field and Description |
|---|---|
static ServiceFactory |
INSTANCE
the singleton instance.
|
| Constructor and Description |
|---|
ServiceFactory()
Creates the factory.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
addExplicitClassLoader(String servicePath,
String serviceName,
ClassLoader classLoader)
Adds an explict classloader for a given service path and name.
Loaders are kept in a linked list for each path and name combination. |
protected void |
addExplicitClassLoaderImpl(String servicePath,
String serviceName,
ClassLoader classLoader)
Adds an explict classloader for a given service path and name.
Loaders are kept in a linked list for each path and name combination. |
static void |
applyResourceIndex(ClassLoader classLoader,
String indexName,
boolean set)
Apply a given resource index.
The index is usually created via the tentackle-maven-plugin's analyze goal with the index option set to something like "META-INF/RESOURCES-INDEX.LIST". |
protected void |
applyResourceIndexImpl(ClassLoader classLoader,
String indexName,
boolean set)
Apply a given resource index.
The index is usually created via the tentackle-maven-plugin's analyze goal with the index option set to something like "META-INF/RESOURCES-INDEX.LIST". |
static <T> T |
createService(Class<T> serviceClass)
Utility method to create a service instance.
This is the standard way to instantiate singletons. |
static <T> T |
createService(Class<T> serviceClass,
Class<? extends T> defaultClass)
Utility method to create a service instance with a default if not found.
|
static <T> Class<T> |
createServiceClass(Class<T> serviceClass)
Utility method to create a service instance.
This is the standard way to instantiate singletons. |
protected ClassLoader |
getClassLoader()
Gets the default classloader.
The classloader to use is determined as follows: use the fixed classloader set by setFixedClassLoader(java.lang.ClassLoader), if not null
else try Thread.currentThread().getContextClassLoader()
if the context classloader is null, use the classloader that loaded the service factory
|
static ClassLoader |
getClassLoader(String servicePath,
String serviceName)
Gets the classloader for a given service path and name.
If an explicit classloader is set, the last defined classloader for the given path and name is returned. |
protected ClassLoader |
getClassLoaderImpl(String servicePath,
String serviceName)
Gets the classloader for a given service path and name.
If an explicit classloader is set, the last defined classloader for the given path and name is returned. |
static ClassLoader |
getExplicitClassLoader(String servicePath,
String serviceName)
Gets the explicit classloader for a given service path and name.
|
protected ClassLoader |
getExplicitClassLoaderImpl(String servicePath,
String serviceName)
Gets the explicit classloader for a given service path and name.
|
Map<ServiceFinderKey,ServiceFinder> |
getFinderMap()
Gets the finder map.
Allows applications to modify or add finders programmatically. The map is threadsafe. |
ClassLoader |
getFixedClassLoader()
Gets the fixed classloader.
|
static ServiceFinder |
getServiceFinder()
Gets a service finder.
If the finder does not exist yet, it will be created. |
static ServiceFinder |
getServiceFinder(ClassLoader loader,
String servicePath)
Gets a service finder for a given classloader and service path.
If the finder does not exist yet, it will be created. |
static ServiceFinder |
getServiceFinder(String servicePath)
Gets a service finder for a given service path.
If the finder does not exist yet, it will be created. |
protected Class<? extends ServiceFinder> |
getServiceFinderClass()
Gets the class of the service finder.
|
protected ServiceFinder |
getServiceFinderImpl()
Gets a service finder.
If the finder does not exist yet, it will be created. |
protected ServiceFinder |
getServiceFinderImpl(ClassLoader loader,
String servicePath)
Gets a service finder for a given classloader and service path.
If the finder does not exist yet, it will be created. |
protected ServiceFinder |
getServiceFinderImpl(String servicePath)
Gets a service finder for a given service path.
If the finder does not exist yet, it will be created. |
static void |
removeExplicitClassLoader(String servicePath,
String serviceName,
ClassLoader classLoader)
Remove]s an explict classloader for a given service path and name.
Loaders are kept in a linked list for each path and name combination. |
protected void |
removeExplicitClassLoaderImpl(String servicePath,
String serviceName,
ClassLoader classLoader)
Remove]s an explict classloader for a given service path and name.
Loaders are kept in a linked list for each path and name combination. |
void |
setFixedClassLoader(ClassLoader classLoader)
Sets the fixed classloader.
Default is Thread.currentThread().getContextClassLoader(). |
public static final ServiceFactory INSTANCE
public static ServiceFinder getServiceFinder(ClassLoader loader, String servicePath)
loader - the classloaderservicePath - the service path prefixpublic static ServiceFinder getServiceFinder(String servicePath)
Thread.currentThread().getContextClassLoader().servicePath - the service path prefixpublic static ServiceFinder getServiceFinder()
Thread.currentThread().getContextClassLoader()
and the service path is "META_INF/services/".public static <T> Class<T> createServiceClass(Class<T> serviceClass)
T - the service typeserviceClass - the service classpublic static <T> T createService(Class<T> serviceClass)
T - the service typeserviceClass - the service classpublic static <T> T createService(Class<T> serviceClass, Class<? extends T> defaultClass)
T - the service typeserviceClass - the service classdefaultClass - the default class if no service foundpublic static ClassLoader getClassLoader(String servicePath, String serviceName)
servicePath - the service pathserviceName - the service namepublic static ClassLoader getExplicitClassLoader(String servicePath, String serviceName)
servicePath - the service pathserviceName - the service namepublic static void addExplicitClassLoader(String servicePath, String serviceName, ClassLoader classLoader)
servicePath - the service pathserviceName - the service nameclassLoader - the classloader, null if removepublic static void removeExplicitClassLoader(String servicePath, String serviceName, ClassLoader classLoader)
servicePath - the service pathserviceName - the service nameclassLoader - the classloader, null if removepublic static void applyResourceIndex(ClassLoader classLoader, String indexName, boolean set)
"META-INF/RESOURCES-INDEX.LIST".
This is a utility method, that can be used during the activation within an OSGI-bundle, for example.
classLoader - the classloader to load the index resourceindexName - the name of the index resourceset - true if set loaders, else remove loaders from the services indexpublic ClassLoader getFixedClassLoader()
public void setFixedClassLoader(ClassLoader classLoader)
classLoader - the classloader, null if defaultprotected ClassLoader getClassLoader()
setFixedClassLoader(java.lang.ClassLoader), if not nullThread.currentThread().getContextClassLoader()protected ClassLoader getClassLoaderImpl(String servicePath, String serviceName)
servicePath - the service pathserviceName - the service nameprotected ClassLoader getExplicitClassLoaderImpl(String servicePath, String serviceName)
servicePath - the service pathserviceName - the service nameprotected void addExplicitClassLoaderImpl(String servicePath, String serviceName, ClassLoader classLoader)
servicePath - the service pathserviceName - the service nameclassLoader - the classloader, null if removeprotected void removeExplicitClassLoaderImpl(String servicePath, String serviceName, ClassLoader classLoader)
servicePath - the service pathserviceName - the service nameclassLoader - the classloader, null if removeprotected void applyResourceIndexImpl(ClassLoader classLoader, String indexName, boolean set)
"META-INF/RESOURCES-INDEX.LIST".
This is a utility method, that can be used during the activation within an OSGI-bundle, for example.
classLoader - the classloader to load the index resourceindexName - the name of the index resourceset - true if set loaders, else remove loaders from the services indexpublic Map<ServiceFinderKey,ServiceFinder> getFinderMap()
protected Class<? extends ServiceFinder> getServiceFinderClass()
protected ServiceFinder getServiceFinderImpl(ClassLoader loader, String servicePath)
loader - the classloaderservicePath - the service path prefixprotected ServiceFinder getServiceFinderImpl(String servicePath)
setFixedClassLoader(java.lang.ClassLoader), if not nullThread.currentThread().getContextClassLoader()servicePath - the service path prefixprotected ServiceFinder getServiceFinderImpl()
Thread.currentThread().getContextClassLoader()
and the service path is "META_INF/services/".Tentackle - distributed, domain- and model-driven