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: * @(#)WSDL11FileReader.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 javax.wsdl.factory.WSDLFactory;
032: import javax.wsdl.xml.WSDLReader;
033:
034: import javax.wsdl.Definition;
035: import javax.wsdl.Service;
036: import javax.wsdl.Port;
037: import javax.wsdl.Binding;
038: import javax.wsdl.PortType;
039: import javax.wsdl.Operation;
040:
041: import java.net.URI;
042: import java.util.LinkedList;
043: import java.util.HashMap;
044: import java.util.logging.Logger;
045: import java.util.StringTokenizer;
046:
047: import javax.xml.namespace.QName;
048:
049: import org.w3c.dom.Document;
050: import org.w3c.dom.Element;
051: import org.w3c.dom.Node;
052: import org.w3c.dom.NodeList;
053: import org.w3c.dom.NamedNodeMap;
054:
055: import javax.xml.parsers.DocumentBuilder;
056: import javax.xml.parsers.DocumentBuilderFactory;
057: import java.io.File;
058:
059: import com.sun.jbi.engine.xslt.TransformationEngineContext;
060:
061: /**
062: * This WSDL artifact handler implementation is used to extract deployment information
063: * from WSDL 1.1 artifacts. It does not validate the artifact file.
064: *
065: * @author Sun Microsystems, Inc.
066: */
067: public class WSDL11FileReader extends UtilBase {
068: /**
069: * Entry point to WSDL API.
070: */
071: private WSDLFactory mFactory;
072:
073: /**
074: * Reads WSDL definitions.
075: */
076: private WSDLReader mReader;
077:
078: /**
079: * WSDL definition object from WSDL1.1 deployment.
080: */
081: private Definition mDefinition;
082:
083: /**
084: * Contains the error message.
085: */
086: private String mErrorMessage;
087:
088: /**
089: * Contains the warning message
090: */
091: private String mWarningMessage;
092:
093: /**
094: * Contains the exception object.
095: */
096: private Exception mException;
097:
098: /**
099: * Internal handle to the logger instance.
100: */
101: private Logger mLogger;
102:
103: /**
104: * Internal handle to the String Translator instance.
105: */
106: private StringTranslator mStringTranslator;
107:
108: /**
109: * Contains the list of endpoints described in the WSDL File.
110: */
111: private ConfigBean[] mEndpointList;
112:
113: /**
114: * Internal handle to the deployment file name.
115: */
116: private String mDeployFileName;
117:
118: /**
119: * Creates a new WSDL11FileReader object.
120: *
121: * @param deployFileName Name of the deployment artifact file
122: */
123: public WSDL11FileReader(String deployFileName) {
124: mLogger = TransformationEngineContext.getInstance().getLogger(
125: "");
126: mStringTranslator = new StringTranslator(
127: "com.sun.jbi.engine.xslt", this .getClass()
128: .getClassLoader());
129: mDeployFileName = deployFileName;
130:
131: try {
132: mFactory = WSDLFactory.newInstance();
133: mReader = mFactory.newWSDLReader();
134:
135: // parser the deployment file - mDeployFileName (WSDL 1.1) and create
136: // definitions
137:
138: mDefinition = mReader.readWSDL(null, mDeployFileName);
139: } catch (Exception ex) {
140: mLogger.severe("Exception while Creating WSDLFactory.");
141: ex.printStackTrace();
142: setValid(false);
143: setError(ex.getMessage());
144: }
145: }
146:
147: public boolean isServiceDefined(QName serviceName) {
148: boolean bRetVal = false;
149:
150: Service service = mDefinition.getService(serviceName);
151: if (service != null) {
152: bRetVal = true;
153: }
154:
155: return bRetVal;
156: }
157:
158: public ConfigBean[] extractEndpoints(QName servName) {
159: java.util.Map servPorts = null;
160: java.util.Iterator portsIter = null;
161: ConfigBean[] mServiceList = null;
162: java.util.List cbList = new LinkedList();
163:
164: Service service = mDefinition.getService(servName);
165: if (service != null) {
166: mLogger.fine("Found Service " + servName.toString());
167: servPorts = service.getPorts();
168: portsIter = servPorts.keySet().iterator();
169: Port aPort = null;
170: Binding pBinding = null;
171: PortType bPType = null;
172: while (portsIter.hasNext()) {
173: aPort = service.getPort((String) portsIter.next());
174: if (aPort != null) {
175: mLogger.fine("Found a port: " + aPort.getName());
176: }
177: pBinding = aPort.getBinding();
178: if (pBinding != null) {
179: mLogger.fine("Found a Binding: "
180: + pBinding.getQName());
181: }
182: bPType = pBinding.getPortType();
183: if (bPType != null) {
184: mLogger.fine("Port Type for Binding: "
185: + pBinding.getQName() + " is :"
186: + bPType.getQName());
187: }
188:
189: ConfigBean cb = new ConfigBean();
190: cb.setValue(ConfigData.SERVICE_NAMESPACE, service
191: .getQName().getNamespaceURI());
192: cb.setValue(ConfigData.TARGET_NAMESPACE, service
193: .getQName().getNamespaceURI());
194: cb.setValue(ConfigData.SERVICENAME_LOCALPART, service
195: .getQName().getLocalPart());
196: cb.setValue(ConfigData.ENDPOINT, aPort.getName());
197:
198: cbList.add(cb);
199:
200: //Register the Dcoument so that getServiceDescription will return it
201: //Need to check the type
202: TransformationEngineContext.getInstance()
203: .addEndpointDoc(
204: service.getQName().toString()
205: + aPort.getName(),
206: mDeployFileName);
207: TransformationEngineContext.getInstance()
208: .addEndpointDef(
209: service.getQName().toString()
210: + aPort.getName(), mDefinition);
211: }
212:
213: mServiceList = new ConfigBean[cbList.size()];
214: for (int x = 0; x < cbList.size(); x++) {
215: mServiceList[x] = (ConfigBean) cbList.get(x);
216: }
217: } else {
218: mLogger.fine("Service Not found in wsdl. "
219: + servName.toString());
220: return null;
221: }
222:
223: return mServiceList;
224: }
225:
226: /* public static void main(String[] args)
227: {
228: System.out.println("WSDL11ArtifactsHandler running....");
229: Document wsdlDocument = null;
230: Document sujbiDocument = null;
231: QName serviceQName = null;
232: Definition wsdlDefs = null;
233: try
234: {
235: System.out.println("Reading WSDL 1.1 SU Deployment file.");
236: DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
237: builderFactory.setNamespaceAware(true);
238: DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
239: sujbiDocument = documentBuilder.parse( new File(args[0]));
240: if(sujbiDocument != null)
241: {
242: System.out.println("Read the WSDL1.1 Deployment file.");
243: System.out.println("");
244: printXMLfromDOM(sujbiDocument.getDocumentElement());
245: System.out.println("");
246:
247: NodeList artifactNodes = sujbiDocument.getElementsByTagNameNS("*", "TEServiceExtns");
248: if ( artifactNodes.getLength() != 1 )
249: {
250: System.out.println("Check the su jbi.xml file for artifacts element");
251: }
252: Node artifactNode = artifactNodes.item(0);
253: NamedNodeMap map = artifactNode.getAttributes();
254:
255: String deployFileName = getAttributeValue( map, "file-name");
256: String deploymentType = getAttributeValue(map,"type");
257:
258: Node serviceNode = null;
259: NodeList serviceNodes = null;
260:
261: serviceNodes = sujbiDocument.getElementsByTagName("provides");
262: if(serviceNodes.getLength() != 1)
263: {
264: System.out.println("No provides element found in su jbi.xml");
265: }
266: else
267: {
268: serviceNode = serviceNodes.item(0);
269: }
270:
271: NamedNodeMap jbiNodeMap = serviceNode.getAttributes();
272: QName interfaceQName = getAttributeQName( sujbiDocument, jbiNodeMap, "interface-name");
273: if ( interfaceQName == null )
274: {
275: System.out.println("Empty Interface name in su jbi");
276: }
277: serviceQName = getAttributeQName (sujbiDocument, jbiNodeMap, "service-name");
278: String endpointName = getAttributeValue (jbiNodeMap, "endpoint-name");
279:
280: System.out.println("Service Name: "+serviceQName.toString());
281: System.out.println("Endpoint Name: "+endpointName);
282: System.out.println("Interface Name: "+interfaceQName.toString());
283:
284: wsdlDefs = mReader.readWSDL(null, args[1]);
285: if(wsdlDefs != null)
286: {
287: System.out.println("Read WSDL Artifact document.");
288: extractEndpoints(wsdlDefs, serviceQName);
289: }
290: }
291:
292: }
293: catch (Exception e)
294: {
295: e.printStackTrace();
296: }
297: }
298:
299:
300: private static String getStyleFor(javax.wsdl.BindingOperation bo)
301: {
302: String sty = null;
303:
304: java.util.List extList = bo.getExtensibilityElements();
305: if(extList != null)
306: {
307: for(int i=0; i<extList.size(); i++)
308: {
309: System.out.println("");
310: System.out.println("BO Ext["+i+"] = "+extList.get(i));
311: System.out.println("");
312: //System.out.println(""+ extList.get(i).getClass().getName());
313: //System.out.println("");
314:
315: if( extList.get(i) instanceof SOAPOperation)
316: {
317: System.out.println("Found a Soap Operation Extension Element. !!!");
318:
319: SOAPOperation sa = (SOAPOperation) extList.get(i);
320:
321: System.out.println("Operation Style : " + sa.getStyle());
322: System.out.println("SOAP Action URI: " + sa.getSoapActionURI());
323: sty = sa.getStyle();
324: //break;
325: }
326: }
327: }
328: return sty;
329: }
330:
331: private static String getStyleFor(javax.wsdl.Binding binding)
332: {
333: String sty = null;
334:
335: java.util.List extList = binding.getExtensibilityElements();
336: if(extList != null)
337: {
338: for(int i=0; i<extList.size(); i++)
339: {
340: System.out.println("");
341: System.out.println("Ext["+i+"] = "+extList.get(i));
342: //System.out.println(""+ extList.get(i).getClass().getName());
343: //System.out.println("");
344:
345: if( extList.get(i) instanceof SOAPBinding)
346: {
347: SOAPBinding sb = (SOAPBinding) extList.get(i);
348: sty = sb.getStyle();
349: break;
350: }
351: }
352: }
353: return sty;
354: }
355:
356: private static boolean isSOAPBinding(javax.wsdl.Binding aBinding)
357: {
358: boolean bFlag = false;
359:
360: java.util.List extList = aBinding.getExtensibilityElements();
361: if(extList != null)
362: {
363: for(int i=0; i<extList.size(); i++)
364: {
365: System.out.println("List["+i+"] = "+extList.get(i));
366: System.out.println("");
367: System.out.println(""+ extList.get(i).getClass().getName());
368:
369: if( extList.get(i) instanceof SOAPBinding)
370: {
371: System.out.println("Found a Soap Binding Extension Element. !!!");
372:
373: SOAPBinding sb = (SOAPBinding) extList.get(i);
374:
375: System.out.println("SOAP Binding Style: " + sb.getStyle());
376: System.out.println("SOAP Binding Transport: " + sb.getTransportURI());
377: bFlag = true;
378: break;
379: }
380: }
381: }
382:
383: return bFlag;
384:
385: }
386: */
387: private static String getAttributeValue(NamedNodeMap map,
388: String attributeName) {
389: Node attributeNode = map.getNamedItem(attributeName);
390: String attributeValue = null;
391: if (attributeNode != null) {
392: attributeValue = attributeNode.getNodeValue();
393: }
394: return attributeValue;
395: }
396:
397: private static QName getAttributeQName(Document document,
398: NamedNodeMap map, String attributeName) {
399: Node attributeNode = map.getNamedItem(attributeName);
400: String attributeValue = null;
401: if (attributeNode != null) {
402: attributeValue = attributeNode.getNodeValue();
403: } else {
404: return null;
405: }
406: String namespacePrefix = null;
407: String localName = null;
408: String namespace = null;
409: StringTokenizer attributeValueTokenizer = new StringTokenizer(
410: attributeValue, ":");
411: if (attributeValueTokenizer.countTokens() == 1) {
412: localName = attributeValue;
413: } else {
414: namespacePrefix = attributeValueTokenizer.nextToken();
415: localName = attributeValueTokenizer.nextToken();
416: }
417: // Find the namespace associate with this namespace prefix if namespace is
418: // not null
419: if (namespacePrefix != null) {
420: NamedNodeMap namespaceMap = document.getDocumentElement()
421: .getAttributes();
422: for (int j = 0; j < namespaceMap.getLength(); j++) {
423: Node namespaceNode = namespaceMap.item(j);
424: if (namespaceNode.getLocalName().trim().equals(
425: namespacePrefix.trim())) {
426: namespace = namespaceNode.getNodeValue();
427: break;
428: }
429: }
430:
431: if (namespace == null) {
432: System.out.println("Namespace null. !!!!");
433: }
434:
435: }
436:
437: if (namespace != null) {
438: return new QName(namespace, localName);
439: } else {
440: return new QName(localName);
441: }
442: }
443:
444: private static void printXMLfromDOM(Element element) {
445: int k;
446: NamedNodeMap attributes;
447: NodeList children = element.getChildNodes();
448: // Start from this element
449: System.out.print("<" + element.getNodeName());
450: // print attibutes inside the element start tag
451: attributes = element.getAttributes();
452: if (attributes != null) {
453: for (k = 0; k < attributes.getLength(); k++) {
454: System.out
455: .print(" " + attributes.item(k).getNodeName());
456: System.out.print("="
457: + attributes.item(k).getNodeValue());
458: }
459: }
460:
461: // check for element value or sub-elements
462: if (element.hasChildNodes()) {
463: System.out.print(">");
464: // print all child elements in the DOM tree
465:
466: for (k = 0; k < children.getLength(); k++) {
467: if (children.item(k).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
468: printXMLfromDOM((Element) children.item(k));
469: } else if (children.item(k).getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
470: System.out.print(children.item(k).getNodeValue());
471: }
472: } // for loop ends here
473: // print end tag
474: System.out.print("</" + element.getNodeName() + ">");
475:
476: } else {
477: // element seems to be empty
478: System.out.print(" />");
479: }// else ends here
480:
481: }// printXMLfromDOM ends here
482:
483: }
|