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: ServiceList.java 6636 2007-04-11 01:12:11Z zjin $
023: */
024:
025: package com.bostechcorp.cbesb.runtime.component.sequencing.processors;
026:
027: import java.io.File;
028: import java.util.Vector;
029:
030: import javax.xml.namespace.QName;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037:
038: import com.bostechcorp.cbesb.common.util.ErrorUtil;
039:
040: public class ServiceList {
041:
042: public static final String SERVICELIST_NS_URI = "http://cbesb.bostechcorp.com/sequencer/servicelist/1.0";
043: public static final String SERVICELIST = "servicelist";
044: public static final String TARGET = "target";
045: public static final String INTERFACE = "interface";
046: public static final String SERVICE = "service";
047: public static final String OPERATION = "operation";
048: public static final String TIMEOUT = "timeout";
049:
050: private ExchangeTarget targets[];
051: private int currentIndex;
052:
053: public ServiceList() {
054:
055: }
056:
057: public int getCurrentIndex() {
058: return currentIndex;
059: }
060:
061: public void setCurrentIndex(int currentIndex) {
062: this .currentIndex = currentIndex;
063: }
064:
065: public int size() {
066: if (targets == null) {
067: return 0;
068: } else {
069: return targets.length;
070: }
071: }
072:
073: public ExchangeTarget getTargetServiceAtIndex(int index) {
074: ExchangeTarget ts = null;
075:
076: if (targets != null) {
077: if (index >= 0 && index < targets.length) {
078: ts = targets[index];
079: }
080: }
081:
082: return ts;
083: }
084:
085: public static ServiceList load(File sourceFile) {
086: Vector<ExchangeTarget> tmpList = new Vector<ExchangeTarget>();
087:
088: try {
089: DocumentBuilderFactory dbf = DocumentBuilderFactory
090: .newInstance();
091: dbf.setNamespaceAware(true);
092: DocumentBuilder db = dbf.newDocumentBuilder();
093: Document doc = db.parse(sourceFile);
094: Element root = doc.getDocumentElement();
095: if (root != null
096: && root.getNamespaceURI()
097: .equals(SERVICELIST_NS_URI)
098: && root.getLocalName().equals(SERVICELIST)) {
099: Node child = root.getFirstChild();
100: while (child != null) {
101: if (child.getNodeType() == Node.ELEMENT_NODE) {
102: if (child.getLocalName().equals(TARGET)) {
103: tmpList
104: .add(loadTarget(doc,
105: (Element) child));
106: }
107: }
108: child = child.getNextSibling();
109: }
110: }
111: } catch (Exception e) {
112:
113: ErrorUtil.printError(
114: "Caught exception while loading servicelist.", e);
115: }
116:
117: ServiceList sl = new ServiceList();
118: sl.targets = new ExchangeTarget[tmpList.size()];
119: for (int i = 0; i < tmpList.size(); i++) {
120: sl.targets[i] = tmpList.elementAt(i);
121: }
122: tmpList.clear();
123: return sl;
124: }
125:
126: private static ExchangeTarget loadTarget(Document doc,
127: Element target) throws Exception {
128: ExchangeTarget exchangeTarget = new ExchangeTarget();
129: Node child = target.getFirstChild();
130: while (child != null) {
131: if (child.getNodeType() == Node.ELEMENT_NODE) {
132: if (child.getLocalName().equals(INTERFACE)) {
133: String value = child.getTextContent();
134: exchangeTarget.setInterface(getQNameFromString(doc,
135: value));
136: } else if (child.getLocalName().equals(SERVICE)) {
137: String value = child.getTextContent();
138: exchangeTarget.setService(getQNameFromString(doc,
139: value));
140: } else if (child.getLocalName().equals(OPERATION)) {
141: String value = child.getTextContent();
142: exchangeTarget.setOperation(getQNameFromString(doc,
143: value));
144: } else if (child.getLocalName().equals(TIMEOUT)) {
145: String value = child.getTextContent();
146: int timeout = Integer.parseInt(value);
147: exchangeTarget.setTimeout(timeout);
148: }
149:
150: }
151: child = child.getNextSibling();
152: }
153: return exchangeTarget;
154: }
155:
156: private static QName getQNameFromString(Document doc, String value) {
157: String prefix;
158: String uri;
159: String localName;
160: int index = value.indexOf(':');
161: if (index >= 0) {
162: //Namespace Prefix exists, lookup the Namespace URI
163: prefix = value.substring(0, index);
164: localName = value.substring(index + 1);
165: uri = doc.lookupNamespaceURI(prefix);
166: } else {
167: //No Namespace Prefix, look for a default NS
168: uri = doc.lookupNamespaceURI(null);
169: localName = value;
170: }
171: return new QName(uri, localName);
172: }
173:
174: public String toString() {
175: StringBuffer buffer = new StringBuffer();
176: buffer.append("Count = " + size() + "\n");
177: for (int i = 0; i < size(); i++) {
178: buffer.append("ExchangeTarget " + i + ":\n");
179: buffer.append(targets[i].toString());
180: }
181: return buffer.toString();
182: }
183: }
|