📄 serviceregistry.java
字号:
/* 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 modifyit under the terms of the GNU General Public License as published bythe 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, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception 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. * * <p>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 * <code>null</code>. * * <p>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 <code>Map<Object, * Set<Object>></code>, 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 <code>null</code>. If no constraints have been set * whatsoever, <code>constraints</code> will be <code>null</code>. * * <p>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 <code>ServiceRegistry</code> for the specified * service categories. * * @param categories the categories to support * * @throws IllegalArgumentException if <code>categories</code> is * <code>null</code>, or if its {@link Iterator#next()} method * returns <code>null</code>. * * @throws ClassCastException if <code>categories</code> does not * iterate over instances of {@link java.lang.Class}. */ public ServiceRegistry(Iterator categories) { ArrayList cats = new ArrayList(/* expected size */ 10); if (categories == null) throw new IllegalArgumentException(); while (categories.hasNext()) { Class cat = (Class) categories.next(); if (cat == null) throw new IllegalArgumentException(); cats.add(cat); } int numCats = cats.size(); this.categories = (Class[]) cats.toArray(new Class[numCats]); this.providers = new LinkedList[numCats]; } /** * Finds service providers that are implementing the specified * Service Provider Interface. * * <p><b>On-demand loading:</b> 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.</p> * * <p><b>Security considerations:</b> Any loaded service providers * are loaded through the specified ClassLoader, or the system * ClassLoader if <code>classLoader</code> is * <code>null</code>. When <code>lookupProviders</code> is called, * the current {@link java.security.AccessControlContext} gets * recorded. This captured security context will determine the * permissions when services get loaded via the <code>next()</code> * method of the returned <code>Iterator</code>.</p> * * @param spi the service provider interface which must be * implemented by any loaded service providers. * * @param loader the class loader that will be used to load the * service providers, or <code>null</code> for the system class * loader. For using the context class loader, see {@link * #lookupProviders(Class)}. * * @return an iterator over instances of <code>spi</code>. * * @throws IllegalArgumentException if <code>spi</code> is * <code>null</code>. */ public static Iterator lookupProviders(Class spi, ClassLoader loader) { return ServiceFactory.lookupProviders(spi, loader); } /** * Finds service providers that are implementing the specified * Service Provider Interface, using the context class loader * for loading providers. * * @param spi the service provider interface which must be * implemented by any loaded service providers. * * @return an iterator over instances of <code>spi</code>. * * @throws IllegalArgumentException if <code>spi</code> is * <code>null</code>. * * @see #lookupProviders(Class, ClassLoader) */ public static Iterator lookupProviders(Class spi) { return ServiceFactory.lookupProviders(spi); } /** * Returns an iterator over all service categories. * * @return an unmodifiable {@link * java.util.Iterator}<{@link java.lang.Class}>. */ public Iterator getCategories() { return new Iterator() { int index = -1; public boolean hasNext() { return index < categories.length - 1; } public Object next() { if (!hasNext()) throw new NoSuchElementException(); return categories[++index]; } public void remove() { throw new UnsupportedOperationException(); } }; } /** * 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 <code>true</code> if <code>provider</code> is the first * provider that gets registered for the specified service category; * <code>false</code> if other providers have already been * registered for the same servide category. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>. * * @throws ClassCastException if <code>provider</code> 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. * * <p>If <code>provider</code> 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 * <code>provider</code> shall be registered. * * @return <code>true</code> if <code>provider</code> is the first * provider that gets registered for the specified service category; * <code>false</code> if other providers have already been * registered for the same servide category. * * @throws IllegalArgumentException if <code>provider</code> is * <code>null</code>, or if <code>category</code> is not among the * categories passed to the {@linkplain #ServiceRegistry(Iterator) * constructor} of this ServiceRegistry. * * @throws ClassCastException if <code>provider</code> does not * implement <code>category</code>. */ public synchronized boolean registerServiceProvider(Object provider, Class category)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -