001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParams.java,v 1.9 2004/12/21 23:15:21 olegk Exp $
003: * $Revision: 510589 $
004: * $Date: 2007-02-22 18:04:52 +0100 (Thu, 22 Feb 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.params;
032:
033: import java.io.Serializable;
034: import java.util.HashMap;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * This class represents a collection of HTTP protocol parameters. Protocol parameters
041: * may be linked together to form a hierarchy. If a particular parameter value has not been
042: * explicitly defined in the collection itself, its value will be drawn from the parent
043: * collection of parameters.
044: *
045: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
046: *
047: * @version $Revision: 510589 $
048: *
049: * @since 3.0
050: */
051: public class DefaultHttpParams implements HttpParams, Serializable,
052: Cloneable {
053:
054: /** Log object for this class. */
055: private static final Log LOG = LogFactory
056: .getLog(DefaultHttpParams.class);
057:
058: /** HttpParams class factory. */
059: private static HttpParamsFactory httpParamsFactory = new DefaultHttpParamsFactory();
060:
061: /**
062: * Gets the default HttpParams to be used.
063: *
064: * @return the value returned from <code>HttpParamsFactory#getDefaultParams()</code>
065: *
066: * @see HttpParamsFactory#getDefaultParams()
067: */
068: public static HttpParams getDefaultParams() {
069: return httpParamsFactory.getDefaultParams();
070: }
071:
072: /**
073: * Sets the factory that will provide the default HttpParams.
074: *
075: * @param httpParamsFactory an instance of HttpParamsFactory
076: *
077: * @see #getDefaultParams()
078: */
079: public static void setHttpParamsFactory(
080: HttpParamsFactory httpParamsFactory) {
081: if (httpParamsFactory == null) {
082: throw new IllegalArgumentException(
083: "httpParamsFactory may not be null");
084: }
085: DefaultHttpParams.httpParamsFactory = httpParamsFactory;
086: }
087:
088: /** The set of default values to defer to */
089: private HttpParams defaults = null;
090:
091: /** Hash map of HTTP parameters that this collection contains */
092: private HashMap parameters = null;
093:
094: /**
095: * Creates a new collection of parameters with the given parent.
096: * The collection will defer to its parent for a default value
097: * if a particular parameter is not explicitly set in the collection
098: * itself.
099: *
100: * @param defaults the parent collection to defer to, if a parameter
101: * is not explictly set in the collection itself.
102: */
103: public DefaultHttpParams(final HttpParams defaults) {
104: super ();
105: this .defaults = defaults;
106: }
107:
108: /**
109: * Creates a new collection of parameters with the collection returned
110: * by {@link #getDefaultParams()} as a parent. The collection will defer
111: * to its parent for a default value if a particular parameter is not
112: * explicitly set in the collection itself.
113: *
114: * @see #getDefaultParams()
115: */
116: public DefaultHttpParams() {
117: this (getDefaultParams());
118: }
119:
120: public synchronized HttpParams getDefaults() {
121: return this .defaults;
122: }
123:
124: public synchronized void setDefaults(final HttpParams params) {
125: this .defaults = params;
126: }
127:
128: public synchronized Object getParameter(final String name) {
129: // See if the parameter has been explicitly defined
130: Object param = null;
131: if (this .parameters != null) {
132: param = this .parameters.get(name);
133: }
134: if (param != null) {
135: // If so, return
136: return param;
137: } else {
138: // If not, see if defaults are available
139: if (this .defaults != null) {
140: // Return default parameter value
141: return this .defaults.getParameter(name);
142: } else {
143: // Otherwise, return null
144: return null;
145: }
146: }
147: }
148:
149: public synchronized void setParameter(final String name,
150: final Object value) {
151: if (this .parameters == null) {
152: this .parameters = new HashMap();
153: }
154: this .parameters.put(name, value);
155: if (LOG.isDebugEnabled()) {
156: LOG.debug("Set parameter " + name + " = " + value);
157: }
158: }
159:
160: /**
161: * Assigns the value to all the parameter with the given names
162: *
163: * @param names array of parameter name
164: * @param value parameter value
165: */
166: public synchronized void setParameters(final String[] names,
167: final Object value) {
168: for (int i = 0; i < names.length; i++) {
169: setParameter(names[i], value);
170: }
171: }
172:
173: public long getLongParameter(final String name, long defaultValue) {
174: Object param = getParameter(name);
175: if (param == null) {
176: return defaultValue;
177: }
178: return ((Long) param).longValue();
179: }
180:
181: public void setLongParameter(final String name, long value) {
182: setParameter(name, new Long(value));
183: }
184:
185: public int getIntParameter(final String name, int defaultValue) {
186: Object param = getParameter(name);
187: if (param == null) {
188: return defaultValue;
189: }
190: return ((Integer) param).intValue();
191: }
192:
193: public void setIntParameter(final String name, int value) {
194: setParameter(name, new Integer(value));
195: }
196:
197: public double getDoubleParameter(final String name,
198: double defaultValue) {
199: Object param = getParameter(name);
200: if (param == null) {
201: return defaultValue;
202: }
203: return ((Double) param).doubleValue();
204: }
205:
206: public void setDoubleParameter(final String name, double value) {
207: setParameter(name, new Double(value));
208: }
209:
210: public boolean getBooleanParameter(final String name,
211: boolean defaultValue) {
212: Object param = getParameter(name);
213: if (param == null) {
214: return defaultValue;
215: }
216: return ((Boolean) param).booleanValue();
217: }
218:
219: public void setBooleanParameter(final String name, boolean value) {
220: setParameter(name, value ? Boolean.TRUE : Boolean.FALSE);// Boolean.valueOf() = Java 1.4+
221: }
222:
223: public boolean isParameterSet(final String name) {
224: return getParameter(name) != null;
225: }
226:
227: public boolean isParameterSetLocally(final String name) {
228: return this .parameters != null
229: && this .parameters.get(name) != null;
230: }
231:
232: public boolean isParameterTrue(final String name) {
233: return getBooleanParameter(name, false);
234: }
235:
236: public boolean isParameterFalse(final String name) {
237: return !getBooleanParameter(name, false);
238: }
239:
240: /**
241: * Removes all parameters from this collection.
242: */
243: public void clear() {
244: this .parameters = null;
245: }
246:
247: /**
248: * Clones this collection of parameters. Please note that paramter values
249: * themselves are not cloned.
250: *
251: * @see java.io.Serializable
252: * @see java.lang.Object#clone()
253: */
254: public Object clone() throws CloneNotSupportedException {
255: DefaultHttpParams clone = (DefaultHttpParams) super .clone();
256: if (this .parameters != null) {
257: clone.parameters = (HashMap) this.parameters.clone();
258: }
259: clone.setDefaults(this.defaults);
260: return clone;
261: }
262: }
|