Search in sources :

Example 1 with TemplateAction

use of com.axway.ats.agent.core.model.TemplateAction in project ats-framework by Axway.

the class ComponentActionMap method registerActionClass.

/**
 * Register a class with action methods
 *
 * @param actionClass   the action class
 */
public void registerActionClass(Class<?> actionClass) {
    log.info("Registering action class '" + actionClass.getName() + "'");
    for (Method classMethod : actionClass.getMethods()) {
        Action actionAnnotation = classMethod.getAnnotation(Action.class);
        TemplateAction templateActionAnnotation = classMethod.getAnnotation(TemplateAction.class);
        if (actionAnnotation != null || templateActionAnnotation != null) {
            String actionClassName = actionClass.getSimpleName();
            String actionMethodName = classMethod.getName();
            String actionName = actionAnnotation != null ? actionAnnotation.name() : templateActionAnnotation.name();
            if (StringUtils.isNullOrEmpty(actionName)) {
                actionName = ActionMethod.buildActionMethodName(classMethod);
            }
            try {
                addAction(actionName, actionClassName, actionMethodName, classMethod, actionClass, actionAnnotation != null);
            } catch (ActionAlreadyDefinedException aaee) {
                // log an error in case of a duplicate action and continue
                log.error(aaee.getMessage());
            }
        }
    }
}
Also used : Action(com.axway.ats.agent.core.model.Action) CallerRelatedAction(com.axway.ats.agent.core.action.CallerRelatedAction) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) ActionMethod(com.axway.ats.agent.core.action.ActionMethod) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) Method(java.lang.reflect.Method) ActionAlreadyDefinedException(com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)

Example 2 with TemplateAction

use of com.axway.ats.agent.core.model.TemplateAction in project ats-framework by Axway.

the class ActionClassGenerator method generateStub.

private String generateStub(String componentName, Class<?> actionClass, String targetActionClassPackage, String originalTargetPackage, Map<String, String> actionJavadocMap) {
    try {
        log.info("Generating stub for action class '" + actionClass.getCanonicalName() + "'");
        // first we need to generate the method definitions
        StringBuilder methodsDefinition = new StringBuilder();
        StringBuilder publicConstants = new StringBuilder();
        Method[] actionClassMethods = actionClass.getMethods();
        for (Method actionClassMethod : actionClassMethods) {
            if (isAnActionClass(actionClassMethod)) {
                Action actionAnnotation = actionClassMethod.getAnnotation(Action.class);
                TemplateAction templateActionAnnotation = actionClassMethod.getAnnotation(TemplateAction.class);
                String actionName;
                if (actionAnnotation != null) {
                    actionName = actionAnnotation.name();
                } else {
                    actionName = templateActionAnnotation.name();
                }
                // if the 'name' attribute is empty, generate an action method name
                if (StringUtils.isNullOrEmpty(actionName)) {
                    actionName = ActionMethod.buildActionMethodName(actionClassMethod);
                }
                // check if this is a transfer action and it has the necessary return type
                String transferUnit = "";
                if (actionAnnotation != null) {
                    transferUnit = actionAnnotation.transferUnit();
                }
                if (actionAnnotation != null && transferUnit.length() > 0 && actionClassMethod.getReturnType() != Long.class) {
                    throw new BuildException("Action '" + actionName + "' has a declared transfer unit, but the return type is not Long");
                }
                String actionJavaDoc = actionJavadocMap.get(actionName);
                // first append the action javadoc (if any)
                if (actionJavaDoc != null) {
                    methodsDefinition.append(actionJavaDoc);
                }
                // then process the method body and append it
                boolean registerActionExecution = true;
                if (actionAnnotation != null) {
                    registerActionExecution = actionAnnotation.registerActionExecution();
                }
                String actionDefinition = generateActionDefinition(actionName, actionClassMethod, transferUnit, registerActionExecution);
                methodsDefinition.append(actionDefinition);
                // get any enum constants
                publicConstants.append(generateEnumConstants(actionClassMethod));
            }
        }
        // generate the public constants
        publicConstants.append(generateConstantsDefinition(actionClass));
        ClassTemplateProcessor classProcessor;
        if (customTemplates.containsKey(actionClass.getName())) {
            // use the custom template supplied
            classProcessor = new ClassTemplateProcessor(new File(customTemplates.get(actionClass.getName())), originalSourcePackage, originalTargetPackage, targetActionClassPackage, actionClass, componentName, methodsDefinition.toString(), publicConstants.toString());
        } else {
            // use default template
            classProcessor = new ClassTemplateProcessor(originalSourcePackage, originalTargetPackage, targetActionClassPackage, actionClass, componentName, methodsDefinition.toString(), publicConstants.toString());
        }
        return classProcessor.processTemplate();
    } catch (Exception e) {
        throw new BuildException(e);
    }
}
Also used : Action(com.axway.ats.agent.core.model.Action) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) TemplateAction(com.axway.ats.agent.core.model.TemplateAction) Method(java.lang.reflect.Method) ActionMethod(com.axway.ats.agent.core.action.ActionMethod) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) AgentException(com.axway.ats.agent.core.exceptions.AgentException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

ActionMethod (com.axway.ats.agent.core.action.ActionMethod)2 Action (com.axway.ats.agent.core.model.Action)2 TemplateAction (com.axway.ats.agent.core.model.TemplateAction)2 Method (java.lang.reflect.Method)2 CallerRelatedAction (com.axway.ats.agent.core.action.CallerRelatedAction)1 TemplateActionMethod (com.axway.ats.agent.core.action.TemplateActionMethod)1 ActionAlreadyDefinedException (com.axway.ats.agent.core.exceptions.ActionAlreadyDefinedException)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 File (java.io.File)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 BuildException (org.apache.tools.ant.BuildException)1 SAXException (org.xml.sax.SAXException)1