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