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.nio.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  
36  /**
37   * A set of supported protocol {@link AsyncScheme}s.
38   * Schemes are identified by lowercase names.
39   *
40   */
41  @Deprecated
42  public final class AsyncSchemeRegistry {
43  
44      /** The available schemes in this registry. */
45      private final Map<String, AsyncScheme> registeredSchemes;
46  
47      /**
48       * Creates a new, empty scheme registry.
49       */
50      public AsyncSchemeRegistry() {
51          super();
52          registeredSchemes = new ConcurrentHashMap<String, AsyncScheme>();
53      }
54  
55      /**
56       * Obtains a scheme by name.
57       *
58       * @param name      the name of the scheme to look up (in lowercase)
59       *
60       * @return  the scheme, never {@code null}
61       *
62       * @throws IllegalStateException
63       *          if the scheme with the given name is not registered
64       */
65      public final AsyncScheme getScheme(final String name) {
66          final AsyncScheme found = get(name);
67          if (found == null) {
68              throw new IllegalStateException
69                  ("Scheme '"+name+"' not registered.");
70          }
71          return found;
72      }
73  
74      /**
75       * Obtains the scheme for a host.
76       * Convenience method for {@code getScheme(host.getSchemeName())}
77       *
78       * @param host      the host for which to obtain the scheme
79       *
80       * @return  the scheme for the given host, never {@code null}
81       *
82       * @throws IllegalStateException
83       *          if a scheme with the respective name is not registered
84       */
85      public final AsyncScheme getScheme(final HttpHost host) {
86          if (host == null) {
87              throw new IllegalArgumentException("Host must not be null.");
88          }
89          return getScheme(host.getSchemeName());
90      }
91  
92      /**
93       * Obtains a scheme by name, if registered.
94       *
95       * @param name      the name of the scheme to look up (in lowercase)
96       *
97       * @return  the scheme, or
98       *          {@code null} if there is none by this name
99       */
100     public final AsyncScheme get(final String name) {
101         if (name == null) {
102             throw new IllegalArgumentException("Name must not be null.");
103         }
104 
105         // leave it to the caller to use the correct name - all lowercase
106         //name = name.toLowerCase(Locale.ENGLISH);
107         final AsyncScheme found = registeredSchemes.get(name);
108         return found;
109     }
110 
111     /**
112      * Registers a scheme.
113      * The scheme can later be retrieved by its name
114      * using {@link #getScheme(String) getScheme} or {@link #get get}.
115      *
116      * @param sch       the scheme to register
117      *
118      * @return  the scheme previously registered with that name, or
119      *          {@code null} if none was registered
120      */
121     public final AsyncScheme register(final AsyncScheme sch) {
122         if (sch == null) {
123             throw new IllegalArgumentException("Scheme must not be null.");
124         }
125 
126         final AsyncScheme old = registeredSchemes.put(sch.getName(), sch);
127         return old;
128     }
129 
130     /**
131      * Unregisters a scheme.
132      *
133      * @param name      the name of the scheme to unregister (in lowercase)
134      *
135      * @return  the unregistered scheme, or
136      *          {@code null} if there was none
137      */
138     public final AsyncScheme unregister(final String name) {
139         if (name == null) {
140             throw new IllegalArgumentException("Name must not be null.");
141         }
142 
143         // leave it to the caller to use the correct name - all lowercase
144         //name = name.toLowerCase(Locale.ENGLISH);
145         final AsyncScheme 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 AsyncScheme 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, AsyncScheme> map) {
165         if (map == null) {
166             return;
167         }
168         registeredSchemes.clear();
169         registeredSchemes.putAll(map);
170     }
171 
172 }
173