01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.service.engine;
19:
20: import java.lang.reflect.Constructor;
21: import java.util.HashMap;
22: import java.util.Map;
23:
24: import org.ofbiz.base.config.GenericConfigException;
25: import org.ofbiz.service.GenericServiceException;
26: import org.ofbiz.service.ServiceDispatcher;
27: import org.ofbiz.service.config.ServiceConfigUtil;
28: import org.ofbiz.base.util.UtilXml;
29: import org.w3c.dom.Element;
30:
31: /**
32: * Generic Engine Factory
33: */
34: public class GenericEngineFactory {
35:
36: protected ServiceDispatcher dispatcher = null;
37: protected Map engines = null;
38:
39: public GenericEngineFactory(ServiceDispatcher dispatcher) {
40: this .dispatcher = dispatcher;
41: engines = new HashMap();
42: }
43:
44: /**
45: * Gets the GenericEngine instance that corresponds to given the name
46: *@param engineName Name of the engine
47: *@return GenericEngine that corresponds to the engineName
48: */
49: public GenericEngine getGenericEngine(String engineName)
50: throws GenericServiceException {
51: Element rootElement = null;
52:
53: try {
54: rootElement = ServiceConfigUtil.getXmlRootElement();
55: } catch (GenericConfigException e) {
56: throw new GenericServiceException(
57: "Error getting Service Engine XML root element", e);
58: }
59: Element engineElement = UtilXml.firstChildElement(rootElement,
60: "engine", "name", engineName);
61:
62: if (engineElement == null) {
63: throw new GenericServiceException(
64: "Cannot find an engine definition for the engine name ["
65: + engineName
66: + "] in the serviceengine.xml file");
67: }
68:
69: String className = engineElement.getAttribute("class");
70:
71: GenericEngine engine = (GenericEngine) engines.get(engineName);
72:
73: if (engine == null) {
74: synchronized (GenericEngineFactory.class) {
75: engine = (GenericEngine) engines.get(engineName);
76: if (engine == null) {
77: Class[] paramTypes = new Class[] { ServiceDispatcher.class };
78: Object[] params = new Object[] { dispatcher };
79:
80: try {
81: ClassLoader loader = Thread.currentThread()
82: .getContextClassLoader();
83: Class c = loader.loadClass(className);
84: Constructor cn = c.getConstructor(paramTypes);
85: engine = (GenericEngine) cn.newInstance(params);
86: } catch (Exception e) {
87: throw new GenericServiceException(e
88: .getMessage(), e);
89: }
90: if (engine != null) {
91: engines.put(engineName, engine);
92: }
93: }
94: }
95: }
96:
97: return engine;
98: }
99: }
|