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