public class FactoryRegistry extends Object
CRSFactory.class
is a category, and
MathTransformFactory.class
is another
category.
For each category, implementations are registered in a file placed in the META-INF/services/
directory, as specified in the CategoryRegistry
javadoc. Those files
are usually bundled into the JAR file distributed by the vendor. If the same META-INF/services/
file appears many time in different JARs, they are processed as if their
content were merged.
Example use:
Set<Class<?>> categories =
Collections.singleton(new Class<?>[] {
MathTransformProvider.class
});
FactoryRegistry registry = new FactoryRegistry(categories);
// get the factories
Predicate filter = null;
Hints hints = null;
Iterator<MathTransform> providers =
registry.getFactories(MathTransformProvider.class, null, hints);
NOTE: This class is not thread safe. Users are responsible for
synchronisation. This is usually done in an utility class wrapping this factory registry (e.g.
ReferencingFactoryFinder
).
NOTE: Java 9 Service Registry Incompatibility. Prior releases of GeoTools
uses Java's built-in ServiceRegistry
to manage instances for our
plug-in system. In Java 9 the service registry was restricted to a limited number of imageio
services and was no longer available for general use. We have introduced CategoryRegistry
to take over instance management, in conjunction with the supported ServiceLoader
discovery.
ReferencingFactoryFinder
,
CoverageFactoryFinder
Modifier and Type | Field and Description |
---|---|
protected static Logger |
LOGGER
The logger for all events related to factory registry.
|
Constructor and Description |
---|
FactoryRegistry(Class<?> category)
Constructs a new registry for the specified category.
|
FactoryRegistry(Class<?>[] categories)
Constructs a new registry for the specified categories.
|
FactoryRegistry(Collection<Class<?>> categories)
Constructs a new registry for the specified categories.
|
Modifier and Type | Method and Description |
---|---|
void |
deregisterAll()
Clear all registered factories.
|
void |
deregisterAll(Class<?> category)
Clear registered factories for a provided category.
|
void |
deregisterFactories(Iterable<?> factories)
Manually deregister factories.
|
void |
deregisterFactories(Iterator<?> factories)
Manually deregister factories.
|
void |
deregisterFactory(Object factory)
Manually deregister a factory
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of
ServiceLoader implementation registration. |
<T> boolean |
deregisterFactory(T factory,
Class<T> category)
Manually deregister a factory
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of
ServiceLoader implementation registration. |
void |
deregisterServiceProvider(Object provider)
Deprecated.
|
<T> void |
deregisterServiceProvider(T provider,
Class<T> category)
Deprecated.
|
void |
deregisterServiceProviders(Iterator<?> providers)
Deprecated.
|
protected void |
finalize() |
Set<ClassLoader> |
getClassLoaders()
Returns all class loaders to be used for scanning plugins.
|
<T> Stream<T> |
getFactories(Class<T> category,
boolean useOrdering)
Factories for the provided category type.
|
<T> Stream<T> |
getFactories(Class<T> category,
Predicate<? super T> factoryFilter,
boolean useOrdering)
Factories for the provided category type.
|
<T> Stream<T> |
getFactories(Class<T> category,
Predicate<? super T> filter,
Hints hints)
Returns the factories in the registry for the specified category, filter and hints.
|
<T> T |
getFactory(Class<T> category,
Predicate<? super T> filter,
Hints hints,
Hints.Key key)
Returns the first factory in the registry for the specified category, using the specified map
of hints (if any).
|
<T> T |
getFactoryByClass(Class<T> category)
Instance of category, or null if not available.
|
<T> T |
getServiceProvider(Class<T> category,
ServiceRegistry.Filter filter,
Hints hints,
Hints.Key key)
Deprecated.
|
<T> T |
getServiceProviderByClass(Class<T> providerClass)
Deprecated.
Please use
getFactoryByClass(Class) |
<T> Iterator<T> |
getServiceProviders(Class<T> category,
boolean useOrdering)
Deprecated.
Replaced by {@link #getFactories(Class, boolean)
|
<T> Iterator<T> |
getServiceProviders(Class<T> category,
ServiceRegistry.Filter filter,
boolean useOrdering)
Deprecated.
Replaced by
getFactories(Class, Predicate, boolean) |
<T> Iterator<T> |
getServiceProviders(Class<T> category,
ServiceRegistry.Filter filter,
Hints hints)
Deprecated.
Replaced with
getFactories(Class, Predicate, Hints) |
protected <T> boolean |
isAcceptable(T factory,
Class<T> category,
Hints hints)
Returns
true if the specified factory meets the requirements specified by a
map of hints . |
static <T> Iterator<T> |
lookupProviders(Class<T> service)
Deprecated.
Replace with
ServiceLoader.load(Class) |
static <T> Iterator<T> |
lookupProviders(Class<T> service,
ClassLoader classLoader)
Deprecated.
Replace with
ServiceLoader.load(Class,ClassLoader) |
void |
registerFactories(Iterable<?> factories)
Manually register factories.
|
void |
registerFactories(Iterator<?> factories)
Manually register factories.
|
void |
registerFactory(Object factory)
Manually register a factory.
|
<T> boolean |
registerFactory(T factory,
Class<T> category)
Manually register a factory.
|
void |
registerServiceProvider(Object provider)
Deprecated.
Replaced wtih
deregisterFactory(Object) |
<T> boolean |
registerServiceProvider(T provider,
Class<T> category)
Deprecated.
Replaced with
registerFactory(Object, Class) |
void |
registerServiceProviders(Iterator<?> providers)
Deprecated.
Replaced with
deregisterFactories(Iterable) |
void |
scanForPlugins()
Scans for factory plug-ins on the application class path.
|
<T> boolean |
setOrdering(Class<T> base,
boolean set,
Predicate<? super T> filter1,
Predicate<? super T> filter2)
Sets or unsets a pairwise ordering between all factories meeting a criterion.
|
<T> boolean |
setOrdering(Class<T> base,
boolean set,
ServiceRegistry.Filter filter1,
ServiceRegistry.Filter filter2)
Deprecated.
Replaced with
setOrdering(Class, boolean, Predicate, Predicate) |
<T> boolean |
setOrdering(Class<T> category,
Comparator<T> comparator)
Set pairwise ordering between all factories according a comparator.
|
<T> boolean |
setOrdering(Class<T> category,
T firstFactory,
T secondFactory)
Define pairwise ordering giving priority to the
firstFactory over the
secondFactory . |
Stream<Class<?>> |
streamCategories() |
<T> boolean |
unsetOrdering(Class<T> category,
T firstFactory,
T secondFactory)
Removes the ordering between the specified factories, so that the first no longer appears
before the second.
|
protected static final Logger LOGGER
public FactoryRegistry(Class<?> category)
category
- The single category.public FactoryRegistry(Class<?>[] categories)
categories
- The categories.public FactoryRegistry(Collection<Class<?>> categories)
categories
- The categories.protected void finalize() throws Throwable
@Deprecated public static <T> Iterator<T> lookupProviders(Class<T> service)
ServiceLoader.load(Class)
@Deprecated public static <T> Iterator<T> lookupProviders(Class<T> service, ClassLoader classLoader)
ServiceLoader.load(Class,ClassLoader)
classLoader
- @Deprecated public <T> T getServiceProviderByClass(Class<T> providerClass)
getFactoryByClass(Class)
public <T> T getFactoryByClass(Class<T> category)
category
- The category to look for. Usually an interface class (not the actual
implementation class).@Deprecated public <T> Iterator<T> getServiceProviders(Class<T> category, boolean useOrdering)
public <T> Stream<T> getFactories(Class<T> category, boolean useOrdering)
category
- The category to look for. Usually an interface class (not the actual
implementation class).useOrdering
- true to use provided pairwise orderings@Deprecated public <T> Iterator<T> getServiceProviders(Class<T> category, ServiceRegistry.Filter filter, boolean useOrdering)
getFactories(Class, Predicate, boolean)
public <T> Stream<T> getFactories(Class<T> category, Predicate<? super T> factoryFilter, boolean useOrdering)
category
- The category to look for. Usually an interface class (not the actual
implementation class).factoryFilter
- Predicate to filter factories, null for all factoriesuseOrdering
- true to use provided pairwise orderings@Deprecated public <T> Iterator<T> getServiceProviders(Class<T> category, ServiceRegistry.Filter filter, Hints hints)
getFactories(Class, Predicate, Hints)
public <T> Stream<T> getFactories(Class<T> category, Predicate<? super T> filter, Hints hints)
T
- The class represented by the category
argument.category
- The category to look for. Usually an interface class (not the actual
implementation class).filter
- The optional filter predicate, or null
.hints
- The optional user requirements, or null
.@Deprecated public <T> T getServiceProvider(Class<T> category, ServiceRegistry.Filter filter, Hints hints, Hints.Key key) throws FactoryRegistryException
getFactory(Class, Predicate, Hints,
org.geotools.util.factory.Hints.Key)
FactoryRegistryException
public <T> T getFactory(Class<T> category, Predicate<? super T> filter, Hints hints, Hints.Key key) throws FactoryRegistryException
FactoryCreator
class change this
behavior however.T
- The class represented by the category
argument.category
- The category to look for. Must be one of the categories declared to the
constructor. Usually an interface class (not the actual implementation class).filter
- An optional filter, or null
if none. This is used for example in order
to select the first factory for some authority.hints
- A map of hints, or null
if none.key
- The key to use for looking for a user-provided instance in the hints, or null
if none.Object
instead of Factory
because the factory implementation doesn't need to be a Geotools one.FactoryNotFoundException
- if no factory was found for the specified category, filter
and hints.FactoryRegistryException
- if a factory can't be returned for some other reason.getFactories(Class, Predicate, Hints)
,
FactoryCreator.getFactory(java.lang.Class<T>, java.util.function.Predicate<? super T>, org.geotools.util.factory.Hints, org.geotools.util.factory.Hints.Key)
protected <T> boolean isAcceptable(T factory, Class<T> category, Hints hints)
true
if the specified factory
meets the requirements specified by a
map of hints
.
The default implementation always returns true
. There is no need to override this
method for AbstractFactory
implementations, since their hints are automatically
checked.
Override this method for non-Geotools implementations. For example a JTS geometry factory
finder may overrides this method in order to check if a GeometryFactory
uses the required CoordinateSequenceFactory
. Such method should be implemented as
below, since this method may be invoked for various kind of objects:
if (provider instanceof GeometryFactory) {
// ... Check the GeometryFactory state here.
}
T
- The class represented by the category
argument.factory
- The factory to checks.category
- The factory category. Usually an interface.hints
- The user requirements, or null
if none.true
if the provider
meets the user requirements.public final Set<ClassLoader> getClassLoaders()
The actual number of class loaders may be smaller if redundancies was found. If some more classloaders should be scanned, they shall be added into the code of this method.
public void scanForPlugins()
@Deprecated public void registerServiceProviders(Iterator<?> providers)
deregisterFactories(Iterable)
public void registerFactories(Iterator<?> factories)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factories
- public void registerFactories(Iterable<?> factories)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factories
- @Deprecated public void registerServiceProvider(Object provider)
deregisterFactory(Object)
public void registerFactory(Object factory)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factory
- @Deprecated public <T> boolean registerServiceProvider(T provider, Class<T> category)
registerFactory(Object, Class)
public <T> boolean registerFactory(T factory, Class<T> category)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factory
- category
- public void deregisterAll()
public void deregisterAll(Class<?> category)
category
- @Deprecated public void deregisterServiceProviders(Iterator<?> providers)
deregisterFactories(Iterator)
public void deregisterFactories(Iterator<?> factories)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factories
- public void deregisterFactories(Iterable<?> factories)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factories
- @Deprecated public void deregisterServiceProvider(Object provider)
deregisterFactory(Object)
public void deregisterFactory(Object factory)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factory
- @Deprecated public <T> void deregisterServiceProvider(T provider, Class<T> category)
deregisterFactory(Object, Class)
public <T> boolean deregisterFactory(T factory, Class<T> category)
Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that
block CLASSPATH visibility of ServiceLoader
implementation registration.
factory
- category
- public <T> boolean setOrdering(Class<T> category, T firstFactory, T secondFactory)
firstFactory
over the
secondFactory
.category
- firstFactory
- secondFactory
- public <T> boolean setOrdering(Class<T> category, Comparator<T> comparator)
compare(factory1, factory2)
should returns:
-1
if factory1
is preferred to factory2
+1
if factory2
is preferred to factory1
0
if there is no preferred order between factory1
and factory2
T
- The class represented by the category
argument.category
- The category to set ordering.comparator
- The comparator to use for ordering.true
if at least one ordering setting has been modified as a consequence of
this call.@Deprecated public <T> boolean setOrdering(Class<T> base, boolean set, ServiceRegistry.Filter filter1, ServiceRegistry.Filter filter2)
setOrdering(Class, boolean, Predicate, Predicate)
public <T> boolean setOrdering(Class<T> base, boolean set, Predicate<? super T> filter1, Predicate<? super T> filter2)
For example in the CRS framework (org.geotools.referencing.FactoryFinder
), this is
used for setting ordering between all factories provided by two vendors, or for two
authorities. If one or both factories are not currently registered, or if the desired
ordering is already set/unset, nothing happens and false is returned.
T
- The class represented by the base
argument.base
- The base category. Only categories assignable
to base
will be processed.set
- true
for setting the ordering, or false
for unsetting.filter1
- Predicate for the preferred factory.filter2
- Predicate for the factory to which filter1
is preferred.true
if the ordering changed as a result of this call.public <T> boolean unsetOrdering(Class<T> category, T firstFactory, T secondFactory)
category
- The category to clear instance order for.firstFactory
- secondFactory
- true
if that ordering was previously definedCopyright © 1996–2019 Geotools. All rights reserved.