KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > ejb > jndi > sampler > JNDISampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jndi/sampler/JNDISampler.java,v 1.8 2004/02/13 02:40:54 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.ejb.jndi.sampler;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
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 /**
38  * Samples the JNDI performance and records them
39  *
40  * @author Khor Soon Hin
41  * Created 2001 Dec 18
42  * @version $Revision: 1.8 $ Last Updated: $Date: 2004/02/13 02:40:54 $
43  */

44 public class JNDISampler implements Sampler
45 {
46   transient private static Logger log = LoggingManager.getLoggerForClass();
47
48   public static final String JavaDoc QUERY = "JNDISampler.query";
49
50   protected static Map JavaDoc keyMap = new HashMap JavaDoc();
51
52   public JNDISampler()
53   {
54   }
55
56   /**
57    * The main method which samples and records the JNDI performance
58    *
59    * @param e the JNDI sampling configuration
60    * @return the measurements captured
61    */

62   public SampleResult sample(Entry e)
63   {
64     log.info("Start : sample1");
65     boolean reflectionStatus = false;
66     // There is a row of ifs condition which may be executed depending on the
67
// state of the MethodConfig state. During reflection only one of these
68
// condition should be become true. However, once one condition becomes
69
// true it will set the MethodConfig state and this will cause the next
70
// condition to become true and so on. Thus else-ifs should be used BUT
71
// these same conditions need to be used such that more than one condition
72
// can be true during sampling so ifs and NOT else-ifs must be used. To
73
// resolve this conflict, ifs are used with stateJustChange is used to act
74
// like else-if
75
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 JavaDoc remoteInterface = null;
84     Object JavaDoc results = null;
85     Object JavaDoc 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 JavaDoc ht = new Hashtable JavaDoc();
94     JndiConfig jndiConfig = null;
95     InitialContext JavaDoc ctx = null;
96     try
97     {
98       jndiConfig = (JndiConfig)e.getConfigElement(JndiConfig.class);
99       // check if InitialContext is already obtained previously
100
ctx = jndiConfig.getInitialContext();
101       if(ctx == null)
102       {
103         // setup the hashtable
104
for(int i = 0 ; i < JndiConfig.JNDI_PROPS.length; i++)
105         {
106           String JavaDoc 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         // initialize initial context
118
start = System.currentTimeMillis();
119         ctx = new InitialContext JavaDoc(ht);
120         end = System.currentTimeMillis();
121         log.info("sample1 : Got initial context");
122         // store the initial context for reuse
123
jndiConfig.setInitialContext(ctx);
124       }
125       // set the initial context lookup time
126
ctxTime = end - start;
127       contextLookupRes.setTime(ctxTime);
128
129       // look up the name
130
LookupConfig lookupConfig =
131         (LookupConfig)e.getConfigElement(LookupConfig.class);
132       String JavaDoc 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 JavaDoc lookupNameClass = ref.getClass();
150  
151       // lookup method name
152
MethodConfig methodConfig =
153         (MethodConfig)e.getConfigElement(MethodConfig.class);
154       // store all reflections result in the model of the gui and not the
155
// MethodConfig obtained from getConfigElement() 'cos that's the clone.
156
// To get the model of the MethodConfigGui, get the MethodConfigGui
157
// from the MethodConfig clone first. All MethodConfig clones cloned
158
// from the same MethodConfig shares the same MethodConfigGui.
159
MethodConfigGui methodConfigGui = methodConfig.getGui();
160       MethodConfig model = methodConfigGui.getModel();
161       // Make all changes on the model of the gui and not the MethodConfig
162
// obtained from getConfigElement() because that is the clone.
163
int state = model.getState();
164       reflectionStatus = model.getReflectionStatus();
165       String JavaDoc[] strings = null;
166       if(log.isDebugEnabled())
167       {
168         log.debug("sample1 : state - " + state);
169         log.debug("sample1 : reflectionStatus - " + reflectionStatus);
170       }
171       // Perform only if :
172
// 1. doing a reflection and in this state
173
// 2. sampling does not perform this step
174
if((state == MethodConfig.METHOD_GET_HOME_NAMES && reflectionStatus
175     && !stateJustChanged))
176 // || (state >= MethodConfig.METHOD_GET_HOME_NAMES && !reflectionStatus))
177
{
178         // for this state, get the list of all methods in the home
179
// interface
180
Method JavaDoc[] methods = lookupNameClass.getMethods();
181         strings = new String JavaDoc[methods.length];
182         for(int i = 0; i < methods.length; i++)
183         {
184           // create method name which includes method signatures
185
strings[i] = getMethodSignature(methods[i]);
186         }
187         model.setMethodHomeList(strings);
188         model.setState(MethodConfig.METHOD_GET_HOME_PARMS);
189         stateJustChanged = true;
190       }
191       // Perform only if :
192
// 1. doing a reflection and in this state
193
// 2. sampling does not perform this step
194
if((state == MethodConfig.METHOD_GET_HOME_PARMS && reflectionStatus
195     && !stateJustChanged))
196 // || (state >= MethodConfig.METHOD_GET_HOME_PARMS && !reflectionStatus))
197
{
198         // for this state, get all the required parms for the selected
199
// method
200
String JavaDoc methodHomeName = methodConfig.getMethodHomeName();
201         if(log.isDebugEnabled())
202         {
203           log.debug("sample1 : selected methodHomeName - " +
204         methodHomeName);
205         }
206         Vector JavaDoc returnValues =
207         getMethodParmsTypes(methodHomeName, lookupNameClass);
208         // the first object of returnValues will be the Method while the
209
// the second object will be the parm types of Method
210
Method JavaDoc method = (Method JavaDoc)returnValues.get(0);
211         Class JavaDoc[] methodParmTypes = (Class JavaDoc[])returnValues.get(1);
212         // once the method is obtained store the parms
213
model.setMethodHomeParms(methodParmTypes);
214         model.setHomeMethod(method);
215         model.setState(MethodConfig.METHOD_INVOKE_HOME);
216         stateJustChanged = true;
217       }
218       // Perform only if :
219
// 1. doing a reflection and in this state
220
// 2. sampling and reflection has been done at least this state
221
// if reflection has not been done till this state then user is not
222
// interested in sampling till this state
223
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 JavaDoc method = model.getHomeMethod();
229         if(log.isDebugEnabled())
230         {
231           log.debug("sample1 : home method to be invoked - " + method);
232         }
233         // only initialize homeMethodRes if method execution is to be measured
234
homeMethodRes = new SampleResult();
235         // gather all parms from MethodConfigGui
236
Object JavaDoc[] 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           // invoke the method
246
start = System.currentTimeMillis();
247           remoteInterface = method.invoke(ref, parmsArray);
248             log.info("return - " + remoteInterface);
249         }
250         catch(IllegalAccessException JavaDoc err)
251         {
252           log.error(err);
253         }
254         catch(InvocationTargetException JavaDoc 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           // if sampling then get the time lapsed
266
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           // if reflection then get all the info required
275
model.setState(MethodConfig.METHOD_GET_REMOTE_NAMES);
276           stateJustChanged = true;
277           // store list of remote interfaces returned
278
model.setRemoteInterfaceList(remoteInterface);
279         }
280       }
281       // Perform only if :
282
// 1. doing a reflection and in this state
283
// 2. sampling does NOT perform this step
284
if((state == MethodConfig.METHOD_GET_REMOTE_NAMES && reflectionStatus
285     && !stateJustChanged))
286 // || (state >= MethodConfig.METHOD_GET_REMOTE_NAMES && !reflectionStatus))
287
{
288         // for this state, get the list of all methods in the remote
289
// interface
290
remoteInterface = model.getRemoteInterfaceType();
291         Class JavaDoc remoteInterfaceClass = remoteInterface.getClass();
292         if(log.isDebugEnabled())
293         {
294           log.debug("updateGui1 : remoteInterfaceClass - " +
295         remoteInterfaceClass);
296         }
297         Method JavaDoc[] methods = remoteInterfaceClass.getMethods();
298         strings = new String JavaDoc[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       // Perform only if :
308
// 1. doing a reflection and in this state
309
// 2. sampling does NOT perform this step
310
if((state == MethodConfig.METHOD_GET_REMOTE_PARMS && reflectionStatus
311     && !stateJustChanged))
312 // || (state >= MethodConfig.METHOD_GET_REMOTE_PARMS && !reflectionStatus))
313
{
314         // for this state, get all the required parms for the selected
315
// method
316
String JavaDoc methodRemoteName = methodConfig.getMethodRemoteName();
317         if(log.isDebugEnabled())
318         {
319           log.debug("sample1 : selected methodRemoteName - " +
320         methodRemoteName);
321         }
322         Object JavaDoc selectedRemoteInterfaceType = model.getRemoteInterfaceType();
323         Class JavaDoc selectedRemoteInterfaceTypeClass =
324         selectedRemoteInterfaceType.getClass();
325         Vector JavaDoc returnValues = getMethodParmsTypes(methodRemoteName,
326         selectedRemoteInterfaceTypeClass);
327         // the first object of returnValues contains the Method while the
328
// the second object the parm types of the Method
329
Method JavaDoc method = (Method JavaDoc)returnValues.get(0);
330         Class JavaDoc[] methodParmTypes = (Class JavaDoc[])returnValues.get(1);
331         // once the method is obtained store the parms
332
model.setMethodRemoteParms(methodParmTypes);
333         model.setRemoteMethod(method);
334         model.setState(MethodConfig.METHOD_INVOKE_REMOTE);
335         stateJustChanged = true;
336       }
337       // Perform only if :
338
// 1. doing a reflection and in this state
339
// 2. sampling and reflection has been done at least this state
340
// if reflection has not been done till this state then user is not
341
// interested in sampling till this state
342
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 JavaDoc method = model.getRemoteMethod();
348         if(log.isDebugEnabled())
349         {
350           log.debug("sample1 : remote method to be invoked - " + method);
351         }
352         Object JavaDoc selectedRemoteInterfaceType = model.getRemoteInterfaceType();
353         // only initialize homeMethodRes if method execution is to be measured
354
remoteMethodRes = new SampleResult();
355         // gather all parms from MethodConfigGui
356
Object JavaDoc[] parmsArray = null;
357         try
358         {
359           parmsArray = methodConfigGui.getMethodParmsValues(
360         MethodConfig.METHOD_INVOKE_REMOTE);
361           // invoke the method
362
start = System.currentTimeMillis();
363           results = method.invoke(selectedRemoteInterfaceType, parmsArray);
364             log.info("return - " + results);
365         }
366         catch(IllegalAccessException JavaDoc err)
367         {
368           log.error(err);
369         }
370         catch(InvocationTargetException JavaDoc 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           // if sampling get the time lapse
382
remoteMethodTime = end - start;
383           remoteMethodRes.setTime(remoteMethodTime);
384           remoteMethodRes.putValue(SampleResult.DISPLAY_NAME, "Remote Method Execution - "
385                 + method.getName());
386           String JavaDoc 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 JavaDoc(true));
394         }
395         else
396         {
397           // if reflection the set state
398
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 JavaDoc resultList = new ArrayList JavaDoc();
407       // don't need to test for null in contextLookupRes and lookupRes
408
// because both cannot be null otherwise error will be thrown
409
resultList.add(contextLookupRes);
410       resultList.add(lookupRes);
411       // test for null in homeMethodRes 'cos a null means that user just want
412
// to get a list of all home methods
413
if(homeMethodRes != null)
414       {
415         resultList.add(homeMethodRes);
416       }
417       // test for null in remoteMethodRes 'cos a null means that user just want
418
// to get a list of all methods of the remote interfaces
419
if(remoteMethodRes != null)
420       {
421         resultList.add(remoteMethodRes);
422       }
423       res.putValue(SampleResult.RESULT_LIST, resultList);
424       res.putValue(SampleResult.TOTAL_TIME, new Long JavaDoc(totalTime));
425
426       log.info("!!!!! ctxTime : " + ctxTime);
427       log.info("!!!!! lookupTime : " + lookupTime);
428       log.info("!!!!! homeMethodTime : " + homeMethodTime);
429     }
430     catch(NamingException JavaDoc err)
431     {
432       log.error(err);
433     }
434     
435     log.info("End : sample1");
436     return res;
437   }
438
439   /**
440    * From the <code>Method</code>, return the method signature i.e.
441    * method name followed by all the parms separated by commas and within
442    * parentheses
443    *
444    * @param method the method which the method signature is required
445    * @return method signature of the method
446    */

447   protected String JavaDoc getMethodSignature(Method JavaDoc method)
448   {
449     log.debug("Start : getMethodSignature1");
450     StringBuffer JavaDoc strbuff = new StringBuffer JavaDoc();
451     Class JavaDoc[] 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 JavaDoc returnVal = strbuff.toString();
465     log.debug("getMethodSignature1 : method signature - " + returnVal);
466     log.debug("End : getMethodSignature1");
467     return returnVal;
468   }
469
470   /**
471    * Given a method name and a class, compares the method name against all
472    * the methods in the class to look for a match. Once found, return an
473    * array containing all the Class of the parms of the method.
474    */

475   protected Vector JavaDoc getMethodParmsTypes(String JavaDoc methodName, Class JavaDoc objectClass)
476   {
477     log.debug("Start : getMethodParms1");
478     Method JavaDoc[] methods = objectClass.getMethods();
479     Method JavaDoc method = null;
480     Class JavaDoc[] methodParmTypes = null;
481     Class JavaDoc[] parameterTypes = null;
482     StringBuffer JavaDoc strbuff = new StringBuffer JavaDoc();
483     for(int i = 0; i < methods.length; i++)
484     {
485       // create method name which includes method signatures
486
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 JavaDoc 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 JavaDoc returnValues = new Vector JavaDoc();
514     returnValues.add(method);
515     returnValues.add(methodParmTypes);
516     log.debug("End : getMethodParms1");
517     return returnValues;
518   }
519 }
520
Popular Tags