/* ServiceRegistry.java -- A simple registry for service providers. Copyright (C) 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package javax.imageio.spi; import gnu.classpath.ServiceFactory; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; /** * A registry for service providers. * * @since 1.4 * * @author Michael Koch (konqueror@gmx.de) * @author Sascha Brawer (brawer@dandelis.ch) */ public class ServiceRegistry { // Package-private to avoid a trampoline. /** * The service categories of this registry. * *
Note that we expect that only very few categories will
* typically be used with a registry. The most common case will be
* one, it seems unlikely that any registry would contain more than
* five or six categories. Therefore, we intentionally avoid the
* overhead of a HashMap.
*
* @see #providers
*/
final Class[] categories;
/**
* The registered providers for each service category, indexed by
* the same index as the {@link #categories} array. If no provider
* is registered for a category, the array entry will be
* null
.
*
*
Note that we expect that only very few providers will
* typically be registered for a category. The most common case will
* be one or two. Therefore, we intentionally avoid the overhead of
* a HashMap.
*/
private final LinkedList[] providers;
/**
* The ordring constaints for each service category, indexed by the
* same index as the {@link #categories} array. The constraints for
* a service category are stored as a Map<Object,
* Set<Object>>
, where the Map’s values are
* those providers that need to come after the key. If no
* constraints are imposed on the providers of a category, the array
* entry will be null
. If no constraints have been set
* whatsoever, constraints
will be null
.
*
*
Note that we expect that only very few constraints will
* typically be imposed on a category. The most common case will
* be zero.
*/
private IdentityHashMap[] constraints;
/**
* Constructs a On-demand loading: Loading and initializing service
* providers is delayed as much as possible. The rationale is that
* typical clients will iterate through the set of installed service
* providers until one is found that matches some criteria (like
* supported formats, or quality of service). In such scenarios, it
* might make sense to install only the frequently needed service
* providers on the local machine. More exotic providers can be put
* onto a server; the server will only be contacted when no suitable
* service could be found locally. Security considerations: Any loaded service providers
* are loaded through the specified ClassLoader, or the system
* ClassLoader if If If If a provider implements the {@link RegisterableService}
* interface, its {@link RegisterableService#onRegistration
* onRegistration} method is invoked in order to inform the provider
* about the addition to this registry. If If If If a provider implements the {@link RegisterableService}
* interface, its {@link RegisterableService#onDeregistration
* onDeregistration} method is invoked in order to inform the
* provider about the removal from this registry. If the provider
* implements several service categories,
* If a provider implements the {@link RegisterableService}
* interface, its {@link RegisterableService#onDeregistration
* onDeregistration} method is invoked in order to inform the
* provider about the removal from this registry. If the provider
* implements several service categories,
* ServiceRegistry
for the specified
* service categories.
*
* @param categories the categories to support
*
* @throws IllegalArgumentException if categories
is
* null
, or if its {@link Iterator#next()} method
* returns null
.
*
* @throws ClassCastException if categories
does not
* iterate over instances of {@link java.lang.Class}.
*/
public ServiceRegistry(IteratorclassLoader
is
* null
. When lookupProviders
is called,
* the current {@link java.security.AccessControlContext} gets
* recorded. This captured security context will determine the
* permissions when services get loaded via the next()
* method of the returned Iterator
.null
for the system class
* loader. For using the context class loader, see {@link
* #lookupProviders(Class)}.
*
* @return an iterator over instances of spi
.
*
* @throws IllegalArgumentException if spi
is
* null
.
*/
public static spi
.
*
* @throws IllegalArgumentException if spi
is
* null
.
*
* @see #lookupProviders(Class, ClassLoader)
*/
public static true
if provider
is the first
* provider that gets registered for the specified service category;
* false
if other providers have already been
* registered for the same servide category.
*
* @throws IllegalArgumentException if provider
is
* null
.
*
* @throws ClassCastException if provider
does not
* implement the specified service provider interface.
*/
private synchronized boolean registerServiceProvider(Object provider,
int cat)
{
LinkedList provs;
boolean result;
Class category;
if (provider == null)
throw new IllegalArgumentException();
category = categories[cat];
if (!category.isInstance(provider))
throw new ClassCastException(category.getName());
provs = providers[cat];
if (provs == null)
{
result = true;
provs = providers[cat] = new LinkedList();
}
else
result = false;
provs.add(provider);
if (provider instanceof RegisterableService)
((RegisterableService) provider).onRegistration(this, category);
return result;
}
/**
* Registers a provider for the specified service category.
*
* provider
implements the {@link
* RegisterableService} interface, its {@link
* RegisterableService#onRegistration onRegistration} method is
* invoked in order to inform the provider about the addition to
* this registry.
*
* @param provider the service provider to be registered.
*
* @param category the service category under which
* provider
shall be registered.
*
* @return true
if provider
is the first
* provider that gets registered for the specified service category;
* false
if other providers have already been
* registered for the same servide category.
*
* @throws IllegalArgumentException if provider
is
* null
, or if category
is not among the
* categories passed to the {@linkplain #ServiceRegistry(Iterator)
* constructor} of this ServiceRegistry.
*
* @throws ClassCastException if provider
does not
* implement category
.
*/
public synchronized provider
implements the {@link
* RegisterableService} interface, its {@link
* RegisterableService#onRegistration onRegistration} method is
* invoked in order to inform the provider about the addition to
* this registry. If provider
implements several
* service categories, onRegistration
gets called
* multiple times.
*
* @param provider the service provider to be registered.
*
* @throws IllegalArgumentException if provider
is
* null
, or if provider
does not implement
* any of the service categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this ServiceRegistry.
*/
public synchronized void registerServiceProvider(Object provider)
{
boolean ok = false;
if (provider == null)
throw new IllegalArgumentException();
for (int i = 0; i < categories.length; i++)
if (categories[i].isInstance(provider))
{
ok = true;
registerServiceProvider(provider, i);
}
if (!ok)
throw new IllegalArgumentException();
}
/**
* Registers a number of providers under all service categories they
* implement.
*
* provider
* implements several service categories,
* onRegistration
gets called multiple times.
*
* @throws IllegalArgumentException if providers
is
* null
, if any iterated provider is null
,
* or if some iterated provider does not implement any of the
* service categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this
* ServiceRegistry
.
*/
public synchronized void registerServiceProviders(Iterator> providers)
{
if (providers == null)
throw new IllegalArgumentException();
while (providers.hasNext())
registerServiceProvider(providers.next());
}
/**
* De-registers a provider for a service category which is specified
* by the class-internal category ID.
*
* @param provider the service provider to be registered.
*
* @param cat the service category, which is identified by an index
* into the {@link #categories} array.
*
* @return true
if provider
was previously
* registered for the specified service category; false
* if if the provider had not been registered.
*
* @throws IllegalArgumentException if provider
is
* null
.
*
* @throws ClassCastException if provider
does not
* implement the specified service provider interface.
*/
private synchronized boolean deregisterServiceProvider(Object provider,
int cat)
{
LinkedList provs;
boolean result;
Class category;
if (provider == null)
throw new IllegalArgumentException();
category = categories[cat];
if (!category.isInstance(provider))
throw new ClassCastException(category.getName());
provs = providers[cat];
if (provs == null)
return false;
result = provs.remove(provider);
if (provs.isEmpty())
providers[cat] = null;
if (result && (provider instanceof RegisterableService))
((RegisterableService) provider).onDeregistration(this, category);
return result;
}
/**
* De-registers a provider for the specified service category.
*
* provider
implements the {@link
* RegisterableService} interface, its {@link
* RegisterableService#onDeregistration onDeregistration} method is
* invoked in order to inform the provider about the removal from
* this registry.
*
* @param provider the service provider to be de-registered.
*
* @param category the service category from which
* provider
shall be de-registered.
*
* @return true
if provider
was previously
* registered for the specified service category; false
* if if the provider had not been registered.
*
* @throws IllegalArgumentException if provider
is
* null
, or if category
is not among the
* categories passed to the {@linkplain #ServiceRegistry(Iterator)
* constructor} of this ServiceRegistry.
*
* @throws ClassCastException if provider
does not
* implement category
.
*/
public synchronized provider
implements the {@link
* RegisterableService} interface, its {@link
* RegisterableService#onDeregistration onDeregistration} method is
* invoked in order to inform the provider about the removal from
* this registry. If provider
implements several
* service categories, onDeregistration
gets called
* multiple times.provider
is
* null
, or if provider
does not implement
* any of the service categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this
* ServiceRegistry
.
*/
public synchronized void deregisterServiceProvider(Object provider)
{
boolean ok = false;
if (provider == null)
throw new IllegalArgumentException();
for (int i = 0; i < categories.length; i++)
if (categories[i].isInstance(provider))
{
ok = true;
deregisterServiceProvider(provider, i);
}
if (!ok)
throw new IllegalArgumentException();
}
/**
* De-registers all providers which have been registered for the
* specified service category.
*
* onDeregistration
gets called multiple times.
*
* @param category the category whose registered providers will be
* de-registered.
*
* @throws IllegalArgumentException if category
is not
* among the categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this
* ServiceRegistry
.
*/
public synchronized void deregisterAll(Class> category)
{
boolean ok = false;
for (int i = 0; i < categories.length; i++)
{
if (categories[i] != category)
continue;
ok = true;
while (providers[i] != null)
deregisterServiceProvider(providers[i].get(0), i);
}
if (!ok)
throw new IllegalArgumentException();
}
/**
* De-registers all service providers.
*
* onDeregistration
gets called multiple times.
*/
public synchronized void deregisterAll()
{
for (int i = 0; i < categories.length; i++)
while (providers[i] != null)
deregisterServiceProvider(providers[i].get(0), i);
}
/**
* Called by the Virtual Machine when it detects that this
* ServiceRegistry
has become garbage. De-registers all
* service providers, which will cause those that implement {@link
* RegisterableService} to receive a {@link
* RegisterableService#onDeregistration onDeregistration}
* notification.
*/
public void finalize()
throws Throwable
{
super.finalize();
deregisterAll();
}
/**
* Determines whether a provider has been registered with this
* registry.
*
* @return true
if provider
has been
* registered under any service category; false
if
* it is not registered.
*
* @throws IllegalArgumentException if provider
is
* null
.
*/
public synchronized boolean contains(Object provider)
{
if (provider == null)
throw new IllegalArgumentException();
// Note that contains is rather unlikely to be ever called,
// so it would be wasteful to keep a special data structure
// (such as a HashSet) for making it a fast operation.
for (int i = 0; i < providers.length; i++)
{
// If provider does not implement categories[i],
// it would not have been possible to register it there.
// In that case, it would be pointless to look there.
if (!categories[i].isInstance(provider))
continue;
// But if the list of registered providers contains provider,
// we have found it.
LinkedList p = providers[i];
if (p != null && p.contains(provider))
return true;
}
return false;
}
/**
* Returns the index in {@link #categories} occupied by the
* specified service category.
*
* @throws IllegalArgumentException if category
is not
* among the categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this ServiceRegistry.
*/
private int getCategoryID(Class category)
{
for (int i = 0; i < categories.length; i++)
if (categories[i] == category)
return i;
throw new IllegalArgumentException();
}
/**
* Retrieves all providers that have been registered for the
* specified service category.
*
* @param category the service category whose providers are
* to be retrieved.
*
* @param useOrdering true
in order to retrieve the
* providers in an order imposed by the {@linkplain #setOrdering
* ordering constraints}; false
in order to retrieve
* the providers in any order.
*
* @throws IllegalArgumentException if category
is not
* among the categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this
* ServiceRegistry
.
*
* @see #getServiceProviders(Class, Filter, boolean)
*/
public null
to
* retrieve all registered providers for the specified
* category.
*
* @param useOrdering true
in order to retrieve the
* providers in an order imposed by the {@linkplain #setOrdering
* ordering constraints}; false
in order to retrieve
* the providers in any order.
*
* @throws IllegalArgumentException if category
is not
* among the categories passed to the {@linkplain
* #ServiceRegistry(Iterator) constructor} of this
* ServiceRegistry
.
*/
public synchronized second
.
*
* @param secondProvider the provider which is supposed to come after
* first
.
*
* @throws IllegalArgumentException if first
and
* second
are referring to the same object, or if one
* of them is null
.
*
* @see #unsetOrdering
* @see #getServiceProviders(Class, Filter, boolean)
*/
public synchronized second
.
*
* @param secondProvider the provider which is supposed to come after
* first
.
*
* @throws IllegalArgumentException if first
and
* second
are referring to the same object, or if one
* of them is null
.
*
* @see #setOrdering
*/
public synchronized second
.
*
* @param second the provider which is supposed to come after
* first
.
*
* @throws IllegalArgumentException if first
and
* second
are referring to the same object, or if one
* of them is null
.
*/
private boolean addConstraint(int catid, Object first, Object second)
{
Set s;
IdentityHashMap cons;
// Also checks argument validity.
removeConstraint(catid, second, first);
if (constraints == null)
constraints = new IdentityHashMap[categories.length];
cons = constraints[catid];
if (cons == null)
cons = constraints[catid] = new IdentityHashMap();
s = (Set) cons.get(first);
if (s == null)
cons.put(first, s = new HashSet());
return s.add(second);
}
/**
* Removes an ordering constraint on service providers.
*
* @param catid the service category ID, which is the
* category’s index into the {@link #categories} array.
*
* @param first the provider which is supposed to come before
* second
.
*
* @param second the provider which is supposed to come after
* first
.
*
* @throws IllegalArgumentException if first
and
* second
are referring to the same object, or if one
* of them is null
.
*/
private boolean removeConstraint(int catid, Object first, Object second)
{
Collection s;
IdentityHashMap cons;
if (first == null || second == null || first == second)
throw new IllegalArgumentException();
if (constraints == null)
return false;
cons = constraints[catid];
if (cons == null)
return false;
s = (Collection) cons.get(first);
if (s == null)
return false;
if (!s.remove(second))
return false;
// If we removed the last constraint for a service category,
// we can get free some memory.
if (cons.isEmpty())
{
constraints[catid] = null;
boolean anyConstraints = false;
for (int i = 0; i < constraints.length; i++)
{
if (constraints[i] != null)
{
anyConstraints = true;
break;
}
}
if (!anyConstraints)
constraints = null;
}
return true;
}
/**
* A filter for selecting service providers that match custom
* criteria.
*
* @see ServiceRegistry#getServiceProviders(Class, Filter,
* boolean)
*
* @since 1.4
*
* @author Michael Koch (konqueror@gmx.de)
* @author Sascha Brawer (brawer@dandelis.ch)
*/
public static interface Filter
{
/**
* Checks whether the specified service provider matches the
* constraints of this Filter.
*
* @param provider the service provider in question.
*
* @return true
if provider
matches the
* criteria; false
if it does not match.
*/
boolean filter(Object provider);
}
}