michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: package ch.boye.httpclientandroidlib.conn.scheme; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.List; michael@0: import java.util.Map; michael@0: import java.util.concurrent.ConcurrentHashMap; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpHost; michael@0: michael@0: /** michael@0: * A set of supported protocol {@link Scheme}s. michael@0: * Schemes are identified by lowercase names. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public final class SchemeRegistry { michael@0: michael@0: /** The available schemes in this registry. */ michael@0: private final ConcurrentHashMap registeredSchemes; michael@0: michael@0: /** michael@0: * Creates a new, empty scheme registry. michael@0: */ michael@0: public SchemeRegistry() { michael@0: super(); michael@0: registeredSchemes = new ConcurrentHashMap(); michael@0: } michael@0: michael@0: /** michael@0: * Obtains a scheme by name. michael@0: * michael@0: * @param name the name of the scheme to look up (in lowercase) michael@0: * michael@0: * @return the scheme, never null michael@0: * michael@0: * @throws IllegalStateException michael@0: * if the scheme with the given name is not registered michael@0: */ michael@0: public final Scheme getScheme(String name) { michael@0: Scheme found = get(name); michael@0: if (found == null) { michael@0: throw new IllegalStateException michael@0: ("Scheme '"+name+"' not registered."); michael@0: } michael@0: return found; michael@0: } michael@0: michael@0: /** michael@0: * Obtains the scheme for a host. michael@0: * Convenience method for getScheme(host.getSchemeName()) michael@0: * michael@0: * @param host the host for which to obtain the scheme michael@0: * michael@0: * @return the scheme for the given host, never null michael@0: * michael@0: * @throws IllegalStateException michael@0: * if a scheme with the respective name is not registered michael@0: */ michael@0: public final Scheme getScheme(HttpHost host) { michael@0: if (host == null) { michael@0: throw new IllegalArgumentException("Host must not be null."); michael@0: } michael@0: return getScheme(host.getSchemeName()); michael@0: } michael@0: michael@0: /** michael@0: * Obtains a scheme by name, if registered. michael@0: * michael@0: * @param name the name of the scheme to look up (in lowercase) michael@0: * michael@0: * @return the scheme, or michael@0: * null if there is none by this name michael@0: */ michael@0: public final Scheme get(String name) { michael@0: if (name == null) michael@0: throw new IllegalArgumentException("Name must not be null."); michael@0: michael@0: // leave it to the caller to use the correct name - all lowercase michael@0: //name = name.toLowerCase(); michael@0: Scheme found = registeredSchemes.get(name); michael@0: return found; michael@0: } michael@0: michael@0: /** michael@0: * Registers a scheme. michael@0: * The scheme can later be retrieved by its name michael@0: * using {@link #getScheme(String) getScheme} or {@link #get get}. michael@0: * michael@0: * @param sch the scheme to register michael@0: * michael@0: * @return the scheme previously registered with that name, or michael@0: * null if none was registered michael@0: */ michael@0: public final Scheme register(Scheme sch) { michael@0: if (sch == null) michael@0: throw new IllegalArgumentException("Scheme must not be null."); michael@0: michael@0: Scheme old = registeredSchemes.put(sch.getName(), sch); michael@0: return old; michael@0: } michael@0: michael@0: /** michael@0: * Unregisters a scheme. michael@0: * michael@0: * @param name the name of the scheme to unregister (in lowercase) michael@0: * michael@0: * @return the unregistered scheme, or michael@0: * null if there was none michael@0: */ michael@0: public final Scheme unregister(String name) { michael@0: if (name == null) michael@0: throw new IllegalArgumentException("Name must not be null."); michael@0: michael@0: // leave it to the caller to use the correct name - all lowercase michael@0: //name = name.toLowerCase(); michael@0: Scheme gone = registeredSchemes.remove(name); michael@0: return gone; michael@0: } michael@0: michael@0: /** michael@0: * Obtains the names of the registered schemes. michael@0: * michael@0: * @return List containing registered scheme names. michael@0: */ michael@0: public final List getSchemeNames() { michael@0: return new ArrayList(registeredSchemes.keySet()); michael@0: } michael@0: michael@0: /** michael@0: * Populates the internal collection of registered {@link Scheme protocol schemes} michael@0: * with the content of the map passed as a parameter. michael@0: * michael@0: * @param map protocol schemes michael@0: */ michael@0: public void setItems(final Map map) { michael@0: if (map == null) { michael@0: return; michael@0: } michael@0: registeredSchemes.clear(); michael@0: registeredSchemes.putAll(map); michael@0: } michael@0: michael@0: } michael@0: