Search in sources :

Example 1 with TemplateActionMethod

use of com.axway.ats.agent.core.action.TemplateActionMethod in project ats-framework by Axway.

the class ComponentActionMap method addAction.

/**
 * Add an action to the component map
 *
 * @param actionName    name of the action
 * @param method        the method which implements the action
 * @throws ActionAlreadyDefinedException    if an action with this name has already been defined
 */
private void addAction(String actionName, String actionClassName, String actionMethodName, Method method, Class<?> actualClass, boolean isRegularAction) throws ActionAlreadyDefinedException {
    ActionMethodContainer methodContainer = actions.get(actionName);
    if (methodContainer == null) {
        methodContainer = new ActionMethodContainer(componentName, actionName);
        actions.put(actionName, methodContainer);
    }
    ActionMethod actionMethod;
    if (isRegularAction) {
        actionMethod = new ActionMethod(componentName, actionName, method, actualClass);
    } else {
        actionMethod = new TemplateActionMethod(componentName, actionName, actionClassName, actionMethodName, method, actualClass);
    }
    methodContainer.add(actionMethod);
}
Also used : ActionMethod(com.axway.ats.agent.core.action.ActionMethod) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) ActionMethodContainer(com.axway.ats.agent.core.action.ActionMethodContainer) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod)

Example 2 with TemplateActionMethod

use of com.axway.ats.agent.core.action.TemplateActionMethod in project ats-framework by Axway.

the class UserTemplateActionInvoker method invokeAndReturnTheLastResponseBody.

public String invokeAndReturnTheLastResponseBody(Class<?> actionClass, String actionMethodName, Object[] parameterValues) throws ActionExecutionException, NoSuchActionException, NoSuchComponentException, SecurityException, InternalComponentException, NoCompatibleMethodFoundException, NoSuchMethodException {
    Object actionObject = resolveActionObject(actionClass);
    Method actionMethod = resolveActionMethod(actionMethodName, parameterValues);
    String actionName = resolveActionName(false, actionMethod);
    // construct a template action and invoke it
    TemplateActionMethod templateActionMethod = new TemplateActionMethod(componentName, actionName, actionClass.getSimpleName(), actionMethod.getName(), actionMethod, actionClass);
    templateActionMethod.setReturnResponseBodyAsString(true);
    String content = (String) templateActionMethod.invoke(actionObject, parameterValues, false);
    if (content == null) {
        throw new ActionExecutionException("Can't return the last response body");
    }
    return content;
}
Also used : TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) Method(java.lang.reflect.Method) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod)

Example 3 with TemplateActionMethod

use of com.axway.ats.agent.core.action.TemplateActionMethod in project ats-framework by Axway.

the class UserTemplateActionInvoker method invoke.

/**
 * Execute a template action and return all requested list of values from the action response.
 * <br>
 * For example if you specify you want "//NODE1/NODE2/NODE3/id" and "//NODE1/NODE2/name",
 * you will get a list of 'id' values and another list of 'name' values
 *
 * @param actionClass the action java class
 * @param actionMethodName the name of action java method
 * @param parameterValues values for the arguments this action accepts
 * @param wantedXpathEntries what response entries are we interested of
 * @return list with list of values matched by all requested XPath entries
 *
 * @throws ActionExecutionException
 * @throws NoSuchActionException
 * @throws NoSuchComponentException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws InternalComponentException
 * @throws NoCompatibleMethodFoundException
 */
public String[][] invoke(Class<?> actionClass, String actionMethodName, Object[] parameterValues, String[] wantedXpathEntries) throws ActionExecutionException, NoSuchActionException, NoSuchComponentException, SecurityException, NoSuchMethodException, InternalComponentException, NoCompatibleMethodFoundException {
    Object actionObject = resolveActionObject(actionClass);
    Method actionMethod = resolveActionMethod(actionMethodName, parameterValues);
    String actionName = resolveActionName(false, actionMethod);
    // construct a template action and invoke it
    TemplateActionMethod templateActionMethod = new TemplateActionMethod(componentName, actionName, actionClass.getSimpleName(), actionMethod.getName(), actionMethod, actionClass);
    templateActionMethod.setWantedXpathEntries(wantedXpathEntries);
    CompositeResult result = (CompositeResult) templateActionMethod.invoke(actionObject, parameterValues, false);
    return (String[][]) result.getReturnResult();
}
Also used : TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) Method(java.lang.reflect.Method) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod)

Example 4 with TemplateActionMethod

use of com.axway.ats.agent.core.action.TemplateActionMethod in project ats-framework by Axway.

the class AbstractActionTask method invokeActions.

/**
 * Invoke one iteration of all actions in the queue
 *
 * @throws InterruptedException
 */
protected final void invokeActions() throws InterruptedException {
    UserActionsMonitoringAgent userActionsMonitoringAgent = UserActionsMonitoringAgent.getInstance(caller);
    if (log.isDebugEnabled()) {
        log.debug("Starting '" + queueName + "' queue for " + (++nIterations) + "th time");
    }
    // generate the input arguments for all action invokers
    generateInputArguments();
    long queueDuration = 0;
    long actionStartTimestamp = 0;
    long actionEndTimestamp = 0;
    if (this.itManager != null) {
        // inform a new iteration is starting now
        this.itManager.setIterationStartTime(this, System.currentTimeMillis());
    }
    try {
        for (int i = 0; i < actionInvokers.size(); i++) {
            // start cycling all actions in this iteration
            ActionInvoker actionInvoker = actionInvokers.get(i);
            Object actionClassInstance = actionClassInstances.get(i);
            ActionMethod actionMethod = actionInvoker.getActionMethod();
            final String actionName = actionInvoker.getActionName();
            final String transferUnit = actionMethod.getTransferUnit();
            // read some settings
            final boolean registerActionExecution = actionMethod.isRegisterActionExecution();
            final boolean registerActionExecutionInQueueExecutionTime = actionMethod.isRegisterActionExecutionInQueueExecutionTime();
            final boolean isTemplateActionMethod = actionMethod instanceof TemplateActionMethod;
            final boolean logCheckpoints = !isTemplateActionMethod || REGISTER_FULL_AND_NET_ACTION_TIME_FOR_TEMPLATE_ACTIONS;
            // checkpoint name. For template actions by default only network time is tracked.
            // Here "-full" adds total action processing including XML (de)serializations, parameterization
            String checkpointName;
            if (!isTemplateActionMethod) {
                checkpointName = actionName;
            } else {
                checkpointName = actionName + "-full";
            }
            // start a checkpoint
            userActionsMonitoringAgent.actionStarted(actionName);
            if (registerActionExecution) {
                actionStartTimestamp = System.currentTimeMillis();
                if (logCheckpoints && !isLoggingInBatchMode) {
                    log.startCheckpoint(checkpointName, transferUnit, actionStartTimestamp);
                }
            }
            // invoke the current action
            Object actionReturnedResult = null;
            try {
                actionReturnedResult = actionInvoker.invoke(actionClassInstance);
            } catch (Exception e) {
                // the action failed - end the checkpoint
                if (registerActionExecution) {
                    if (logCheckpoints) {
                        if (isLoggingInBatchMode) {
                            log.insertCheckpoint(checkpointName, actionStartTimestamp, 0, 0, transferUnit, CheckpointResult.FAILED);
                        } else {
                            log.endCheckpoint(checkpointName, 0, CheckpointResult.FAILED);
                        }
                    }
                    QueueExecutionStatistics.getInstance().registerActionExecutionResult(queueName, actionName, false);
                    log.insertCheckpoint(ATS_ACTION__QUEUE_EXECUTION_TIME, queueDuration, CheckpointResult.FAILED);
                }
                // re-throw the exception
                throw e;
            } finally {
                userActionsMonitoringAgent.actionEnded(actionName);
            }
            // the action passed
            if (registerActionExecution) {
                actionEndTimestamp = System.currentTimeMillis();
                long responseTimeMs = actionEndTimestamp - actionStartTimestamp;
                long transferSize = 0;
                if (transferUnit.length() > 0) {
                    transferSize = (Long) actionReturnedResult;
                }
                if (logCheckpoints) {
                    if (isLoggingInBatchMode) {
                        log.insertCheckpoint(checkpointName, actionStartTimestamp, responseTimeMs, transferSize, transferUnit, CheckpointResult.PASSED);
                    } else {
                        log.endCheckpoint(checkpointName, transferSize, CheckpointResult.PASSED, actionEndTimestamp);
                    }
                }
                if (registerActionExecutionInQueueExecutionTime) {
                    if (isTemplateActionMethod) {
                        // add net time in queue instead of full processing time
                        if (actionReturnedResult instanceof CompositeResult) {
                            CompositeResult res = (CompositeResult) actionReturnedResult;
                            responseTimeMs = res.getReqRespNetworkTime();
                        }
                    }
                    queueDuration += responseTimeMs;
                }
                QueueExecutionStatistics.getInstance().registerActionExecutionResult(queueName, actionName, true);
            }
        }
    // end cycling all actions in this iteration
    } catch (Exception e) {
        // We are particularly interested if the thread was interrupted
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof InterruptedException) {
            // the thread was interrupted
            if (this.timedOut) {
                // the thread was interrupted due to timeout, log the timeout and go to next iteration
                log.error("Iteration timed out in " + this.timedOutSeconds + " seconds - skipping to next iteration");
                // reset our flag as we will start another iteration
                this.timedOut = false;
                this.timedOutSeconds = 0;
            } else {
                // the thread interrupted, but not due to timeout, maybe the user cancelled the queue
                this.externallyInterrupted = true;
                throw (InterruptedException) cause;
            }
        } else {
            // some kind of generic exception has occurred
            log.error("Exception caught during invocation - skipping to next iteration", e);
        }
        // continue to the next iteration
        return;
    } finally {
        if (this.itManager != null) {
            this.itManager.clearIterationStartTime();
        }
    }
    log.insertCheckpoint(ATS_ACTION__QUEUE_EXECUTION_TIME, queueDuration, CheckpointResult.PASSED);
}
Also used : CompositeResult(com.axway.ats.agent.core.templateactions.CompositeResult) ActionMethod(com.axway.ats.agent.core.action.ActionMethod) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) NoSuchActionException(com.axway.ats.agent.core.exceptions.NoSuchActionException) NoCompatibleMethodFoundException(com.axway.ats.agent.core.exceptions.NoCompatibleMethodFoundException) NoSuchComponentException(com.axway.ats.agent.core.exceptions.NoSuchComponentException) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) ActionInvoker(com.axway.ats.agent.core.action.ActionInvoker) UserActionsMonitoringAgent(com.axway.ats.agent.core.monitoring.UserActionsMonitoringAgent)

Aggregations

TemplateActionMethod (com.axway.ats.agent.core.action.TemplateActionMethod)4 ActionMethod (com.axway.ats.agent.core.action.ActionMethod)2 ActionExecutionException (com.axway.ats.agent.core.exceptions.ActionExecutionException)2 Method (java.lang.reflect.Method)2 ActionInvoker (com.axway.ats.agent.core.action.ActionInvoker)1 ActionMethodContainer (com.axway.ats.agent.core.action.ActionMethodContainer)1 NoCompatibleMethodFoundException (com.axway.ats.agent.core.exceptions.NoCompatibleMethodFoundException)1 NoSuchActionException (com.axway.ats.agent.core.exceptions.NoSuchActionException)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 UserActionsMonitoringAgent (com.axway.ats.agent.core.monitoring.UserActionsMonitoringAgent)1 CompositeResult (com.axway.ats.agent.core.templateactions.CompositeResult)1