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.extract.extractors;
17  
18  import java.util.List;
19  
20  import net.sf.composite.SimpleComposite;
21  import net.sf.composite.extract.ComponentAccessor;
22  import net.sf.composite.extract.ComponentAccessorException;
23  import net.sf.composite.util.Assert;
24  import net.sf.composite.util.ObjectUtils;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /***
30   * A convenient base class for component extractors.  This base class takes care
31   * of exception handling, argument validation and logging.
32   * 
33   * @author Matt Sgarlata
34   * @since Mar 11, 2005
35   */
36  public abstract class BaseComponentAccessor implements ComponentAccessor {
37  
38  	protected final Log log = LogFactory.getLog(getClass());
39  	
40  	/***
41  	 * Implementation of
42  	 * {@link ComponentAccessor#getComponents(Object)}.
43  	 * 
44  	 * @param composite
45  	 *            the composite from which the components will be extracted
46  	 * @return the components
47  	 * @throws Exception
48  	 *             if the components could not be extracted for some reason
49  	 */
50  	protected abstract List getComponentsImpl(Object composite) throws Exception;
51  
52  	public final List getComponents(Object composite) {
53  		Assert.notNull(composite, "composite");
54  		
55  		try {
56  			List components = getComponentsImpl(composite);
57  
58  			if (log.isTraceEnabled()) {
59  				log.trace("Extracted components "
60  					+ ObjectUtils.getObjectDescription(components)
61  					+ " from composite "
62  					+ ObjectUtils.getObjectDescription(composite));
63  			}
64  
65  			return components;
66  		}
67  		catch (ComponentAccessorException e) {
68  			throw e;
69  		}
70  		catch (Exception e) {
71  			throw new ComponentAccessorException(
72  				"Unable to extract components of composite "
73  				+ ObjectUtils.getObjectDescription(composite), e);
74  		}
75  	}
76  
77  	/***
78  	 * Implementation of
79  	 * {@link ComponentAccessor#setComponents(SimpleComposite, Object[])}.
80  	 * 
81  	 * @param composite
82  	 *            the composite for which the components will be set
83  	 * @param components
84  	 *            the components
85  	 * @throws Exception
86  	 *             if the components could not be set for some reason
87  	 */
88  	protected abstract void setComponentsImpl(Object composite, List components) throws Exception;
89  	
90  	public final void setComponents(Object composite, List components) throws ComponentAccessorException {
91  		Assert.notNull(composite, "composite");
92  		
93  		if (log.isTraceEnabled()) {
94  			log.trace("Setting components of composite "
95  				+ ObjectUtils.getObjectDescription(composite) + " to "
96  				+ ObjectUtils.getObjectDescription(components));
97  		}
98  
99  		try {
100 			setComponentsImpl(composite, components);
101 		}
102 		catch (ComponentAccessorException e) {
103 			throw e;
104 		}
105 		catch (Exception e) {
106 			throw new ComponentAccessorException("Unable to set components of composite "
107 				+ ObjectUtils.getObjectDescription(composite) + " to "
108 				+ ObjectUtils.getObjectDescription(components), e);
109 		}
110 	}
111 	
112 }