1 18 19 package org.apache.jmeter.engine.util; 20 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.jmeter.functions.Function; 29 import org.apache.jmeter.functions.InvalidVariableException; 30 import org.apache.jmeter.samplers.SampleResult; 31 import org.apache.jmeter.samplers.Sampler; 32 import org.apache.jmeter.threads.JMeterContext; 33 import org.apache.jmeter.threads.JMeterContextService; 34 import org.apache.jmeter.util.JMeterUtils; 35 import org.apache.jorphan.logging.LoggingManager; 36 import org.apache.jorphan.reflect.ClassFinder; 37 import org.apache.log.Logger; 38 39 40 46 public class CompoundVariable implements Function 47 { 48 transient private static Logger log = 49 LoggingManager.getLoggerForClass(); 50 51 private String rawParameters; 52 53 static FunctionParser functionParser = new FunctionParser(); 54 55 static Map functions = new HashMap (); 56 private boolean hasFunction, isDynamic; 57 58 private String permanentResults = ""; 59 60 LinkedList compiledComponents = new LinkedList (); 61 62 static { 63 try 64 { 65 List classes = 66 ClassFinder.findClassesThatExtend( 67 JMeterUtils.getSearchPaths(), 68 new Class [] { Function.class }, 69 true); 70 Iterator iter = classes.iterator(); 71 while (iter.hasNext()) 72 { 73 Function tempFunc = 74 (Function) Class 75 .forName((String ) iter.next()) 76 .newInstance(); 77 functions.put(tempFunc.getReferenceKey(), tempFunc.getClass()); 78 } 79 } 80 catch (Exception err) 81 { 82 log.error("", err); 83 } 84 } 85 86 87 public CompoundVariable() 88 { 89 super(); 90 isDynamic = true; 91 hasFunction = false; 92 } 93 94 public CompoundVariable(String parameters) 95 { 96 this(); 97 try 98 { 99 setParameters(parameters); 100 } 101 catch (InvalidVariableException e) 102 { 103 } 104 } 105 106 public String execute() 107 { 108 if (isDynamic) 109 { 110 JMeterContext context = JMeterContextService.getContext(); 111 SampleResult previousResult = context.getPreviousResult(); 112 Sampler currentSampler = context.getCurrentSampler(); 113 return execute(previousResult, currentSampler); 114 } 115 else 116 { 117 return permanentResults; 118 } 119 } 120 121 125 public String getRawParameters() 126 { 127 return rawParameters; 128 } 129 130 133 public String execute(SampleResult previousResult, Sampler currentSampler) 134 { 135 if (compiledComponents == null || compiledComponents.size() == 0) 136 { 137 return ""; 138 } 139 boolean testDynamic = false; 140 StringBuffer results = new StringBuffer (); 141 Iterator iter = compiledComponents.iterator(); 142 while (iter.hasNext()) 143 { 144 Object item = iter.next(); 145 if (item instanceof Function) 146 { 147 testDynamic = true; 148 try 149 { 150 results.append( 151 ((Function) item).execute( 152 previousResult, 153 currentSampler)); 154 } 155 catch (InvalidVariableException e) 156 { 157 } 158 } 159 else if (item instanceof SimpleVariable) 160 { 161 testDynamic = true; 162 results.append(((SimpleVariable) item).toString()); 163 } 164 else 165 { 166 results.append(item); 167 } 168 } 169 if(!testDynamic) 170 { 171 isDynamic = false; 172 permanentResults = results.toString(); 173 } 174 return results.toString(); 175 } 176 177 public CompoundVariable getFunction() 178 { 179 CompoundVariable func = new CompoundVariable(); 180 func.compiledComponents = (LinkedList ) compiledComponents.clone(); 181 func.rawParameters = rawParameters; 182 return func; 183 } 184 185 public List getArgumentDesc() 186 { 187 return new LinkedList (); 188 } 189 190 public void clear() 191 { 192 hasFunction = false; 193 compiledComponents.clear(); 194 } 195 196 public void setParameters(String parameters) 197 throws InvalidVariableException 198 { 199 this.rawParameters = parameters; 200 if (parameters == null || parameters.length() == 0) 201 { 202 return; 203 } 204 205 compiledComponents = functionParser.compileString(parameters); 206 if (compiledComponents.size() > 1 207 || !(compiledComponents.get(0) instanceof String )) 208 { 209 hasFunction = true; 210 } 211 } 212 213 216 public void setParameters(Collection parameters) 217 throws InvalidVariableException 218 { 219 } 220 221 static Object getNamedFunction(String functionName) 222 throws InvalidVariableException 223 { 224 if(functions.containsKey(functionName)) 225 { 226 try 227 { 228 return (Function) ((Class ) functions.get(functionName)) 229 .newInstance(); 230 } 231 catch (Exception e) 232 { 233 log.error("", e); 234 throw new InvalidVariableException(); 235 } 236 } 237 else 238 { 239 return new SimpleVariable(functionName); 240 } 241 } 242 243 public boolean hasFunction() 244 { 245 return hasFunction; 246 } 247 248 251 public String getReferenceKey() 252 { 253 return ""; 254 } 255 263 } 264 | Popular Tags |