001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/ValidationEngine.java,v 1.4 2007/10/31 17:00:52 roberthofstra Exp $
003: * $Revision: 1.4 $
004: * $Date: 2007/10/31 17:00:52 $
005: *
006: *
007: * Created on Oct 6, 2004
008: *
009: * All right reserved(c) 2004, Knowlogy
010: *
011: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
012: * All Knowlogy brand and product names are trademarks or registered trademarks
013: * of Knowlogy in the Netherlands and other countries.
014: *
015: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
016: * or translated into any human or computer language, in any form, or by any means, electronic,
017: * mechanical, magnetic, optical, chemical, manual, or otherwise,
018: * without the prior written permission of the copyright owner, Knowlogy.
019: *
020: */
021: package nl.knowlogy.validation;
022:
023: import nl.knowlogy.validation.metadata.ClassMetadata;
024:
025: import org.apache.commons.collections.FastHashMap;
026: import org.apache.log4j.Logger;
027:
028: /**
029: * The ValidationEngine class is responsible for validating
030: * objects.
031: *
032: *
033: * @author <a href="mailto:robert.hofstra@knowlogy.nl">Robert Hofstra, Knowlogy</a>
034: *
035: */
036: public class ValidationEngine {
037:
038: private static final Logger logger = Logger
039: .getLogger(ValidationEngine.class);
040: private static final ValidationEngine validationEngine = new ValidationEngine();
041: private FastHashMap validatorsMap;
042: private ValidatorSupplier validatorSupplier;
043:
044: public static ValidationEngine getInstance() {
045: return validationEngine;
046: }
047:
048: static {
049: try {
050: // trick to load, if existing, the AnnotatedClassesValidatorFactory, so it's
051: // registers itself as a validatorsupplier
052: Class
053: .forName("nl.knowlogy.validation.annotations.AnnotatedClassesValidatorFactory");
054: logger.info("Loaded the AnnotatedClassesValidatorFactory");
055: } catch (Exception ex) {
056: logger
057: .info("Not loaded the AnnotatedClassesValidatorFactory");
058: }
059: }
060:
061: public static void validate(Object object, Messages messages) {
062: getInstance().doValidate(object, messages);
063: }
064:
065: /**
066: * Validates if a value is valid for the property of a javabean from clazz.
067: *
068: * This is for validation a value without setting it actualy.
069: *
070: * @param clazz of the javabean for which the value must be validated
071: * @param propertyName name of the property
072: * @param value value
073: * @param messages a messages container to which properties are added.
074: */
075: public static void validateProperty(Class clazz,
076: String propertyName, Object value, Messages messages) {
077: getInstance().doValidateProperty(clazz, propertyName, value,
078: messages);
079: }
080:
081: public static void validateGroup(Object object, String groupName,
082: Messages messages) {
083: getInstance().doValidateGroup(object, groupName, messages);
084: }
085:
086: public static void validate(Object object) {
087: getInstance().doValidate(object);
088: }
089:
090: public static ClassValidator getClassValidator(Class clazz) {
091: return getInstance().doGetValidator(clazz);
092: }
093:
094: public static void registerValidator(ClassValidator classValidator) {
095: getInstance().doRegisterValidator(classValidator);
096: }
097:
098: public static void setValidatorSupplier(
099: ValidatorSupplier validatorSupplier) {
100: getInstance().doSetValidatorSupplier(validatorSupplier);
101: }
102:
103: public static ClassMetadata getClassMetaData(Class clazz) {
104: return getInstance().doGetClassMetaData(clazz);
105: }
106:
107: public synchronized void doRegisterValidator(
108: ClassValidator classValidator) {
109: logger.debug("Registered classValidator " + classValidator);
110: validatorsMap.setFast(false);
111: validatorsMap.put(classValidator.getClazz().getName(),
112: classValidator);
113: validatorsMap.setFast(true);
114: }
115:
116: protected synchronized void doSetValidatorSupplier(
117: ValidatorSupplier validatorSupplier) {
118: this .validatorSupplier = validatorSupplier;
119: }
120:
121: protected ClassMetadata doGetClassMetaData(Class clazz) {
122: ClassValidator classValidator = doGetValidator(clazz);
123: if (classValidator instanceof ClassMetadata) {
124: return (ClassMetadata) classValidator;
125: }
126: return null;
127: }
128:
129: protected void doValidateProperty(Class clazz, String propertyName,
130: Object value, Messages messages) {
131: ClassValidator classValidator = doGetValidator(clazz);
132: if (classValidator != null) {
133: classValidator.validatePropertyValue(propertyName, value,
134: messages);
135: }
136: }
137:
138: protected ClassValidator doGetValidator(Class clazz) {
139: ClassValidator classValidator = (ClassValidator) validationEngine.validatorsMap
140: .get(clazz.getName());
141: if (classValidator == null) {
142: if (validatorSupplier != null) {
143: classValidator = validatorSupplier.getValidator(clazz);
144: if (classValidator != null) {
145: registerValidator(classValidator);
146: }
147: }
148: // still null
149: if (classValidator == null) {
150: try {
151: // force registration of xdoclet generated classes by loading the class
152: String validatorClassName = clazz.getName()
153: + "ClassValidator";
154: Class.forName(validatorClassName);
155: classValidator = (ClassValidator) validationEngine.validatorsMap
156: .get(clazz.getName());
157: } catch (Exception ex) {
158: // do nothing
159: }
160: }
161: if (classValidator == null) {
162: // @todo still null maybe register a Null validator for this class
163: // to reconize no validationEngine available
164: }
165: }
166: return classValidator;
167: }
168:
169: protected ClassValidator doGetValidator(Object object) {
170: return doGetValidator(object.getClass());
171: }
172:
173: protected void doValidate(Object object, Messages messages) {
174: ClassValidator classValidator = doGetValidator(object
175: .getClass());
176: if (classValidator != null) {
177: classValidator.validate(object, messages);
178: }
179: if (object instanceof Validatable) {
180: ((Validatable) object).validate(messages);
181: }
182: }
183:
184: protected void doValidate(Object object) {
185: Messages messages = new MessagesImpl();
186: doValidate(object, messages);
187: if (messages.getNumberOfErrorMessages() > 0) {
188: throw new ValidationException(messages);
189: }
190: }
191:
192: protected void doValidateGroup(Object object, String groupName,
193: Messages messages) {
194: ClassValidator classValidator = doGetValidator(object
195: .getClass());
196: if (classValidator != null) {
197: classValidator.validateGroup(object, groupName, messages);
198: }
199: }
200:
201: protected ValidationEngine() {
202: validatorsMap = new FastHashMap();
203: validatorsMap.setFast(true);
204: }
205:
206: public interface ValidatorSupplier {
207:
208: ClassValidator getValidator(Class clazz);
209:
210: }
211: }
|