001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.jmeter.protocol.http.sampler;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: import org.apache.commons.httpclient.HttpVersion;
032: import org.apache.commons.httpclient.ProtocolException;
033: import org.apache.commons.httpclient.params.HttpParams;
034: import org.apache.jorphan.logging.LoggingManager;
035: import org.apache.jorphan.util.JOrphanUtils;
036: import org.apache.log.Logger;
037:
038: /*
039: * Utility class to set up default HttpClient parameters from a file
040: */
041: public class HttpClientDefaultParameters {
042:
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: // Non-instantiable
047: private HttpClientDefaultParameters() {
048: }
049:
050: public static void load(String file, HttpParams params) {
051: log.info("Reading httpclient parameters from " + file);
052: File f = new File(file);
053: InputStream is = null;
054: Properties props = new Properties();
055: try {
056: is = new FileInputStream(f);
057: props.load(is);
058: Iterator pi = props.entrySet().iterator();
059: while (pi.hasNext()) {
060: Map.Entry me = (Map.Entry) pi.next();
061: String key = (String) me.getKey();
062: String value = (String) me.getValue();
063: int typeSep = key.indexOf("$"); // $NON-NLS-1$
064: try {
065: if (typeSep > 0) {
066: String type = key.substring(typeSep + 1);// get past separator
067: String name = key.substring(0, typeSep);
068: log.info("Defining " + name + " as " + value
069: + " (" + type + ")");
070: if (type.equals("Integer")) {
071: params.setParameter(name, Integer
072: .valueOf(value));
073: } else if (type.equals("Long")) {
074: params.setParameter(name, Long
075: .valueOf(value));
076: } else if (type.equals("Boolean")) {
077: params.setParameter(name, Boolean
078: .valueOf(value));
079: } else if (type.equals("HttpVersion")) {
080: params.setParameter(name, HttpVersion
081: .parse("HTTP/" + value));
082: } else {
083: log.warn("Unexpected type: " + type);
084: }
085: } else {
086: log.info("Defining " + key + " as " + value);
087: params.setParameter(key, value);
088: }
089: } catch (NumberFormatException e) {
090: log.error("Error in property: " + key + "=" + value
091: + " " + e.toString());
092: } catch (ProtocolException e) {
093: log.error("Error in property: " + key + "=" + value
094: + " " + e.toString());
095: }
096: }
097: } catch (FileNotFoundException e) {
098: log.error("Problem loading properties " + e.toString());
099: } catch (IOException e) {
100: log.error("Problem loading properties " + e.toString());
101: } finally {
102: JOrphanUtils.closeQuietly(is);
103: }
104: }
105: // public static void main(String args[]){
106: // load("bin/httpclient.parameters",
107: // org.apache.commons.httpclient.params.DefaultHttpParams.getDefaultParams());
108: // }
109: }
|