View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/params/DefaultHttpParams.java $
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  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, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient.params;
32  
33  import java.io.Serializable;
34  import java.util.HashMap;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /***
40   * This class represents a collection of HTTP protocol parameters. Protocol parameters
41   * may be linked together to form a hierarchy. If a particular parameter value has not been
42   * explicitly defined in the collection itself, its value will be drawn from the parent 
43   * collection of parameters.
44   * 
45   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46   * 
47   * @version $Revision$
48   * 
49   * @since 3.0
50   */
51  public class DefaultHttpParams implements HttpParams, Serializable, Cloneable {
52  
53      /*** Log object for this class. */
54      private static final Log LOG = LogFactory.getLog(DefaultHttpParams.class);
55  
56      /*** HttpParams class factory. */
57      private static HttpParamsFactory httpParamsFactory = new DefaultHttpParamsFactory();
58  
59      /***
60       * Gets the default HttpParams to be used.
61       * 
62       * @return the value returned from <code>HttpParamsFactory#getDefaultParams()</code>
63       * 
64       * @see HttpParamsFactory#getDefaultParams()
65       */
66      public static HttpParams getDefaultParams() {
67          return httpParamsFactory.getDefaultParams();
68      }
69      
70      /***
71       * Sets the factory that will provide the default HttpParams.
72       * 
73       * @param httpParamsFactory an instance of HttpParamsFactory
74       * 
75       * @see #getDefaultParams()
76       */
77      public static void setHttpParamsFactory(HttpParamsFactory httpParamsFactory) {
78          if (httpParamsFactory == null) {
79              throw new IllegalArgumentException("httpParamsFactory may not be null");
80          }
81          DefaultHttpParams.httpParamsFactory = httpParamsFactory;
82      }
83  
84      /*** The set of default values to defer to */
85      private HttpParams defaults = null;
86  
87      /*** Hash map of HTTP parameters that this collection contains */
88      private HashMap parameters = null;
89      
90      /***
91       * Creates a new collection of parameters with the given parent. 
92       * The collection will defer to its parent for a default value 
93       * if a particular parameter is not explicitly set in the collection
94       * itself.
95       * 
96       * @param defaults the parent collection to defer to, if a parameter
97       * is not explictly set in the collection itself.
98       */
99      public DefaultHttpParams(final HttpParams defaults) {
100         super();
101         this.defaults = defaults; 
102     }
103     
104     /***
105      * Creates a new collection of parameters with the collection returned
106      * by {@link #getDefaultParams()} as a parent. The collection will defer
107      * to its parent for a default value if a particular parameter is not 
108      * explicitly set in the collection itself.
109      * 
110      * @see #getDefaultParams()
111      */
112     public DefaultHttpParams() {
113         this(getDefaultParams());
114     }
115     
116     public synchronized HttpParams getDefaults() {
117         return this.defaults;
118     }
119     
120     public synchronized void setDefaults(final HttpParams params) {
121         this.defaults = params;
122     }
123     
124     public synchronized Object getParameter(final String name) {
125         // See if the parameter has been explicitly defined
126         Object param = null;
127         if (this.parameters != null) {
128             param = this.parameters.get(name);
129         }    
130         if (param != null) {
131             // If so, return
132             return param;
133         } else {
134             // If not, see if defaults are available
135             if (this.defaults != null) {
136                 // Return default parameter value
137                 return this.defaults.getParameter(name);
138             } else {
139                 // Otherwise, return null
140                 return null;
141             }
142         }
143     }
144 
145     public synchronized void setParameter(final String name, final Object value) {
146         if (this.parameters == null) {
147             this.parameters = new HashMap();
148         }
149         this.parameters.put(name, value);
150         if (LOG.isDebugEnabled()) {
151             LOG.debug("Set parameter " + name + " = " + value);
152         }
153     }
154     
155     /***
156      * Assigns the value to all the parameter with the given names
157      * 
158      * @param names array of parameter name
159      * @param value parameter value
160      */ 
161     public synchronized void setParameters(final String[] names, final Object value) {
162         for (int i = 0; i < names.length; i++) {
163             setParameter(names[i], value);
164         }
165     }
166 
167     public long getLongParameter(final String name, long defaultValue) { 
168         Object param = getParameter(name);
169         if (param == null) {
170             return defaultValue;
171         }
172         return ((Long)param).longValue();
173     }
174     
175     public void setLongParameter(final String name, long value) {
176         setParameter(name, new Long(value));
177     }
178 
179     public int getIntParameter(final String name, int defaultValue) { 
180         Object param = getParameter(name);
181         if (param == null) {
182             return defaultValue;
183         }
184         return ((Integer)param).intValue();
185     }
186     
187     public void setIntParameter(final String name, int value) {
188         setParameter(name, new Integer(value));
189     }
190 
191     public double getDoubleParameter(final String name, double defaultValue) { 
192         Object param = getParameter(name);
193         if (param == null) {
194             return defaultValue;
195         }
196         return ((Double)param).doubleValue();
197     }
198     
199     public void setDoubleParameter(final String name, double value) {
200         setParameter(name, new Double(value));
201     }
202 
203     public boolean getBooleanParameter(final String name, boolean defaultValue) { 
204         Object param = getParameter(name);
205         if (param == null) {
206             return defaultValue;
207         }
208         return ((Boolean)param).booleanValue();
209     }
210     
211     public void setBooleanParameter(final String name, boolean value) {
212         setParameter(name, value ? Boolean.TRUE : Boolean.FALSE);// Boolean.valueOf() = Java 1.4+
213     }
214 
215     public boolean isParameterSet(final String name) {
216         return getParameter(name) != null;
217     }
218         
219     public boolean isParameterSetLocally(final String name) {
220         return this.parameters != null && this.parameters.get(name) != null;
221     }
222         
223     public boolean isParameterTrue(final String name) {
224         return getBooleanParameter(name, false);
225     }
226         
227     public boolean isParameterFalse(final String name) {
228         return !getBooleanParameter(name, false);
229     }
230 
231     /***
232      * Removes all parameters from this collection. 
233      */
234     public void clear() {
235         this.parameters = null;
236     }
237 
238     /***
239      * Clones this collection of parameters. Please note that paramter values
240      * themselves are not cloned. 
241      * 
242      * @see java.io.Serializable
243      * @see java.lang.Object#clone()
244      */
245     public Object clone() throws CloneNotSupportedException
246     {
247         DefaultHttpParams clone = (DefaultHttpParams)super.clone();
248         if (this.parameters != null) {
249             clone.parameters = (HashMap)this.parameters.clone(); 
250         }
251         clone.setDefaults(this.defaults);
252         return clone;
253     }
254 }