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$
023: */
024: package com.bostechcorp.cbesb.runtime.cbr;
025:
026: import java.io.File;
027: import java.util.Vector;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.NamedNodeMap;
038: import org.w3c.dom.Node;
039:
040: import com.bostechcorp.cbesb.common.runtime.CbesbException;
041: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
042:
043: public class RoutingRulesHandler {
044: protected final transient Log logger = LogFactory
045: .getLog(getClass());
046:
047: public static final String ROUTINGRULES_NS_URI = "http://cbesb.bostechcorp.com/cbr/routingrules/1.0";
048:
049: public static final String ROUTINGRULES = "routingRules";
050:
051: public static final String ROUTINGRULE = "routingRule";
052:
053: public static final String EXPRESSION = "expression";
054:
055: public static final String TARGET = "target";
056:
057: public static final String INTERFACE = "interface";
058:
059: public static final String SERVICE = "service";
060:
061: public static final String OPERATION = "operation";
062:
063: public static final String TIMEOUT = "timeout";
064:
065: private RoutingRuleElement routingRules[];
066:
067: private int currentIndex;
068:
069: public RoutingRulesHandler() {
070: }
071:
072: public int getCurrentIndex() {
073: return currentIndex;
074: }
075:
076: public void setCurrentIndex(int currentIndex) {
077: this .currentIndex = currentIndex;
078: }
079:
080: public int size() {
081: if (routingRules == null) {
082: return 0;
083: } else {
084: return routingRules.length;
085: }
086: }
087:
088: public RoutingRuleElement getRoutingRuleAtIndex(int index) {
089: RoutingRuleElement re = null;
090:
091: if (routingRules != null) {
092: if (index >= 0 && index < routingRules.length) {
093: re = routingRules[index];
094: }
095: }
096:
097: return re;
098: }
099:
100: public static RoutingRulesHandler load(File sourceFile)
101: throws CbesbException {
102: Log logger = LogFactory.getLog(RoutingRulesHandler.class);
103:
104: Vector<RoutingRuleElement> tmpRules = new Vector<RoutingRuleElement>();
105:
106: try {
107: DocumentBuilderFactory dbf = DocumentBuilderFactory
108: .newInstance();
109: dbf.setNamespaceAware(true);
110: DocumentBuilder db = dbf.newDocumentBuilder();
111: Document doc = db.parse(sourceFile);
112: Element root = doc.getDocumentElement();
113: if (root != null
114: && root.getNamespaceURI().equals(
115: ROUTINGRULES_NS_URI)
116: && root.getLocalName().equals(ROUTINGRULES)) {
117: Node child = root.getFirstChild();
118: while (child != null) {
119: if (child.getNodeType() == Node.ELEMENT_NODE) {
120: if (child.getLocalName().equals(ROUTINGRULE)) {
121: tmpRules.add(loadRoutingRule(doc,
122: (Element) child));
123: }
124: }
125: child = child.getNextSibling();
126: }
127: }
128: } catch (Exception e) {
129:
130: throw new ConfigurationException(
131: "Fail to load the CBR routing rule file '"
132: + sourceFile + "'",
133: "Check the routing rule file", e);
134: }
135:
136: RoutingRulesHandler rr = new RoutingRulesHandler();
137: rr.routingRules = new RoutingRuleElement[tmpRules.size()];
138: for (int i = 0; i < tmpRules.size(); i++) {
139: rr.routingRules[i] = tmpRules.elementAt(i);
140: }
141: tmpRules.clear();
142: return rr;
143: }
144:
145: private static RoutingRuleElement loadRoutingRule(Document doc,
146: Element routingRule) throws Exception {
147:
148: RoutingRuleElement routingRuleElement = new RoutingRuleElement();
149: Vector<TargetElement> tmpTargets = new Vector<TargetElement>();
150:
151: Node child = routingRule.getFirstChild();
152: while (child != null) {
153: if (child.getNodeType() == Node.ELEMENT_NODE) {
154: if (child.getLocalName().equals(EXPRESSION)) {
155: NamedNodeMap attrs = child.getAttributes();
156: String type = attrs.getNamedItem("type")
157: .getNodeValue();
158: routingRuleElement.setExpressionType(type);
159:
160: String value = child.getTextContent();
161: routingRuleElement.setExpression(value);
162: } else if (child.getLocalName().equals(TARGET)) {
163: tmpTargets.add(loadTarget(doc, (Element) child));
164: }
165: }
166: child = child.getNextSibling();
167: }
168:
169: TargetElement[] targetArray = new TargetElement[tmpTargets
170: .size()];
171: for (int i = 0; i < tmpTargets.size(); i++) {
172: targetArray[i] = tmpTargets.elementAt(i);
173: }
174: tmpTargets.clear();
175: routingRuleElement.setTargets(targetArray);
176:
177: return routingRuleElement;
178: }
179:
180: private static TargetElement loadTarget(Document doc, Element target)
181: throws Exception {
182: TargetElement targetElement = new TargetElement();
183: Node child = target.getFirstChild();
184: while (child != null) {
185: if (child.getNodeType() == Node.ELEMENT_NODE) {
186: if (child.getLocalName().equals(INTERFACE)) {
187: String value = child.getTextContent();
188: targetElement.setInterface(getQNameFromString(doc,
189: value));
190: } else if (child.getLocalName().equals(SERVICE)) {
191: String value = child.getTextContent();
192: targetElement.setService(getQNameFromString(doc,
193: value));
194: } else if (child.getLocalName().equals(OPERATION)) {
195: String value = child.getTextContent();
196: targetElement.setOperation(getQNameFromString(doc,
197: value));
198: } else if (child.getLocalName().equals(TIMEOUT)) {
199: String value = child.getTextContent();
200: int timeout = Integer.parseInt(value);
201: targetElement.setTimeout(timeout);
202: }
203:
204: }
205: child = child.getNextSibling();
206: }
207: return targetElement;
208: }
209:
210: private static QName getQNameFromString(Document doc, String value) {
211: String prefix;
212: String uri;
213: String localName;
214: int index = value.indexOf(':');
215: if (index >= 0) {
216: // Namespace Prefix exists, lookup the Namespace URI
217: prefix = value.substring(0, index);
218: localName = value.substring(index + 1);
219: uri = doc.lookupNamespaceURI(prefix);
220: } else {
221: // No Namespace Prefix, look for a default NS
222: uri = doc.lookupNamespaceURI(null);
223: localName = value;
224: }
225: return new QName(uri, localName);
226: }
227:
228: public String toString() {
229: StringBuffer buffer = new StringBuffer();
230: buffer.append("Count = " + size() + "\n");
231: for (int i = 0; i < size(); i++) {
232: buffer.append("RoutingRuleElement " + i + ":\n");
233: buffer.append(routingRules[i].toString());
234: }
235: return buffer.toString();
236: }
237: }
|