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