View Javadoc

1   package org.ocltf.templateobject;
2   
3   import java.net.URL;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Iterator;
7   
8   import org.apache.commons.beanutils.PropertyUtils;
9   import org.apache.commons.lang.StringUtils;
10  import org.apache.commons.lang.builder.ToStringBuilder;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.ocltf.common.ComponentContainer;
14  import org.ocltf.common.Contexts;
15  import org.ocltf.common.Plugin;
16  import org.ocltf.utils.ExceptionUtils;
17  
18  /***
19   * Contains the configuration of a template object which can be made 
20   * available to a template. Template objects are any objects
21   * that are made available to a template.  Anything can be a template
22   * object, as long as it has a default public constructor.
23   * 
24   * @author Chad Brandon
25   */
26  public class TemplateObject {
27  	
28  	private static Log logger = LogFactory.getLog(TemplateObject.class);
29  	
30  	private String name;
31  	private String className;
32      private Plugin owner;
33  	
34  	private Collection propertyReferences = new ArrayList();
35  	
36  	private URL resource;
37  
38  	/***
39  	 * Gets the current name of this TemplateObjectConfig.
40  	 * @return String
41  	 */
42  	public String getName() {
43  		String methodName = "getName";
44  		if (StringUtils.isEmpty(name)) {
45  			throw new TemplateObjectException(methodName 
46  				+ " - templateObject (" 
47  				+ this 
48  				+ ") has no name defined");
49  		}
50  		return name;
51  	}
52  
53  	/***
54  	 * Returns the template object instance.
55  	 * @return TemplateObject
56  	 */
57  	public Object getInstance() {
58  		String methodName = "getInstance";
59  		if (StringUtils.isEmpty(name)) {
60  			throw new TemplateObjectException(methodName 
61  				+ " - templateObject (" 
62  				+ this 
63  				+ ") has no name defined");
64  		}
65  
66  		try {
67              Object templateObject = 
68                  ComponentContainer.instance().findComponent(className);
69              if (templateObject == null) {
70              	templateObject = 
71                      ComponentContainer.instance().registerComponentType(className);
72                  this.setProperties(templateObject);
73              }
74              
75              return templateObject;
76  		} catch (Exception ex) {
77  			String errMsg = "Error performing " + methodName;
78  			logger.error(errMsg, ex);
79  			throw new TemplateObjectException(errMsg, ex);
80  		}
81  	}
82  	
83  	/***
84  	 * Sets all the properties of the templateObject object.
85  	 * 
86  	 * @param templateObject
87  	 */
88  	protected void setProperties(Object templateObject) {
89  		Iterator referenceIt = this.propertyReferences.iterator();
90  		while (referenceIt.hasNext()) {
91  			String reference = (String)referenceIt.next();
92  
93  			Object propertyValue =   
94  				Contexts.instance().lookupContextProperty(
95  						this.getOwner().getName(), reference);
96  			
97  			if (logger.isDebugEnabled()) {
98  				logger.debug("setting property (" 
99  					+ name + ") with value (" + propertyValue 
100 					+ ") on templateObject (" 
101 					+ templateObject + ")");
102 			}
103 			try {
104 				PropertyUtils.setProperty(templateObject, reference, propertyValue);
105 			} catch (Exception ex) {
106 				String errMsg = "Error setting property (" 
107 					+ name + ") with '" + reference 
108 					+ "' on templateObject --> " + templateObject;
109 				logger.error(errMsg, ex);
110 				//don't throw the exception
111 			}
112 
113 		}
114 	}
115 	
116 
117 	/***
118 	 * Sets the name of the transformation object (this
119 	 * name will be what the transformation class is stored
120 	 * under in the template)
121 	 * @param name
122 	 */
123 	public void setName(String name) {
124 		this.name = StringUtils.trimToEmpty(name);
125 	}
126 
127 	/***
128 	 * Sets the class of the transformation object.
129 	 * 
130 	 * @param className 
131 	 */
132 	public void setClassName(String className) {
133 		String methodName = "setTransformationClass";
134 		ExceptionUtils.checkEmpty(methodName, "className", className);
135 		this.className = className;
136 	}
137 	
138 	/***
139 	 * Adds a templateObject property reference (used to customize templateObjects)
140 	 * 
141 	 * @param reference
142 	 */
143 	public void addPropertyReference(String reference) {
144 		this.propertyReferences.add(reference);
145 	}
146     
147     /***
148      * Sets the owner of this TemplateObject.
149      * 
150      * @param plugin
151      */
152     public void setOwner(Plugin owner) {
153     	this.owner = owner;
154     }
155     
156     /***
157      * Returns the owner of this templateObject.
158      * 
159      * @return Plugin the owner of this templateObject
160      */
161     public Plugin getOwner() {
162         String methodName = "getOwner";
163         if (this.owner == null) {
164         	throw new TemplateObjectException(methodName
165                 + " - owner can not be null");
166         }
167         return this.owner;
168     }
169 	
170 	/***
171 	 * The resource in which the templateObject was found.
172 	 * 
173 	 * @return URL
174 	 */
175 	public URL getResource() {
176 		return resource;
177 	}
178 
179 	/***
180 	 * Sets the resource in which the templateObject was found.
181 	 * @param resource
182 	 */
183 	public void setResource(URL resource) {
184 		this.resource = resource;
185 	}
186 
187     /***
188      * @see java.lang.Object#toString()
189      */
190 	public String toString() {
191 		return ToStringBuilder.reflectionToString(this);
192 	}
193 }