1 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 51 public class DefaultHttpParams implements HttpParams, Serializable , Cloneable { 52 53 54 private static final Log LOG = LogFactory.getLog(DefaultHttpParams.class); 55 56 57 private static HttpParamsFactory httpParamsFactory = new DefaultHttpParamsFactory(); 58 59 66 public static HttpParams getDefaultParams() { 67 return httpParamsFactory.getDefaultParams(); 68 } 69 70 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 85 private HttpParams defaults = null; 86 87 88 private HashMap parameters = null; 89 90 99 public DefaultHttpParams(final HttpParams defaults) { 100 super(); 101 this.defaults = defaults; 102 } 103 104 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 Object param = null; 127 if (this.parameters != null) { 128 param = this.parameters.get(name); 129 } 130 if (param != null) { 131 return param; 133 } else { 134 if (this.defaults != null) { 136 return this.defaults.getParameter(name); 138 } else { 139 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 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); } 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 234 public void clear() { 235 this.parameters = null; 236 } 237 238 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 } 255 | Popular Tags |