KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > engine > GenericEngineFactory


1 /*
2  * $Id: GenericEngineFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.engine;
26
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
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 JavaDoc;
37
38 /**
39  * Generic Engine Factory
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 2.0
45  */

46 public class GenericEngineFactory {
47
48     protected ServiceDispatcher dispatcher = null;
49     protected Map JavaDoc engines = null;
50     
51     public GenericEngineFactory(ServiceDispatcher dispatcher) {
52         this.dispatcher = dispatcher;
53         engines = new HashMap JavaDoc();
54     }
55
56     /**
57      * Gets the GenericEngine instance that corresponds to given the name
58      *@param engineName Name of the engine
59      *@return GenericEngine that corresponds to the engineName
60      */

61     public GenericEngine getGenericEngine(String JavaDoc engineName) throws GenericServiceException {
62         Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] paramTypes = new Class JavaDoc[] { ServiceDispatcher.class };
84                     Object JavaDoc[] params = new Object JavaDoc[] { dispatcher };
85
86                     try {
87                         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
88                         Class JavaDoc c = loader.loadClass(className);
89                         Constructor JavaDoc cn = c.getConstructor(paramTypes);
90                         engine = (GenericEngine) cn.newInstance(params);
91                     } catch (Exception JavaDoc 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