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.binding.jms.deploy;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.DocumentFragment;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.NamedNodeMap;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.NodeList;
037:
038: import java.util.Collection;
039: import java.util.Iterator;
040: import java.util.LinkedList;
041: import java.util.List;
042: import java.util.Map;
043: import java.util.logging.Logger;
044:
045: import javax.wsdl.Binding;
046: import javax.wsdl.Definition;
047: import javax.wsdl.OperationType;
048: import javax.wsdl.Port;
049: import javax.wsdl.PortType;
050: import javax.wsdl.extensions.UnknownExtensibilityElement;
051: import javax.wsdl.factory.WSDLFactory;
052: import javax.wsdl.xml.WSDLReader;
053:
054: import com.sun.jbi.StringTranslator;
055: import com.sun.jbi.binding.jms.EndpointBean;
056: import com.sun.jbi.binding.jms.JMSBindingContext;
057: import com.sun.jbi.binding.jms.JMSBindingResolver;
058: import com.sun.jbi.binding.jms.JMSBindingResources;
059:
060: import com.sun.jbi.binding.jms.config.ConfigConstants;
061:
062: import javax.xml.namespace.QName;
063:
064: /**
065: * Reads the configuration file file.xml and loads the data. into the
066: * EndpointBean objects.
067: *
068: * @author Sun Microsystems Inc.
069: */
070: public class WSDL11FileReader extends
071: com.sun.jbi.binding.jms.util.UtilBase implements
072: JMSBindingResources {
073: /**
074: * Definitions document.
075: */
076: private Definition mDefinition;
077:
078: /**
079: * Document object of the XML config file.
080: */
081: private Document mDoc;
082:
083: /**
084: * Resolver.
085: */
086:
087: private JMSBindingResolver mResolver;
088:
089: /**
090: * Logger Object.
091: */
092: private Logger mLogger;
093:
094: /**
095: * Translator.
096: */
097:
098: private StringTranslator mStringTranslator;
099:
100: /**
101: * The list of endpoints as configured in the config file. This list
102: * contains all the encpoints and their attibutes.
103: */
104: private EndpointBean[] mEndpointInfoList = null;
105:
106: /**
107: * The total number of end points in the config file.
108: */
109: private int mTotalEndpoints = 0;
110:
111: /**
112: * WSDL file name
113: */
114: private String mWsdlFile;
115:
116: /**
117: * Creates a new WSDL11FileReader object.
118: */
119: public WSDL11FileReader(JMSBindingResolver rslv) {
120: setValid(true);
121: mResolver = rslv;
122: mLogger = JMSBindingContext.getInstance().getLogger();
123: mStringTranslator = JMSBindingContext.getInstance()
124: .getStringTranslator();
125: }
126:
127: /**
128: * Returns the Bean object corresponding to the endpoint.
129: *
130: * @return endpoint Bean object.
131: */
132:
133: public EndpointBean getBean(String endpoint) {
134: /*Search for the bean corresponding to the service and endpoint name
135: */
136: for (int j = 0; j < mEndpointInfoList.length; j++) {
137: String tmp = mEndpointInfoList[j].getUniqueName();
138: if (tmp.trim().equals(endpoint)) {
139: return mEndpointInfoList[j];
140: }
141: }
142: return null;
143: }
144:
145: /**
146: * Gets the endpoint list corresponding to a config file.
147: *
148: * @return End point Bean list
149: */
150:
151: public EndpointBean[] getEndpoint() {
152: return mEndpointInfoList;
153: }
154:
155: /**
156: * Returns the total number of endopoints in the config file.
157: *
158: * @return int number of endpoints.
159: */
160: public int getEndpointCount() {
161: return mTotalEndpoints;
162: }
163:
164: /**
165: * Initializes the config file and loads services.
166: *
167: * @param mDeployFileName Name of the config file.
168: */
169: public void init(String mDeployFileName) {
170: try {
171: mWsdlFile = mDeployFileName;
172: javax.wsdl.factory.WSDLFactory mFactory = WSDLFactory
173: .newInstance();
174: javax.wsdl.xml.WSDLReader mReader = mFactory
175: .newWSDLReader();
176:
177: // parser the deployment file - mDeployFileName (WSDL 1.1) and create
178: // definitions
179: mDefinition = mReader.readWSDL(null, mDeployFileName);
180: } catch (Exception ex) {
181: mLogger.severe("WSDL 1.1 parse exception");
182: ex.printStackTrace();
183: return;
184: }
185:
186: loadServicesList();
187: }
188:
189: /**
190: * Gets the value for an attribute.
191: *
192: * @param map map object.
193: * @param attr attribute.
194: *
195: * @return value.
196: */
197: private String getAttrValue(NamedNodeMap map, String attr) {
198: Node attrnode = map.getNamedItem(attr);
199: String value = null;
200:
201: try {
202: value = attrnode.getNodeValue();
203: } catch (Exception e) {
204: ;
205: }
206:
207: return value;
208: }
209:
210: /**
211: * Sets the con factory.
212: *
213: * @param map map object.
214: * @param eb endpoint bean.
215: */
216: private void setConnectionFactory(NamedNodeMap map, EndpointBean eb) {
217: String val = getValue(map, ConfigConstants.CONNECTION_FACTORY);
218: eb.setValue(ConfigConstants.CONNECTION_FACTORY, val);
219: }
220:
221: /**
222: * Sets the connection password.
223: *
224: * @param map map object.
225: * @param eb endpoint bean.
226: */
227: private void setConnectionPassword(NamedNodeMap map, EndpointBean eb) {
228: String val = getValue(map, ConfigConstants.CONNECTION_PASSWORD);
229: eb.setValue(ConfigConstants.CONNECTION_PASSWORD, val);
230: }
231:
232: /**
233: * Sets connection user name.
234: *
235: * @param map map object.
236: * @param eb endpoint bean.
237: */
238: private void setConnectionUsername(NamedNodeMap map, EndpointBean eb) {
239: String val = getValue(map, ConfigConstants.CONNECTION_USER_ID);
240: mLogger.fine("User ID " + val);
241: eb.setValue(ConfigConstants.CONNECTION_USER_ID, val);
242: }
243:
244: /**
245: * Sets destination name.
246: *
247: * @param map map object.
248: * @param eb endpoint bean.
249: */
250: private void setDestinationName(NamedNodeMap map, EndpointBean eb) {
251: String val = getValue(map, ConfigConstants.DESTINATION_NAME);
252:
253: if ((val == null) || (val.trim().equals(""))) {
254: setError(mStringTranslator
255: .getString(JMS_DESTINATION_NAME_NULL));
256: }
257:
258: eb.setValue(ConfigConstants.DESTINATION_NAME, val);
259: }
260:
261: /**
262: * Sets the destination style.
263: *
264: * @param map map object.
265: * @param eb endpoint bean.
266: */
267: private void setDestinationStyle(NamedNodeMap map, EndpointBean eb) {
268: String val = getValue(map, ConfigConstants.DESTINATION_STYLE);
269: eb.setValue(ConfigConstants.DESTINATION_STYLE, val);
270:
271: if (val.trim().equalsIgnoreCase(ConfigConstants.QUEUE_STRING)) {
272: eb.setStyle(ConfigConstants.QUEUE);
273: } else {
274: eb.setStyle(ConfigConstants.TOPIC);
275: }
276: }
277:
278: /**
279: * Sets durability.
280: *
281: * @param map map object.
282: * @param eb endpoint bean.
283: */
284: private void setDurability(NamedNodeMap map, EndpointBean eb) {
285: String val = getValue(map, ConfigConstants.DURABILITY);
286:
287: if (val == null) {
288: val = ConfigConstants.DEFAULT_DURABILITY;
289: }
290:
291: eb.setValue(ConfigConstants.DURABILITY, val);
292: }
293:
294: /**
295: * Gets the node array.
296: *
297: * @param nodeList node list.
298: * @param name element name.
299: *
300: * @return node array.
301: */
302: private Node[] getElementNodeArrayByName(NodeList nodeList,
303: String name) {
304: Node[] retNode = null;
305: List nodeArr = new LinkedList();
306:
307: //nodeList will have one element for binding. Get all child elements for binding
308: //loop thorugh them to find operation elements
309: NodeList tmpNodeList = nodeList.item(0).getChildNodes();
310:
311: for (int g = 0; g < tmpNodeList.getLength(); g++) {
312: Node n = tmpNodeList.item(g);
313:
314: if ((n != null) && (n.getNodeType() == Node.ELEMENT_NODE)) {
315: if ((n.getLocalName().equals(name))) {
316: nodeArr.add(n);
317: }
318: }
319: }
320:
321: // for loop
322: retNode = new Node[nodeArr.size()];
323:
324: for (int k = 0; k < nodeArr.size(); k++) {
325: retNode[k] = (Node) nodeArr.get(k);
326: }
327:
328: return retNode;
329: }
330:
331: /**
332: * Mehotd to determin if the binding information in the Document fragment
333: * is a file binding or not.
334: *
335: * @param aBinding document fragment.
336: *
337: * @return true if JMS binding.
338: */
339: private boolean isJMSBinding(javax.wsdl.Binding aBinding) {
340: boolean bFlag = false;
341:
342: java.util.List extList = aBinding.getExtensibilityElements();
343: List boper = aBinding.getBindingOperations();
344: if (extList != null) {
345: for (int i = 0; i < extList.size(); i++) {
346: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
347: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
348: .get(i);
349:
350: if (sb.getElementType().getNamespaceURI().equals(
351: ConfigConstants.JMS_NAMESPACE)
352: && sb.getElementType().getLocalPart()
353: .equals("binding")) {
354: bFlag = true;
355: break;
356: }
357: }
358: }
359: }
360:
361: return bFlag;
362: }
363:
364: /**
365: * Sets the message selector.
366: *
367: * @param map map object.
368: * @param eb endpoint bean.
369: */
370: private void setMessageSelector(NamedNodeMap map, EndpointBean eb) {
371: String val = getValue(map, ConfigConstants.MESSAGE_SELECTOR);
372:
373: if (val == null) {
374: val = "";
375: }
376:
377: eb.setValue(ConfigConstants.MESSAGE_SELECTOR, val);
378: }
379:
380: private void validateType(String type) {
381: if ((type.equalsIgnoreCase("TextMessage"))
382: || (type.equalsIgnoreCase("ByteMessage"))
383: || (type.equalsIgnoreCase("StreamMessage"))
384: || (type.equalsIgnoreCase("ObjectMessage"))
385: || (type.equalsIgnoreCase("MapMessage"))
386: || (type.equalsIgnoreCase("Message"))) {
387: return;
388: }
389: setError("Invalid message type specified in Binding operation - Type : "
390: + type);
391:
392: }
393:
394: /**
395: * Sets the operation.
396: *
397: * @param eb endpoint bean.
398: * @param b binding.
399: */
400: private void setOperations(EndpointBean eb, Binding b) {
401: try {
402: List bopers = b.getBindingOperations();
403: PortType bindingif = b.getPortType();
404:
405: Iterator boperiter = null;
406:
407: if (bopers != null) {
408: boperiter = bopers.iterator();
409: }
410:
411: while (boperiter.hasNext()) {
412: javax.wsdl.BindingOperation boper = (javax.wsdl.BindingOperation) boperiter
413: .next();
414: String bindingopername = boper.getOperation().getName();
415: String input = null;
416: String output = null;
417: String mep = null;
418: List inputext = null;
419:
420: if (!boper.getOperation().getStyle().equals(
421: OperationType.NOTIFICATION)) {
422: inputext = boper.getBindingInput()
423: .getExtensibilityElements();
424: input = getMessageType(inputext);
425: if ((input == null) || (input.trim().equals(""))) {
426: input = ConfigConstants.DEFAULT_INPUT_MESSAGE_TYPE;
427: }
428: validateType(input);
429: }
430:
431: if (!boper.getOperation().getStyle().equals(
432: OperationType.ONE_WAY)) {
433: List outputext = boper.getBindingOutput()
434: .getExtensibilityElements();
435: output = getMessageType(outputext);
436: if ((output == null) || (output.trim().equals(""))) {
437: output = ConfigConstants.DEFAULT_OUTPUT_MESSAGE_TYPE;
438: }
439: validateType(output);
440: }
441:
442: mep = resolveMep(boper.getOperation().getStyle());
443: if (isValid()) {
444: eb.addOperation("", bindingopername, mep, input,
445: output);
446: }
447: }
448: } catch (Exception e) {
449: e.printStackTrace();
450: setError("Cannot set operations - " + e.getMessage());
451: }
452: }
453:
454: /**
455: *
456: *
457: * @param opstyle
458: *
459: * @return
460: */
461: private String resolveMep(OperationType opstyle) {
462: String mep = null;
463:
464: if (opstyle.equals(OperationType.ONE_WAY)) {
465: mep = ConfigConstants.IN_ONLY;
466: } else if (opstyle.equals(OperationType.REQUEST_RESPONSE)) {
467: mep = ConfigConstants.IN_OUT;
468: } else if (opstyle.equals(OperationType.NOTIFICATION)) {
469: mep = ConfigConstants.IN_ONLY;
470: } else if (opstyle.equals(OperationType.SOLICIT_RESPONSE)) {
471: mep = ConfigConstants.IN_OUT;
472: } else {
473: setError("Unknown Style of operation " + opstyle);
474: }
475:
476: return mep;
477: }
478:
479: /**
480: * DOCUMENT ME!
481: *
482: * @param inputext
483: *
484: * @return
485: */
486: private String getMessageType(List inputext) {
487: String type = null;
488:
489: try {
490: for (int i = 0; i < inputext.size(); i++) {
491: Object obj = inputext.get(i);
492:
493: if (obj instanceof UnknownExtensibilityElement) {
494: UnknownExtensibilityElement jmselement = (UnknownExtensibilityElement) obj;
495:
496: if (jmselement.getElementType().getNamespaceURI()
497: .equals(ConfigConstants.JMS_NAMESPACE)
498: && jmselement.getElementType()
499: .getLocalPart().equals("message")) {
500: type = getValue(jmselement.getElement()
501: .getAttributes(), "type");
502: }
503: }
504: }
505: } catch (Exception e) {
506: return null;
507: }
508:
509: return type;
510: }
511:
512: /**
513: * Sets the reply to.
514: *
515: * @param map map object.
516: * @param eb endpoint bean.
517: */
518: private void setReplyTo(NamedNodeMap map, EndpointBean eb) {
519: String val = getValue(map, ConfigConstants.REPLY_TO);
520:
521: if (val == null) {
522: val = "";
523: }
524:
525: eb.setValue(ConfigConstants.REPLY_TO, val);
526: }
527:
528: /**
529: * Sets the role.
530: *
531: * @param map map object.
532: * @param eb endpoint bean.
533: */
534: private void setRole(NamedNodeMap map, EndpointBean eb) {
535: String role = getValue(map, ConfigConstants.ENDPOINT_TYPE);
536:
537: if (role == null) {
538: eb.setRole(ConfigConstants.CONSUMER);
539: role = "consumer";
540: } else {
541: if (role.trim().equalsIgnoreCase(
542: ConfigConstants.PROVIDER_STRING)) {
543: eb.setRole(ConfigConstants.PROVIDER);
544: } else if (role.trim().equalsIgnoreCase(
545: ConfigConstants.CONSUMER_STRING)) {
546: eb.setRole(ConfigConstants.CONSUMER);
547: } else {
548: setError("Endpoint should be Provider or Consumer");
549:
550: return;
551: }
552: }
553:
554: eb.setValue(ConfigConstants.ENDPOINT_TYPE, role);
555: }
556:
557: /**
558: * Sets the time to live.
559: *
560: * @param map map object.
561: * @param eb endpoint bean.
562: */
563: private void setTimetoLive(NamedNodeMap map, EndpointBean eb) {
564: String val = getValue(map, ConfigConstants.TIME_TO_LIVE);
565: long l;
566:
567: try {
568: l = Long.parseLong(val);
569: } catch (Exception e) {
570: setError(mStringTranslator.getString(JMS_INVALID_TIMEOUT));
571: e.printStackTrace();
572:
573: return;
574: }
575:
576: eb.setValue(ConfigConstants.TIME_TO_LIVE, val);
577: }
578:
579: /**
580: * Gets value of an attribute form the map.
581: *
582: * @param map map.
583: * @param attr attribute.
584: *
585: * @return attribute value.
586: */
587: private String getValue(NamedNodeMap map, String attr) {
588: Node attrnode = map.getNamedItem(attr);
589: String value = null;
590:
591: try {
592: value = attrnode.getNodeValue();
593: } catch (Exception e) {
594: ;
595: }
596:
597: return value;
598: }
599:
600: /**
601: * Loads the endpoint bean.
602: *
603: * @param ep endpoint reference.
604: * @param eb endpoint bean.
605: */
606: private void loadEndpointBean(Port ep, EndpointBean eb) {
607: List extList = ep.getExtensibilityElements();
608:
609: if (extList != null) {
610: for (int i = 0; i < extList.size(); i++) {
611: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
612: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
613: .get(i);
614:
615: if (sb.getElementType().getNamespaceURI().equals(
616: ConfigConstants.JMS_NAMESPACE)
617: && sb.getElementType().getLocalPart()
618: .equals("artifacts")) {
619: Element epnode = sb.getElement();
620: NamedNodeMap epattributes = epnode
621: .getAttributes();
622: checkRequired(epattributes,
623: ConfigConstants.DESTINATION_NAME);
624: checkRequired(epattributes,
625: ConfigConstants.DESTINATION_STYLE);
626: checkRequired(epattributes,
627: ConfigConstants.CONNECTION_FACTORY);
628: if (!isValid()) {
629: break;
630: }
631: setRole(epattributes, eb);
632: setDestinationName(epattributes, eb);
633: setDestinationStyle(epattributes, eb);
634: setDurability(epattributes, eb);
635: setConnectionFactory(epattributes, eb);
636: setConnectionUsername(epattributes, eb);
637: setConnectionPassword(epattributes, eb);
638: setMessageSelector(epattributes, eb);
639: setTimetoLive(epattributes, eb);
640: setReplyTo(epattributes, eb);
641: }
642: }
643: }
644: }
645:
646: if (!isValid()) {
647: setError("\n\tEndpoint : " + ep.getName());
648: }
649: }
650:
651: /**
652: * Parses the config files and loads them into bean objects.
653: */
654: private void loadServicesList() {
655: java.util.Map servicemap = mDefinition.getServices();
656: List eplist = new LinkedList();
657: Collection services = servicemap.values();
658:
659: Iterator iter = services.iterator();
660:
661: while (iter.hasNext()) {
662: try {
663: javax.wsdl.Service service = (javax.wsdl.Service) iter
664: .next();
665:
666: Map endpointsmap = service.getPorts();
667: Collection endpoints = endpointsmap.values();
668:
669: Iterator epiter = endpoints.iterator();
670:
671: while (epiter.hasNext()) {
672: javax.wsdl.Port ep = (javax.wsdl.Port) epiter
673: .next();
674:
675: javax.wsdl.Binding b = ep.getBinding();
676:
677: if (!isJMSBinding(b)) {
678: continue;
679: }
680:
681: EndpointBean eb = new EndpointBean();
682: eb.setWsdlDefinition(mDefinition);
683: eb.setDeploymentType("WSDL11");
684: loadEndpointBean(ep, eb);
685:
686: eb.setValue(ConfigConstants.SERVICE_NAMESPACE,
687: service.getQName().getNamespaceURI());
688: eb.setValue(ConfigConstants.SERVICENAME, service
689: .getQName().getLocalPart());
690: eb.setValue(ConfigConstants.ENDPOINTNAME, ep
691: .getName());
692: eb.setValue(ConfigConstants.INTERFACE_LOCALNAME, ep
693: .getBinding().getPortType().getQName()
694: .getLocalPart());
695: eb.setValue(ConfigConstants.INTERFACE_NAMESPACE, ep
696: .getBinding().getPortType().getQName()
697: .getNamespaceURI());
698: setOperations(eb, b);
699:
700: if (!isValid()) {
701: setError(("\n\t" + "Service : " + service
702: .getQName().toString()));
703: eplist.clear();
704:
705: return;
706: }
707:
708: eplist.add(eb);
709: mResolver.addEndpointDoc(service.getQName()
710: .toString()
711: + ep.getName(), mWsdlFile);
712: }
713: } catch (Exception e) {
714: setError(mStringTranslator.getString(
715: JMS_ENDPOINT_VALIDATION_ERROR, e.getMessage()));
716: setException(e);
717: e.printStackTrace();
718:
719: return;
720: }
721: }
722:
723: mEndpointInfoList = new EndpointBean[eplist.size()];
724:
725: for (int x = 0; x < eplist.size(); x++) {
726: mEndpointInfoList[x] = (EndpointBean) eplist.get(x);
727: }
728:
729: mTotalEndpoints = eplist.size();
730: }
731:
732: /**
733: * Checks if the mep is valid for the style.
734: *
735: * @param eb endpoint bean.
736: * @param mep mep.
737: *
738: * @return true if valid.
739: */
740: private boolean operationAllowed(EndpointBean eb, String mep) {
741: int style = eb.getStyle();
742: boolean allowed = true;
743:
744: if (style == ConfigConstants.TOPIC) {
745: if (mep.trim().equals(ConfigConstants.IN_OUT)
746: || mep.trim().equals(
747: ConfigConstants.IN_OPTIONAL_OUT)
748: || mep.trim().equals(ConfigConstants.OUT_IN)
749: || mep.trim().equals(
750: ConfigConstants.OUT_OPTIONAL_IN)
751: || mep.trim().equals(
752: ConfigConstants.ROBUST_OUT_ONLY)) {
753: allowed = false;
754: }
755: }
756:
757: return allowed;
758: }
759:
760: /**
761: * Checks the madatory information.
762: *
763: * @param map map object.
764: * @param attr attribute to be checked.
765: */
766: private void checkRequired(NamedNodeMap map, String attr) {
767: Node attrnode = map.getNamedItem(attr);
768: String curvalue;
769: if ((attrnode == null)) {
770: setError(mStringTranslator.getString(
771: JMS_REQUIRED_ATTR_ERROR, attr));
772: return;
773: }
774:
775: try {
776: curvalue = attrnode.getNodeValue().trim();
777:
778: if (curvalue.equals("")) {
779: setError(mStringTranslator.getString(
780: JMS_REQUIRED_ATTR_ERROR, attr));
781: }
782: } catch (Exception e) {
783: setError(mStringTranslator.getString(
784: JMS_REQUIRED_ATTR_ERROR, attr));
785: setException(e);
786: }
787: }
788:
789: /**
790: * DOCUMENT ME!
791: *
792: * @param args
793: */
794: /*
795: public static void main(String [] args)
796: {
797: WSDL11FileReader reader = new WSDL11FileReader();
798: reader.init("endpoints.wsdl");
799: reader.dump();
800:
801: System.out.println("reader.getError() = " + reader.getError());
802: }
803: */
804: }
|