com.google.code.appengine.imageio.spi.ServiceRegistry Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Rustem V. Rafikov
*/
package com.google.code.appengine.imageio.spi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Stack;
import java.util.Map.Entry;
import org.apache.harmony.x.imageio.internal.nls.Messages;
public class ServiceRegistry {
CategoriesMap categories = new CategoriesMap(this);
public ServiceRegistry(Iterator> categoriesIterator) {
if (null == categoriesIterator) {
throw new IllegalArgumentException(Messages.getString("imageio.5D"));
}
while(categoriesIterator.hasNext()) {
Class> c = categoriesIterator.next();
categories.addCategory(c);
}
}
public static Iterator lookupProviders(Class providerClass, ClassLoader loader) {
return new LookupProvidersIterator(providerClass, loader);
}
public static Iterator lookupProviders(Class providerClass) {
return lookupProviders(providerClass, Thread.currentThread().getContextClassLoader());
}
public boolean registerServiceProvider(T provider, Class category) {
return categories.addProvider(provider, category);
}
public void registerServiceProviders(Iterator> providers) {
for (Iterator> iterator = providers; iterator.hasNext();) {
categories.addProvider(iterator.next(), null);
}
}
public void registerServiceProvider(Object provider) {
categories.addProvider(provider, null);
}
public boolean deregisterServiceProvider(T provider, Class category) {
return categories.removeProvider(provider, category);
}
public void deregisterServiceProvider(Object provider) {
categories.removeProvider(provider);
}
// @SuppressWarnings("unchecked")
public Iterator getServiceProviders(Class category, Filter filter, boolean useOrdering) {
return new FilteredIterator(filter, (Iterator) categories.getProviders(category, useOrdering));
}
// @SuppressWarnings("unchecked")
public Iterator getServiceProviders(Class category, boolean useOrdering) {
return (Iterator) categories.getProviders(category, useOrdering);
}
public T getServiceProviderByClass(Class providerClass) {
return categories.getServiceProviderByClass(providerClass);
}
public boolean setOrdering(Class category, T firstProvider, T secondProvider) {
return categories.setOrdering(category, firstProvider, secondProvider);
}
public boolean unsetOrdering(Class category, T firstProvider, T secondProvider) {
return categories.unsetOrdering(category, firstProvider, secondProvider);
}
public void deregisterAll(Class> category) {
categories.removeAll(category);
}
public void deregisterAll() {
categories.removeAll();
}
@Override
public void finalize() throws Throwable {
deregisterAll();
}
public boolean contains(Object provider) {
if (provider == null) {
throw new IllegalArgumentException(Messages.getString("imageio.5E"));
}
return categories.contains(provider);
}
public Iterator> getCategories() {
return categories.list();
}
public static interface Filter {
boolean filter(Object provider);
}
private static class CategoriesMap {
Map, ProvidersMap> categories = new HashMap, ProvidersMap>();
ServiceRegistry registry;
public CategoriesMap(ServiceRegistry registry) {
this.registry = registry;
}
boolean contains(Object provider) {
for (Map.Entry, ProvidersMap> e : categories.entrySet()) {
ProvidersMap providers = e.getValue();
if (providers.contains(provider)) {
return true;
}
}
return false;
}
boolean setOrdering(Class category, T firstProvider, T secondProvider) {
ProvidersMap providers = categories.get(category);
if (providers == null) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
return providers.setOrdering(firstProvider, secondProvider);
}
boolean unsetOrdering(Class category, T firstProvider, T secondProvider) {
ProvidersMap providers = categories.get(category);
if (providers == null) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
return providers.unsetOrdering(firstProvider, secondProvider);
}
Iterator> getProviders(Class> category, boolean useOrdering) {
ProvidersMap providers = categories.get(category);
if (null == providers) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
return providers.getProviders(useOrdering);
}
T getServiceProviderByClass(Class providerClass) {
for (Map.Entry, ProvidersMap> e : categories.entrySet()) {
if (e.getKey().isAssignableFrom(providerClass)) {
T provider = e.getValue().getServiceProviderByClass(providerClass);
if (provider != null) {
return provider;
}
}
}
return null;
}
Iterator> list() {
return categories.keySet().iterator();
}
void addCategory(Class> category) {
categories.put(category, new ProvidersMap());
}
/**
* Adds a provider to the category. If category
is
* null
then the provider will be added to all categories
* which the provider is assignable from.
* @param provider provider to add
* @param category category to add provider to
* @return if there were such provider in some category
*/
boolean addProvider(Object provider, Class> category) {
if (provider == null) {
throw new IllegalArgumentException(Messages.getString("imageio.5E"));
}
boolean rt;
if (category == null) {
rt = findAndAdd(provider);
} else {
rt = addToNamed(provider, category);
}
if (provider instanceof RegisterableService) {
((RegisterableService) provider).onRegistration(registry, category);
}
return rt;
}
private boolean addToNamed(Object provider, Class> category) {
if (!category.isAssignableFrom(provider.getClass())) {
throw new ClassCastException();
}
Object obj = categories.get(category);
if (null == obj) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
return ((ProvidersMap) obj).addProvider(provider);
}
private boolean findAndAdd(Object provider) {
boolean rt = false;
for (Entry, ProvidersMap> e : categories.entrySet()) {
if (e.getKey().isAssignableFrom(provider.getClass())) {
rt |= e.getValue().addProvider(provider);
}
}
return rt;
}
boolean removeProvider(Object provider, Class> category) {
if (provider == null) {
throw new IllegalArgumentException(Messages.getString("imageio.5E"));
}
if (!category.isAssignableFrom(provider.getClass())) {
throw new ClassCastException();
}
Object obj = categories.get(category);
if (obj == null) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
return ((ProvidersMap) obj).removeProvider(provider, registry, category);
}
void removeProvider(Object provider) {
if (provider == null) {
throw new IllegalArgumentException(Messages.getString("imageio.5E"));
}
for (Entry, ProvidersMap> e : categories.entrySet()) {
ProvidersMap providers = e.getValue();
providers.removeProvider(provider, registry, e.getKey());
}
}
void removeAll(Class> category) {
Object obj = categories.get(category);
if (obj == null) {
throw new IllegalArgumentException(Messages.getString("imageio.92", category));
}
((ProvidersMap) obj).clear(registry);
}
void removeAll() {
for ( Map.Entry, ProvidersMap> e : categories.entrySet()) {
removeAll(e.getKey());
}
}
}
private static class ProvidersMap {
Map, Object> providers = new HashMap, Object>();
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy