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: * @(#)WSDLFileReader.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.wsdl2.Binding;
032: import com.sun.jbi.wsdl2.Description;
033: import com.sun.jbi.wsdl2.Document;
034: import com.sun.jbi.wsdl2.Endpoint;
035:
036: import org.w3c.dom.DocumentFragment;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.NamedNodeMap;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041:
042: import java.util.LinkedList;
043: import java.util.List;
044: import java.util.logging.Logger;
045:
046: import com.sun.jbi.engine.xslt.TransformationEngineContext;
047:
048: /**
049: * Reads the configuration file service.wsdl and loads the data into the
050: * ConfigBean objects.
051: *
052: * @author Sun Microsystems, Inc.
053: */
054: public class WSDLFileReader extends UtilBase {
055: /**
056: *
057: */
058: private Description mDefinition;
059:
060: /**
061: * Logger Object
062: */
063: private Logger mLog;
064:
065: /**
066: *
067: */
068: private StringTranslator mStringTranslator;
069:
070: /**
071: * The list of endpoints as configured in the config file. This list
072: * contains all the encpoints and their attibutes
073: */
074: private ConfigBean[] mServiceList = null;
075:
076: /**
077: * The total number of end points in the config file
078: */
079: private int mTotalServices = 0;
080:
081: /**
082: * Creates a new WSDLFileReader object.
083: */
084: public WSDLFileReader() {
085: setValid(true);
086:
087: // mResolver = rslv;
088: mLog = TransformationEngineContext.getInstance().getLogger("");
089: mStringTranslator = new StringTranslator(
090: "com.sun.jbi.engine.xslt", this .getClass()
091: .getClassLoader());
092: }
093:
094: /**
095: * Gets the endpoint list corresponding to a config file.
096: *
097: * @return End point Bean list
098: */
099: public ConfigBean[] getServices() {
100: return mServiceList;
101: }
102:
103: /**
104: * Returns the total number of endopoints in the config file.
105: *
106: * @return int number of endpoints.
107: */
108: public int getServicesCount() {
109: return mTotalServices;
110: }
111:
112: /**
113: * Initializes the config file and loads services.
114: *
115: * @param doc Name of the config file.
116: */
117: public void init(Description doc) {
118: mDefinition = doc;
119: loadServicesList();
120: }
121:
122: /**
123: * DOCUMENT ME!
124: *
125: * @param map NOT YET DOCUMENTED
126: * @param attr NOT YET DOCUMENTED
127: *
128: * @return NOT YET DOCUMENTED
129: */
130: private String getAttrValue(NamedNodeMap map, String attr) {
131: Node attrnode = map.getNamedItem(attr);
132: String value = null;
133:
134: try {
135: value = attrnode.getNodeValue();
136: } catch (Exception e) {
137: ;
138: }
139:
140: return value;
141: }
142:
143: /**
144: * Utility method for setting the Bean object from the XML Nodes.
145: *
146: * @param node The node which needs to be
147: * @param sb The end point bean object which has to be updated
148: * @param tagName the tag name which needs to be read.
149: */
150: private void setByTagName(Node node, ConfigBean sb, String tagName) {
151: Element ele = (Element) node;
152:
153: // mLog.finer(" Element: "+ele.toString());
154: // mLog.finer(" tagName: "+tagName);
155: NodeList namelist = ele.getElementsByTagNameNS(
156: ConfigData.TE_NAMESPACE, tagName);
157:
158: if (namelist != null) {
159: for (int i = 0; i < namelist.getLength(); i++) {
160: Element name = (Element) namelist.item(i);
161: String sValue = null;
162:
163: try {
164: sValue = ((Node) (name.getChildNodes().item(0)))
165: .getNodeValue().trim();
166: } catch (NullPointerException ne) {
167: ne.printStackTrace();
168:
169: return;
170: }
171:
172: mLog.finer("** " + tagName + " = " + sValue + " **");
173: sb.setValue(tagName, sValue);
174: }
175: } else {
176: mLog.finer("namelist null for tagname " + tagName);
177: }
178: }
179:
180: /**
181: * DOCUMENT ME!
182: *
183: * @param nodeList - Node List
184: * @param name - element name (need not be an extension element)
185: *
186: * @return Node matching node.
187: */
188: private Node getElementNodeByName(NodeList nodeList, String name) {
189: Node retNode = null;
190:
191: for (int g = 0; g < nodeList.getLength(); g++) {
192: Node n = nodeList.item(g);
193:
194: if ((n != null) && (n.getNodeType() == Node.ELEMENT_NODE)) {
195: //mLog.finer("Node:"+n.getNodeName());
196: if ((n.getLocalName().equals(name))) {
197: retNode = n;
198:
199: break;
200: }
201: }
202: }
203:
204: // outer for loop
205: return retNode;
206: }
207:
208: /**
209: * Mehotd to determine the binding information in the Document fragment
210: *
211: * @param df document fragment
212: *
213: * @return boolean TRUE/FALSE if the Binding is of type TE
214: */
215: private boolean isTEBinding(DocumentFragment df) {
216: NamedNodeMap n = df.getFirstChild().getAttributes();
217:
218: for (int g = 0; g < n.getLength(); g++) {
219: if (n.item(g).getLocalName().equals("type")) {
220: if (n.item(g).getNodeValue().equals(
221: ConfigData.SERVICE_ENGINE_TYPE)) {
222: return true;
223: }
224: }
225: }
226:
227: return false;
228: }
229:
230: /**
231: * DOCUMENT ME!
232: *
233: * @param map NOT YET DOCUMENTED
234: * @param attr NOT YET DOCUMENTED
235: *
236: * @return NOT YET DOCUMENTED
237: */
238: private String getValue(NamedNodeMap map, String attr) {
239: Node attrnode = map.getNamedItemNS(ConfigData.TE_NAMESPACE,
240: attr);
241: String value = null;
242:
243: try {
244: value = attrnode.getNodeValue();
245: } catch (Exception e) {
246: ;
247: }
248:
249: return value;
250: }
251:
252: /**
253: * DOCUMENT ME!
254: *
255: * @param ep NOT YET DOCUMENTED
256: * @param eb NOT YET DOCUMENTED
257: */
258: private void loadConfigBean(Endpoint ep, ConfigBean eb) {
259: DocumentFragment docfrag = ep.toXmlDocumentFragment();
260:
261: //Endpoint Node.
262: Node epnode = docfrag.getFirstChild();
263: NamedNodeMap epattributes = epnode.getAttributes();
264:
265: Node colh = getElementNodeByName(epnode.getChildNodes(),
266: ConfigData.COLUMN_HEADERS);
267:
268: if (colh != null) {
269: setByTagName(epnode, eb, ConfigData.COLUMN_HEADERS);
270: setByTagName(colh, eb, ConfigData.COLUMN_HEADER);
271: }
272:
273: if (!isValid()) {
274: setError("\n\tEndpoint : " + ep.getName() + getError());
275: }
276: }
277:
278: /**
279: * Parses the config files and loads them into bean objects.
280: */
281: private void loadServicesList() {
282: int services = mDefinition.getServicesLength();
283: List eplist = new LinkedList();
284:
285: //mLog.finer("Found "+services+" Service(s).");
286: for (int i = 0; i < services; i++) {
287: try {
288: com.sun.jbi.wsdl2.Service ser = mDefinition
289: .getService(i);
290:
291: mLog.finer("Service ser= " + ser.toXmlString());
292: int endpoints = ser.getEndpointsLength();
293:
294: for (int k = 0; k < endpoints; k++) {
295: Endpoint ep = ser.getEndpoint(k);
296: Binding b = ep.getBinding();
297:
298: mLog.finer("Binding b= " + b.toXmlString());
299: if (!isTEBinding(b.toXmlDocumentFragment())) {
300: continue;
301: }
302:
303: ConfigBean eb = new ConfigBean();
304: //loadConfigBean(ep, eb);
305: eb.setValue(ConfigData.SERVICE_NAMESPACE, ser
306: .getQName().getNamespaceURI());
307: eb.setValue(ConfigData.TARGET_NAMESPACE, ser
308: .getQName().getNamespaceURI());
309:
310: //eb.setValue(ConfigData.SERVICE_NAME,ser.getQName().getLocalPart());
311: eb.setValue(ConfigData.SERVICENAME_LOCALPART, ser
312: .getQName().getLocalPart());
313: eb.setValue(ConfigData.ENDPOINT, ep.getName());
314:
315: if (!isValid()) {
316: setError(("\n\t" + "Service : "
317: + ser.getQName().toString() + getError()));
318: eplist.clear();
319:
320: return;
321: }
322:
323: eplist.add(eb);
324: TransformationEngineContext.getInstance()
325: .addEndpointDoc(
326: ser.getQName().toString()
327: + ep.getName(),
328: mDefinition.toXmlDocument());
329: }
330: } catch (Exception e) {
331: setError("Error occurred during endpoint validation "
332: + e.getMessage());
333: setException(e);
334: setValid(false);
335:
336: return;
337: }
338: }
339:
340: mServiceList = new ConfigBean[eplist.size()];
341:
342: for (int x = 0; x < eplist.size(); x++) {
343: mServiceList[x] = (ConfigBean) eplist.get(x);
344: }
345:
346: mTotalServices = eplist.size();
347: }
348: }
|