1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.BufferedReader ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 import java.io.Serializable ; 25 import java.text.DecimalFormat ; 26 import java.util.Collection ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 import org.apache.jmeter.engine.event.LoopIterationEvent; 31 import org.apache.jmeter.engine.util.CompoundVariable; 32 import org.apache.jmeter.samplers.SampleResult; 33 import org.apache.jmeter.samplers.Sampler; 34 import org.apache.jmeter.testelement.TestListener; 35 import org.apache.jmeter.threads.JMeterVariables; 36 import org.apache.jmeter.util.JMeterUtils; 37 import org.apache.jorphan.logging.LoggingManager; 38 import org.apache.jorphan.util.JMeterStopThreadException; 39 import org.apache.log.Logger; 40 41 63 public class StringFromFile 64 extends AbstractFunction 65 implements Serializable , TestListener 66 { 67 private static Logger log = LoggingManager.getLoggerForClass(); 68 69 private static final List desc = new LinkedList (); 70 private static final String KEY = "_StringFromFile"; 73 static final String ERR_IND = "**ERR**"; 75 static { 76 desc.add(JMeterUtils.getResString("string_from_file_file_name")); desc.add(JMeterUtils.getResString("function_name_param")); desc.add(JMeterUtils.getResString("string_from_file_seq_start")); desc.add(JMeterUtils.getResString("string_from_file_seq_final")); } 81 private static final int MIN_PARAM_COUNT = 1; 82 private static final int PARAM_NAME = 2; 83 private static final int PARAM_START = 3; 84 private static final int PARAM_END = 4; 85 private static final int MAX_PARAM_COUNT = 4; 86 87 transient private String myValue = ERR_IND; 88 transient private String myName = "StringFromFile_"; transient private Object [] values; 90 transient private BufferedReader myBread = null; transient private FileReader fis; transient private boolean firstTime = false; transient private String fileName; 95 public StringFromFile() 96 { 97 if (log.isDebugEnabled()) 98 { 99 log.debug("++++++++ Construct "+this); 100 } 101 } 102 103 public Object clone() 104 { 105 StringFromFile newReader = new StringFromFile(); 106 if (log.isDebugEnabled()) 107 { log.debug(this +"::StringFromFile.clone()", new Throwable ("debug")); } 110 111 return newReader; 112 } 113 114 private void closeFile(){ 115 if (myBread == null) return; 116 String tn = Thread.currentThread().getName(); 117 log.info(tn + " closing file " + fileName); try { 119 myBread.close(); 120 fis.close(); 121 } catch (IOException e) { 122 log.error("closeFile() error: " + e.toString()); } 124 } 125 126 private static final int COUNT_UNUSED = -2; 127 private int myStart = COUNT_UNUSED; 128 private int myCurrent = COUNT_UNUSED; 129 private int myEnd = COUNT_UNUSED; 130 131 private void openFile() 132 { 133 String tn = Thread.currentThread().getName(); 134 fileName = ((CompoundVariable) values[0]).execute(); 135 136 String start = ""; 137 if (values.length >= PARAM_START) 138 { 139 start = ((CompoundVariable) values[PARAM_START-1]).execute(); 140 try 141 { 142 myStart = Integer.valueOf(start).intValue(); 143 } 144 catch (NumberFormatException e) 145 { 146 myStart=COUNT_UNUSED; } 148 } 149 if (myCurrent == COUNT_UNUSED) myCurrent = myStart == COUNT_UNUSED ? 1 : myStart; 152 153 if (values.length >= PARAM_END) 154 { 155 String tmp = ((CompoundVariable) values[PARAM_END-1]).execute(); 156 try 157 { 158 myEnd = Integer.valueOf(tmp).intValue(); 159 } 160 catch (NumberFormatException e) 161 { 162 myEnd=COUNT_UNUSED; } 164 165 } 166 167 if (values.length >= PARAM_START) 168 { 169 log.info(tn+" Start = "+myStart+" Current = "+myCurrent+" End = "+myEnd); if (myEnd != COUNT_UNUSED){ 171 if (myCurrent > myEnd){ 172 log.info(tn+" No more files to process, "+myCurrent+" > "+myEnd); myBread=null; 174 return; 175 } 176 } 177 183 if (myStart != COUNT_UNUSED) { 185 log.info(tn + " using format "+fileName); 186 try { 187 DecimalFormat myFormatter = new DecimalFormat (fileName); 188 fileName = myFormatter.format(myCurrent); 189 } 190 catch (NumberFormatException e) 191 { 192 log.warn("Bad file name format ",e); 193 } 194 } 195 myCurrent++; } 197 198 log.info(tn + " opening file " + fileName); try 200 { 201 fis = new FileReader (fileName); 202 myBread = new BufferedReader (fis); 203 } 204 catch (Exception e) 205 { 206 log.error("openFile() error: " + e.toString()); myBread=null; 208 } 209 } 210 211 214 public synchronized String execute( 215 SampleResult previousResult, 216 Sampler currentSampler) 217 throws InvalidVariableException 218 { 219 220 JMeterVariables vars = getVariables(); 221 222 if (values.length >= PARAM_NAME) 223 { 224 myName = ((CompoundVariable) values[PARAM_NAME-1]).execute(); 225 } 226 227 myValue = ERR_IND; 228 229 235 if (firstTime) { 236 openFile(); 237 firstTime=false; 238 } 239 240 if (null != myBread) 241 { try 243 { 244 String line = myBread.readLine(); 245 if (line == null) 246 { String tn = Thread.currentThread().getName(); 248 log.info(tn+" EOF on file " + fileName); closeFile(); 250 openFile(); 251 if (myBread != null) { 252 line = myBread.readLine(); 253 } else { 254 line = ERR_IND; 255 if (myEnd != COUNT_UNUSED){ log.info(tn + " Detected end of sequence."); 257 throw new JMeterStopThreadException("End of sequence"); 258 } 259 } 260 } 261 myValue = line; 262 } 263 catch (IOException e) 264 { 265 String tn = Thread.currentThread().getName(); 266 log.error(tn + " error reading file " + e.toString()); } 268 } else { if (myEnd != COUNT_UNUSED){ String tn = Thread.currentThread().getName(); 271 log.info(tn + " Detected end of sequence."); 272 throw new JMeterStopThreadException("End of sequence"); 273 } 274 } 275 276 if (myName.length() > 0){ 277 vars.put(myName, myValue); 278 } 279 280 if (log.isDebugEnabled()){ 281 String tn = Thread.currentThread().getName(); 282 log.debug(tn + " name:" + myName + " value:" + myValue); } 285 286 return myValue; 287 288 } 289 290 299 public synchronized void setParameters(Collection parameters) 300 throws InvalidVariableException 301 { 302 303 log.debug(this +"::StringFromFile.setParameters()"); 305 values = parameters.toArray(); 306 307 if ((values.length > MAX_PARAM_COUNT) || (values.length < MIN_PARAM_COUNT)) 308 { 309 throw new InvalidVariableException("Wrong number of parameters"); } 311 312 StringBuffer sb = new StringBuffer (40); 313 sb.append("setParameters("); for (int i = 0; i< values.length;i++){ 315 if (i > 0) sb.append(","); 316 sb.append(((CompoundVariable) values[i]).getRawParameters()); 317 } 318 sb.append(")"); log.info(sb.toString()); 320 321 322 firstTime = true; 326 } 327 328 331 public String getReferenceKey() 332 { 333 return KEY; 334 } 335 336 339 public List getArgumentDesc() 340 { 341 return desc; 342 } 343 344 public void testStarted() 345 { 346 } 348 349 public void testStarted(String host) 350 { 351 } 353 354 public void testEnded() 355 { 356 this.testEnded(""); 357 } 358 359 public void testEnded(String host) 360 { 361 closeFile(); 362 } 363 364 public void testIterationStart(LoopIterationEvent event) 365 { 366 } 368 } | Popular Tags |