001: /*
002: * $Id: GenericEngineFactory.java,v 1.1 2003/08/17 05:12:39 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.engine;
026:
027: import java.lang.reflect.Constructor;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import org.ofbiz.base.config.GenericConfigException;
032: import org.ofbiz.service.GenericServiceException;
033: import org.ofbiz.service.ServiceDispatcher;
034: import org.ofbiz.service.config.ServiceConfigUtil;
035: import org.ofbiz.base.util.UtilXml;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Generic Engine Factory
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.1 $
044: * @since 2.0
045: */
046: public class GenericEngineFactory {
047:
048: protected ServiceDispatcher dispatcher = null;
049: protected Map engines = null;
050:
051: public GenericEngineFactory(ServiceDispatcher dispatcher) {
052: this .dispatcher = dispatcher;
053: engines = new HashMap();
054: }
055:
056: /**
057: * Gets the GenericEngine instance that corresponds to given the name
058: *@param engineName Name of the engine
059: *@return GenericEngine that corresponds to the engineName
060: */
061: public GenericEngine getGenericEngine(String engineName)
062: throws GenericServiceException {
063: Element rootElement = null;
064:
065: try {
066: rootElement = ServiceConfigUtil.getXmlRootElement();
067: } catch (GenericConfigException e) {
068: throw new GenericServiceException(
069: "Error getting Service Engine XML root element", e);
070: }
071: Element engineElement = UtilXml.firstChildElement(rootElement,
072: "engine", "name", engineName);
073:
074: if (engineElement == null) {
075: throw new GenericServiceException(
076: "Cannot find an engine definition for the engine name ["
077: + engineName
078: + "] in the serviceengine.xml file");
079: }
080:
081: String className = engineElement.getAttribute("class");
082:
083: GenericEngine engine = (GenericEngine) engines.get(engineName);
084:
085: if (engine == null) {
086: synchronized (GenericEngineFactory.class) {
087: engine = (GenericEngine) engines.get(engineName);
088: if (engine == null) {
089: Class[] paramTypes = new Class[] { ServiceDispatcher.class };
090: Object[] params = new Object[] { dispatcher };
091:
092: try {
093: ClassLoader loader = Thread.currentThread()
094: .getContextClassLoader();
095: Class c = loader.loadClass(className);
096: Constructor cn = c.getConstructor(paramTypes);
097: engine = (GenericEngine) cn.newInstance(params);
098: } catch (Exception e) {
099: throw new GenericServiceException(e
100: .getMessage(), e);
101: }
102: if (engine != null) {
103: engines.put(engineName, engine);
104: }
105: }
106: }
107: }
108:
109: return engine;
110: }
111: }
|