Class FactoryRegistry
- Object
-
- FactoryRegistry
-
- Direct Known Subclasses:
AnnotatedBeanProcessFactory.BeanFactoryRegistry,FactoryCreator
public class FactoryRegistry extends Object
A registry for factories, organized by categories (usually by interface). For exampleCRSFactory.classis a category, andMathTransformFactory.classis another category.For each category, implementations are registered in a file placed in the
META-INF/services/directory, as specified in theCategoryRegistryjavadoc. Those files are usually bundled into the JAR file distributed by the vendor. If the sameMETA-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
ServiceRegistryto 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 introducedCategoryRegistryto take over instance management, in conjunction with the supportedServiceLoaderdiscovery.- Since:
- 2.1
- Author:
- Martin Desruisseaux, Richard Gould, Jody Garnett
- See Also:
ReferencingFactoryFinder,CoverageFactoryFinder
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidderegisterAll()Clear all registered factories.voidderegisterAll(Class<?> category)Clear registered factories for a provided category.voidderegisterFactories(Iterable<?> factories)Manually deregister factories.voidderegisterFactories(Iterator<?> factories)Manually deregister factories.voidderegisterFactory(Object factory)Manually deregister a factory<T> booleanderegisterFactory(T factory, Class<T> category)Manually deregister a factoryprotected voidfinalize()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> TgetFactory(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> TgetFactoryByClass(Class<T> category)Instance of category, or null if not available.protected <T> booleanisAcceptable(T factory, Class<T> category, Hints hints)Returnstrueif the specifiedfactorymeets the requirements specified by a map ofhints.voidregisterFactories(Iterable<?> factories)Manually register factories.voidregisterFactories(Iterator<?> factories)Manually register factories.voidregisterFactory(Object factory)Manually register a factory.<T> booleanregisterFactory(T factory, Class<T> category)Manually register a factory.voidscanForPlugins()Scans for factory plug-ins on the application class path.<T> booleansetOrdering(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> booleansetOrdering(Class<T> category, Comparator<T> comparator)Set pairwise ordering between all factories according a comparator.<T> booleansetOrdering(Class<T> category, T firstFactory, T secondFactory)Define pairwise ordering giving priority to thefirstFactoryover thesecondFactory.Stream<Class<?>>streamCategories()<T> booleanunsetOrdering(Class<T> category, T firstFactory, T secondFactory)Removes the ordering between the specified factories, so that the first no longer appears before the second.
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
The logger for all events related to factory registry.
-
-
Constructor Detail
-
FactoryRegistry
public FactoryRegistry(Class<?> category)
Constructs a new registry for the specified category.- Parameters:
category- The single category.- Since:
- 2.4
-
FactoryRegistry
public FactoryRegistry(Class<?>... categories)
Constructs a new registry for the specified categories.- Parameters:
categories- The categories.- Since:
- 2.4
-
FactoryRegistry
public FactoryRegistry(Collection<Class<?>> categories)
Constructs a new registry for the specified categories.- Parameters:
categories- The categories.
-
-
Method Detail
-
finalize
protected void finalize() throws Throwable
-
getFactoryByClass
public <T> T getFactoryByClass(Class<T> category)
Instance of category, or null if not available.- Parameters:
category- The category to look for. Usually an interface class (not the actual implementation class).- Returns:
- instance, or null of not available
- Since:
- 19
-
getFactories
public <T> Stream<T> getFactories(Class<T> category, boolean useOrdering)
Factories for the provided category type.- Parameters:
category- The category to look for. Usually an interface class (not the actual implementation class).useOrdering- true to use provided pairwise orderings- Returns:
- factories registered for category
- Since:
- 19
-
getFactories
public <T> Stream<T> getFactories(Class<T> category, Predicate<? super T> factoryFilter, boolean useOrdering)
Factories for the provided category type.- Parameters:
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- Returns:
- factories registered for category
- Since:
- 19
-
getFactories
public <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. Factories that are not available will be ignored. This method will scan for plugins the first time it is invoked for the given category.- Type Parameters:
T- The class represented by thecategoryargument.- Parameters:
category- The category to look for. Usually an interface class (not the actual implementation class).filter- The optional filter predicate, ornull.hints- The optional user requirements, ornull.- Returns:
- Factories ready to use for the specified category, filter and hints.
- Since:
- 19
-
getFactory
public <T> T getFactory(Class<T> category, Predicate<? super T> filter, Hints hints, Hints.Key key) throws FactoryRegistryException
Returns the first factory in the registry for the specified category, using the specified map of hints (if any). This method may scan for plugins the first time it is invoked. Except as a result of this scan, no new factory instance is created by the default implementation of this method. TheFactoryCreatorclass change this behavior however.- Type Parameters:
T- The class represented by thecategoryargument.- Parameters:
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, ornullif none. This is used for example in order to select the first factory for some authority.hints- A map of hints, ornullif none.key- The key to use for looking for a user-provided instance in the hints, ornullif none.- Returns:
- A factory available for use for the specified category and
hints. The returns type is
Objectinstead ofFactorybecause the factory implementation doesn't need to be a Geotools one. - Throws:
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.- See Also:
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)
-
isAcceptable
protected <T> boolean isAcceptable(T factory, Class<T> category, Hints hints)Returnstrueif the specifiedfactorymeets the requirements specified by a map ofhints.The default implementation always returns
true. There is no need to override this method forAbstractFactoryimplementations, 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
GeometryFactoryuses the requiredCoordinateSequenceFactory. 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. }- Type Parameters:
T- The class represented by thecategoryargument.- Parameters:
factory- The factory to checks.category- The factory category. Usually an interface.hints- The user requirements, ornullif none.- Returns:
trueif theprovidermeets the user requirements.
-
getClassLoaders
public final Set<ClassLoader> getClassLoaders()
Returns all class loaders to be used for scanning plugins. Current implementation returns the following class loaders:
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.- Returns:
- All classloaders to be used for scanning plugins.
-
scanForPlugins
public void scanForPlugins()
Scans for factory plug-ins on the application class path. This method is needed because the application class path can theoretically change, or additional plug-ins may become available. Rather than re-scanning the classpath on every invocation of the API, the class path is scanned automatically only on the first invocation. Clients can call this method to prompt a re-scan. Thus this method need only be invoked by sophisticated applications which dynamically make new plug-ins available at runtime.
-
registerFactories
public void registerFactories(Iterator<?> factories)
Manually register factories.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
registerFactories
public void registerFactories(Iterable<?> factories)
Manually register factories.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
registerFactory
public void registerFactory(Object factory)
Manually register a factory.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
registerFactory
public <T> boolean registerFactory(T factory, Class<T> category)Manually register a factory.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
deregisterAll
public void deregisterAll()
Clear all registered factories.
-
deregisterAll
public void deregisterAll(Class<?> category)
Clear registered factories for a provided category.
-
deregisterFactories
public void deregisterFactories(Iterator<?> factories)
Manually deregister factories.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
deregisterFactories
public void deregisterFactories(Iterable<?> factories)
Manually deregister factories.Used to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
deregisterFactory
public void deregisterFactory(Object factory)
Manually deregister a factoryUsed to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
deregisterFactory
public <T> boolean deregisterFactory(T factory, Class<T> category)Manually deregister a factoryUsed to facilitate integration with other plug-in systems, such as OSGi or Spring, that block CLASSPATH visibility of
ServiceLoaderimplementation registration.
-
setOrdering
public <T> boolean setOrdering(Class<T> category, T firstFactory, T secondFactory)
Define pairwise ordering giving priority to thefirstFactoryover thesecondFactory.- Returns:
- if this call establishes a new order
-
setOrdering
public <T> boolean setOrdering(Class<T> category, Comparator<T> comparator)
Set pairwise ordering between all factories according a comparator. Calls tocompare(factory1, factory2)should returns:-1iffactory1is preferred tofactory2+1iffactory2is preferred tofactory10if there is no preferred order betweenfactory1andfactory2
- Type Parameters:
T- The class represented by thecategoryargument.- Parameters:
category- The category to set ordering.comparator- The comparator to use for ordering.- Returns:
trueif at least one ordering setting has been modified as a consequence of this call.
-
setOrdering
public <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.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.- Type Parameters:
T- The class represented by thebaseargument.- Parameters:
base- The base category. Only categories assignable tobasewill be processed.set-truefor setting the ordering, orfalsefor unsetting.filter1- Predicate for the preferred factory.filter2- Predicate for the factory to whichfilter1is preferred.- Returns:
trueif the ordering changed as a result of this call.
-
unsetOrdering
public <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.- Parameters:
category- The category to clear instance order for.- Returns:
trueif that ordering was previously defined
-
-