001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.java.sampler;
020:
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.jmeter.config.Arguments;
025: import org.apache.jorphan.logging.LoggingManager;
026: import org.apache.log.Logger;
027:
028: /**
029: * JavaSamplerContext is used to provide context information to a
030: * JavaSamplerClient implementation. This currently consists of the
031: * initialization parameters which were specified in the GUI. Additional data
032: * may be accessible through JavaSamplerContext in the future.
033: *
034: * @author <a href="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
035: * @version $Revision: 493789 $
036: */
037: public class JavaSamplerContext {
038: /*
039: * Implementation notes:
040: *
041: * All of the methods in this class are currently read-only. If update
042: * methods are included in the future, they should be defined so that a
043: * single instance of JavaSamplerContext can be associated with each thread.
044: * Therefore, no synchronization should be needed. The same instance should
045: * be used for the call to setupTest, all calls to runTest, and the call to
046: * teardownTest.
047: */
048:
049: /** Logging */
050: private static transient Logger log = LoggingManager
051: .getLoggerForClass();
052:
053: /**
054: * Map containing the initialization parameters for the JavaSamplerClient.
055: */
056: private Map params = null;
057:
058: /**
059: * Create a new JavaSampler with the specified initialization parameters.
060: *
061: * @param args
062: * the initialization parameters.
063: */
064: public JavaSamplerContext(Arguments args) {
065: this .params = args.getArgumentsAsMap();
066: }
067:
068: /**
069: * Determine whether or not a value has been specified for the parameter
070: * with this name.
071: *
072: * @param name
073: * the name of the parameter to test
074: * @return true if the parameter value has been specified, false otherwise.
075: */
076: public boolean containsParameter(String name) {
077: return params.containsKey(name);
078: }
079:
080: /**
081: * Get an iterator of the parameter names. Each entry in the Iterator is a
082: * String.
083: *
084: * @return an Iterator of Strings listing the names of the parameters which
085: * have been specified for this test.
086: */
087: public Iterator getParameterNamesIterator() {
088: return params.keySet().iterator();
089: }
090:
091: /**
092: * Get the value of a specific parameter as a String, or null if the value
093: * was not specified.
094: *
095: * @param name
096: * the name of the parameter whose value should be retrieved
097: * @return the value of the parameter, or null if the value was not
098: * specified
099: */
100: public String getParameter(String name) {
101: return getParameter(name, null);
102: }
103:
104: /**
105: * Get the value of a specified parameter as a String, or return the
106: * specified default value if the value was not specified.
107: *
108: * @param name
109: * the name of the parameter whose value should be retrieved
110: * @param defaultValue
111: * the default value to return if the value of this parameter was
112: * not specified
113: * @return the value of the parameter, or the default value if the parameter
114: * was not specified
115: */
116: public String getParameter(String name, String defaultValue) {
117: if (params == null || !params.containsKey(name)) {
118: return defaultValue;
119: }
120: return (String) params.get(name);
121: }
122:
123: /**
124: * Get the value of a specified parameter as an integer. An exception will
125: * be thrown if the parameter is not specified or if it is not an integer.
126: * The value may be specified in decimal, hexadecimal, or octal, as defined
127: * by Integer.decode().
128: *
129: * @param name
130: * the name of the parameter whose value should be retrieved
131: * @return the value of the parameter
132: *
133: * @throws NumberFormatException
134: * if the parameter is not specified or is not an integer
135: *
136: * @see java.lang.Integer#decode(java.lang.String)
137: */
138: public int getIntParameter(String name)
139: throws NumberFormatException {
140: if (params == null || !params.containsKey(name)) {
141: throw new NumberFormatException(
142: "No value for parameter named '" + name + "'.");
143: }
144:
145: return Integer.decode((String) params.get(name)).intValue();
146: }
147:
148: /**
149: * Get the value of a specified parameter as an integer, or return the
150: * specified default value if the value was not specified or is not an
151: * integer. A warning will be logged if the value is not an integer. The
152: * value may be specified in decimal, hexadecimal, or octal, as defined by
153: * Integer.decode().
154: *
155: * @param name
156: * the name of the parameter whose value should be retrieved
157: * @param defaultValue
158: * the default value to return if the value of this parameter was
159: * not specified
160: * @return the value of the parameter, or the default value if the parameter
161: * was not specified
162: *
163: * @see java.lang.Integer#decode(java.lang.String)
164: */
165: public int getIntParameter(String name, int defaultValue) {
166: if (params == null || !params.containsKey(name)) {
167: return defaultValue;
168: }
169:
170: try {
171: return Integer.decode((String) params.get(name)).intValue();
172: } catch (NumberFormatException e) {
173: log.warn("Value for parameter '" + name
174: + "' not an integer: '" + params.get(name)
175: + "'. Using default: '" + defaultValue + "'.", e);
176: return defaultValue;
177: }
178: }
179:
180: /**
181: * Get the value of a specified parameter as a long. An exception will be
182: * thrown if the parameter is not specified or if it is not a long. The
183: * value may be specified in decimal, hexadecimal, or octal, as defined by
184: * Long.decode().
185: *
186: * @param name
187: * the name of the parameter whose value should be retrieved
188: * @return the value of the parameter
189: *
190: * @throws NumberFormatException
191: * if the parameter is not specified or is not a long
192: *
193: * @see Long#decode(String)
194: */
195: public long getLongParameter(String name)
196: throws NumberFormatException {
197: if (params == null || !params.containsKey(name)) {
198: throw new NumberFormatException(
199: "No value for parameter named '" + name + "'.");
200: }
201:
202: return Long.decode((String) params.get(name)).longValue();
203: }
204:
205: /**
206: * Get the value of a specified parameter as along, or return the specified
207: * default value if the value was not specified or is not a long. A warning
208: * will be logged if the value is not a long. The value may be specified in
209: * decimal, hexadecimal, or octal, as defined by Long.decode().
210: *
211: * @param name
212: * the name of the parameter whose value should be retrieved
213: * @param defaultValue
214: * the default value to return if the value of this parameter was
215: * not specified
216: * @return the value of the parameter, or the default value if the parameter
217: * was not specified
218: *
219: * @see Long#decode(String)
220: */
221: public long getLongParameter(String name, long defaultValue) {
222: if (params == null || !params.containsKey(name)) {
223: return defaultValue;
224: }
225: try {
226: return Long.decode((String) params.get(name)).longValue();
227: } catch (NumberFormatException e) {
228: log.warn("Value for parameter '" + name + "' not a long: '"
229: + params.get(name) + "'. Using default: '"
230: + defaultValue + "'.", e);
231: return defaultValue;
232: }
233: }
234: }
|