001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SUDescriptorReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt.util;
030:
031: import com.sun.jbi.engine.xslt.TEResources;
032: import com.sun.jbi.engine.xslt.TransformationEngineContext;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038: import org.w3c.dom.Attr;
039:
040: import org.w3c.dom.NamedNodeMap;
041: import java.util.logging.Logger;
042:
043: import javax.xml.parsers.DocumentBuilderFactory;
044: import javax.xml.namespace.QName;
045:
046: import java.util.StringTokenizer;
047:
048: /**
049: *
050: * @author Sun Microsystems Inc.
051: */
052: public class SUDescriptorReader extends UtilBase implements TEResources {
053: /**
054: * Document object of the XML config file.
055: */
056: private Document mDoc;
057:
058: /**
059: * Logger Object
060: */
061: private Logger mLog;
062:
063: /**
064: * i18n
065: */
066: private StringTranslator mTranslator;
067:
068: /**
069: *
070: */
071: private ConfigBean[] mEPList;
072:
073: /**
074: * The total number of end points in the config file
075: */
076: private int mTotalEndpoints = 0;
077:
078: private int mNoOfConsumers;
079:
080: private int mNoOfProviders;
081:
082: /**
083: * Creates a new ConfigReader object.
084: */
085: public SUDescriptorReader() {
086: mLog = TransformationEngineContext.getInstance().getLogger("");
087: mTranslator = new StringTranslator("com.sun.jbi.engine.xslt",
088: this .getClass().getClassLoader());
089: setValid(true);
090: }
091:
092: /**
093: *
094: *
095: * @return NOT YET DOCUMENTED
096: */
097: public int getConsumerCount() {
098: return mNoOfConsumers;
099: }
100:
101: /**
102: *
103: *
104: * @param node NOT YET DOCUMENTED
105: * @param ep NOT YET DOCUMENTED
106: */
107: public void setEndpoint(Node node, ConfigBean ep) {
108: NamedNodeMap map = node.getAttributes();
109:
110: try {
111: String epname = map.getNamedItem("endpoint-name")
112: .getNodeValue();
113: String sername = map.getNamedItem("service-name")
114: .getNodeValue();
115: mLog.finer("LOOK OUT" + epname + " " + sername);
116:
117: ep.setValue(ConfigData.SERVICE_NAMESPACE,
118: getNamespace(sername));
119: ep.setValue(ConfigData.SERVICENAME_LOCALPART,
120: getLocalName(sername));
121: ep.setValue(ConfigData.ENDPOINT, epname);
122: } catch (Exception e) {
123: mLog.severe("Failed loading DD");
124: e.printStackTrace();
125: setError(getError()
126: + mTranslator.getString(TE_LOAD_DD_FAILED) + "\n"
127: + e.getMessage());
128: setValid(false);
129: }
130:
131: }
132:
133: public boolean isPresent(QName sername, String epname) {
134: mLog.finer("Cheking for " + sername + " " + " " + epname);
135: for (int ep = 0; ep < mEPList.length; ep++) {
136: QName sname = new QName((String) mEPList[ep].getValue(
137: ConfigData.SERVICE_NAMESPACE).elementAt(0),
138: (String) mEPList[ep].getValue(
139: ConfigData.SERVICENAME_LOCALPART)
140: .elementAt(0));
141:
142: mLog.finer("Comparing " + sname.toString() + " with "
143: + sername.toString());
144:
145: String epn = (String) mEPList[ep].getValue(
146: ConfigData.ENDPOINT).elementAt(0);
147:
148: mLog.finer("And Comparing " + epn + " with " + epname);
149:
150: if ((sname.toString().equals(sername.toString()))
151: && (epn.equals(epname))) {
152: mLog.finer("Found Service with name: "
153: + sname.toString() + " and endpoint name: "
154: + epname);
155: return true;
156: }
157: }
158: mLog.finer("isPresent returning false. Nothing useful found.");
159: return false;
160: }
161:
162: /**
163: *
164: *
165: * @return NOT YET DOCUMENTED
166: */
167: public int getProviderCount() {
168: return mNoOfProviders;
169: }
170:
171: /**
172: * Initializes the config file and loads services.
173: *
174: * @param doc Name of the config file.
175: */
176: public void init(Document doc) {
177: try {
178: mDoc = doc;
179: mDoc.getDocumentElement().normalize();
180: //check();
181: } catch (Exception genException) {
182: mLog.severe(mTranslator.getString(TE_LOAD_DD_FAILED));
183: genException.printStackTrace();
184: setException(genException);
185: setError(getError()
186: + mTranslator.getString(TE_LOAD_DD_FAILED) + "\n"
187: + genException.getMessage());
188: setValid(false);
189: }
190: }
191:
192: public ConfigBean[] loadServices() {
193: NodeList providers = mDoc.getElementsByTagName("provides");
194: mNoOfProviders = providers.getLength();
195:
196: mEPList = new ConfigBean[mNoOfProviders];
197:
198: for (int i = 0; i < mNoOfProviders; i++) {
199: Node node = providers.item(i);
200: ConfigBean sb = new ConfigBean();
201: setEndpoint(node, sb);
202:
203: if (!isValid()) {
204: setError(mTranslator.getString(TE_LOAD_DD_FAILED)
205: + "\n" + getError());
206:
207: return null;
208: }
209:
210: mEPList[i] = sb;
211: }
212:
213: return mEPList;
214: }
215:
216: /**
217: *
218: */
219: public void check() {
220: try {
221: NodeList providers = mDoc.getElementsByTagName("provides");
222: mNoOfProviders = providers.getLength();
223:
224: NodeList consumers = mDoc.getElementsByTagName("consumes");
225: mNoOfConsumers = consumers.getLength();
226:
227: if (getConsumerCount() != 0) {
228: setError(mTranslator
229: .getString(TE_INCONSISTENT_CONSUMER_DATA));
230: setValid(false);
231: }
232:
233: if (getProviderCount() != 1) {
234: setError(mTranslator
235: .getString(TE_INCONSISTENT_PROVIDER_DATA));
236: setValid(false);
237: }
238: } catch (Exception e) {
239: mLog.severe("Failed loading DD");
240: e.printStackTrace();
241: setError(getError()
242: + mTranslator.getString(TE_LOAD_DD_FAILED) + "\n"
243: + e.getMessage());
244: setValid(false);
245: }
246: }
247:
248: /**
249: * Gets the local name from the quname.
250: *
251: * @param qname Qualified name of service.
252: *
253: * @return String local name
254: */
255: private String getLocalName(String qname) {
256: StringTokenizer tok = new StringTokenizer(qname, ":");
257:
258: try {
259: if (tok.countTokens() == 1) {
260: return qname;
261: }
262:
263: tok.nextToken();
264:
265: return tok.nextToken();
266: } catch (Exception e) {
267: return "";
268: }
269: }
270:
271: /**
272: * Gets the namespace from the qname.
273: *
274: * @param qname Qname of service
275: *
276: * @return namespace namespace of service
277: */
278: private String getNamespace(String qname) {
279: StringTokenizer tok = new StringTokenizer(qname, ":");
280: String prefix = null;
281:
282: try {
283: if (tok.countTokens() == 1) {
284: return "";
285: }
286:
287: prefix = tok.nextToken();
288:
289: NamedNodeMap map = mDoc.getDocumentElement()
290: .getAttributes();
291:
292: for (int j = 0; j < map.getLength(); j++) {
293: Node n = map.item(j);
294:
295: if (n.getLocalName().trim().equals(prefix.trim())) {
296: return n.getNodeValue();
297: }
298: }
299: } catch (Exception e) {
300: ;
301: }
302:
303: return "";
304: }
305:
306: // ********************************* New APIs *********************** //
307:
308: public String getExtensionAttribute(String attrName) {
309: NamedNodeMap attrNodeMap = null;
310: String attrValue = null;
311: try {
312: // Retrieve the "TEServiceExtns" node from the document
313: NodeList nl = mDoc.getElementsByTagNameNS(
314: ConfigData.TE_NAMESPACE, "TEServiceExtns");
315: Node teXtnNode = nl.item(0);
316: attrNodeMap = teXtnNode.getAttributes();
317:
318: for (int i = 0; i < attrNodeMap.getLength(); i++) {
319: if (attrNodeMap.item(i).getLocalName().equals(attrName)) {
320: attrValue = ((Attr) attrNodeMap.item(i)).getValue();
321: break;
322: }
323: }
324:
325: mLog.fine("Extn Attribute:" + attrValue);
326: } catch (Exception e) {
327: mLog.severe("Exception Getting Attribute: "
328: + e.getMessage());
329: e.printStackTrace();
330: } catch (Throwable t) {
331: t.printStackTrace();
332: mLog.severe("Got a throwable thing" + t.getMessage());
333: }
334: return attrValue;
335: }
336:
337: public String getExtnNodeValue(String nodename) {
338: String nodeVal = null;
339: NamedNodeMap nodemap = null;
340:
341: NodeList nl = mDoc.getElementsByTagNameNS(
342: ConfigData.TE_NAMESPACE, "TEServiceExtns");
343: Node teXtnNode = nl.item(0);
344: NodeList chNodes = teXtnNode.getChildNodes();
345:
346: for (int i = 0; i < chNodes.getLength(); i++) {
347: if (chNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
348: if (chNodes.item(i).getLocalName().equals(nodename)) {
349: Element el = (Element) chNodes.item(i);
350: try {
351: nodeVal = ((Node) (el.getChildNodes().item(0)))
352: .getNodeValue().trim();
353: } catch (NullPointerException ne) {
354: ne.printStackTrace();
355: }
356: mLog.fine(nodename + " = " + nodeVal);
357: break;
358: }
359: }
360: }
361:
362: return nodeVal;
363: }
364:
365: /**
366: * DOCUMENT ME!
367: *
368: * @param nodeList - Node List
369: * @param name - element name (need not be an extension element)
370: *
371: * @return Node matching node.
372: */
373: private Node getElementNodeByName(NodeList nodeList, String name) {
374: Node retNode = null;
375:
376: for (int g = 0; g < nodeList.getLength(); g++) {
377: Node n = nodeList.item(g);
378:
379: if ((n != null) && (n.getNodeType() == Node.ELEMENT_NODE)) {
380: mLog.finer("Node:" + n.getNodeName());
381: if ((n.getLocalName().equals(name))) {
382: retNode = n;
383: break;
384: }
385: }
386: }
387: // outer for loop
388: return retNode;
389: }
390:
391: /**
392: * Utility method for setting the Bean object from the XML Nodes.
393: *
394: * @param node The node which needs to be
395: * @param sb The end point bean object which has to be updated
396: * @param tagName the tag name which needs to be read.
397: */
398: private void setByTagName(Node node, ConfigBean sb, String tagName) {
399: Element ele = (Element) node;
400:
401: mLog.finer(" Element: " + ele.toString());
402: mLog.finer(" tagName: " + tagName);
403: NodeList namelist = ele.getElementsByTagNameNS(
404: ConfigData.TE_NAMESPACE, tagName);
405:
406: if (namelist != null) {
407: for (int i = 0; i < namelist.getLength(); i++) {
408: Element name = (Element) namelist.item(i);
409: String sValue = null;
410:
411: try {
412: sValue = ((Node) (name.getChildNodes().item(0)))
413: .getNodeValue().trim();
414: } catch (NullPointerException ne) {
415: ne.printStackTrace();
416:
417: return;
418: }
419:
420: mLog.finer("** " + tagName + " = " + sValue + " **");
421: sb.setValue(tagName, sValue);
422: }
423: } else {
424: mLog.finer("namelist null for tagname " + tagName);
425: }
426: }
427:
428: public void populateServiceInformation(ConfigBean sBean) {
429: String val = getExtnNodeValue(ConfigData.SERVICEID);
430:
431: if (val != null) {
432: mLog.finer("Service Id = " + val);
433: sBean.setValue(ConfigData.SERVICEID, val);
434: } else {
435: mLog.severe("Service Id is NULL.");
436: }
437:
438: val = getExtnNodeValue(ConfigData.CREATE_CACHE);
439:
440: if (val != null) {
441: mLog.finer("Create Cache = " + val);
442: sBean.setValue(ConfigData.CREATE_CACHE, val);
443: } else {
444: mLog.finer("Create Cache is NULL.");
445: }
446:
447: val = getExtnNodeValue(ConfigData.COLUMN_SEPARATOR);
448:
449: if (val != null) {
450: mLog.finer("Column Separator = " + val);
451: sBean.setValue(ConfigData.COLUMN_SEPARATOR, val);
452: } else {
453: mLog.finer("Column Separator is not specified.");
454: }
455:
456: val = getExtnNodeValue(ConfigData.FIRST_ROW_COL_HEADERS);
457:
458: if (val != null) {
459: mLog.finer("First Column Headers? = " + val);
460: sBean.setValue(ConfigData.FIRST_ROW_COL_HEADERS, val);
461: } else {
462: mLog.finer("First Column Header is Not specified..");
463: }
464:
465: populateColumnInfo(sBean);
466: }
467:
468: private void populateColumnInfo(ConfigBean bean) {
469: NodeList nl = mDoc.getElementsByTagNameNS(
470: ConfigData.TE_NAMESPACE, "TEServiceExtns");
471: Node teXtnNode = nl.item(0);
472: NodeList chNodes = teXtnNode.getChildNodes();
473:
474: Node colhs = getElementNodeByName(chNodes,
475: ConfigData.COLUMN_HEADERS);
476:
477: if (colhs != null) {
478: setByTagName(colhs, bean, ConfigData.COLUMN_HEADER);
479: }
480:
481: }
482: }
|