Search in sources :

Example 1 with Mapping

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

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

the class ActionFactoryImpl method refer.

@Override
public List<Map<String, Object>> refer() {
    final List<Map<String, Object>> references = new ArrayList<>();
    for (Object mapper : getMappers()) {
        for (Method method : mapper.getClass().getDeclaredMethods()) {
            if (!method.isAnnotationPresent(Mapping.class) || !(mapper instanceof ActionMapper)) {
                continue;
            }
            final Mapping mapping = method.getAnnotation(Mapping.class);
            final List<String> commands = ((ActionMapper) mapper).referMapping(mapping);
            HashMap<String, Object> reference = new HashMap<>();
            reference.put("commands", commands);
            reference.put("pattern", mapping.value());
            reference.put("args", mapping.args());
            reference.put("reference", mapping.reference());
            references.add(reference);
        }
    }
    sortReferences(references);
    return references;
}
Also used : ActionMapper(com.cognifide.cq.cqsm.api.actions.ActionMapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mapping(com.cognifide.cq.cqsm.api.actions.annotations.Mapping) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

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