001: package org.andromda.core.common;
002:
003: import java.net.URL;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: import org.andromda.core.configuration.Namespaces;
012: import org.apache.commons.lang.StringUtils;
013: import org.apache.commons.lang.builder.ToStringBuilder;
014: import org.apache.log4j.Logger;
015:
016: /**
017: * Contains the configuration of a template object which are objects that are
018: * made available to the cartridge templates.
019: *
020: * @author Chad Brandon
021: */
022: public class TemplateObject {
023: private static Logger logger = Logger
024: .getLogger(TemplateObject.class);
025:
026: /**
027: * The name of this template object made available to the
028: * template.
029: */
030: private String name;
031:
032: /**
033: * Gets the current name of this TemplateObject.
034: *
035: * @return String
036: */
037: public String getName() {
038: final String methodName = "TemplateObject.getName";
039: if (StringUtils.isEmpty(this .name)) {
040: throw new TemplateObjectException(methodName
041: + " - templateObject '" + this
042: + "' has no name defined");
043: }
044: return this .name;
045: }
046:
047: /**
048: * Caches the template objects.
049: */
050: private final Map objectCache = new HashMap();
051:
052: /**
053: * Returns the actuall object instance described by this
054: * template object.
055: *
056: * @return the actual object instance.
057: */
058: public Object getObject() {
059: final String methodName = "TemplateObject.getTemplateObject";
060: if (StringUtils.isEmpty(this .className)) {
061: throw new TemplateObjectException(methodName
062: + " - templateObject '" + this
063: + "' has no className defined");
064: }
065: Object templateObject = this .objectCache.get(this .className);
066: try {
067: if (templateObject == null) {
068: final Class templateObjectClass = ClassUtils
069: .loadClass(this .className);
070: templateObject = templateObjectClass.newInstance();
071: this .objectCache.put(this .className, templateObject);
072: }
073:
074: // - we want to set the properties each time we retrieve (in case they've changed)
075: this .setProperties(templateObject);
076: } catch (final Throwable throwable) {
077: throw new TemplateObjectException(throwable);
078: }
079: return templateObject;
080: }
081:
082: /**
083: * Sets all the nested properties on the templateObject object.
084: *
085: * @param templateObject the template object on which to populate properties.
086: */
087: protected void setProperties(final Object templateObject) {
088: for (final Iterator iterator = propertyReferences.iterator(); iterator
089: .hasNext();) {
090: final String reference = (String) iterator.next();
091: String value = Namespaces.instance().getPropertyValue(
092: this .getNamespace(), reference);
093: if (value != null) {
094: if (this .getLogger().isDebugEnabled()) {
095: this .getLogger().debug(
096: "populating template object '" + this .name
097: + "' property '" + reference
098: + "' with value '" + value
099: + "' for namespace '" + namespace
100: + "'");
101: }
102: try {
103: Introspector.instance().setProperty(templateObject,
104: reference, value);
105: } catch (final Exception exception) {
106: // - don't throw the exception
107: final String message = "Error setting property '"
108: + reference + "' with '" + value
109: + "' on templateObject --> '"
110: + templateObject + "'";
111: logger.warn(message);
112: }
113: }
114: }
115: }
116:
117: /**
118: * Sets the name of the template object (this name will be what the template class is stored under in the template)
119: *
120: * @param name the name of the template object.
121: */
122: public void setName(final String name) {
123: this .name = StringUtils.trimToEmpty(name);
124: }
125:
126: /**
127: * The name of the class for this template object.
128: */
129: private String className;
130:
131: /**
132: * Sets the class of the transformation object.
133: *
134: * @param className the name of the template object class.
135: */
136: public void setClassName(final String className) {
137: ExceptionUtils.checkEmpty("className", className);
138: this .className = className;
139: }
140:
141: /**
142: * The property references that configure this template object.
143: */
144: private final Collection propertyReferences = new ArrayList();
145:
146: /**
147: * Adds a templateObject property reference (used to customize templateObjects). Property references are used to
148: * populate bean like properties of template objects.
149: *
150: * @param reference the name of the property reference.
151: */
152: public void addPropertyReference(final String reference) {
153: this .propertyReferences.add(reference);
154: }
155:
156: /**
157: * The resource in which the template object was found.
158: */
159: private URL resource;
160:
161: /**
162: * The resource in which the templateObject was found.
163: *
164: * @return the resource as a URL.
165: */
166: public URL getResource() {
167: return resource;
168: }
169:
170: /**
171: * Sets the resource in which the templateObject was defined.
172: *
173: * @param resource the resource on which this template object was defined.
174: */
175: public void setResource(final URL resource) {
176: this .resource = resource;
177: }
178:
179: /**
180: * The namespace to which this template object belongs.
181: */
182: private String namespace;
183:
184: /**
185: * Gets the namespace to which this template object belongs.
186: *
187: * @return Returns the namespace.
188: */
189: public String getNamespace() {
190: return namespace;
191: }
192:
193: /**
194: * Sets the namespace to which this template object belongs.
195: *
196: * @param namespace The namespace to set.
197: */
198: public void setNamespace(final String namespace) {
199: this .namespace = StringUtils.trimToEmpty(namespace);
200: }
201:
202: /**
203: * Gets the namespace logger (the logger under which output for this
204: * template object should be written).
205: *
206: * @return the logger instance.
207: */
208: protected Logger getLogger() {
209: return AndroMDALogger.getNamespaceLogger(this .namespace);
210: }
211:
212: /**
213: * @see java.lang.Object#toString()
214: */
215: public String toString() {
216: return ToStringBuilder.reflectionToString(this);
217: }
218: }
|