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.Contract;
36  import org.apache.http.annotation.ThreadingBehavior;
37  import org.apache.http.util.Args;
38  
39  /**
40   * A set of supported protocol {@link Scheme}s.
41   * Schemes are identified by lowercase names.
42   *
43   * @since 4.0
44   *
45   * @deprecated (4.3) use {@link org.apache.http.config.Registry}
46   */
47  @Contract(threading = ThreadingBehavior.SAFE)
48  @Deprecated
49  public final class SchemeRegistry {
50  
51      /** The available schemes in this registry. */
52      private final ConcurrentHashMap<String,Scheme> registeredSchemes;
53  
54      /**
55       * Creates a new, empty scheme registry.
56       */
57      public SchemeRegistry() {
58          super();
59          registeredSchemes = new ConcurrentHashMap<String,Scheme>();
60      }
61  
62      /**
63       * Obtains a scheme by name.
64       *
65       * @param name      the name of the scheme to look up (in lowercase)
66       *
67       * @return  the scheme, never {@code null}
68       *
69       * @throws IllegalStateException
70       *          if the scheme with the given name is not registered
71       */
72      public Scheme getScheme(final String name) {
73          final Scheme found = get(name);
74          if (found == null) {
75              throw new IllegalStateException
76                  ("Scheme '"+name+"' not registered.");
77          }
78          return found;
79      }
80  
81      /**
82       * Obtains the scheme for a host.
83       * Convenience method for {@code getScheme(host.getSchemeName())}
84       *
85       * @param host the host for which to obtain the scheme
86       *
87       * @return the scheme for the given host, never {@code null}
88       *
89       * @throws IllegalStateException
90       *          if a scheme with the respective name is not registered
91       */
92      public Scheme getScheme(final HttpHost host) {
93          Args.notNull(host, "Host");
94          return getScheme(host.getSchemeName());
95      }
96  
97      /**
98       * Obtains a scheme by name, if registered.
99       *
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 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 Scheme/Scheme.html#Scheme">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 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 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