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