mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/SchemeRegistry.java

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:55f6a3051ba3
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 ch.boye.httpclientandroidlib.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 ch.boye.httpclientandroidlib.annotation.ThreadSafe;
35
36 import ch.boye.httpclientandroidlib.HttpHost;
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 @ThreadSafe
45 public final class SchemeRegistry {
46
47 /** The available schemes in this registry. */
48 private final ConcurrentHashMap<String,Scheme> registeredSchemes;
49
50 /**
51 * Creates a new, empty scheme registry.
52 */
53 public SchemeRegistry() {
54 super();
55 registeredSchemes = new ConcurrentHashMap<String,Scheme>();
56 }
57
58 /**
59 * Obtains a scheme by name.
60 *
61 * @param name the name of the scheme to look up (in lowercase)
62 *
63 * @return the scheme, never <code>null</code>
64 *
65 * @throws IllegalStateException
66 * if the scheme with the given name is not registered
67 */
68 public final Scheme getScheme(String name) {
69 Scheme found = get(name);
70 if (found == null) {
71 throw new IllegalStateException
72 ("Scheme '"+name+"' not registered.");
73 }
74 return found;
75 }
76
77 /**
78 * Obtains the scheme for a host.
79 * Convenience method for <code>getScheme(host.getSchemeName())</pre>
80 *
81 * @param host the host for which to obtain the scheme
82 *
83 * @return the scheme for the given host, never <code>null</code>
84 *
85 * @throws IllegalStateException
86 * if a scheme with the respective name is not registered
87 */
88 public final Scheme getScheme(HttpHost host) {
89 if (host == null) {
90 throw new IllegalArgumentException("Host must not be null.");
91 }
92 return getScheme(host.getSchemeName());
93 }
94
95 /**
96 * Obtains a scheme by name, if registered.
97 *
98 * @param name the name of the scheme to look up (in lowercase)
99 *
100 * @return the scheme, or
101 * <code>null</code> if there is none by this name
102 */
103 public final Scheme get(String name) {
104 if (name == null)
105 throw new IllegalArgumentException("Name must not be null.");
106
107 // leave it to the caller to use the correct name - all lowercase
108 //name = name.toLowerCase();
109 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</code> if none was registered
122 */
123 public final Scheme register(Scheme sch) {
124 if (sch == null)
125 throw new IllegalArgumentException("Scheme must not be null.");
126
127 Scheme old = registeredSchemes.put(sch.getName(), sch);
128 return old;
129 }
130
131 /**
132 * Unregisters a scheme.
133 *
134 * @param name the name of the scheme to unregister (in lowercase)
135 *
136 * @return the unregistered scheme, or
137 * <code>null</code> if there was none
138 */
139 public final Scheme unregister(String name) {
140 if (name == null)
141 throw new IllegalArgumentException("Name must not be null.");
142
143 // leave it to the caller to use the correct name - all lowercase
144 //name = name.toLowerCase();
145 Scheme gone = registeredSchemes.remove(name);
146 return gone;
147 }
148
149 /**
150 * Obtains the names of the registered schemes.
151 *
152 * @return List containing registered scheme names.
153 */
154 public final List<String> getSchemeNames() {
155 return new ArrayList<String>(registeredSchemes.keySet());
156 }
157
158 /**
159 * Populates the internal collection of registered {@link Scheme protocol schemes}
160 * with the content of the map passed as a parameter.
161 *
162 * @param map protocol schemes
163 */
164 public void setItems(final Map<String, Scheme> map) {
165 if (map == null) {
166 return;
167 }
168 registeredSchemes.clear();
169 registeredSchemes.putAll(map);
170 }
171
172 }
173

mercurial