001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.constraint;
013:
014: import java.lang.reflect.Method;
015: import java.util.Map;
016:
017: import net.sf.oval.Validator;
018: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
019: import net.sf.oval.context.OValContext;
020: import net.sf.oval.exception.ReflectionException;
021: import net.sf.oval.internal.CollectionFactoryHolder;
022:
023: /**
024: * @author Sebastian Thomschke
025: */
026: public class ValidateWithMethodCheck extends
027: AbstractAnnotationCheck<ValidateWithMethod> {
028: private static final long serialVersionUID = 1L;
029:
030: private boolean ignoreIfNull;
031: private String methodName;
032: private Class parameterType;
033:
034: @Override
035: public void configure(final ValidateWithMethod constraintAnnotation) {
036: super .configure(constraintAnnotation);
037: setMethodName(constraintAnnotation.methodName());
038: setParameterType(constraintAnnotation.parameterType());
039: setIgnoreIfNull(constraintAnnotation.ignoreIfNull());
040: }
041:
042: @Override
043: public Map<String, String> getMessageVariables() {
044: final Map<String, String> messageVariables = CollectionFactoryHolder
045: .getFactory().createMap(4);
046: messageVariables.put("ignoreIfNull", Boolean
047: .toString(ignoreIfNull));
048: messageVariables.put("methodName", methodName);
049: messageVariables.put("parameterType", parameterType.getName());
050: return messageVariables;
051: }
052:
053: /**
054: * @return the methodName
055: */
056: public String getMethodName() {
057: return methodName;
058: }
059:
060: /**
061: * @return the parameterType
062: */
063: public Class getParameterType() {
064: return parameterType;
065: }
066:
067: /**
068: * @return the ignoreIfNull
069: */
070: public boolean isIgnoreIfNull() {
071: return ignoreIfNull;
072: }
073:
074: public boolean isSatisfied(final Object validatedObject,
075: final Object value, final OValContext context,
076: final Validator validator) throws ReflectionException {
077: if (value == null && ignoreIfNull)
078: return true;
079:
080: try {
081: final Method method = validatedObject.getClass()
082: .getDeclaredMethod(methodName, parameterType);
083: method.setAccessible(true);
084: return ((Boolean) method.invoke(validatedObject, value))
085: .booleanValue();
086: } catch (Exception e) {
087: throw new ReflectionException("Calling validation method "
088: + validatedObject.getClass().getName() + "."
089: + methodName + "("
090: + validatedObject.getClass().getName()
091: + ") failed.", e);
092: }
093: }
094:
095: /**
096: * @param ignoreIfNull the ignoreIfNull to set
097: */
098: public void setIgnoreIfNull(final boolean ignoreIfNull) {
099: this .ignoreIfNull = ignoreIfNull;
100: }
101:
102: /**
103: * @param methodName the methodName to set
104: */
105: public void setMethodName(final String methodName) {
106: this .methodName = methodName;
107: }
108:
109: /**
110: * @param parameterType the parameterType to set
111: */
112: public void setParameterType(final Class parameterType) {
113: this.parameterType = parameterType;
114: }
115: }
|