001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: CcslConfig.java 11995 2008-02-18 08:39:52Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.util.Hashtable;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031:
032: import javax.xml.namespace.QName;
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035:
036: import org.apache.commons.logging.Log;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041:
042: import com.sun.org.apache.xpath.internal.XPathAPI;
043:
044: public class CcslConfig {
045: private static final String CCSL_CONFIG_FILE_NAME = "META-INF/ccsl.xml";
046:
047: private String installRoot;
048: private String componentClassName;
049: private boolean defaultSaveErrors;
050: private boolean defaultAddRecord;
051: private boolean defaultStripRecord;
052: private boolean useSendMessage;
053: Hashtable<String, EndpointConfig> endpoints = new Hashtable<String, EndpointConfig>();
054: Hashtable<String, LinkedList<EndpointConfig>> suEndpoints = new Hashtable<String, LinkedList<EndpointConfig>>();
055: private Log log;
056:
057: public CcslConfig(Log log, String installRoot) {
058: this .log = log;
059: this .installRoot = installRoot;
060: try {
061: // Read the component level XML file
062: DocumentBuilderFactory factory = DocumentBuilderFactory
063: .newInstance();
064: DocumentBuilder builder = factory.newDocumentBuilder();
065: Document doc = builder.parse(new File(installRoot,
066: CCSL_CONFIG_FILE_NAME));
067:
068: // Get the real component class name
069: Node componentNode = XPathAPI.selectSingleNode(doc,
070: "/ccslComponentConfig/componentClassName");
071: componentClassName = componentNode.getTextContent();
072:
073: // Get the component default settings
074: Element defaultSettingsElement = (Element) XPathAPI
075: .selectSingleNode(doc,
076: "/ccslComponentConfig/defaultSettings");
077: defaultSaveErrors = Boolean
078: .parseBoolean(defaultSettingsElement
079: .getAttribute("saveErrors"));
080: defaultAddRecord = Boolean
081: .parseBoolean(defaultSettingsElement
082: .getAttribute("addRecord"));
083: defaultStripRecord = Boolean
084: .parseBoolean(defaultSettingsElement
085: .getAttribute("stripRecord"));
086: useSendMessage = Boolean
087: .parseBoolean(defaultSettingsElement
088: .getAttribute("useSendMessage"));
089: } catch (Exception e) {
090: log.error("\n\nerror reading component level ccsl.xml "
091: + e.getMessage());
092: if (log.isDebugEnabled()) {
093: log.debug("\n\nerror reading component level ccsl.xml",
094: e);
095: }
096: }
097: }
098:
099: public void loadSuSettings(String suName, String suRoot) {
100: log.debug("suName is [" + suName + "] and suRoot is [" + suRoot
101: + "].");
102: String rootDir = suRoot;
103: LinkedList<EndpointConfig> theseEndpoints = new LinkedList<EndpointConfig>();
104: try {
105: // Read the SU level XML file
106: DocumentBuilderFactory factory = DocumentBuilderFactory
107: .newInstance();
108: DocumentBuilder builder = factory.newDocumentBuilder();
109: Document doc = builder.parse(new File(suRoot,
110: CCSL_CONFIG_FILE_NAME));
111:
112: // load the list of endpoints
113: NodeList endpointNodes = XPathAPI.selectNodeList(doc,
114: "/ccslSuConfig/endpoints/endpoint");
115: for (int nodeIndex = 0; nodeIndex < endpointNodes
116: .getLength(); nodeIndex++) {
117: Element endpointElement = (Element) endpointNodes
118: .item(nodeIndex);
119: // get the endpoint level configuration
120: EndpointConfig endpoint = new EndpointConfig();
121: endpoint.setSuRoot(suRoot);
122: QName endpointService = new QName(endpointElement
123: .getAttribute("serviceNS"), endpointElement
124: .getAttribute("serviceLocal"));
125: String endpointName = endpointElement
126: .getAttribute("name");
127: String endpointKey = endpointService + ":"
128: + endpointName;
129: log.debug("found endpointkey: " + endpointKey);
130: endpoint.setSaveErrors(endpointElement
131: .getAttribute("saveErrors"), defaultSaveErrors);
132: endpoint.setAddRecord(endpointElement
133: .getAttribute("addRecord"), defaultAddRecord);
134: log.debug("addRecord is "
135: + endpointElement.getAttribute("addRecord"));
136: endpoint.setStripRecord(endpointElement
137: .getAttribute("stripRecord"),
138: defaultStripRecord);
139: endpoint
140: .setSendMessage(endpointElement
141: .getAttribute("useSendMessage"),
142: useSendMessage);
143: log.debug("useSendMessage is "
144: + endpointElement
145: .getAttribute("useSendMessage"));
146:
147: // get the UPOCs
148: NodeList upocNodes = XPathAPI.selectNodeList(
149: endpointElement, "upoc");
150: for (int upocIndex = 0; upocIndex < upocNodes
151: .getLength(); upocIndex++) {
152: Element upocElement = (Element) upocNodes
153: .item(upocIndex);
154: UpocConfig upoc = new UpocConfig();
155: String context = upocElement
156: .getAttribute("context");
157: if (context.equals("start"))
158: endpoint.setNeedToRunStart(true);
159: else if (context.equals("stop"))
160: endpoint.setNeedToRunStop(true);
161: String[] result = CcslUtil.getRootScriptPath(
162: suRoot, upocElement.getAttribute("class"));
163: String scriptRootDir = result[0];
164: String className = result[1];
165: upoc.setType(upocElement.getAttribute("type"));
166: upoc.setClassName(className);
167: upoc.setMethod(upocElement.getAttribute("method"));
168: String rootAttribute = upocElement
169: .getAttribute("root");
170: // if (rootAttribute != null && rootAttribute.length() > 0) rootDir = rootAttribute;
171: // else rootDir = scriptRootDir;
172: upoc.setRootDir(scriptRootDir);
173: endpoint.putUpoc(context, upoc);
174: }
175: log.debug("endpoints put -> key = " + endpointKey);
176: endpoints.put(endpointKey, endpoint);
177: theseEndpoints.add(endpoint);
178: }
179: } catch (Exception e) {
180: // The SU level ccsl.xml is not required so we can ignore file not found
181: if (!(e instanceof FileNotFoundException)) {
182: log.error("\n\nerror reading component level ccsl.xml "
183: + e.getMessage());
184: if (log.isDebugEnabled()) {
185: log
186: .debug(
187: "\n\nerror reading component level ccsl.xml",
188: e);
189: }
190: }
191: }
192: suEndpoints.put(suName, theseEndpoints);
193: }
194:
195: public String getComponentClassName() {
196: return componentClassName;
197: }
198:
199: public EndpointConfig getEndpointConfig(String endpointKey) {
200: return (EndpointConfig) endpoints.get(endpointKey);
201: }
202:
203: public String getInstallRoot() {
204: return installRoot;
205: }
206:
207: public boolean getDefaultSaveErrors() {
208: return defaultSaveErrors;
209: }
210:
211: public boolean getDafaultAddRecord() {
212: return defaultAddRecord;
213: }
214:
215: public boolean getDefaultStripRecord() {
216: return defaultStripRecord;
217: }
218:
219: public boolean getUseSendMessage() {
220: return useSendMessage;
221: }
222:
223: public Log getLog() {
224: return log;
225: }
226:
227: public void stopSuUpocs(String suName) {
228: LinkedList<EndpointConfig> endpoints = suEndpoints.get(suName);
229: if (endpoints == null)
230: return;
231: for (Iterator<EndpointConfig> i = endpoints.iterator(); i
232: .hasNext();) {
233: EndpointConfig endpoint = i.next();
234: if (endpoint.getNeedToRunStop()) {
235: UpocConfig stopUpoc = endpoint.getUpoc("stop");
236: if (stopUpoc != null) {
237: try {
238: CcslUpoc.runScript(log, stopUpoc.getRootDir(),
239: "stop", stopUpoc.getType(), stopUpoc
240: .getClassName(), stopUpoc
241: .getMethod(), null, null, null,
242: null, this );
243: } catch (Exception e) {
244: log.error("stop UPOC error:" + e.getMessage());
245: if (log.isDebugEnabled()) {
246: log.debug("stop UPOC error:", e);
247: }
248: }
249: endpoint.setNeedToRunStart(true);
250: endpoint.setNeedToRunStop(false);
251: }
252:
253: }
254: }
255: }
256: }
|