use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.
the class TemplateActionMethod method doInvoke.
/**
* Invoke action and if needed return requested XPath entries as String[][]
*
*/
@Override
protected Object doInvoke(Object instance, List<String> parameterNames, Object[] parameterValues) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ActionExecutionException {
if (log.isDebugEnabled()) {
log.debug("Executing '" + actionName + "' with arguments " + StringUtils.methodInputArgumentsToString(parameterValues));
}
TemplateActionsResponseVerificationConfigurator responseVerificationConfigurator = (TemplateActionsResponseVerificationConfigurator) ThreadContext.getAttribute(ThreadContext.TEMPLATE_ACTION_VERIFICATION_CONFIGURATOR);
String actionsXml = getActionsXml();
// insert any customer parameters into the thread scope map
for (int iParameter = 0; iParameter < parameterNames.size(); iParameter++) {
Object value = parameterValues[iParameter];
// if the value is an object with more than one values, add them in a Queue
if (value != null && (value instanceof Iterable || value.getClass().isArray())) {
Queue<Object> queue = new LinkedList<Object>();
if (value instanceof Iterable) {
for (Object oneValue : (Iterable<?>) value) {
queue.add(oneValue);
}
} else {
for (Object oneValue : (Object[]) value) {
queue.add(oneValue);
}
}
value = queue;
}
ThreadContext.setAttribute(parameterNames.get(iParameter), value);
}
long actionStartTimestamp = -1;
long totalTimeOfAllActionStepsNet = 0;
long totalTimeOfAllActionStepsBetweenReqAndResp = 0;
Object objectToReturn = null;
String checkpointName;
if (AbstractActionTask.REGISTER_FULL_AND_NET_ACTION_TIME_FOR_TEMPLATE_ACTIONS) {
checkpointName = actionName + "-net";
} else {
checkpointName = actionName;
}
try {
log.info("START running template actions from " + actionsXml);
actionStartTimestamp = System.currentTimeMillis();
if (isRegisterActionExecution() && !isLoggingInBatchMode) {
autoLogger.startCheckpoint(checkpointName, "", actionStartTimestamp);
}
XmlReader xmlReader = new XmlReader(actionsXml);
XmlUtilities xmlUtilities = new XmlUtilities();
long xmlParsingTime = System.currentTimeMillis() - actionStartTimestamp;
// this is the actual Action start time, skipping the actionXML parsing time
actionStartTimestamp += xmlParsingTime;
int actionNum = 1;
long currentTimeOfActionStepRequest;
NetworkingStopWatch stopWatch = new NetworkingStopWatch(actionMethodName);
while (xmlReader.goToNextAction()) {
String actionStep = actionMethodName + "[" + actionNum + "]";
stopWatch.step0_SetNewContext(actionStep);
String httpUrl = xmlReader.getRequestHttpUrl();
String httpMethod = xmlReader.getRequestHttpMethod();
List<ActionHeader> httpHeaders = xmlReader.getRequestHttpHeaders();
// connect to the specified URL
HttpClient httpClient = new HttpClient(httpUrl, httpMethod, httpHeaders, stopWatch);
// send HTTP request
String fileToSend = xmlReader.getRequestResourceFile();
httpClient.sendHttpRequest(actionStep, fileToSend, xmlReader.hasParamsInRequestResourceFile());
currentTimeOfActionStepRequest = stopWatch.getNetworkingTime();
// Measure and log time between last data sent and start of receive.
// Thread could be suspended but this could be server processing time too.
// and log request time
stopWatch.step5_StartInterimTimer();
// read response
ActionResponseObject expectedHttpResponseNode = xmlReader.getResponse();
// httpClient.disconnect(); // TODO: why this is needed. This is also before getting response
if (xmlReader.isLastAction() && (wantedXpathEntries != null || returnResponseBodyAsString)) {
if (returnResponseBodyAsString) {
// this is the last action and user wants to extract the response content as string
ActionParser actualHttpResponse = xmlUtilities.readActionResponse(httpClient, actionsXml, actionNum, true);
String contentAsString = actualHttpResponse.getBodyContentAsString();
actualHttpResponse.cleanupMembers();
objectToReturn = contentAsString;
// log response time below and after that return result
} else {
// this is the last action and user wants to extract some data from the response
ActionParser actualHttpResponse = xmlUtilities.readActionResponse(httpClient, actionsXml, actionNum, false);
String[][] extractedXpathEntries = XmlUtilities.extractXpathEntries(null, wantedXpathEntries);
actualHttpResponse.cleanupMembers();
objectToReturn = extractedXpathEntries;
// log response time below and after that return result
}
} else {
// verify the received response
xmlUtilities.verifyResponse(actionsXml, actionMethodName, actionNum, expectedHttpResponseNode, httpClient, responseVerificationConfigurator);
}
long currentTimeOfActionStepEnd = stopWatch.getNetworkingTime();
totalTimeOfAllActionStepsNet += currentTimeOfActionStepEnd;
totalTimeOfAllActionStepsBetweenReqAndResp += stopWatch.getTimeBetweenReqAndResponse();
if (HttpClient.logTimer.isTraceEnabled()) {
HttpClient.logTimer.trace("This action step " + actionStep + " time between end of send request and start of getting response time took " + stopWatch.getTimeBetweenReqAndResponse() + " ms");
HttpClient.logTimer.trace("This action step " + actionStep + " response network time took " + (currentTimeOfActionStepEnd - currentTimeOfActionStepRequest) + " ms");
HttpClient.logTimer.trace("This action step " + actionStep + " total network time took " + currentTimeOfActionStepEnd + " ms");
}
actionNum++;
}
if (isRegisterActionExecution()) {
if (HttpClient.logTimer.isTraceEnabled()) {
HttpClient.logTimer.trace("\t Total net time: " + totalTimeOfAllActionStepsNet + "(action " + actionsXml + ")| actionStartTimestamp : " + (actionStartTimestamp - xmlParsingTime) + "| total time between Req and Resp: " + totalTimeOfAllActionStepsBetweenReqAndResp);
}
if (isLoggingInBatchMode) {
autoLogger.insertCheckpoint(checkpointName, actionStartTimestamp - xmlParsingTime, totalTimeOfAllActionStepsNet, 0L, /* transferSize */
"", /* unit name */
CheckpointResult.PASSED);
} else {
autoLogger.endCheckpoint(checkpointName, 0L, /* transferSize */
CheckpointResult.PASSED, actionStartTimestamp - xmlParsingTime + /* the XML parsing time was previously added to the actionStartTimestamp (but after starting the Checkpoint) */
totalTimeOfAllActionStepsNet);
}
}
log.info("COMPLETE running template actions from " + actionsXml);
} catch (Exception e) {
if (isRegisterActionExecution()) {
if (isLoggingInBatchMode) {
autoLogger.insertCheckpoint(checkpointName, 0L, /* response time */
CheckpointResult.FAILED);
} else {
autoLogger.endCheckpoint(checkpointName, 0L, /* transfer size */
CheckpointResult.FAILED);
}
}
throw new ActionExecutionException("Error executing a template action", e);
}
if (AbstractActionTask.REGISTER_FULL_AND_NET_ACTION_TIME_FOR_TEMPLATE_ACTIONS) {
// Time between end of request and start of response reading - could be enabled only for some detailed investigations
autoLogger.insertCheckpoint(actionName + "-betweenReqAndResp", actionStartTimestamp, totalTimeOfAllActionStepsBetweenReqAndResp, 0L, null, CheckpointResult.PASSED);
}
return new CompositeResult(objectToReturn, totalTimeOfAllActionStepsNet);
}
use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.
the class ComponentActionMap method getActionClassInstance.
/**
* Get a new instance of the action class implementing the given action.
* This method will not use the cache, it will always create a new instance.
*
* @param actionName name of the action
* @return the instance of the action class
*
* @throws ActionExecutionException if the class instance cannot be created
*/
public Object getActionClassInstance(ActionMethod actionMethod) throws ActionExecutionException, NoSuchActionException {
//first check if we have such action defined
Class<?> actionClass = actionMethod.getMethod().getDeclaringClass();
String actionClassName = actionClass.getName();
try {
return actionClass.newInstance();
} catch (IllegalAccessException iae) {
throw new ActionExecutionException("Could not access action class " + actionClassName, iae);
} catch (InstantiationException ie) {
throw new ActionExecutionException("Could not instantiate action class " + actionClassName, ie);
}
}
use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.
the class ComponentActionMap method getCachedActionClassInstance.
/**
* Get the cached instance of the action class
* If the instance is not cached, create it and cache it
*
* @param actionClass the action class
* @return the instance of the action class
*
* @throws ActionExecutionException if the class instance cannot be created
*/
public Object getCachedActionClassInstance(String caller, Class<?> actionClass) throws ActionExecutionException, NoSuchActionException {
//first check if we have such action defined
String actionClassName = actionClass.getName();
try {
Object actionClassInstance = actionClassInstances.get(actionClassName);
if (actionClassInstance == null) {
Class<?> actionSuperClass = actionClass.getSuperclass();
if (actionSuperClass != null && actionSuperClass.getSimpleName().equals(CallerRelatedAction.class.getSimpleName())) {
// this is a CallerRelatedAction, we will pass the caller to it
actionClassInstance = actionClass.getDeclaredConstructor(String.class).newInstance(caller);
} else {
// this is a regular Action class
actionClassInstance = actionClass.newInstance();
}
actionClassInstances.put(actionClassName, actionClassInstance);
}
return actionClassInstance;
} catch (IllegalAccessException iae) {
throw new ActionExecutionException("Could not access action class " + actionClassName, iae);
} catch (InstantiationException ie) {
throw new ActionExecutionException("Could not instantiate action class " + actionClassName, ie);
} catch (InvocationTargetException ite) {
throw new ActionExecutionException("Could not instantiate action class " + actionClassName, ite);
} catch (NoSuchMethodException nsme) {
throw new ActionExecutionException("Could not instantiate action class " + actionClassName, nsme);
}
}
use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.
the class LoadClient method executeQueuedActions.
/**
* Execute all queued actions
*
* @throws AgentException
*/
@PublicAtsApi
public void executeQueuedActions() throws AgentException {
configureAgentLoaders();
boolean isThereUsernameConfigurator = false;
boolean isThereUsernameParam = false;
for (ParameterDataConfig dataConfig : loaderDataConfig.getParameterConfigurations()) {
// check if UsernameDataConfigurator is used more than once
if (isThereUsernameConfigurator) {
throw new ActionExecutionException("You have used Username Data Configurator more than once. This is not allowed.");
}
if (dataConfig instanceof UsernameDataConfig) {
isThereUsernameConfigurator = true;
// user names are specified
((UsernameDataConfig) dataConfig).verifyUsernamesAreWEnough(threadingPattern.getThreadCount());
} else if ("username".equals(dataConfig.getParameterName())) {
isThereUsernameParam = true;
}
}
if (isThereUsernameConfigurator && isThereUsernameParam) {
throw new ActionExecutionException("The parameter \"username\" can not be used for another data configurator " + "when Username data configurator is used.");
}
actionQueue.executeQueuedActions(new ArrayList<String>(this.loaderAddresses), threadingPattern, loaderDataConfig);
}
use of com.axway.ats.agent.core.exceptions.ActionExecutionException in project ats-framework by Axway.
the class EnvironmentHandler method getEnvironmentCleanupHandlerInstance.
/**
* Get the instance of the cleanup class for the given component. If such does not exist, create it.
*
* @param className the FQDN of the cleanup class
* @return the instance of the cleanup class
*
* @throws ActionExecutionException if the class instance cannot be created
*/
private EnvironmentCleanupHandler getEnvironmentCleanupHandlerInstance(Component component) throws ActionExecutionException {
String componentName = component.getComponentName();
Class<? extends EnvironmentCleanupHandler> cleanupHandler = component.getActionMap().getCleanupHandler();
//no cleanup handler defined for the component
if (cleanupHandler == null) {
return null;
}
try {
EnvironmentCleanupHandler cleanupClassInstance = cleanupClassInstances.get(componentName);
if (cleanupClassInstance == null) {
cleanupClassInstance = cleanupHandler.newInstance();
cleanupClassInstances.put(componentName, cleanupClassInstance);
}
return cleanupClassInstance;
} catch (IllegalAccessException iae) {
throw new ActionExecutionException("Could not access cleanup class " + cleanupHandler.getName(), iae);
} catch (InstantiationException ie) {
throw new ActionExecutionException("Could not instantiate cleanup class " + cleanupHandler.getName(), ie);
}
}
Aggregations