001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.conn.scheme;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.Map;
032import java.util.concurrent.ConcurrentHashMap;
033
034import org.apache.http.HttpHost;
035import org.apache.http.annotation.Contract;
036import org.apache.http.annotation.ThreadingBehavior;
037import org.apache.http.util.Args;
038
039/**
040 * A set of supported protocol {@link Scheme}s.
041 * Schemes are identified by lowercase names.
042 *
043 * @since 4.0
044 *
045 * @deprecated (4.3) use {@link org.apache.http.config.Registry}
046 */
047@Contract(threading = ThreadingBehavior.SAFE)
048@Deprecated
049public final class SchemeRegistry {
050
051    /** The available schemes in this registry. */
052    private final ConcurrentHashMap<String,Scheme> registeredSchemes;
053
054    /**
055     * Creates a new, empty scheme registry.
056     */
057    public SchemeRegistry() {
058        super();
059        registeredSchemes = new ConcurrentHashMap<String,Scheme>();
060    }
061
062    /**
063     * Obtains a scheme by name.
064     *
065     * @param name      the name of the scheme to look up (in lowercase)
066     *
067     * @return  the scheme, never {@code null}
068     *
069     * @throws IllegalStateException
070     *          if the scheme with the given name is not registered
071     */
072    public final Scheme getScheme(final String name) {
073        final Scheme found = get(name);
074        if (found == null) {
075            throw new IllegalStateException
076                ("Scheme '"+name+"' not registered.");
077        }
078        return found;
079    }
080
081    /**
082     * Obtains the scheme for a host.
083     * Convenience method for {@code getScheme(host.getSchemeName())}
084     *
085     * @param host the host for which to obtain the scheme
086     *
087     * @return the scheme for the given host, never {@code null}
088     *
089     * @throws IllegalStateException
090     *          if a scheme with the respective name is not registered
091     */
092    public final Scheme getScheme(final HttpHost host) {
093        Args.notNull(host, "Host");
094        return getScheme(host.getSchemeName());
095    }
096
097    /**
098     * Obtains a scheme by name, if registered.
099     *
100     * @param name      the name of the scheme to look up (in lowercase)
101     *
102     * @return  the scheme, or
103     *          {@code null} if there is none by this name
104     */
105    public final Scheme get(final String name) {
106        Args.notNull(name, "Scheme name");
107        // leave it to the caller to use the correct name - all lowercase
108        //name = name.toLowerCase(Locale.ENGLISH);
109        final Scheme found = registeredSchemes.get(name);
110        return found;
111    }
112
113    /**
114     * Registers a scheme.
115     * The scheme can later be retrieved by its name
116     * using {@link #getScheme(String) getScheme} or {@link #get get}.
117     *
118     * @param sch       the scheme to register
119     *
120     * @return  the scheme previously registered with that name, or
121     *          {@code null} if none was registered
122     */
123    public final Scheme register(final Scheme sch) {
124        Args.notNull(sch, "Scheme");
125        final Scheme old = registeredSchemes.put(sch.getName(), sch);
126        return old;
127    }
128
129    /**
130     * Unregisters a scheme.
131     *
132     * @param name      the name of the scheme to unregister (in lowercase)
133     *
134     * @return  the unregistered scheme, or
135     *          {@code null} if there was none
136     */
137    public final Scheme unregister(final String name) {
138        Args.notNull(name, "Scheme name");
139        // leave it to the caller to use the correct name - all lowercase
140        //name = name.toLowerCase(Locale.ENGLISH);
141        final Scheme gone = registeredSchemes.remove(name);
142        return gone;
143    }
144
145    /**
146     * Obtains the names of the registered schemes.
147     *
148     * @return  List containing registered scheme names.
149     */
150    public final List<String> getSchemeNames() {
151        return new ArrayList<String>(registeredSchemes.keySet());
152    }
153
154    /**
155     * Populates the internal collection of registered {@link Scheme protocol schemes}
156     * with the content of the map passed as a parameter.
157     *
158     * @param map protocol schemes
159     */
160    public void setItems(final Map<String, Scheme> map) {
161        if (map == null) {
162            return;
163        }
164        registeredSchemes.clear();
165        registeredSchemes.putAll(map);
166    }
167
168}
169