View Javadoc

1   package org.ocltf.common;
2   
3   import java.net.URL;
4   
5   import org.apache.commons.digester.xmlrules.DigesterLoader;
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.ocltf.utils.ExceptionUtils;
9   import org.ocltf.utils.XmlResourceUtils;
10  
11  /***
12   * Creates and returns Objects based on the a set of 
13   * Apache Digester rules.
14   * 
15   * @author Chad Brandon
16   */
17  public class XmlObjectFactory {
18  
19  	private static final Log logger = LogFactory.getLog(XmlObjectFactory.class);
20  	
21  	private static final String RULES_SUFFIX = "-Rules";
22  
23  	/***
24  	 * Returns a new instance of the object specified in 
25  	 * the rulesXml
26  	 * 
27  	 * @param objectClass - the class for the object we are instantiating
28  	 *        (the expected rules will be passed on this objectClass name)
29  	 * @param objectXml - the digester XML that configures the object.
30  	 * @return
31  	 */
32  	public static Object getInstance(Class objectClass, URL objectXml) {
33  		String methodName = "getInstance";
34  		if (logger.isDebugEnabled()) {
35  			logger.debug("performing " + methodName
36  				+ " with objectXml (" + objectXml+ ")");
37  		}
38  		ExceptionUtils.checkNull(methodName, "objectXml", objectXml);
39  		ExceptionUtils.checkNull(methodName, "objectClass", objectClass);
40  		
41  		ClassLoader loader = Thread.currentThread().getContextClassLoader();
42  		
43  		URL objectRulesXml =
44  			XmlResourceUtils.getResource(objectClass.getName() + RULES_SUFFIX);
45  
46  		Object object = null;
47  		try {
48  			object = getObject(objectRulesXml, objectXml, loader);
49  		} catch (Exception ex) {
50  			String errMsg = "Could not load XmlOjbect from configuration file --> '"
51  				+ objectXml + "'";
52  			logger.error(errMsg, ex);
53  			throw new XmlObjectFactoryException(errMsg, ex);	
54  		}
55  		return object;
56  	}
57  
58  	/***
59  	 * Returns a configured Object based on the objectXml configuration file
60  	 * 
61  	 * @param objectRulesXml the path to the XML resource config file that contains
62  	 *        the rules about the Object configuration file
63  	 * @param objectXml the path to the Object XML config file.
64  	 * @param loader the ClassLoader to use with Digester.
65  	 * @return Object the created instance.
66  	 */
67  	private static Object getObject(URL objectRulesXml, URL objectXml, ClassLoader loader) {
68  		String methodName = "getObject";
69  		if (logger.isDebugEnabled()) {
70  			logger.debug("performing "+ methodName
71  				+ " with objectRulesXml ("
72  				+ objectRulesXml + ") and objectXml ("
73  				+ objectXml + ") and loader (" + loader + ")");
74  		}
75  		ExceptionUtils.checkNull(methodName, "objectRulesXml", objectRulesXml);
76  		ExceptionUtils.checkNull(methodName, "objectXml", objectXml);
77  
78  		Object object = null;
79  		try {
80  			object =
81  				(Object) DigesterLoader.load(objectRulesXml, loader, objectXml);
82  		} catch (Exception ex) {
83  			String errMsg = "Error performing " + methodName;
84  			logger.error(errMsg, ex);
85  			throw new XmlObjectFactoryException(errMsg, ex);	
86  		}
87  		return object;
88  	}
89  }