KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > java > sampler > JavaSamplerContext


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler/JavaSamplerContext.java,v 1.8 2004/02/10 00:46:44 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.java.sampler;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.jmeter.config.Arguments;
25 import org.apache.jorphan.logging.LoggingManager;
26 import org.apache.log.Logger;
27
28 /**
29  * JavaSamplerContext is used to provide context information to a
30  * JavaSamplerClient implementation. This currently consists of
31  * the initialization parameters which were specified in the GUI.
32  * Additional data may be accessible through JavaSamplerContext
33  * in the future.
34  *
35  * @author <a HREF="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
36  * @version $Revision: 1.8 $
37  */

38 public class JavaSamplerContext
39 {
40     /*
41      * Implementation notes:
42      *
43      * All of the methods in this class are currently read-only.
44      * If update methods are included in the future, they should
45      * be defined so that a single instance of JavaSamplerContext
46      * can be associated with each thread. Therefore, no
47      * synchronization should be needed. The same instance should
48      * be used for the call to setupTest, all calls to runTest,
49      * and the call to teardownTest.
50      */

51
52     /** Logging */
53     private static transient Logger log = LoggingManager.getLoggerForClass();
54
55     /**
56      * Map containing the initialization parameters for the
57      * JavaSamplerClient.
58      */

59     private Map JavaDoc params = null;
60
61     /**
62      * Create a new JavaSampler with the specified initialization
63      * parameters.
64      *
65      * @param args the initialization parameters.
66      */

67     public JavaSamplerContext(Arguments args)
68     {
69         this.params = args.getArgumentsAsMap();
70     }
71
72     /**
73      * Determine whether or not a value has been specified for the
74      * parameter with this name.
75      *
76      * @param name the name of the parameter to test
77      * @return true if the parameter value has been specified,
78      * false otherwise.
79      */

80     public boolean containsParameter(String JavaDoc name)
81     {
82         return params.containsKey(name);
83     }
84
85     /**
86      * Get an iterator of the parameter names. Each entry in the
87      * Iterator is a String.
88      *
89      * @return an Iterator of Strings listing the names of the
90      * parameters which have been specified for this
91      * test.
92      */

93     public Iterator JavaDoc getParameterNamesIterator()
94     {
95         return params.keySet().iterator();
96     }
97
98     /**
99      * Get the value of a specific parameter as a String, or null
100      * if the value was not specified.
101      *
102      * @param name the name of the parameter whose value should
103      * be retrieved
104      * @return the value of the parameter, or null if the
105      * value was not specified
106      */

107     public String JavaDoc getParameter(String JavaDoc name)
108     {
109         return getParameter(name, null);
110     }
111
112     /**
113      * Get the value of a specified parameter as a String, or return
114      * the specified default value if the value was not specified.
115      *
116      * @param name the name of the parameter whose value
117      * should be retrieved
118      * @param defaultValue the default value to return if the
119      * value of this parameter was not
120      * specified
121      * @return the value of the parameter, or the
122      * default value if the parameter was
123      * not specified
124      */

125     public String JavaDoc getParameter(String JavaDoc name, String JavaDoc defaultValue)
126     {
127         if (params == null || !params.containsKey(name))
128         {
129             return defaultValue;
130         }
131         return (String JavaDoc) params.get(name);
132     }
133
134     /**
135      * Get the value of a specified parameter as an integer. An
136      * exception will be thrown if the parameter is not specified
137      * or if it is not an integer. The value may be specified in
138      * decimal, hexadecimal, or octal, as defined by
139      * Integer.decode().
140      *
141      * @param name the name of the parameter whose value
142      * should be retrieved
143      * @return the value of the parameter
144      *
145      * @throws NumberFormatException if the parameter is not
146      * specified or is not an integer
147      *
148      * @see java.lang.Integer#decode(java.lang.String)
149      */

150     public int getIntParameter(String JavaDoc name) throws NumberFormatException JavaDoc
151     {
152         if (params == null || !params.containsKey(name))
153         {
154             throw new NumberFormatException JavaDoc(
155                 "No value for parameter named '" + name + "'.");
156         }
157
158         return Integer.decode((String JavaDoc) params.get(name)).intValue();
159     }
160
161     /**
162      * Get the value of a specified parameter as an integer, or
163      * return the specified default value if the value was not
164      * specified or is not an integer. A warning will be
165      * logged if the value is not an integer. The value may
166      * be specified in decimal, hexadecimal, or octal, as defined
167      * by Integer.decode().
168      *
169      * @param name the name of the parameter whose value
170      * should be retrieved
171      * @param defaultValue the default value to return if the
172      * value of this parameter was not
173      * specified
174      * @return the value of the parameter, or the
175      * default value if the parameter was
176      * not specified
177      *
178      * @see java.lang.Integer#decode(java.lang.String)
179      */

180     public int getIntParameter(String JavaDoc name, int defaultValue)
181     {
182         if (params == null || !params.containsKey(name))
183         {
184             return defaultValue;
185         }
186
187         try
188         {
189             return Integer.decode((String JavaDoc) params.get(name)).intValue();
190         }
191         catch (NumberFormatException JavaDoc e)
192         {
193             log.warn(
194                 "Value for parameter '"
195                     + name
196                     + "' not an integer: '"
197                     + params.get(name)
198                     + "'. Using default: '"
199                     + defaultValue
200                     + "'.",
201                 e);
202             return defaultValue;
203         }
204     }
205
206     /**
207      * Get the value of a specified parameter as a long. An
208      * exception will be thrown if the parameter is not specified
209      * or if it is not a long. The value may be specified in
210      * decimal, hexadecimal, or octal, as defined by
211      * Long.decode().
212      *
213      * @param name the name of the parameter whose value
214      * should be retrieved
215      * @return the value of the parameter
216      *
217      * @throws NumberFormatException if the parameter is not
218      * specified or is not a long
219      *
220      * @see Long#decode(String)
221      */

222     public long getLongParameter(String JavaDoc name) throws NumberFormatException JavaDoc
223     {
224         if (params == null || !params.containsKey(name))
225         {
226             throw new NumberFormatException JavaDoc(
227                 "No value for parameter named '" + name + "'.");
228         }
229
230         return Long.decode((String JavaDoc) params.get(name)).longValue();
231     }
232
233     /**
234      * Get the value of a specified parameter as along, or
235      * return the specified default value if the value was not
236      * specified or is not a long. A warning will be
237      * logged if the value is not a long. The value may
238      * be specified in decimal, hexadecimal, or octal, as defined
239      * by Long.decode().
240      *
241      * @param name the name of the parameter whose value
242      * should be retrieved
243      * @param defaultValue the default value to return if the
244      * value of this parameter was not
245      * specified
246      * @return the value of the parameter, or the
247      * default value if the parameter was
248      * not specified
249      *
250      * @see Long#decode(String)
251      */

252     public long getLongParameter(String JavaDoc name, long defaultValue)
253     {
254         if (params == null || !params.containsKey(name))
255         {
256             return defaultValue;
257         }
258         try
259         {
260             return Long.decode((String JavaDoc) params.get(name)).longValue();
261         }
262         catch (NumberFormatException JavaDoc e)
263         {
264             log.warn(
265                 "Value for parameter '"
266                     + name
267                     + "' not a long: '"
268                     + params.get(name)
269                     + "'. Using default: '"
270                     + defaultValue
271                     + "'.",
272                 e);
273             return defaultValue;
274         }
275     }
276 }
277
Popular Tags