Object remoteInterface = null;
Object results = null;
Object ref = null;
SampleResult res = new SampleResult();
SampleResult contextLookupRes = new SampleResult();
contextLookupRes.putValue(SampleResult.DISPLAY_NAME, "Context Lookup");
SampleResult lookupRes = new SampleResult();
SampleResult homeMethodRes = null;
SampleResult remoteMethodRes = null;
Hashtable ht = new Hashtable();
JndiConfig jndiConfig = null;
InitialContext ctx = null;
try
{
jndiConfig = (JndiConfig)e.getConfigElement(JndiConfig.class);
// check if InitialContext is already obtained previously
ctx = jndiConfig.getInitialContext();
if(ctx == null)
{
// setup the hashtable
for(int i = 0 ; i < JndiConfig.JNDI_PROPS.length; i++)
{
String value = jndiConfig.getValue(i);
if(value != null)
{
if(log.isDebugEnabled())
{
log.debug("sample1 : JNDI env - " +
JndiConfig.JNDI_PROPS[i] + " = " + value);
}
ht.put(JndiConfig.JNDI_PROPS[i], value);
}
}
// initialize initial context
start = System.currentTimeMillis();
ctx = new InitialContext(ht);
end = System.currentTimeMillis();
log.info("sample1 : Got initial context");
// store the initial context for reuse
jndiConfig.setInitialContext(ctx);
}
// set the initial context lookup time
ctxTime = end - start;
contextLookupRes.setTime(ctxTime);
// look up the name
LookupConfig lookupConfig =
(LookupConfig)e.getConfigElement(LookupConfig.class);
String lookupName = null;
if(lookupConfig != null)
{
lookupName = lookupConfig.getLookupName();
if(log.isDebugEnabled())
{
log.debug("sample1 : LookupName - " + lookupName);
}
start = System.currentTimeMillis();
ref = ctx.lookup(lookupName);
end = System.currentTimeMillis();
lookupTime = end - start;
log.info("Got remote interface");
lookupRes.setTime(lookupTime);
lookupRes.putValue(SampleResult.DISPLAY_NAME,
"Remote Interface Lookup - " + lookupName);
}
Class lookupNameClass = ref.getClass();
// lookup method name
MethodConfig methodConfig =
(MethodConfig)e.getConfigElement(MethodConfig.class);
// store all reflections result in the model of the gui and not the
// MethodConfig obtained from getConfigElement() 'cos that's the clone.
// To get the model of the MethodConfigGui, get the MethodConfigGui
// from the MethodConfig clone first. All MethodConfig clones cloned
// from the same MethodConfig shares the same MethodConfigGui.
MethodConfigGui methodConfigGui = methodConfig.getGui();
MethodConfig model = methodConfigGui.getModel();
// Make all changes on the model of the gui and not the MethodConfig
// obtained from getConfigElement() because that is the clone.
int state = model.getState();
reflectionStatus = model.getReflectionStatus();
String[] strings = null;
if(log.isDebugEnabled())
{
log.debug("sample1 : state - " + state);
log.debug("sample1 : reflectionStatus - " + reflectionStatus);
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling does not perform this step
if((state == MethodConfig.METHOD_GET_HOME_NAMES && reflectionStatus
&& !stateJustChanged))
// || (state >= MethodConfig.METHOD_GET_HOME_NAMES && !reflectionStatus))
{
// for this state, get the list of all methods in the home
// interface
Method[] methods = lookupNameClass.getMethods();
strings = new String[methods.length];
for(int i = 0; i < methods.length; i++)
{
// create method name which includes method signatures
strings[i] = getMethodSignature(methods[i]);
}
model.setMethodHomeList(strings);
model.setState(MethodConfig.METHOD_GET_HOME_PARMS);
stateJustChanged = true;
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling does not perform this step
if((state == MethodConfig.METHOD_GET_HOME_PARMS && reflectionStatus
&& !stateJustChanged))
// || (state >= MethodConfig.METHOD_GET_HOME_PARMS && !reflectionStatus))
{
// for this state, get all the required parms for the selected
// method
String methodHomeName = methodConfig.getMethodHomeName();
if(log.isDebugEnabled())
{
log.debug("sample1 : selected methodHomeName - " +
methodHomeName);
}
Vector returnValues =
getMethodParmsTypes(methodHomeName, lookupNameClass);
// the first object of returnValues will be the Method while the
// the second object will be the parm types of Method
Method method = (Method)returnValues.get(0);
Class[] methodParmTypes = (Class[])returnValues.get(1);
// once the method is obtained store the parms
model.setMethodHomeParms(methodParmTypes);
model.setHomeMethod(method);
model.setState(MethodConfig.METHOD_INVOKE_HOME);
stateJustChanged = true;
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling and reflection has been done at least this state
// if reflection has not been done till this state then user is not
// interested in sampling till this state
if((state == MethodConfig.METHOD_INVOKE_HOME && reflectionStatus
&& !stateJustChanged)
|| (state >= MethodConfig.METHOD_INVOKE_HOME && !reflectionStatus))
{
log.debug("sample1 : METHOD_INVOKE_HOME");
Method method = model.getHomeMethod();
if(log.isDebugEnabled())
{
log.debug("sample1 : home method to be invoked - " + method);
}
// only initialize homeMethodRes if method execution is to be measured
homeMethodRes = new SampleResult();
// gather all parms from MethodConfigGui
Object[] parmsArray = null;
try
{
parmsArray = methodConfigGui.getMethodParmsValues(
MethodConfig.METHOD_INVOKE_HOME);
if(log.isDebugEnabled())
{
log.debug("sample1 : home method parms - " + parmsArray);
}
// invoke the method
start = System.currentTimeMillis();
remoteInterface = method.invoke(ref, parmsArray);
log.info("return - " + remoteInterface);
}
catch(IllegalAccessException err)
{
log.error(err);
}
catch(InvocationTargetException err)
{
log.error(err);
}
catch(MethodConfigUserObjectException err)
{
log.error(err);
}
end = System.currentTimeMillis();
if(!reflectionStatus)
{
// if sampling then get the time lapsed
homeMethodTime = end - start;
homeMethodRes.setTime(homeMethodTime);
homeMethodRes.putValue(SampleResult.DISPLAY_NAME, "Home Method Execution - "
+ method.getName());
homeMethodRes.putValue(SampleResult.SUCCESS, Boolean.TRUE);
}
else
{
// if reflection then get all the info required
model.setState(MethodConfig.METHOD_GET_REMOTE_NAMES);
stateJustChanged = true;
// store list of remote interfaces returned
model.setRemoteInterfaceList(remoteInterface);
}
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling does NOT perform this step
if((state == MethodConfig.METHOD_GET_REMOTE_NAMES && reflectionStatus
&& !stateJustChanged))
// || (state >= MethodConfig.METHOD_GET_REMOTE_NAMES && !reflectionStatus))
{
// for this state, get the list of all methods in the remote
// interface
remoteInterface = model.getRemoteInterfaceType();
Class remoteInterfaceClass = remoteInterface.getClass();
if(log.isDebugEnabled())
{
log.debug("updateGui1 : remoteInterfaceClass - " +
remoteInterfaceClass);
}
Method[] methods = remoteInterfaceClass.getMethods();
strings = new String[methods.length];
for(int i = 0; i < methods.length; i++)
{
strings[i] = getMethodSignature(methods[i]);
}
model.setMethodRemoteList(strings);
model.setState(MethodConfig.METHOD_GET_REMOTE_PARMS);
stateJustChanged = true;
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling does NOT perform this step
if((state == MethodConfig.METHOD_GET_REMOTE_PARMS && reflectionStatus
&& !stateJustChanged))
// || (state >= MethodConfig.METHOD_GET_REMOTE_PARMS && !reflectionStatus))
{
// for this state, get all the required parms for the selected
// method
String methodRemoteName = methodConfig.getMethodRemoteName();
if(log.isDebugEnabled())
{
log.debug("sample1 : selected methodRemoteName - " +
methodRemoteName);
}
Object selectedRemoteInterfaceType = model.getRemoteInterfaceType();
Class selectedRemoteInterfaceTypeClass =
selectedRemoteInterfaceType.getClass();
Vector returnValues = getMethodParmsTypes(methodRemoteName,
selectedRemoteInterfaceTypeClass);
// the first object of returnValues contains the Method while the
// the second object the parm types of the Method
Method method = (Method)returnValues.get(0);
Class[] methodParmTypes = (Class[])returnValues.get(1);
// once the method is obtained store the parms
model.setMethodRemoteParms(methodParmTypes);
model.setRemoteMethod(method);
model.setState(MethodConfig.METHOD_INVOKE_REMOTE);
stateJustChanged = true;
}
// Perform only if :
// 1. doing a reflection and in this state
// 2. sampling and reflection has been done at least this state
// if reflection has not been done till this state then user is not
// interested in sampling till this state
if((state == MethodConfig.METHOD_INVOKE_REMOTE && reflectionStatus
&& !stateJustChanged)
|| (state >= MethodConfig.METHOD_INVOKE_REMOTE && !reflectionStatus))
{
log.debug("sample1 : METHOD_INVOKE_REMOTE");
Method method = model.getRemoteMethod();
if(log.isDebugEnabled())
{
log.debug("sample1 : remote method to be invoked - " + method);
}
Object selectedRemoteInterfaceType = model.getRemoteInterfaceType();
// only initialize homeMethodRes if method execution is to be measured
remoteMethodRes = new SampleResult();
// gather all parms from MethodConfigGui
Object[] parmsArray = null;
try
{
parmsArray = methodConfigGui.getMethodParmsValues(
MethodConfig.METHOD_INVOKE_REMOTE);
// invoke the method
start = System.currentTimeMillis();
results = method.invoke(selectedRemoteInterfaceType, parmsArray);
log.info("return - " + results);
}
catch(IllegalAccessException err)
{
log.error(err);
}
catch(InvocationTargetException err)
{
log.error(err);
}
catch(MethodConfigUserObjectException err)
{
log.error(err);
}
end = System.currentTimeMillis();
if(!reflectionStatus)
{
// if sampling get the time lapse
remoteMethodTime = end - start;
remoteMethodRes.setTime(remoteMethodTime);
remoteMethodRes.putValue(SampleResult.DISPLAY_NAME, "Remote Method Execution - "
+ method.getName());
String resultsString = results.toString();
byte[] resultBytes = null;
if(resultsString != null)
{
resultBytes = resultsString.getBytes();
}
remoteMethodRes.putValue(SampleResult.TEXT_RESPONSE, resultBytes);
remoteMethodRes.putValue(SampleResult.SUCCESS, new Boolean(true));
}
else
{
// if reflection the set state
model.setState(MethodConfig.METHOD_COMPLETE);