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());
}
}
}
}
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);
}
}
Aggregations