View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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  }