001: package org.vraptor.component;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.util.List;
006: import java.util.ResourceBundle;
007:
008: import org.apache.log4j.Logger;
009: import org.vraptor.LogicException;
010: import org.vraptor.LogicRequest;
011: import org.vraptor.ValidationErrorsFactory;
012: import org.vraptor.annotations.Viewless;
013: import org.vraptor.validator.UnstableValidationException;
014: import org.vraptor.validator.ValidationErrors;
015:
016: /**
017: * Represents a logic method.
018: *
019: * @author Guilherme Silveira
020: */
021: public class DefaultLogicMethod implements LogicMethod {
022:
023: private static final Logger LOG = Logger
024: .getLogger(DefaultLogicMethod.class);
025:
026: private final String name;
027:
028: private final Method method;
029:
030: private final boolean shouldRedirect;
031:
032: private final Method validateMethod;
033:
034: private final List<MethodParameter> parameters;
035:
036: private final ValidationErrorsFactory validationFactory;
037:
038: private ComponentType type;
039:
040: /**
041: * Constructs it based on the logics name and method
042: */
043: public DefaultLogicMethod(ValidationErrorsFactory factory,
044: String name, Method method, Method validateMethod,
045: List<MethodParameter> parameters) {
046: this .validationFactory = factory;
047: this .name = name;
048: this .method = method;
049: this .shouldRedirect = !this .method
050: .isAnnotationPresent(Viewless.class);
051: this .validateMethod = validateMethod;
052: this .parameters = parameters;
053: if (LOG.isDebugEnabled()) {
054: LOG.debug("Logic method " + method.getName()
055: + " was read with validation "
056: + this .validateMethod + " and parameters "
057: + parameters);
058: }
059: }
060:
061: public String getName() {
062: return this .name;
063: }
064:
065: public String execute(Object component, LogicRequest context,
066: Object[] params) throws LogicException {
067: try {
068: Object result = method.invoke(component, params);
069: return result == null ? "ok" : result.toString();
070: } catch (IllegalArgumentException e) {
071: throw new LogicException(e);
072: } catch (IllegalAccessException e) {
073: throw new LogicException(e);
074: } catch (InvocationTargetException e) {
075: throw new LogicException(e.getCause());
076: }
077: }
078:
079: public ValidationErrors validate(Object component,
080: LogicRequest context, ResourceBundle bundle, Object[] params)
081: throws UnstableValidationException {
082: ValidationErrors errors = validationFactory.newInstance();
083: if (validateMethod != null) {
084: try {
085: validateMethod.invoke(component,
086: addValidationErrorsToArray(errors, params));
087: } catch (IllegalArgumentException e) {
088: LOG
089: .error(
090: "Nasty validation method has thrown an exception!",
091: e);
092: throw new UnstableValidationException(
093: "Error during validation process", e);
094: } catch (IllegalAccessException e) {
095: LOG
096: .error(
097: "Nasty validation method has thrown an exception!",
098: e);
099: throw new UnstableValidationException(
100: "Error during validation process", e);
101: } catch (InvocationTargetException e) {
102: LOG
103: .error(
104: "Nasty validation method has thrown an exception!",
105: e);
106: throw new UnstableValidationException(
107: "Error during validation process", e);
108: }
109: }
110: return errors;
111: }
112:
113: private Object[] addValidationErrorsToArray(
114: ValidationErrors errors, Object[] params) {
115:
116: Object[] types = new Object[params.length + 1];
117: types[0] = errors;
118:
119: int i = 1;
120:
121: for (Object param : params) {
122: types[i++] = param;
123: }
124: return types;
125: }
126:
127: public boolean shouldRedirect() {
128: return this .shouldRedirect;
129: }
130:
131: public Method getMetadata() {
132: return method;
133: }
134:
135: public List<MethodParameter> getParameters() {
136: return parameters;
137: }
138:
139: public ComponentType getComponentType() {
140: return type;
141: }
142:
143: public void setComponentType(ComponentType componentType) {
144: type = componentType;
145: }
146:
147: }
|