1 18 19 package org.apache.jmeter.ejb.jndi.sampler; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Hashtable ; 26 import java.util.Map ; 27 import java.util.Vector ; 28 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 32 import org.apache.jmeter.samplers.Entry; 33 import org.apache.jmeter.samplers.SampleResult; 34 import org.apache.jmeter.samplers.Sampler; 35 import org.apache.jorphan.logging.LoggingManager; 36 import org.apache.log.Logger; 37 44 public class JNDISampler implements Sampler 45 { 46 transient private static Logger log = LoggingManager.getLoggerForClass(); 47 48 public static final String QUERY = "JNDISampler.query"; 49 50 protected static Map keyMap = new HashMap (); 51 52 public JNDISampler() 53 { 54 } 55 56 62 public SampleResult sample(Entry e) 63 { 64 log.info("Start : sample1"); 65 boolean reflectionStatus = false; 66 boolean stateJustChanged = false; 76 long start = (long)0; 77 long end = (long)0; 78 long ctxTime = (long)0; 79 long lookupTime = (long)0; 80 long homeMethodTime = (long)0; 81 long remoteMethodTime = (long)0; 82 83 Object remoteInterface = null; 84 Object results = null; 85 Object ref = null; 86 87 SampleResult res = new SampleResult(); 88 SampleResult contextLookupRes = new SampleResult(); 89 contextLookupRes.putValue(SampleResult.DISPLAY_NAME, "Context Lookup"); 90 SampleResult lookupRes = new SampleResult(); 91 SampleResult homeMethodRes = null; 92 SampleResult remoteMethodRes = null; 93 Hashtable ht = new Hashtable (); 94 JndiConfig jndiConfig = null; 95 InitialContext ctx = null; 96 try 97 { 98 jndiConfig = (JndiConfig)e.getConfigElement(JndiConfig.class); 99 ctx = jndiConfig.getInitialContext(); 101 if(ctx == null) 102 { 103 for(int i = 0 ; i < JndiConfig.JNDI_PROPS.length; i++) 105 { 106 String value = jndiConfig.getValue(i); 107 if(value != null) 108 { 109 if(log.isDebugEnabled()) 110 { 111 log.debug("sample1 : JNDI env - " + 112 JndiConfig.JNDI_PROPS[i] + " = " + value); 113 } 114 ht.put(JndiConfig.JNDI_PROPS[i], value); 115 } 116 } 117 start = System.currentTimeMillis(); 119 ctx = new InitialContext (ht); 120 end = System.currentTimeMillis(); 121 log.info("sample1 : Got initial context"); 122 jndiConfig.setInitialContext(ctx); 124 } 125 ctxTime = end - start; 127 contextLookupRes.setTime(ctxTime); 128 129 LookupConfig lookupConfig = 131 (LookupConfig)e.getConfigElement(LookupConfig.class); 132 String lookupName = null; 133 if(lookupConfig != null) 134 { 135 lookupName = lookupConfig.getLookupName(); 136 if(log.isDebugEnabled()) 137 { 138 log.debug("sample1 : LookupName - " + lookupName); 139 } 140 start = System.currentTimeMillis(); 141 ref = ctx.lookup(lookupName); 142 end = System.currentTimeMillis(); 143 lookupTime = end - start; 144 log.info("Got remote interface"); 145 lookupRes.setTime(lookupTime); 146 lookupRes.putValue(SampleResult.DISPLAY_NAME, 147 "Remote Interface Lookup - " + lookupName); 148 } 149 Class lookupNameClass = ref.getClass(); 150 151 MethodConfig methodConfig = 153 (MethodConfig)e.getConfigElement(MethodConfig.class); 154 MethodConfigGui methodConfigGui = methodConfig.getGui(); 160 MethodConfig model = methodConfigGui.getModel(); 161 int state = model.getState(); 164 reflectionStatus = model.getReflectionStatus(); 165 String [] strings = null; 166 if(log.isDebugEnabled()) 167 { 168 log.debug("sample1 : state - " + state); 169 log.debug("sample1 : reflectionStatus - " + reflectionStatus); 170 } 171 if((state == MethodConfig.METHOD_GET_HOME_NAMES && reflectionStatus 175 && !stateJustChanged)) 176 { 178 Method [] methods = lookupNameClass.getMethods(); 181 strings = new String [methods.length]; 182 for(int i = 0; i < methods.length; i++) 183 { 184 strings[i] = getMethodSignature(methods[i]); 186 } 187 model.setMethodHomeList(strings); 188 model.setState(MethodConfig.METHOD_GET_HOME_PARMS); 189 stateJustChanged = true; 190 } 191 if((state == MethodConfig.METHOD_GET_HOME_PARMS && reflectionStatus 195 && !stateJustChanged)) 196 { 198 String methodHomeName = methodConfig.getMethodHomeName(); 201 if(log.isDebugEnabled()) 202 { 203 log.debug("sample1 : selected methodHomeName - " + 204 methodHomeName); 205 } 206 Vector returnValues = 207 getMethodParmsTypes(methodHomeName, lookupNameClass); 208 Method method = (Method )returnValues.get(0); 211 Class [] methodParmTypes = (Class [])returnValues.get(1); 212 model.setMethodHomeParms(methodParmTypes); 214 model.setHomeMethod(method); 215 model.setState(MethodConfig.METHOD_INVOKE_HOME); 216 stateJustChanged = true; 217 } 218 if((state == MethodConfig.METHOD_INVOKE_HOME && reflectionStatus 224 && !stateJustChanged) 225 || (state >= MethodConfig.METHOD_INVOKE_HOME && !reflectionStatus)) 226 { 227 log.debug("sample1 : METHOD_INVOKE_HOME"); 228 Method method = model.getHomeMethod(); 229 if(log.isDebugEnabled()) 230 { 231 log.debug("sample1 : home method to be invoked - " + method); 232 } 233 homeMethodRes = new SampleResult(); 235 Object [] parmsArray = null; 237 try 238 { 239 parmsArray = methodConfigGui.getMethodParmsValues( 240 MethodConfig.METHOD_INVOKE_HOME); 241 if(log.isDebugEnabled()) 242 { 243 log.debug("sample1 : home method parms - " + parmsArray); 244 } 245 start = System.currentTimeMillis(); 247 remoteInterface = method.invoke(ref, parmsArray); 248 log.info("return - " + remoteInterface); 249 } 250 catch(IllegalAccessException err) 251 { 252 log.error(err); 253 } 254 catch(InvocationTargetException err) 255 { 256 log.error(err); 257 } 258 catch(MethodConfigUserObjectException err) 259 { 260 log.error(err); 261 } 262 end = System.currentTimeMillis(); 263 if(!reflectionStatus) 264 { 265 homeMethodTime = end - start; 267 homeMethodRes.setTime(homeMethodTime); 268 homeMethodRes.putValue(SampleResult.DISPLAY_NAME, "Home Method Execution - " 269 + method.getName()); 270 homeMethodRes.putValue(SampleResult.SUCCESS, Boolean.TRUE); 271 } 272 else 273 { 274 model.setState(MethodConfig.METHOD_GET_REMOTE_NAMES); 276 stateJustChanged = true; 277 model.setRemoteInterfaceList(remoteInterface); 279 } 280 } 281 if((state == MethodConfig.METHOD_GET_REMOTE_NAMES && reflectionStatus 285 && !stateJustChanged)) 286 { 288 remoteInterface = model.getRemoteInterfaceType(); 291 Class remoteInterfaceClass = remoteInterface.getClass(); 292 if(log.isDebugEnabled()) 293 { 294 log.debug("updateGui1 : remoteInterfaceClass - " + 295 remoteInterfaceClass); 296 } 297 Method [] methods = remoteInterfaceClass.getMethods(); 298 strings = new String [methods.length]; 299 for(int i = 0; i < methods.length; i++) 300 { 301 strings[i] = getMethodSignature(methods[i]); 302 } 303 model.setMethodRemoteList(strings); 304 model.setState(MethodConfig.METHOD_GET_REMOTE_PARMS); 305 stateJustChanged = true; 306 } 307 if((state == MethodConfig.METHOD_GET_REMOTE_PARMS && reflectionStatus 311 && !stateJustChanged)) 312 { 314 String methodRemoteName = methodConfig.getMethodRemoteName(); 317 if(log.isDebugEnabled()) 318 { 319 log.debug("sample1 : selected methodRemoteName - " + 320 methodRemoteName); 321 } 322 Object selectedRemoteInterfaceType = model.getRemoteInterfaceType(); 323 Class selectedRemoteInterfaceTypeClass = 324 selectedRemoteInterfaceType.getClass(); 325 Vector returnValues = getMethodParmsTypes(methodRemoteName, 326 selectedRemoteInterfaceTypeClass); 327 Method method = (Method )returnValues.get(0); 330 Class [] methodParmTypes = (Class [])returnValues.get(1); 331 model.setMethodRemoteParms(methodParmTypes); 333 model.setRemoteMethod(method); 334 model.setState(MethodConfig.METHOD_INVOKE_REMOTE); 335 stateJustChanged = true; 336 } 337 if((state == MethodConfig.METHOD_INVOKE_REMOTE && reflectionStatus 343 && !stateJustChanged) 344 || (state >= MethodConfig.METHOD_INVOKE_REMOTE && !reflectionStatus)) 345 { 346 log.debug("sample1 : METHOD_INVOKE_REMOTE"); 347 Method method = model.getRemoteMethod(); 348 if(log.isDebugEnabled()) 349 { 350 log.debug("sample1 : remote method to be invoked - " + method); 351 } 352 Object selectedRemoteInterfaceType = model.getRemoteInterfaceType(); 353 remoteMethodRes = new SampleResult(); 355 Object [] parmsArray = null; 357 try 358 { 359 parmsArray = methodConfigGui.getMethodParmsValues( 360 MethodConfig.METHOD_INVOKE_REMOTE); 361 start = System.currentTimeMillis(); 363 results = method.invoke(selectedRemoteInterfaceType, parmsArray); 364 log.info("return - " + results); 365 } 366 catch(IllegalAccessException err) 367 { 368 log.error(err); 369 } 370 catch(InvocationTargetException err) 371 { 372 log.error(err); 373 } 374 catch(MethodConfigUserObjectException err) 375 { 376 log.error(err); 377 } 378 end = System.currentTimeMillis(); 379 if(!reflectionStatus) 380 { 381 remoteMethodTime = end - start; 383 remoteMethodRes.setTime(remoteMethodTime); 384 remoteMethodRes.putValue(SampleResult.DISPLAY_NAME, "Remote Method Execution - " 385 + method.getName()); 386 String resultsString = results.toString(); 387 byte[] resultBytes = null; 388 if(resultsString != null) 389 { 390 resultBytes = resultsString.getBytes(); 391 } 392 remoteMethodRes.putValue(SampleResult.TEXT_RESPONSE, resultBytes); 393 remoteMethodRes.putValue(SampleResult.SUCCESS, new Boolean (true)); 394 } 395 else 396 { 397 model.setState(MethodConfig.METHOD_COMPLETE); 399 stateJustChanged = true; 400 } 401 } 402 403 long totalTime = ctxTime + lookupTime + homeMethodTime + remoteMethodTime; 404 res.setTime(0); 405 res.putValue(SampleResult.DISPLAY_NAME, lookupName); 406 ArrayList resultList = new ArrayList (); 407 resultList.add(contextLookupRes); 410 resultList.add(lookupRes); 411 if(homeMethodRes != null) 414 { 415 resultList.add(homeMethodRes); 416 } 417 if(remoteMethodRes != null) 420 { 421 resultList.add(remoteMethodRes); 422 } 423 res.putValue(SampleResult.RESULT_LIST, resultList); 424 res.putValue(SampleResult.TOTAL_TIME, new Long (totalTime)); 425 426 log.info("!!!!! ctxTime : " + ctxTime); 427 log.info("!!!!! lookupTime : " + lookupTime); 428 log.info("!!!!! homeMethodTime : " + homeMethodTime); 429 } 430 catch(NamingException err) 431 { 432 log.error(err); 433 } 434 435 log.info("End : sample1"); 436 return res; 437 } 438 439 447 protected String getMethodSignature(Method method) 448 { 449 log.debug("Start : getMethodSignature1"); 450 StringBuffer strbuff = new StringBuffer (); 451 Class [] parameterTypes = method.getParameterTypes(); 452 strbuff.append(method.getName()); 453 strbuff.append("("); 454 if(parameterTypes.length > 0) 455 { 456 for(int j = 0; j < (parameterTypes.length - 1); j++) 457 { 458 strbuff.append(parameterTypes[j].toString()); 459 strbuff.append(", "); 460 } 461 strbuff.append(parameterTypes[parameterTypes.length - 1]); 462 } 463 strbuff.append(")"); 464 String returnVal = strbuff.toString(); 465 log.debug("getMethodSignature1 : method signature - " + returnVal); 466 log.debug("End : getMethodSignature1"); 467 return returnVal; 468 } 469 470 475 protected Vector getMethodParmsTypes(String methodName, Class objectClass) 476 { 477 log.debug("Start : getMethodParms1"); 478 Method [] methods = objectClass.getMethods(); 479 Method method = null; 480 Class [] methodParmTypes = null; 481 Class [] parameterTypes = null; 482 StringBuffer strbuff = new StringBuffer (); 483 for(int i = 0; i < methods.length; i++) 484 { 485 parameterTypes = methods[i].getParameterTypes(); 487 strbuff.delete(0, strbuff.length()); 488 strbuff.append(methods[i].getName()); 489 strbuff.append("("); 490 if(parameterTypes.length > 0) 491 { 492 for(int j = 0; j < (parameterTypes.length - 1); j++) 493 { 494 strbuff.append(parameterTypes[j].toString()); 495 strbuff.append(", "); 496 } 497 strbuff.append(parameterTypes[parameterTypes.length - 1]); 498 } 499 strbuff.append(")"); 500 String name = strbuff.toString(); 501 if(log.isDebugEnabled()) 502 { 503 log.debug("getMethodParms1 : current method to be compared - " 504 + name); 505 } 506 if(name.equals(methodName)) 507 { 508 method = methods[i]; 509 methodParmTypes = parameterTypes; 510 break; 511 } 512 } 513 Vector returnValues = new Vector (); 514 returnValues.add(method); 515 returnValues.add(methodParmTypes); 516 log.debug("End : getMethodParms1"); 517 return returnValues; 518 } 519 } 520 | Popular Tags |