/* * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.imageio.spi; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.ServiceLoader; /** * A registry for service provider instances. * *
A service is a well-known set of interfaces and (usually * abstract) classes. A service provider is a specific * implementation of a service. The classes in a provider typically * implement the interface or subclass the class defined by the * service itself. * *
Service providers are stored in one or more categories,
* each of which is defined by a class of interface (described by a
* Class
object) that all of its members must implement.
* The set of categories may be changed dynamically.
*
*
Only a single instance of a given leaf class (that is, the
* actual class returned by getClass()
, as opposed to any
* inherited classes or interfaces) may be registered. That is,
* suppose that the
* com.mycompany.mypkg.GreenServiceProvider
class
* implements the com.mycompany.mypkg.MyService
* interface. If a GreenServiceProvider
instance is
* registered, it will be stored in the category defined by the
* MyService
class. If a new instance of
* GreenServiceProvider
is registered, it will replace
* the previous instance. In practice, service provider objects are
* usually singletons so this behavior is appropriate.
*
*
To declare a service provider, a services
* subdirectory is placed within the META-INF
directory
* that is present in every JAR file. This directory contains a file
* for each service provider interface that has one or more
* implementation classes present in the JAR file. For example, if
* the JAR file contained a class named
* com.mycompany.mypkg.MyServiceImpl
which implements the
* javax.someapi.SomeService
interface, the JAR file
* would contain a file named:
* META-INF/services/javax.someapi.SomeService* * containing the line: * *
* com.mycompany.mypkg.MyService ** *
The service provider classes should be to be lightweight and * quick to load. Implementations of these interfaces should avoid * complex dependencies on other classes and on native code. The usual * pattern for more complex services is to register a lightweight * proxy for the heavyweight service. * *
An application may customize the contents of a registry as it * sees fit, so long as it has the appropriate runtime permission. * *
For more details on declaring service providers, and the JAR
* format in general, see the
* JAR File Specification.
*
* @see RegisterableService
*
*/
public class ServiceRegistry {
// Class -> Registry
private Map categoryMap = new HashMap();
/**
* Constructs a This method transforms the name of the given service class
* into a provider-configuration filename as described in the
* class comment and then uses the Because it is possible for extensions to be installed into
* a running Java virtual machine, this method may return
* different results each time it is invoked.
*
* @param providerClass a If If For each entry of If The The ordering will be used by the
* The ordering will be used by the
* ServiceRegistry
instance with a
* set of categories taken from the categories
* argument.
*
* @param categories an Iterator
containing
* Class
objects to be used to define categories.
*
* @exception IllegalArgumentException if
* categories
is null
.
*/
public ServiceRegistry(IteratorError
' below should be changed to 'a
// ServiceConfigurationError
'.
/**
* Searches for implementations of a particular service class
* using the given class loader.
*
* getResources
* method of the given class loader to find all available files
* with that name. These files are then read and parsed to
* produce a list of provider-class names. The iterator that is
* returned uses the given class loader to look up and then
* instantiate each element of the list.
*
* Class
object indicating the
* class or interface of the service providers being detected.
*
* @param loader the class loader to be used to load
* provider-configuration files and instantiate provider classes,
* or null
if the system class loader (or, failing that
* the bootstrap class loader) is to be used.
*
* @return An Iterator
that yields provider objects
* for the given service, in some arbitrary order. The iterator
* will throw an Error
if a provider-configuration
* file violates the specified format or if a provider class
* cannot be found and instantiated.
*
* @exception IllegalArgumentException if
* providerClass
is null
.
*/
public static
* ClassLoader cl = Thread.currentThread().getContextClassLoader();
* return Service.providers(service, cl);
*
*
* @param providerClass a Class
object indicating the
* class or interface of the service providers being detected.
*
* @return An Iterator
that yields provider objects
* for the given service, in some arbitrary order. The iterator
* will throw an Error
if a provider-configuration
* file violates the specified format or if a provider class
* cannot be found and instantiated.
*
* @exception IllegalArgumentException if
* providerClass
is null
.
*/
public static Iterator
of Class
objects
* indicating the current set of categories. The iterator will be
* empty if no categories exist.
*
* @return an Iterator
containing
* Class
objects.
*/
public Iteratorprovider
implements the
* RegisterableService
interface, its
* onRegistration
method will be called. Its
* onDeregistration
method will be called each time
* it is deregistered from a category, for example if a
* category is removed or the registry is garbage collected.
*
* @param provider the service provide object to be registered.
* @param category the category under which to register the
* provider.
*
* @return true if no provider of the same class was previously
* registered in the same category category.
*
* @exception IllegalArgumentException if provider
is
* null
.
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
* @exception ClassCastException if provider does not implement
* the Class
defined by category
.
*/
public Class
it implements.
*
* provider
implements the
* RegisterableService
interface, its
* onRegistration
method will be called once for each
* category it is registered under. Its
* onDeregistration
method will be called each time
* it is deregistered from a category or when the registry is
* finalized.
*
* @param provider the service provider object to be registered.
*
* @exception IllegalArgumentException if
* provider
is null
.
*/
public void registerServiceProvider(Object provider) {
if (provider == null) {
throw new IllegalArgumentException("provider == null!");
}
Iterator regs = getSubRegistries(provider);
while (regs.hasNext()) {
SubRegistry reg = (SubRegistry)regs.next();
reg.registerServiceProvider(provider);
}
}
/**
* Adds a set of service provider objects, taken from an
* Iterator
to the registry. Each provider is
* associated within each category present in the registry whose
* Class
it implements.
*
* providers
that implements
* the RegisterableService
interface, its
* onRegistration
method will be called once for each
* category it is registered under. Its
* onDeregistration
method will be called each time
* it is deregistered from a category or when the registry is
* finalized.
*
* @param providers an Iterator containing service provider
* objects to be registered.
*
* @exception IllegalArgumentException if providers
* is null
or contains a null
entry.
*/
public void registerServiceProviders(Iterator> providers) {
if (providers == null) {
throw new IllegalArgumentException("provider == null!");
}
while (providers.hasNext()) {
registerServiceProvider(providers.next());
}
}
/**
* Removes a service provider object from the given category. If
* the provider was not previously registered, nothing happens and
* false
is returned. Otherwise, true
* is returned. If an object of the same class as
* provider
but not equal (using ==
) to
* provider
is registered, it will not be
* deregistered.
*
* provider
implements the
* RegisterableService
interface, its
* onDeregistration
method will be called.
*
* @param provider the service provider object to be deregistered.
* @param category the category from which to deregister the
* provider.
*
* @return true
if the provider was previously
* registered in the same category category,
* false
otherwise.
*
* @exception IllegalArgumentException if provider
is
* null
.
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
* @exception ClassCastException if provider does not implement
* the class defined by category
.
*/
public provider
is
* null
.
*/
public void deregisterServiceProvider(Object provider) {
if (provider == null) {
throw new IllegalArgumentException("provider == null!");
}
Iterator regs = getSubRegistries(provider);
while (regs.hasNext()) {
SubRegistry reg = (SubRegistry)regs.next();
reg.deregisterServiceProvider(provider);
}
}
/**
* Returns true
if provider
is currently
* registered.
*
* @param provider the service provider object to be queried.
*
* @return true
if the given provider has been
* registered.
*
* @exception IllegalArgumentException if provider
is
* null
.
*/
public boolean contains(Object provider) {
if (provider == null) {
throw new IllegalArgumentException("provider == null!");
}
Iterator regs = getSubRegistries(provider);
while (regs.hasNext()) {
SubRegistry reg = (SubRegistry)regs.next();
if (reg.contains(provider)) {
return true;
}
}
return false;
}
/**
* Returns an Iterator
containing all registered
* service providers in the given category. If
* useOrdering
is false
, the iterator
* will return all of the server provider objects in an arbitrary
* order. Otherwise, the ordering will respect any pairwise
* orderings that have been set. If the graph of pairwise
* orderings contains cycles, any providers that belong to a cycle
* will not be returned.
*
* @param category the category to be retrieved from.
* @param useOrdering true
if pairwise orderings
* should be taken account in ordering the returned objects.
*
* @return an Iterator
containing service provider
* objects from the given category, possibly in order.
*
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
*/
public ServiceRegistry.getServiceProviders
to select
* providers matching an arbitrary criterion. Classes that
* implement this interface should be defined in order to make use
* of the getServiceProviders
method of
* ServiceRegistry
that takes a Filter
.
*
* @see ServiceRegistry#getServiceProviders(Class, ServiceRegistry.Filter, boolean)
*/
public interface Filter {
/**
* Returns true
if the given
* provider
object matches the criterion defined
* by this Filter
.
*
* @param provider a service provider Object
.
*
* @return true if the provider matches the criterion.
*/
boolean filter(Object provider);
}
/**
* Returns an Iterator
containing service provider
* objects within a given category that satisfy a criterion
* imposed by the supplied ServiceRegistry.Filter
* object's filter
method.
*
* useOrdering
argument controls the
* ordering of the results using the same rules as
* getServiceProviders(Class, boolean)
.
*
* @param category the category to be retrieved from.
* @param filter an instance of ServiceRegistry.Filter
* whose filter
method will be invoked.
* @param useOrdering true
if pairwise orderings
* should be taken account in ordering the returned objects.
*
* @return an Iterator
containing service provider
* objects from the given category, possibly in order.
*
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
*/
public null
* is returned.
*
* @param providerClass the Class
of the desired
* service provider object.
*
* @return a currently registered service provider object with the
* desired Class
type, or null
is none is
* present.
*
* @exception IllegalArgumentException if providerClass
is
* null
.
*/
public false
is returned. If the providers previously
* were ordered in the reverse direction, that ordering is
* removed.
*
* getServiceProviders
methods when their
* useOrdering
argument is true
.
*
* @param category a Class
object indicating the
* category under which the preference is to be established.
* @param firstProvider the preferred provider.
* @param secondProvider the provider to which
* firstProvider
is preferred.
*
* @return true
if a previously unset ordering
* was established.
*
* @exception IllegalArgumentException if either provider is
* null
or they are the same object.
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
*/
public false
is returned.
*
* getServiceProviders
methods when their
* useOrdering
argument is true
.
*
* @param category a Class
object indicating the
* category under which the preference is to be disestablished.
* @param firstProvider the formerly preferred provider.
* @param secondProvider the provider to which
* firstProvider
was formerly preferred.
*
* @return true
if a previously set ordering was
* disestablished.
*
* @exception IllegalArgumentException if either provider is
* null
or they are the same object.
* @exception IllegalArgumentException if there is no category
* corresponding to category
.
*/
public category
.
*/
public void deregisterAll(Class> category) {
SubRegistry reg = (SubRegistry)categoryMap.get(category);
if (reg == null) {
throw new IllegalArgumentException("category unknown!");
}
reg.clear();
}
/**
* Deregisters all currently registered service providers from all
* categories.
*/
public void deregisterAll() {
Iterator iter = categoryMap.values().iterator();
while (iter.hasNext()) {
SubRegistry reg = (SubRegistry)iter.next();
reg.clear();
}
}
/**
* Finalizes this object prior to garbage collection. The
* deregisterAll
method is called to deregister all
* currently registered service providers. This method should not
* be called from application code.
*
* @exception Throwable if an error occurs during superclass
* finalization.
*/
public void finalize() throws Throwable {
deregisterAll();
super.finalize();
}
}
/**
* A portion of a registry dealing with a single superclass or
* interface.
*/
class SubRegistry {
ServiceRegistry registry;
Class category;
// Provider Objects organized by partial oridering
PartiallyOrderedSet poset = new PartiallyOrderedSet();
// Class -> Provider Object of that class
MapIterators
with a filter function.
* This provides an iterator for a subset without duplication.
*/
class FilterIterator