Search in sources :

Example 1 with ActionDescriptor

use of com.cognifide.cq.cqsm.api.actions.ActionDescriptor 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 ActionDescriptor

use of com.cognifide.cq.cqsm.api.actions.ActionDescriptor in project APM by Cognifide.

the class ScriptManagerImpl method execute.

private Progress execute(Script script, final Mode mode, Map<String, String> customDefinitions, ResourceResolver resolver) throws ExecutionException, RepositoryException {
    if (script == null) {
        throw new ExecutionException("Script is not specified");
    }
    if (mode == null) {
        throw new ExecutionException("Execution mode is not specified");
    }
    final String path = script.getPath();
    actionFactory.update();
    LOG.info(String.format("Script execution started: %s [%s]", path, mode));
    Progress progress = new ProgressImpl(resolver.getUserID());
    final List<ActionDescriptor> descriptors = parseAllDescriptors(script, customDefinitions, resolver);
    final ActionExecutor actionExecutor = createExecutor(mode, resolver);
    final Context context = actionExecutor.getContext();
    final SessionSavingPolicy savingPolicy = context.getSavingPolicy();
    eventManager.trigger(Event.BEFORE_EXECUTE, script, mode, progress);
    for (ActionDescriptor descriptor : descriptors) {
        ActionResult result = actionExecutor.execute(descriptor);
        progress.addEntry(descriptor, result);
        if ((Status.ERROR == result.getStatus()) && (Mode.DRY_RUN != mode)) {
            eventManager.trigger(Event.AFTER_EXECUTE, script, mode, progress);
            return progress;
        }
        savingPolicy.save(context.getSession(), SessionSavingMode.EVERY_ACTION);
    }
    savingPolicy.save(context.getSession(), SessionSavingMode.SINGLE);
    eventManager.trigger(Event.AFTER_EXECUTE, script, mode, progress);
    return progress;
}
Also used : Context(com.cognifide.cq.cqsm.api.executors.Context) ProgressImpl(com.cognifide.cq.cqsm.core.progress.ProgressImpl) ActionExecutor(com.cognifide.cq.cqsm.core.actions.executor.ActionExecutor) Progress(com.cognifide.cq.cqsm.api.logger.Progress) ActionResult(com.cognifide.cq.cqsm.api.actions.ActionResult) ActionDescriptor(com.cognifide.cq.cqsm.api.actions.ActionDescriptor) SessionSavingPolicy(com.cognifide.cq.cqsm.core.sessions.SessionSavingPolicy) ExecutionException(com.cognifide.cq.cqsm.api.exceptions.ExecutionException)

Example 3 with ActionDescriptor

use of com.cognifide.cq.cqsm.api.actions.ActionDescriptor in project APM by Cognifide.

the class ScriptManagerImpl method parseIncludeDescriptors.

private List<ActionDescriptor> parseIncludeDescriptors(Script script, Map<String, String> definitions, List<Script> includes, ResourceResolver resolver) throws ExecutionException {
    final List<ActionDescriptor> descriptors = new LinkedList<>();
    LineIterator lineIterator = IOUtils.lineIterator(new StringReader(script.getData()));
    while (lineIterator.hasNext()) {
        String line = lineIterator.next();
        if (ScriptUtils.isAction(line)) {
            final String command = ScriptUtils.parseCommand(line, definitions);
            final ActionDescriptor descriptor = actionFactory.evaluate(command);
            final Action action = descriptor.getAction();
            descriptors.add(descriptor);
            if (action instanceof DefinitionProvider) {
                definitions.putAll(((DefinitionProvider) action).provideDefinitions(definitions));
            } else if (action instanceof ScriptProvider) {
                getIncludes(definitions, includes, resolver, descriptors, (ScriptProvider) action);
            }
        }
    }
    return descriptors;
}
Also used : Action(com.cognifide.cq.cqsm.api.actions.Action) DefinitionProvider(com.cognifide.cq.cqsm.api.actions.interfaces.DefinitionProvider) ScriptProvider(com.cognifide.cq.cqsm.api.actions.interfaces.ScriptProvider) ActionDescriptor(com.cognifide.cq.cqsm.api.actions.ActionDescriptor) StringReader(java.io.StringReader) LineIterator(org.apache.commons.io.LineIterator) LinkedList(java.util.LinkedList)

Aggregations

ActionDescriptor (com.cognifide.cq.cqsm.api.actions.ActionDescriptor)3 Action (com.cognifide.cq.cqsm.api.actions.Action)1 ActionMapper (com.cognifide.cq.cqsm.api.actions.ActionMapper)1 ActionResult (com.cognifide.cq.cqsm.api.actions.ActionResult)1 Mapping (com.cognifide.cq.cqsm.api.actions.annotations.Mapping)1 DefinitionProvider (com.cognifide.cq.cqsm.api.actions.interfaces.DefinitionProvider)1 ScriptProvider (com.cognifide.cq.cqsm.api.actions.interfaces.ScriptProvider)1 ActionCreationException (com.cognifide.cq.cqsm.api.exceptions.ActionCreationException)1 ExecutionException (com.cognifide.cq.cqsm.api.exceptions.ExecutionException)1 Context (com.cognifide.cq.cqsm.api.executors.Context)1 Progress (com.cognifide.cq.cqsm.api.logger.Progress)1 ActionExecutor (com.cognifide.cq.cqsm.core.actions.executor.ActionExecutor)1 ProgressImpl (com.cognifide.cq.cqsm.core.progress.ProgressImpl)1 SessionSavingPolicy (com.cognifide.cq.cqsm.core.sessions.SessionSavingPolicy)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1