Package org.apache.sis.internal.system
Class DefaultFactories
java.lang.Object
org.apache.sis.internal.system.SystemListener
org.apache.sis.internal.system.DefaultFactories
- All Implemented Interfaces:
EventListener
Default factories defined in the
sis-utility
module.
This is a temporary placeholder until we leverage the "dependency injection" pattern.
A candidate replacement is JSR-330.- Since:
- 0.3
- Version:
- 1.2
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionCache of factories found byServiceLoader
fromMETA-INF/services
files content. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Discards cached factories when the classpath has changed.static <T> ServiceLoader<T>
createServiceLoader
(Class<T> service) Returns a service loader for the given type using the default class loader.static <T> T
forBuildin
(Class<T> type) Returns a factory which is guaranteed to be present.static <T,
I extends T>
IforBuildin
(Class<T> type, Class<I> impl) Returns a factory of the given type, making sure that it is an implementation of the given class.static <T> T
Returns the default factory implementing the given interface.static ClassLoader
Returns the context class loader, but makes sure that it has Apache SIS on its classpath.static <T> boolean
isDefaultInstance
(Class<T> type, T factory) Returnstrue
if the default factory of the given type is the given instance.Methods inherited from class org.apache.sis.internal.system.SystemListener
add, databaseChanged, fireClasspathChanged, remove, removeModule
-
Field Details
-
FACTORIES
Cache of factories found byServiceLoader
fromMETA-INF/services
files content.
-
-
Constructor Details
-
DefaultFactories
private DefaultFactories()For the singleton system listener only.
-
-
Method Details
-
classpathChanged
protected void classpathChanged()Discards cached factories when the classpath has changed.- Specified by:
classpathChanged
in classSystemListener
-
isDefaultInstance
Returnstrue
if the default factory of the given type is the given instance. Anull
factory is interpreted as the default one.- Type Parameters:
T
- the interface type.- Parameters:
type
- the interface type.factory
- the factory implementation to test, ornull
.- Returns:
true
if the given factory implementation is the default instance.
-
forClass
Returns the default factory implementing the given interface. This method gives preference to Apache SIS implementation of factories if present. This is a temporary mechanism while we are waiting for a real dependency injection mechanism.- Type Parameters:
T
- the interface type.- Parameters:
type
- the interface type.- Returns:
- a factory implementing the given interface, or
null
if none.
-
forBuildin
Returns a factory which is guaranteed to be present. If the factory is not found, this will be considered a configuration error (corrupted JAR files of incorrect classpath).- Type Parameters:
T
- the interface type.- Parameters:
type
- the interface type.- Returns:
- a factory implementing the given interface.
- Since:
- 0.6
-
forBuildin
Returns a factory of the given type, making sure that it is an implementation of the given class. Use this method only when we know that Apache SIS registers only one implementation of a given service.- Type Parameters:
T
- the interface type.I
- the requested implementation class.- Parameters:
type
- the interface type.impl
- the requested implementation class.- Returns:
- a factory implementing the given interface.
- Since:
- 0.6
-
createServiceLoader
Returns a service loader for the given type using the default class loader. The default is the current thread context class loader, provided that it can access at least the Apache SIS stores.- Type Parameters:
T
- the compile-time value ofservice
argument.- Parameters:
service
- the interface or abstract class representing the service.- Returns:
- a new service loader for the given service type.
- Since:
- 0.8
-
getContextClassLoader
Returns the context class loader, but makes sure that it has Apache SIS on its classpath. First, this method invokesThread.getContextClassLoader()
for the current thread. Then this method scans over all Apache SIS classes on the stack trace. For each SIS class, its loader is compared to the above-cited context class loader. If the context class loader is equal or is a child of the SIS loader, then it is left unchanged. Otherwise the context class loader is replaced by the SIS one.The intent of this method is to ensure that
ServiceLoader.load(Class)
will find the Apache SIS services even in an environment that defined an unsuitable context class loader.- Returns:
- the context class loader if suitable, or another class loader otherwise.
- Throws:
SecurityException
- if this method is not allowed to get the current thread context class loader or one of its parent.- Since:
- 0.8
-