1 25 package org.ofbiz.service.engine; 26 27 import java.lang.reflect.Constructor ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import org.ofbiz.base.config.GenericConfigException; 32 import org.ofbiz.service.GenericServiceException; 33 import org.ofbiz.service.ServiceDispatcher; 34 import org.ofbiz.service.config.ServiceConfigUtil; 35 import org.ofbiz.base.util.UtilXml; 36 import org.w3c.dom.Element ; 37 38 46 public class GenericEngineFactory { 47 48 protected ServiceDispatcher dispatcher = null; 49 protected Map engines = null; 50 51 public GenericEngineFactory(ServiceDispatcher dispatcher) { 52 this.dispatcher = dispatcher; 53 engines = new HashMap (); 54 } 55 56 61 public GenericEngine getGenericEngine(String engineName) throws GenericServiceException { 62 Element rootElement = null; 63 64 try { 65 rootElement = ServiceConfigUtil.getXmlRootElement(); 66 } catch (GenericConfigException e) { 67 throw new GenericServiceException("Error getting Service Engine XML root element", e); 68 } 69 Element engineElement = UtilXml.firstChildElement(rootElement, "engine", "name", engineName); 70 71 if (engineElement == null) { 72 throw new GenericServiceException("Cannot find an engine definition for the engine name [" + engineName + "] in the serviceengine.xml file"); 73 } 74 75 String className = engineElement.getAttribute("class"); 76 77 GenericEngine engine = (GenericEngine) engines.get(engineName); 78 79 if (engine == null) { 80 synchronized (GenericEngineFactory.class) { 81 engine = (GenericEngine) engines.get(engineName); 82 if (engine == null) { 83 Class [] paramTypes = new Class [] { ServiceDispatcher.class }; 84 Object [] params = new Object [] { dispatcher }; 85 86 try { 87 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 88 Class c = loader.loadClass(className); 89 Constructor cn = c.getConstructor(paramTypes); 90 engine = (GenericEngine) cn.newInstance(params); 91 } catch (Exception e) { 92 throw new GenericServiceException(e.getMessage(), e); 93 } 94 if (engine != null) { 95 engines.put(engineName, engine); 96 } 97 } 98 } 99 } 100 101 return engine; 102 } 103 } 104 105 | Popular Tags |