1 18 19 package org.apache.jmeter.protocol.java.sampler; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.apache.jmeter.config.Arguments; 25 import org.apache.jorphan.logging.LoggingManager; 26 import org.apache.log.Logger; 27 28 38 public class JavaSamplerContext 39 { 40 51 52 53 private static transient Logger log = LoggingManager.getLoggerForClass(); 54 55 59 private Map params = null; 60 61 67 public JavaSamplerContext(Arguments args) 68 { 69 this.params = args.getArgumentsAsMap(); 70 } 71 72 80 public boolean containsParameter(String name) 81 { 82 return params.containsKey(name); 83 } 84 85 93 public Iterator getParameterNamesIterator() 94 { 95 return params.keySet().iterator(); 96 } 97 98 107 public String getParameter(String name) 108 { 109 return getParameter(name, null); 110 } 111 112 125 public String getParameter(String name, String defaultValue) 126 { 127 if (params == null || !params.containsKey(name)) 128 { 129 return defaultValue; 130 } 131 return (String ) params.get(name); 132 } 133 134 150 public int getIntParameter(String name) throws NumberFormatException 151 { 152 if (params == null || !params.containsKey(name)) 153 { 154 throw new NumberFormatException ( 155 "No value for parameter named '" + name + "'."); 156 } 157 158 return Integer.decode((String ) params.get(name)).intValue(); 159 } 160 161 180 public int getIntParameter(String 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 ) params.get(name)).intValue(); 190 } 191 catch (NumberFormatException 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 222 public long getLongParameter(String name) throws NumberFormatException 223 { 224 if (params == null || !params.containsKey(name)) 225 { 226 throw new NumberFormatException ( 227 "No value for parameter named '" + name + "'."); 228 } 229 230 return Long.decode((String ) params.get(name)).longValue(); 231 } 232 233 252 public long getLongParameter(String name, long defaultValue) 253 { 254 if (params == null || !params.containsKey(name)) 255 { 256 return defaultValue; 257 } 258 try 259 { 260 return Long.decode((String ) params.get(name)).longValue(); 261 } 262 catch (NumberFormatException 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 |