1 18 19 package org.apache.jmeter.protocol.java.sampler; 20 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.jmeter.config.Arguments; 26 import org.apache.jmeter.engine.event.LoopIterationEvent; 27 import org.apache.jmeter.samplers.AbstractSampler; 28 import org.apache.jmeter.samplers.Entry; 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jmeter.testelement.TestListener; 31 import org.apache.jmeter.testelement.property.TestElementProperty; 32 import org.apache.jorphan.logging.LoggingManager; 33 import org.apache.log.Logger; 34 35 43 public class JavaSampler extends AbstractSampler implements TestListener 44 { 45 49 public static final String CLASSNAME = "classname"; 50 51 54 public static final String ARGUMENTS = "arguments"; 55 56 60 private transient JavaSamplerClient javaClient = null; 61 62 67 private transient JavaSamplerContext context = null; 68 69 72 private static transient Logger log = LoggingManager.getLoggerForClass(); 73 74 78 private static Set allSamplers = new HashSet (); 79 80 83 public JavaSampler() 84 { 85 setArguments(new Arguments()); 86 synchronized (allSamplers) 87 { 88 allSamplers.add(this); 89 } 90 } 91 92 98 public void setArguments(Arguments args) 99 { 100 setProperty(new TestElementProperty(ARGUMENTS, args)); 101 } 102 103 109 public Arguments getArguments() 110 { 111 return (Arguments) getProperty(ARGUMENTS).getObjectValue(); 112 } 113 114 117 private void releaseJavaClient () 118 { 119 if (javaClient != null) 120 { 121 javaClient.teardownTest(context); 122 } 123 javaClient = null; 124 context = null; 125 } 126 127 132 public void setClassname(String classname) 133 { 134 setProperty(CLASSNAME, classname); 135 } 136 137 142 public String getClassname() 143 { 144 return getPropertyAsString(CLASSNAME); 145 } 146 147 158 public SampleResult sample(Entry entry) 159 { 160 context = new JavaSamplerContext(getArguments()); 161 if (javaClient == null) 162 { 163 log.debug(whoAmI() + "Creating Java Client"); 164 createJavaClient(); 165 javaClient.setupTest(context); 166 } 167 168 return createJavaClient().runTest(context); 169 } 170 171 181 private JavaSamplerClient createJavaClient() 182 { 183 if (javaClient == null) 184 { 185 try 186 { 187 Class javaClass = Class.forName(getClassname().trim(), 188 false, 189 Thread.currentThread().getContextClassLoader()); 190 javaClient = (JavaSamplerClient) javaClass.newInstance(); 191 context = new JavaSamplerContext(getArguments()); 192 193 if (log.isDebugEnabled()) 194 { 195 log.debug( 196 whoAmI() 197 + "\tCreated:\t" 198 + getClassname() 199 + "@" 200 + Integer.toHexString(javaClient.hashCode())); 201 } 202 } 203 catch (Exception e) 204 { 205 log.error( 206 whoAmI() + "\tException creating: " + getClassname(), 207 e); 208 javaClient = new ErrorSamplerClient(); 209 } 210 } 211 return javaClient; 212 } 213 214 227 228 234 private String whoAmI() 235 { 236 StringBuffer sb = new StringBuffer (); 237 sb.append(Thread.currentThread().getName()); 238 sb.append("@"); 239 sb.append(Integer.toHexString(hashCode())); 240 return sb.toString(); 241 } 242 243 245 public void testStarted() 246 { 247 log.debug(whoAmI() + "\ttestStarted"); 248 } 249 250 251 public void testStarted(String host) 252 { 253 log.debug(whoAmI() + "\ttestStarted(" + host + ")"); 254 } 255 256 263 public void testEnded() 264 { 265 log.debug(whoAmI() + "\ttestEnded"); 266 synchronized (allSamplers) 267 { 268 Iterator i = allSamplers.iterator(); 269 while (i.hasNext()) 270 { 271 JavaSampler sampler = (JavaSampler) i.next(); 272 sampler.releaseJavaClient(); 273 i.remove(); 274 } 275 } 276 } 277 278 279 public void testEnded(String host) 280 { 281 testEnded(); 282 } 283 284 285 public void testIterationStart(LoopIterationEvent event) 286 { 287 } 288 289 296 class ErrorSamplerClient extends AbstractJavaSamplerClient 297 { 298 302 public SampleResult runTest(JavaSamplerContext context) 303 { 304 log.debug(whoAmI() + "\trunTest"); 305 Thread.yield(); 306 SampleResult results = new SampleResult(); 307 results.setSuccessful(false); 308 results.setResponseData( 309 ("Class not found: " + getClassname()).getBytes()); 310 results.setSampleLabel("ERROR: " + getClassname()); 311 return results; 312 } 313 } 314 } 315 | Popular Tags |