Search in sources :

Example 1 with ActionCreationException

use of com.cognifide.cq.cqsm.api.exceptions.ActionCreationException in project APM by Cognifide.

the class ActionFactoryImpl method evaluate.

@Override
public ActionDescriptor evaluate(String command) throws ActionCreationException {
    for (Object mapper : getMappers()) {
        for (Method method : mapper.getClass().getDeclaredMethods()) {
            if (!method.isAnnotationPresent(Mapping.class)) {
                continue;
            }
            final Mapping annotation = method.getAnnotation(Mapping.class);
            for (final String regex : annotation.value()) {
                final Pattern pattern = Pattern.compile("^" + regex + "$");
                final Matcher matcher = pattern.matcher(command);
                if (matcher.matches()) {
                    final List<Object> args = new ArrayList<>();
                    final List<String> rawArgs = new ArrayList<>();
                    final Type[] parameterTypes = method.getGenericParameterTypes();
                    for (int i = 1; i <= matcher.groupCount(); i++) {
                        rawArgs.add(matcher.group(i));
                        if (mapper instanceof ActionMapper) {
                            args.add(((ActionMapper) mapper).mapParameter(matcher.group(i), parameterTypes[i - 1]));
                        } else {
                            args.add(matcher.group(i));
                        }
                    }
                    try {
                        return new ActionDescriptor(command, (Action) method.invoke(mapper, args.toArray()), rawArgs);
                    } catch (IllegalAccessException e) {
                        LOG.error("Cannot access action mapper method: {} while processing command: {}", e.getMessage(), command);
                    } catch (InvocationTargetException e) {
                        LOG.error("Cannot invoke action mapper method: {} while processing command: {}", e.getMessage(), command);
                    }
                }
            }
        }
    }
    throw new ActionCreationException(String.format("Cannot find action for command: %s", command));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ActionDescriptor(com.cognifide.cq.cqsm.api.actions.ActionDescriptor) ArrayList(java.util.ArrayList) Mapping(com.cognifide.cq.cqsm.api.actions.annotations.Mapping) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(java.lang.reflect.Type) ActionMapper(com.cognifide.cq.cqsm.api.actions.ActionMapper) ActionCreationException(com.cognifide.cq.cqsm.api.exceptions.ActionCreationException)

Example 2 with ActionCreationException

use of com.cognifide.cq.cqsm.api.exceptions.ActionCreationException in project APM by Cognifide.

the class ScriptUtils method parseCommand.

public static String parseCommand(String command, Map<String, String> definitions) throws ActionCreationException {
    command = StringUtils.strip(command);
    Set<String> definitionNames = new HashSet<>();
    Matcher matcher = DEFINITION.matcher(command);
    while (matcher.find()) {
        String definitionName = matcher.group(1);
        definitionNames.add(definitionName);
    }
    for (String definitionName : definitionNames) {
        if (definitions == null) {
            throw new ActionCreationException("Definitions map is not specified");
        }
        String definitionValue = definitions.get(definitionName);
        if (definitionValue == null) {
            throw new ActionCreationException("Definition " + definitionName + " not found");
        }
        command = StringUtils.replace(command, String.format("${%s}", definitionName), definitionValue);
    }
    return command;
}
Also used : ActionCreationException(com.cognifide.cq.cqsm.api.exceptions.ActionCreationException) Matcher(java.util.regex.Matcher) HashSet(java.util.HashSet)

Example 3 with ActionCreationException

use of com.cognifide.cq.cqsm.api.exceptions.ActionCreationException in project APM by Cognifide.

the class ScriptManagerImpl method getIncludes.

private void getIncludes(Map<String, String> definitions, List<Script> includes, ResourceResolver resolver, List<ActionDescriptor> descriptors, ScriptProvider action) throws ExecutionException {
    for (String path : action.provideScripts()) {
        Script include = scriptFinder.find(path, resolver);
        if (include != null) {
            includes.add(include);
            descriptors.addAll(parseIncludeDescriptors(include, definitions, includes, resolver));
        } else {
            throw new ActionCreationException(String.format("Included script: '%s' does not exists.", path));
        }
    }
}
Also used : ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) Script(com.cognifide.cq.cqsm.api.scripts.Script) ActionCreationException(com.cognifide.cq.cqsm.api.exceptions.ActionCreationException)

Aggregations

ActionCreationException (com.cognifide.cq.cqsm.api.exceptions.ActionCreationException)3 Matcher (java.util.regex.Matcher)2 ActionDescriptor (com.cognifide.cq.cqsm.api.actions.ActionDescriptor)1 ActionMapper (com.cognifide.cq.cqsm.api.actions.ActionMapper)1 Mapping (com.cognifide.cq.cqsm.api.actions.annotations.Mapping)1 ModifiableScript (com.cognifide.cq.cqsm.api.scripts.ModifiableScript)1 Script (com.cognifide.cq.cqsm.api.scripts.Script)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1