1 18 19 package org.apache.jmeter.protocol.tcp.sampler; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.Socket ; 28 import java.net.UnknownHostException ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.Set ; 35 36 import org.apache.jmeter.config.ConfigTestElement; 37 import org.apache.jmeter.util.JMeterUtils; 38 import org.apache.jmeter.engine.event.LoopIterationEvent; 39 import org.apache.jmeter.samplers.AbstractSampler; 40 import org.apache.jmeter.samplers.Entry; 41 import org.apache.jmeter.samplers.SampleResult; 42 import org.apache.jmeter.testelement.TestListener; 43 import org.apache.jorphan.logging.LoggingManager; 44 import org.apache.log.Logger; 45 46 51 public class TCPSampler extends AbstractSampler implements TestListener 52 { 53 private static final Logger log = LoggingManager.getLoggerForClass(); 54 55 public final static String SERVER = "TCPSampler.server"; public final static String PORT = "TCPSampler.port"; public final static String FILENAME = "TCPSampler.filename"; public final static String CLASSNAME = "TCPSampler.classname"; public final static String NODELAY = "TCPSampler.nodelay"; public final static String TIMEOUT = "TCPSampler.timeout"; public final static String REQUEST = "TCPSampler.request"; 63 private final static String TCPKEY = "TCP"; private final static String ERRKEY = "ERR"; 66 private static Set allSockets = new HashSet (); 68 71 private final static String STATUS_PREFIX = JMeterUtils.getPropDefault("tcp.status.prefix",""); 73 private final static String STATUS_SUFFIX = JMeterUtils.getPropDefault("tcp.status.suffix",""); 74 75 private final static String STATUS_PROPERTIES = JMeterUtils.getPropDefault("tcp.status.properties",""); 76 private final static Properties statusProps = new Properties (); 77 private static boolean haveStatusProps = false; 78 79 static 80 { 81 log.info("Protocol Handler name="+getClassname()); 82 log.info("Status prefix="+STATUS_PREFIX); 83 log.info("Status suffix="+STATUS_SUFFIX); 84 log.info("Status properties="+STATUS_PROPERTIES); 85 if (STATUS_PROPERTIES.length()>0) 86 { 87 File f = new File (STATUS_PROPERTIES); 88 try { 89 statusProps.load(new FileInputStream (f)); 90 log.info("Successfully loaded properties"); 91 haveStatusProps = true; 92 } catch (FileNotFoundException e) { 93 log.info("Property file not found"); 94 } catch (IOException e) { 95 log.info("Property file error "+e.toString()); 96 } 97 } 98 } 99 100 101 private static ThreadLocal tp = new ThreadLocal (){ 102 protected Object initialValue(){ 103 return new HashMap (); 104 } 105 }; 106 107 private transient TCPClient protocolHandler; 108 109 public TCPSampler() 110 { 111 log.debug("Created "+this); 112 protocolHandler=getProtocol(); 113 log.debug("Using Protocol Handler: " 114 +protocolHandler.getClass().getName()); 115 } 116 117 private String getError(){ 118 Map cp = (Map ) tp.get(); 119 return (String ) cp.get(ERRKEY); 120 } 121 122 private Socket getSocket() { 123 Map cp = (Map ) tp.get(); 124 Socket con = (Socket ) cp.get(TCPKEY); 125 if (con != null) { 126 log.debug(this+" Reusing connection "+con); return (Socket ) con; 128 } 129 130 try 132 { 133 con = new Socket (getServer(),getPort()); 134 con.setSoTimeout(getTimeout()); 135 con.setTcpNoDelay(getNoDelay()); 136 137 log.debug(this+" Timeout "+getTimeout()+" NoDelay "+getNoDelay()); log.debug("Created new connection "+con); cp.put(TCPKEY,con); 140 allSockets.add(con); } 142 catch (UnknownHostException e) 143 { 144 log.warn("Unknown host for "+getLabel(),e); cp.put(ERRKEY,e.toString()); 146 } 147 catch (IOException e) 148 { 149 log.warn("Could not create socket for "+getLabel(),e); cp.put(ERRKEY,e.toString()); 151 } 152 return con; 153 } 154 155 public String getUsername() 156 { 157 return getPropertyAsString(ConfigTestElement.USERNAME); 158 } 159 160 public String getPassword() 161 { 162 return getPropertyAsString(ConfigTestElement.PASSWORD); 163 } 164 165 public void setServer(String newServer) 166 { 167 this.setProperty(SERVER, newServer); 168 } 169 public String getServer() 170 { 171 return getPropertyAsString(SERVER); 172 } 173 public void setPort(String newFilename) 174 { 175 this.setProperty(PORT, newFilename); 176 } 177 public int getPort() 178 { 179 return getPropertyAsInt(PORT); 180 } 181 182 public void setFilename(String newFilename) 183 { 184 this.setProperty(FILENAME, newFilename); 185 } 186 public String getFilename() 187 { 188 return getPropertyAsString(FILENAME); 189 } 190 191 192 public void setRequestData(String newRequestData) 193 { 194 this.setProperty(REQUEST, newRequestData); 195 } 196 197 public String getRequestData() 198 { 199 return getPropertyAsString(REQUEST); 200 } 201 202 203 public void setTimeout(String newTimeout) 204 { 205 this.setProperty(FILENAME, newTimeout); 206 } 207 public int getTimeout() 208 { 209 return getPropertyAsInt(TIMEOUT); 210 } 211 212 213 public void setNoDelay(String newNoDelay) 214 { 215 this.setProperty(NODELAY, newNoDelay); 216 } 217 218 public boolean getNoDelay() 219 { 220 return getPropertyAsBoolean(NODELAY); 221 } 222 223 224 225 232 public String getLabel() 233 { 234 return ("tcp://" + this.getServer() + ":" + this.getPort()); } 236 237 private static String getClassname() 238 { 239 String className = JMeterUtils.getPropDefault("tcp.handler","TCPClientImpl"); 240 return className; 241 } 242 243 private static final String protoPrefix = "org.apache.jmeter.protocol.tcp.sampler."; 244 private Class getClass(String className) 245 { 246 Class c = null; 247 try 248 { 249 c = Class.forName(className 250 ,false,Thread.currentThread().getContextClassLoader()); 251 } 252 catch (ClassNotFoundException e) 253 { 254 try 255 { 256 c = Class.forName(protoPrefix+className 257 ,false,Thread.currentThread().getContextClassLoader()); 258 } 259 catch (ClassNotFoundException e1) 260 { 261 log.error("Could not find protocol class "+ className); 262 } 263 } 264 return c; 265 266 } 267 268 private TCPClient getProtocol(){ 269 TCPClient TCPClient = null; 270 Class javaClass = getClass(getClassname()); 271 try 272 { 273 TCPClient = (TCPClient) javaClass.newInstance(); 274 if (log.isDebugEnabled()) 275 { 276 log.debug(this 277 + "Created: " 278 + getClassname() 279 + "@" 280 + Integer.toHexString(TCPClient.hashCode())); 281 } 282 } 283 catch (Exception e) 284 { 285 log.error( 286 this + " Exception creating: " + getClassname(),e); 287 } 288 return TCPClient; 289 } 290 291 public SampleResult sample(Entry e) { 293 log.debug(getLabel()+" "+getFilename()+" "+getUsername()+" "+getPassword()); 294 SampleResult res = new SampleResult(); 295 boolean isSuccessful = false; 296 res.setSampleLabel(getName()); res.setSamplerData("Host: "+getServer()+" Port: "+getPort()); 298 res.sampleStart(); 299 try 300 { 301 Socket sock = getSocket(); 302 if (sock == null){ 303 res.setResponseCode("500"); 304 res.setResponseMessage(getError()); 305 } else { 306 InputStream is = sock.getInputStream(); 307 OutputStream os = sock.getOutputStream(); 308 String req = getRequestData(); 309 res.setSamplerData(req); 311 protocolHandler.write(os,req); 312 String in = protocolHandler.read(is); 313 res.setResponseData(in.getBytes()); 314 res.setDataType(SampleResult.TEXT); 315 res.setResponseCode("200"); 316 res.setResponseMessage("OK"); 317 isSuccessful = true; 318 if (STATUS_PREFIX.length() > 0) 320 { 321 int i = in.indexOf(STATUS_PREFIX); 322 int j = in.indexOf(STATUS_SUFFIX,i+STATUS_PREFIX.length()); 323 if (i != -1 && j > i) 324 { 325 String rc = in.substring(i+STATUS_PREFIX.length(),j); 326 res.setResponseCode(rc); 327 isSuccessful = checkResponseCode(rc); 328 if (haveStatusProps) 329 { 330 res.setResponseMessage( 331 statusProps.getProperty(rc,"Status code not found in properties")); 332 } 333 else 334 { 335 res.setResponseMessage("No status property file"); 336 } 337 } 338 else 339 { 340 res.setResponseCode("999"); 341 res.setResponseMessage("Status value not found"); 342 isSuccessful=false; 343 } 344 } 345 } 346 } 347 catch (Exception ex) 348 { 349 log.debug("",ex); 350 res.setResponseCode("500"); 351 res.setResponseMessage(ex.toString()); 352 } 353 354 res.sampleEnd(); 356 357 res.setSuccessful(isSuccessful); 359 360 return res; 361 } 362 363 367 private boolean checkResponseCode(String rc) { 368 if (rc.compareTo("400")>=0 && rc.compareTo("499")<=0) 369 { 370 return false; 371 } 372 if (rc.compareTo("500")>=0 && rc.compareTo("599")<=0) 373 { 374 return false; 375 } 376 return true; 377 } 378 379 private void disconnectAll(){ 380 synchronized (allSockets) 381 { 382 Iterator i = allSockets.iterator(); 383 while (i.hasNext()) 384 { 385 Socket socket = (Socket ) i.next(); 386 try 387 { 388 socket.close(); 389 } 390 catch (IOException e) 391 { 392 log.warn("Error closing socket ",e); 393 } finally { 394 i.remove(); 395 } 396 } 397 } 398 } 399 400 401 404 public void testStarted() { 406 log.debug(this+" test started"); 407 } 408 409 412 public void testEnded() { 414 log.debug(this+" test ended"); 415 disconnectAll(); 416 } 417 418 421 public void testStarted(String host) 422 { 423 log.debug(this+" test started on "+host); 424 426 } 427 428 431 public void testEnded(String host) 432 { 433 log.debug(this+" test ended on "+host); 434 disconnectAll(); 435 436 } 437 438 441 public void testIterationStart(LoopIterationEvent event) 442 { 443 log.debug(this+" test iteration start on "+event.getIteration()); 444 446 } 447 } 448 | Popular Tags |