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.binding.jms.deploy;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.JMSBindingContext;
035: import com.sun.jbi.binding.jms.JMSBindingResolver;
036: import com.sun.jbi.binding.jms.JMSBindingResources;
037:
038: import com.sun.jbi.binding.jms.config.ConfigConstants;
039: import com.sun.jbi.wsdl2.Binding;
040: import com.sun.jbi.wsdl2.BindingOperation;
041: import com.sun.jbi.wsdl2.Description;
042:
043: import com.sun.jbi.wsdl2.Endpoint;
044: import com.sun.jbi.wsdl2.Interface;
045: import com.sun.jbi.wsdl2.InterfaceOperation;
046:
047: import java.util.LinkedList;
048: import java.util.List;
049:
050: import java.util.logging.Logger;
051:
052: import javax.xml.namespace.QName;
053:
054: import org.w3c.dom.Document;
055: import org.w3c.dom.DocumentFragment;
056: import org.w3c.dom.NamedNodeMap;
057: import org.w3c.dom.Node;
058: import org.w3c.dom.NodeList;
059:
060: /**
061: * Reads the configuration file file.xml and loads the data. into the
062: * EndpointBean objects.
063: *
064: * @author Sun Microsystems Inc.
065: */
066: public class WSDLFileReader extends
067: com.sun.jbi.binding.jms.util.UtilBase implements
068: JMSBindingResources {
069: /**
070: * Description document.
071: */
072: private Description mDefinition;
073:
074: /**
075: * Document object of the XML config file.
076: */
077: private Document mDoc;
078: /**
079: * Resolver.
080: */
081: private JMSBindingResolver mResolver;
082: /**
083: * Logger Object.
084: */
085: private Logger mLogger;
086: /**
087: * Translator.
088: */
089: private StringTranslator mStringTranslator;
090: /**
091: * The list of endpoints as configured in the config file. This list
092: * contains all the encpoints and their attibutes.
093: */
094: private EndpointBean[] mEndpointInfoList = null;
095:
096: /**
097: * The total number of end points in the config file.
098: */
099: private int mTotalEndpoints = 0;
100:
101: /**
102: * Creates a new WSDLFileReader object.
103: *
104: * @param rslv resolver object
105: */
106: public WSDLFileReader(JMSBindingResolver rslv) {
107: setValid(true);
108: mResolver = rslv;
109: mLogger = JMSBindingContext.getInstance().getLogger();
110: mStringTranslator = JMSBindingContext.getInstance()
111: .getStringTranslator();
112: }
113:
114: /**
115: * Returns the Bean object corresponding to the endpoint.
116: *
117: * @param endpoint endpoint name.
118: *
119: * @return endpoint Bean object.
120: */
121: public EndpointBean getBean(String endpoint) {
122: /*Search for the bean corresponding to the service and endpoint name
123: */
124: for (int j = 0; j < mEndpointInfoList.length; j++) {
125: String tmp = mEndpointInfoList[j].getUniqueName();
126:
127: if (tmp.trim().equals(endpoint)) {
128: return mEndpointInfoList[j];
129: }
130: }
131:
132: return null;
133: }
134:
135: /**
136: * Gets the endpoint list corresponding to a config file.
137: *
138: * @return End point Bean list
139: */
140: public EndpointBean[] getEndpoint() {
141: return mEndpointInfoList;
142: }
143:
144: /**
145: * Returns the total number of endopoints in the config file.
146: *
147: * @return int number of endpoints.
148: */
149: public int getEndpointCount() {
150: return mTotalEndpoints;
151: }
152:
153: /**
154: * Initializes the config file and loads services.
155: *
156: * @param doc Name of the config file.
157: */
158: public void init(Description doc) {
159: mDefinition = doc;
160: loadServicesList();
161: }
162:
163: /**
164: * Gets the value for an attribute.
165: *
166: * @param map map object.
167: * @param attr attribute.
168: *
169: * @return value.
170: */
171: private String getAttrValue(NamedNodeMap map, String attr) {
172: Node attrnode = map.getNamedItem(attr);
173: String value = null;
174:
175: try {
176: value = attrnode.getNodeValue();
177: } catch (Exception e) {
178: ;
179: }
180:
181: return value;
182: }
183:
184: /**
185: * Sets the con factory.
186: *
187: * @param map map object.
188: * @param eb endpoint bean.
189: */
190: private void setConnectionFactory(NamedNodeMap map, EndpointBean eb) {
191: String val = getValue(map, ConfigConstants.CONNECTION_FACTORY);
192: eb.setValue(ConfigConstants.CONNECTION_FACTORY, val);
193: }
194:
195: /**
196: * Sets the connection password.
197: *
198: * @param map map object.
199: * @param eb endpoint bean.
200: */
201: private void setConnectionPassword(NamedNodeMap map, EndpointBean eb) {
202: String val = getValue(map, ConfigConstants.CONNECTION_PASSWORD);
203: eb.setValue(ConfigConstants.CONNECTION_PASSWORD, val);
204: }
205:
206: /**
207: * Sets connection user name.
208: *
209: * @param map map object.
210: * @param eb endpoint bean.
211: */
212: private void setConnectionUsername(NamedNodeMap map, EndpointBean eb) {
213: String val = getValue(map, ConfigConstants.CONNECTION_USER_ID);
214: mLogger.fine("User ID " + val);
215: eb.setValue(ConfigConstants.CONNECTION_USER_ID, val);
216: }
217:
218: /**
219: * Sets destination name.
220: *
221: * @param map map object.
222: * @param eb endpoint bean.
223: */
224: private void setDestinationName(NamedNodeMap map, EndpointBean eb) {
225: String val = getValue(map, ConfigConstants.DESTINATION_NAME);
226:
227: if ((val == null) || (val.trim().equals(""))) {
228: setError(mStringTranslator
229: .getString(JMS_DESTINATION_NAME_NULL));
230: }
231:
232: eb.setValue(ConfigConstants.DESTINATION_NAME, val);
233: }
234:
235: /**
236: * Sets the destination style.
237: *
238: * @param map map object.
239: * @param eb endpoint bean.
240: */
241: private void setDestinationStyle(NamedNodeMap map, EndpointBean eb) {
242: String val = getValue(map, ConfigConstants.DESTINATION_STYLE);
243: eb.setValue(ConfigConstants.DESTINATION_STYLE, val);
244:
245: if (val.trim().equalsIgnoreCase(ConfigConstants.QUEUE_STRING)) {
246: eb.setStyle(ConfigConstants.QUEUE);
247: } else {
248: eb.setStyle(ConfigConstants.TOPIC);
249: }
250: }
251:
252: /**
253: * Sets durability.
254: *
255: * @param map map object.
256: * @param eb endpoint bean.
257: */
258: private void setDurability(NamedNodeMap map, EndpointBean eb) {
259: String val = getValue(map, ConfigConstants.DURABILITY);
260:
261: if (val == null) {
262: val = ConfigConstants.DEFAULT_DURABILITY;
263: }
264:
265: eb.setValue(ConfigConstants.DURABILITY, val);
266: }
267:
268: /**
269: * Gets the node array.
270: *
271: * @param nodeList node list.
272: * @param name element name.
273: *
274: * @return node array.
275: */
276: private Node[] getElementNodeArrayByName(NodeList nodeList,
277: String name) {
278: Node[] retNode = null;
279: List nodeArr = new LinkedList();
280:
281: //nodeList will have one element for binding. Get all child elements for binding
282: //loop thorugh them to find operation elements
283: NodeList tmpNodeList = nodeList.item(0).getChildNodes();
284:
285: for (int g = 0; g < tmpNodeList.getLength(); g++) {
286: Node n = tmpNodeList.item(g);
287:
288: if ((n != null) && (n.getNodeType() == Node.ELEMENT_NODE)) {
289: if ((n.getLocalName().equals(name))) {
290: nodeArr.add(n);
291: }
292: }
293: }
294:
295: // for loop
296: retNode = new Node[nodeArr.size()];
297:
298: for (int k = 0; k < nodeArr.size(); k++) {
299: retNode[k] = (Node) nodeArr.get(k);
300: }
301:
302: return retNode;
303: }
304:
305: /**
306: * Mehotd to determin if the binding information in the Document fragment
307: * is a file binding or not.
308: *
309: * @param df document fragment.
310: *
311: * @return true if JMS binding.
312: */
313: private boolean isJMSBinding(DocumentFragment df) {
314: NamedNodeMap n = df.getFirstChild().getAttributes();
315:
316: /* can replace below code once the "types" attribute of binding
317: *is in place
318: */
319: for (int g = 0; g < n.getLength(); g++) {
320: if (n.item(g).getLocalName().equals("type")) {
321: if (n.item(g).getNodeValue().equals(
322: ConfigConstants.JMS_NAMESPACE)) {
323: return true;
324: }
325: }
326: }
327:
328: return false;
329: }
330:
331: /**
332: * Sets the message selector.
333: *
334: * @param map map object.
335: * @param eb endpoint bean.
336: */
337: private void setMessageSelector(NamedNodeMap map, EndpointBean eb) {
338: String val = getValue(map, ConfigConstants.MESSAGE_SELECTOR);
339:
340: if (val == null) {
341: val = "";
342: }
343:
344: eb.setValue(ConfigConstants.MESSAGE_SELECTOR, val);
345: }
346:
347: /**
348: * Sets the operation.
349: *
350: * @param eb endpoint bean.
351: * @param b binding.
352: */
353: private void setOperations(EndpointBean eb, Binding b) {
354: Interface bindingif = b.getInterface();
355: Node[] allOpNodes = getElementNodeArrayByName(b
356: .toXmlDocumentFragment().getChildNodes(), "operation");
357:
358: for (int oper = 0; oper < b.getOperationsLength(); oper++) {
359: BindingOperation boper = b.getOperation(oper);
360: QName bindingopername = boper.getInterfaceOperation();
361:
362: String input = null;
363: String output = null;
364: String mep = null;
365:
366: for (int k = 0; k < allOpNodes.length; k++) {
367: NamedNodeMap nm = allOpNodes[k].getAttributes();
368:
369: if (nm != null) {
370: String val = getAttrValue(nm, "ref");
371:
372: if ((val != null)
373: && val.equals(bindingopername
374: .getLocalPart().trim())) {
375: input = getValue(nm,
376: ConfigConstants.INPUT_MESSAGE_TYPE);
377: output = getValue(nm,
378: ConfigConstants.OUTPUT_MESSAGE_TYPE);
379:
380: break;
381: }
382: }
383: }
384:
385: if ((input == null) || (input.trim().equals(""))) {
386: input = ConfigConstants.DEFAULT_INPUT_MESSAGE_TYPE;
387: }
388:
389: if ((output == null) || (output.trim().equals(""))) {
390: output = ConfigConstants.DEFAULT_OUTPUT_MESSAGE_TYPE;
391: }
392:
393: int ifoperlength = bindingif.getOperationsLength();
394:
395: for (int j = 0; j < ifoperlength; j++) {
396: InterfaceOperation op = bindingif.getOperation(j);
397: QName ifopername = op.getQualifiedName();
398:
399: if (bindingopername.getLocalPart().trim().equals(
400: ifopername.getLocalPart().trim())) {
401: mep = op.getPattern();
402: checkMEP(mep);
403: if (mep != null) {
404: if (operationAllowed(eb, mep)) {
405: eb
406: .addOperation(ifopername
407: .getNamespaceURI(),
408: ifopername.getLocalPart(),
409: mep, input, output);
410: } else {
411: mLogger
412: .info("operation inconsistent with style");
413: setWarning("Operation inconsistent with Destination style"
414: + ifopername.toString()
415: + " MEP "
416: + mep);
417: }
418: }
419: }
420: }
421: }
422: }
423:
424: /**
425: * Sets the reply to.
426: *
427: * @param map map object.
428: * @param eb endpoint bean.
429: */
430: private void setReplyTo(NamedNodeMap map, EndpointBean eb) {
431: String val = getValue(map, ConfigConstants.REPLY_TO);
432:
433: if (val == null) {
434: val = "";
435: }
436:
437: eb.setValue(ConfigConstants.REPLY_TO, val);
438: }
439:
440: /**
441: * Sets the role.
442: *
443: * @param map map object.
444: * @param eb endpoint bean.
445: */
446: private void setRole(NamedNodeMap map, EndpointBean eb) {
447: String role = getValue(map, ConfigConstants.ENDPOINT_TYPE);
448:
449: if (role == null) {
450: eb.setRole(ConfigConstants.CONSUMER);
451: } else {
452: if (role.trim().equalsIgnoreCase(
453: ConfigConstants.PROVIDER_STRING)) {
454: eb.setRole(ConfigConstants.PROVIDER);
455: } else if (role.trim().equalsIgnoreCase(
456: ConfigConstants.CONSUMER_STRING)) {
457: eb.setRole(ConfigConstants.CONSUMER);
458: } else {
459: setError("Endpoint should be Provider or Consumer");
460:
461: return;
462: }
463: }
464: eb.setValue(ConfigConstants.ENDPOINT_TYPE, role);
465: }
466:
467: /**
468: * Sets the time to live.
469: *
470: * @param map map object.
471: * @param eb endpoint bean.
472: */
473: private void setTimetoLive(NamedNodeMap map, EndpointBean eb) {
474: String val = getValue(map, ConfigConstants.TIME_TO_LIVE);
475: long l;
476:
477: try {
478: l = Long.parseLong(val);
479: } catch (Exception e) {
480: setError(mStringTranslator.getString(JMS_INVALID_TIMEOUT));
481:
482: return;
483: }
484:
485: eb.setValue(ConfigConstants.TIME_TO_LIVE, val);
486: }
487:
488: /**
489: * Gets value of an attribute form the map.
490: *
491: * @param map map.
492: * @param attr attribute.
493: *
494: * @return attribute value.
495: */
496: private String getValue(NamedNodeMap map, String attr) {
497: Node attrnode = map.getNamedItemNS(
498: ConfigConstants.JMS_NAMESPACE, attr);
499: String value = null;
500:
501: try {
502: value = attrnode.getNodeValue();
503: } catch (Exception e) {
504: ;
505: }
506:
507: return value;
508: }
509:
510: /**
511: * Loads the endpoint bean.
512: *
513: * @param ep endpoint reference.
514: * @param eb endpoint bean.
515: */
516: private void loadEndpointBean(Endpoint ep, EndpointBean eb) {
517: DocumentFragment docfrag = ep.toXmlDocumentFragment();
518: Node epnode = docfrag.getFirstChild();
519: NamedNodeMap epattributes = epnode.getAttributes();
520: setRole(epattributes, eb);
521: setDestinationName(epattributes, eb);
522: setDestinationStyle(epattributes, eb);
523: setDurability(epattributes, eb);
524: setConnectionFactory(epattributes, eb);
525: setConnectionUsername(epattributes, eb);
526: setConnectionPassword(epattributes, eb);
527: setMessageSelector(epattributes, eb);
528: setTimetoLive(epattributes, eb);
529: setReplyTo(epattributes, eb);
530:
531: if (!isValid()) {
532: setError("\n\tEndpoint : " + ep.getName() + getError());
533: }
534: }
535:
536: /**
537: * Parses the config files and loads them into bean objects.
538: */
539: private void loadServicesList() {
540: int services = mDefinition.getServicesLength();
541: List eplist = new LinkedList();
542:
543: for (int i = 0; i < services; i++) {
544: try {
545: com.sun.jbi.wsdl2.Service ser = mDefinition
546: .getService(i);
547: int endpoints = ser.getEndpointsLength();
548:
549: for (int k = 0; k < endpoints; k++) {
550: Endpoint ep = ser.getEndpoint(k);
551: Binding b = ep.getBinding();
552:
553: if (!isJMSBinding(b.toXmlDocumentFragment())) {
554: continue;
555: }
556:
557: EndpointBean eb = new EndpointBean();
558: eb.setWsdlDefinition(mDefinition);
559: eb.setDeploymentType("WSDL20");
560:
561: loadEndpointBean(ep, eb);
562:
563: eb.setValue(ConfigConstants.SERVICE_NAMESPACE, ser
564: .getQName().getNamespaceURI());
565: eb.setValue(ConfigConstants.SERVICENAME, ser
566: .getQName().getLocalPart());
567: eb.setValue(ConfigConstants.ENDPOINTNAME, ep
568: .getName());
569: eb.setValue(ConfigConstants.INTERFACE_LOCALNAME, ep
570: .getBinding().getInterface().getQName()
571: .getLocalPart());
572: eb.setValue(ConfigConstants.INTERFACE_NAMESPACE, ep
573: .getBinding().getInterface().getQName()
574: .getNamespaceURI());
575: setOperations(eb, b);
576: if (!isValid()) {
577: setError(("\n\t" + "Service : " + ser
578: .getQName().toString()));
579: eplist.clear();
580:
581: return;
582: }
583:
584: eplist.add(eb);
585: mResolver
586: .addEndpointDoc(ser.getQName().toString()
587: + ep.getName(), mDefinition
588: .toXmlDocument());
589: }
590: } catch (Throwable e) {
591: setError(mStringTranslator.getString(
592: JMS_ENDPOINT_VALIDATION_ERROR, e.getMessage()));
593:
594: //setException(e);
595: e.printStackTrace();
596:
597: return;
598: }
599: }
600:
601: mEndpointInfoList = new EndpointBean[eplist.size()];
602:
603: for (int x = 0; x < eplist.size(); x++) {
604: mEndpointInfoList[x] = (EndpointBean) eplist.get(x);
605: }
606:
607: mTotalEndpoints = eplist.size();
608: }
609:
610: /**
611: * Checks if the mep is valid for the style.
612: *
613: * @param eb endpoint bean.
614: * @param mep mep.
615: *
616: * @return true if valid.
617: */
618: private boolean operationAllowed(EndpointBean eb, String mep) {
619: int style = eb.getStyle();
620: boolean allowed = true;
621:
622: if (style == ConfigConstants.TOPIC) {
623: if (mep.trim().equals(ConfigConstants.IN_OUT)
624: || mep.trim().equals(
625: ConfigConstants.IN_OPTIONAL_OUT)
626: || mep.trim().equals(ConfigConstants.OUT_IN)
627: || mep.trim().equals(
628: ConfigConstants.OUT_OPTIONAL_IN)
629: || mep.trim().equals(
630: ConfigConstants.ROBUST_OUT_ONLY)) {
631: allowed = false;
632: }
633: }
634:
635: return allowed;
636: }
637:
638: /**
639: * Checks the MEP.
640: *
641: * @param pattern.
642: */
643:
644: private void checkMEP(String pattern) {
645: if (pattern != null) {
646: if (pattern.trim().equals(ConfigConstants.IN_OUT)
647: || pattern.trim().equals(
648: ConfigConstants.IN_OPTIONAL_OUT)
649: || pattern.trim().equals(ConfigConstants.OUT_IN)
650: || pattern.trim().equals(
651: ConfigConstants.OUT_OPTIONAL_IN)
652: || pattern.trim().equals(
653: ConfigConstants.ROBUST_OUT_ONLY)
654: || pattern.trim().equals(ConfigConstants.IN_ONLY)
655: || pattern.trim().equals(ConfigConstants.OUT_ONLY)
656: || pattern.trim().equals(
657: ConfigConstants.ROBUST_IN_ONLY)) {
658: ;
659: } else {
660: setError("MEP "
661: + pattern
662: + " is not a valid WSDL message exchange pattern");
663: }
664: }
665:
666: }
667: }
|