1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.composite.validate.validators;
17
18 import net.sf.composite.Defaults;
19 import net.sf.composite.extract.ComponentAccessor;
20 import net.sf.composite.util.Assert;
21 import net.sf.composite.util.ObjectUtils;
22 import net.sf.composite.validate.ComponentValidationException;
23 import net.sf.composite.validate.ComponentValidator;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * A convenient base class for component validators. This base class takes care
30 * of exception handling, argument validation and logging.
31 *
32 * @author Matt Sgarlata
33 * @since Mar 11, 2005
34 */
35 public abstract class BaseCompositeValidator implements ComponentValidator {
36
37 protected final Log log = LogFactory.getLog(getClass());
38
39 private ComponentAccessor componentAccessor;
40
41 protected abstract void validateImpl(Object composite)
42 throws Exception;
43
44 public final void validate(Object composite)
45 throws ComponentValidationException {
46 Assert.notNull(composite, "composite");
47
48 try {
49 validateImpl(composite);
50 }
51 catch (ComponentValidationException e) {
52 throw e;
53 }
54 catch (Exception e) {
55 throw new ComponentValidationException(
56 "Unable to determine if composite "
57 + ObjectUtils.getObjectDescription(composite)
58 + " is valid", e);
59 }
60 }
61
62 public ComponentAccessor getComponentAccessor() {
63 if (componentAccessor == null) {
64 setComponentAccessor(Defaults.createComponentAccessor());
65 }
66 return componentAccessor;
67 }
68 public void setComponentAccessor(ComponentAccessor componentAccessor) {
69 this.componentAccessor = componentAccessor;
70 }
71 }