org.geotoolkit.factory
Class Factories

Object
  extended by Static
      extended by Factories

public final class Factories
extends Static

Static methods relative to the factories. There are many aspects in the way Geotk manages factories on a system-wide basis:

Since:
2.4
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys), Jody Garnett (Refractions)
Module:
utility/geotk-utility (download)    View source code for this class

Method Summary
static void addChangeListener(ChangeListener listener)
          Adds the specified listener to the list of objects to inform when a system-wide configuration changed.
static void addFactoryIteratorProvider(FactoryIteratorProvider provider)
          Adds an alternative way to search for factory implementations.
static void listProviders(Collection<FactoryRegistry> registries, Writer out, Locale locale)
          Lists all available factory implementations in a tabular format.
static
<T> Iterator<T>
orderForClassLoader(ClassLoader classLoader, Iterator<T> iterator)
          Returns an iterator giving precedence to classes loaded by the given class loaderĀ or one of its parents/children.
static void removeChangeListener(ChangeListener listener)
          Removes the specified listener from the list of objects to inform when a system-wide configuration changed.
static void removeFactoryIteratorProvider(FactoryIteratorProvider provider)
          Removes a provider that was previously added.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addChangeListener

public static void addChangeListener(ChangeListener listener)
Adds the specified listener to the list of objects to inform when a system-wide configuration changed. The methods which may fire a change event are:

Parameters:
listener - The listener to add.

removeChangeListener

public static void removeChangeListener(ChangeListener listener)
Removes the specified listener from the list of objects to inform when a system-wide configuration changed.

Parameters:
listener - The listener to remove.

addFactoryIteratorProvider

@Configuration
public static void addFactoryIteratorProvider(FactoryIteratorProvider provider)
Adds an alternative way to search for factory implementations. FactoryRegistry has a default mechanism bundled in it, which uses the content of all META-INF/services directories found on the classpath. This addFactoryIteratorProvider method allows to specify additional discovery algorithms. It may be useful in the context of some frameworks that use the constructor injection pattern, like the Spring framework.

If the given provider was not already registered, then this method notifies every listeners registered with addChangeListener(ChangeListener).

Parameters:
provider - A new provider for factory iterators.

removeFactoryIteratorProvider

@Configuration
public static void removeFactoryIteratorProvider(FactoryIteratorProvider provider)
Removes a provider that was previously added. Note that factories already obtained from the specified provider will not be deregistered by this method.

If the given provider was found, then this method notifies every listeners registered with addChangeListener(ChangeListener).

Parameters:
provider - The provider to remove.

listProviders

@Debug
public static void listProviders(Collection<FactoryRegistry> registries,
                                       Writer out,
                                       Locale locale)
                          throws IOException
Lists all available factory implementations in a tabular format. For each factory interface, the first implementation listed is the default one. This method provides a way to check the state of a system, usually for debugging purpose.

Parameters:
registries - Where the factories are registered.
out - The output stream where to format the list.
locale - The locale for the list, or null.
Throws:
IOException - if an error occurs while writing to out.
Since:
3.00
See Also:
FactoryFinder.listProviders(java.io.Writer, java.util.Locale)

orderForClassLoader

public static <T> Iterator<T> orderForClassLoader(ClassLoader classLoader,
                                                  Iterator<T> iterator)
Returns an iterator giving precedence to classes loaded by the given class loaderĀ or one of its parents/children. This method is used as a safety when there is a risk that many copies of the same library (for example in a web container) register the same JDK service.

Example with Image I/O
The geotk-coverageio module defines new ImageReader implementations, which are automatically discovered by the standard JDK through the META-INF/services/ mechanism. If a web container contains two copies of the Geotoolkit.org library - one for each web application - then all image formats like NetcdfImageReader will be registered twice, because the JVM will find two NetcdfImageReader.Spi classes loaded by two different class loaders.

The service provider instance returned by IIORegistry may be somewhat random in the above scenario. This leads to subtle and hard-to-identify bugs. This method reduces the risk by giving precedence to SPI classes loaded by the same class loader than the application. However users are still encouraged to load, for each running JVM, only one copy of the Geotoolkit.org library to be shared by all applications.

Type Parameters:
T - The type of elements in the iterator.
Parameters:
classLoader - The desired class loader, or null for the bootstrap class loader.
iterator - The iterator to wrap.
Returns:
An iterator giving precedences to classes loaded by the given class loader or one of its parents/children.
Since:
3.20


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.