View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.conn.scheme;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  import org.apache.http.HttpHost;
35  import org.apache.http.annotation.ThreadSafe;
36  import org.apache.http.util.Args;
37  
38  /**
39   * A set of supported protocol {@link Scheme}s.
40   * Schemes are identified by lowercase names.
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.3) use {@link org.apache.http.config.Registry}
45   */
46  @ThreadSafe
47  @Deprecated
48  public final class SchemeRegistry {
49  
50      /** The available schemes in this registry. */
51      private final ConcurrentHashMap<String,Scheme> registeredSchemes;
52  
53      /**
54       * Creates a new, empty scheme registry.
55       */
56      public SchemeRegistry() {
57          super();
58          registeredSchemes = new ConcurrentHashMap<String,Scheme>();
59      }
60  
61      /**
62       * Obtains a scheme by name.
63       *
64       * @param name      the name of the scheme to look up (in lowercase)
65       *
66       * @return  the scheme, never {@code null}
67       *
68       * @throws IllegalStateException
69       *          if the scheme with the given name is not registered
70       */
71      public final Scheme getScheme(final String name) {
72          final Scheme found = get(name);
73          if (found == null) {
74              throw new IllegalStateException
75                  ("Scheme '"+name+"' not registered.");
76          }
77          return found;
78      }
79  
80      /**
81       * Obtains the scheme for a host.
82       * Convenience method for {@code getScheme(host.getSchemeName())}
83       *
84       * @param host the host for which to obtain the scheme
85       *
86       * @return the scheme for the given host, never {@code null}
87       *
88       * @throws IllegalStateException
89       *          if a scheme with the respective name is not registered
90       */
91      public final Scheme getScheme(final HttpHost host) {
92          Args.notNull(host, "Host");
93          return getScheme(host.getSchemeName());
94      }
95  
96      /**
97       * Obtains a scheme by name, if registered.
98       *
99       * @param name      the name of the scheme to look up (in lowercase)
100      *
101      * @return  the scheme, or
102      *          {@code null} if there is none by this name
103      */
104     public final Scheme get(final String name) {
105         Args.notNull(name, "Scheme name");
106         // leave it to the caller to use the correct name - all lowercase
107         //name = name.toLowerCase(Locale.ENGLISH);
108         final Scheme found = registeredSchemes.get(name);
109         return found;
110     }
111 
112     /**
113      * Registers a scheme.
114      * The scheme can later be retrieved by its name
115      * using {@link #getScheme(String) getScheme} or {@link #get get}.
116      *
117      * @param sch       the scheme to register
118      *
119      * @return  the scheme previously registered with that name, or
120      *          {@code null} if none was registered
121      */
122     public final Scheme register(final Scheme sch) {
123         Args.notNull(sch, "Scheme");
124         final Scheme old = registeredSchemes.put(sch.getName(), sch);
125         return old;
126     }
127 
128     /**
129      * Unregisters a scheme.
130      *
131      * @param name      the name of the scheme to unregister (in lowercase)
132      *
133      * @return  the unregistered scheme, or
134      *          {@code null} if there was none
135      */
136     public final Scheme unregister(final String name) {
137         Args.notNull(name, "Scheme name");
138         // leave it to the caller to use the correct name - all lowercase
139         //name = name.toLowerCase(Locale.ENGLISH);
140         final Scheme gone = registeredSchemes.remove(name);
141         return gone;
142     }
143 
144     /**
145      * Obtains the names of the registered schemes.
146      *
147      * @return  List containing registered scheme names.
148      */
149     public final List<String> getSchemeNames() {
150         return new ArrayList<String>(registeredSchemes.keySet());
151     }
152 
153     /**
154      * Populates the internal collection of registered {@link Scheme protocol schemes}
155      * with the content of the map passed as a parameter.
156      *
157      * @param map protocol schemes
158      */
159     public void setItems(final Map<String, Scheme> map) {
160         if (map == null) {
161             return;
162         }
163         registeredSchemes.clear();
164         registeredSchemes.putAll(map);
165     }
166 
167 }
168