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.file.util;
030:
031: import com.sun.jbi.binding.file.EndpointBean;
032: import com.sun.jbi.binding.file.FileBindingResolver;
033: import com.sun.jbi.binding.file.FileBindingResources;
034:
035: import com.sun.jbi.binding.file.MessageProcessor;
036:
037: import javax.wsdl.Binding;
038: import javax.wsdl.Definition;
039: import javax.wsdl.OperationType;
040: import javax.wsdl.Port;
041: import javax.wsdl.PortType;
042: import javax.wsdl.extensions.UnknownExtensibilityElement;
043: import javax.wsdl.factory.WSDLFactory;
044: import javax.wsdl.xml.WSDLReader;
045:
046: import org.w3c.dom.DocumentFragment;
047: import org.w3c.dom.NamedNodeMap;
048: import org.w3c.dom.Node;
049: import org.w3c.dom.NodeList;
050: import org.w3c.dom.Element;
051:
052: import java.io.File;
053:
054: import java.net.URI;
055: import java.net.URISyntaxException;
056:
057: import java.util.LinkedList;
058: import java.util.List;
059: import java.util.logging.Logger;
060: import java.util.Iterator;
061: import java.util.Collection;
062:
063: import java.util.Map;
064:
065: import javax.xml.namespace.QName;
066:
067: /**
068: * Reads the configuration WSDL file and loads the data into the EndpointBean
069: * objects.
070: *
071: * @author Sun Microsystems, Inc.
072: */
073: public class WSDL11FileReader extends UtilBase implements
074: FileBindingResources {
075: /**
076: * WSDL Definitions object
077: */
078: private Definition mDefinition;
079:
080: /**
081: * Resolver object.
082: */
083: private FileBindingResolver mResolver;
084:
085: /**
086: * Helper class for i18n.
087: */
088: private StringTranslator mTranslator;
089:
090: /**
091: * The list of endpoints as configured in the config file. This list
092: * contains all the endpoints 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: * Wsdl 1.1 file name
103: */
104: private String mWsdlFile;
105:
106: /**
107: * Creates a new WSDL11FileReader object.
108: *
109: * @param rslv resolver object
110: */
111: public WSDL11FileReader(FileBindingResolver rslv, String wsdlfile) {
112: setValid(true);
113: mResolver = rslv;
114: mWsdlFile = wsdlfile;
115: mTranslator = new StringTranslator();
116: }
117:
118: /**
119: * Returns the Bean object corresponding to the endpoint.
120: *
121: * @param endpoint endpoint name.
122: *
123: * @return endpoint Bean object.
124: */
125: public EndpointBean getBean(String endpoint) {
126: /*Search for the bean corresponding to the service and endpoint name
127: */
128: for (int j = 0; j < mEndpointInfoList.length; j++) {
129: String tmp = mEndpointInfoList[j].getUniqueName();
130:
131: if (tmp.trim().equals(endpoint)) {
132: return mEndpointInfoList[j];
133: }
134: }
135:
136: return null;
137: }
138:
139: /**
140: * Gets the endpoint list corresponding to a config file.
141: *
142: * @return End point Bean list
143: */
144: public EndpointBean[] getEndpoint() {
145: return mEndpointInfoList;
146: }
147:
148: /**
149: * Returns the total number of endopoints in the wsld file.
150: *
151: * @return int number of endpoints.
152: */
153: public int getEndpointCount() {
154: return mTotalEndpoints;
155: }
156:
157: /**
158: * Initializes the config file and loads services.
159: *
160: * @param doc Name of the config file.
161: */
162: public void init(Definition doc) {
163: mDefinition = doc;
164: loadServicesList();
165: }
166:
167: /**
168: * Util method that gets the attribute value.
169: *
170: * @param map attribute node map
171: * @param attr attribute name
172: *
173: * @return attribute value
174: */
175: private String getAttrValue(NamedNodeMap map, String attr) {
176: Node attrnode = map.getNamedItem(attr);
177: String value = null;
178:
179: try {
180: value = attrnode.getNodeValue();
181: } catch (Exception e) {
182: ;
183: }
184:
185: return value;
186: }
187:
188: /**
189: * Sets the direction to inbound or outbound.
190: *
191: * @param map node map.
192: * @param eb endpoint bean.
193: */
194: private void setDirection(NamedNodeMap map, EndpointBean eb) {
195: String val = getValue(map, ConfigData.ENDPOINT_TYPE);
196:
197: if (val.trim().equalsIgnoreCase(ConfigData.PROVIDER_STRING)) {
198: eb.setRole(ConfigData.PROVIDER);
199: eb.setValue(ConfigData.ENDPOINT_TYPE, val);
200: } else if (val.trim().equalsIgnoreCase(
201: ConfigData.CONSUMER_STRING)) {
202: eb.setRole(ConfigData.CONSUMER);
203: eb.setValue(ConfigData.ENDPOINT_TYPE, val);
204: } else {
205: setError(getError()
206: + "\n\t"
207: + mTranslator.getString(FBC_WSDL_INVALID_DIRECTION,
208: val));
209: eb.setValue(ConfigData.ENDPOINT_TYPE, "");
210: setValid(false);
211:
212: return;
213: }
214: }
215:
216: /**
217: * Checks if a binging is file binding
218: *
219: * @param df Document fragment
220: *
221: * @return true if file binding
222: */
223: private boolean isFileBinding(Binding aBinding) {
224:
225: java.util.List extList = aBinding.getExtensibilityElements();
226: List boper = aBinding.getBindingOperations();
227: if (extList != null) {
228: for (int i = 0; i < extList.size(); i++) {
229: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
230: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
231: .get(i);
232:
233: if ((sb.getElementType().getNamespaceURI()
234: .equals(ConfigData.FILENAMESPACE))
235: && (sb.getElementType().getLocalPart()
236: .equals("binding"))) {
237: return true;
238: }
239: }
240: }
241: }
242: return false;
243: }
244:
245: /**
246: * Sets the input pattern.
247: *
248: * @param map Map containing endpoint info.
249: * @param eb Bean attributes.
250: */
251: private void setInputPattern(NamedNodeMap map, EndpointBean eb) {
252: String val = getValue(map, ConfigData.INPUTPATTERN);
253:
254: if (val == null) {
255: val = "";
256: }
257:
258: eb.setValue(ConfigData.INPUTPATTERN, val);
259: }
260:
261: /**
262: * Sets the input, output and processed location.
263: *
264: * @param map Map containing enpoint info
265: * @param eb bean attributes.
266: * @param attribute attribute name.
267: * @param configdata config data.
268: */
269: private void setLocation(NamedNodeMap map, EndpointBean eb,
270: String attribute, String configdata) {
271: String val = getValue(map, attribute);
272:
273: File f = null;
274:
275: try {
276: f = new File(new URI(val));
277: } catch (IllegalArgumentException ie) {
278: setValid(false);
279: setError(getError() + " "
280: + mTranslator.getString(FBC_NOT_VALID, val) + "\n"
281: + ie.getMessage());
282: setException(ie);
283:
284: return;
285: } catch (URISyntaxException use) {
286: setValid(false);
287: setError(getError() + "\n\t"
288: + mTranslator.getString(FBC_WSDL_INVALID_URI, val)
289: + "\n" + use.getMessage());
290: setException(use);
291:
292: return;
293: }
294:
295: eb.setValue(configdata, f.getAbsolutePath());
296: }
297:
298: /**
299: * DOCUMENT ME!
300: *
301: * @param inputext
302: *
303: * @return
304: */
305: private String getMessageType(List inputext) {
306: String type = null;
307:
308: try {
309: for (int i = 0; i < inputext.size(); i++) {
310: Object obj = inputext.get(i);
311:
312: if (obj instanceof UnknownExtensibilityElement) {
313: UnknownExtensibilityElement fileelement = (UnknownExtensibilityElement) obj;
314:
315: if (fileelement.getElementType().getNamespaceURI()
316: .equals(ConfigData.FILENAMESPACE)
317: && fileelement.getElementType()
318: .getLocalPart().equals("message")) {
319: type = getValue(fileelement.getElement()
320: .getAttributes(), "type");
321: }
322: }
323: }
324: } catch (Exception e) {
325: return null;
326: }
327:
328: return type;
329: }
330:
331: private String getOperationExt(List inputext, String attr) {
332: String type = null;
333:
334: try {
335: for (int i = 0; i < inputext.size(); i++) {
336: Object obj = inputext.get(i);
337:
338: if (obj instanceof UnknownExtensibilityElement) {
339: UnknownExtensibilityElement fileelement = (UnknownExtensibilityElement) obj;
340:
341: if (fileelement.getElementType().getNamespaceURI()
342: .equals(ConfigData.FILENAMESPACE)
343: && fileelement.getElementType()
344: .getLocalPart().equals("artifacts")) {
345: type = getValue(fileelement.getElement()
346: .getAttributes(), attr);
347: }
348: }
349: }
350: } catch (Exception e) {
351: return null;
352: }
353:
354: return type;
355:
356: }
357:
358: /**
359: * Sets operations from the WSLD doc.
360: *
361: * @param eb Endpoint bean
362: * @param b binding
363: */
364: private void setOperations(EndpointBean eb, Binding b) {
365: try {
366: List bopers = b.getBindingOperations();
367: PortType bindingif = b.getPortType();
368:
369: Iterator boperiter = null;
370:
371: if (bopers != null) {
372: boperiter = bopers.iterator();
373: }
374:
375: while (boperiter.hasNext()) {
376: javax.wsdl.BindingOperation boper = (javax.wsdl.BindingOperation) boperiter
377: .next();
378: String bindingopername = boper.getOperation().getName();
379: String input = null;
380: String output = null;
381: String mep = null;
382: String outfileext = null;
383: String outprefix = null;
384: List inputext = null;
385:
386: outfileext = getOperationExt(boper
387: .getExtensibilityElements(),
388: ConfigData.OUTPUTEXTENSION);
389:
390: outprefix = getOperationExt(boper
391: .getExtensibilityElements(),
392: ConfigData.OUTPUTPREFIX);
393:
394: if (!boper.getOperation().getStyle().equals(
395: OperationType.NOTIFICATION)) {
396: inputext = boper.getBindingInput()
397: .getExtensibilityElements();
398: input = getMessageType(inputext);
399: if ((input == null) || (input.trim().equals(""))) {
400: input = ConfigData.DEFAULT_MESSAGE_TYPE;
401: }
402: }
403:
404: if (!boper.getOperation().getStyle().equals(
405: OperationType.ONE_WAY)) {
406: List outputext = boper.getBindingOutput()
407: .getExtensibilityElements();
408: output = getMessageType(outputext);
409: if ((output == null) || (output.trim().equals(""))) {
410: output = ConfigData.DEFAULT_MESSAGE_TYPE;
411: }
412: }
413: mep = resolveMep(boper.getOperation().getStyle());
414: if (isValid()) {
415: eb.addOperation("", bindingopername, mep, input,
416: output, outfileext, outprefix);
417: }
418: }
419: } catch (Exception e) {
420: e.printStackTrace();
421: setError("Cannot set operations - " + e.getMessage());
422: }
423: }
424:
425: /**
426: * Sets the output extension.
427: *
428: * @param map Node map.
429: * @param eb endpoint bean.
430: */
431: private void setOutputExtension(NamedNodeMap map, EndpointBean eb) {
432: String val = getValue(map, ConfigData.OUTPUTEXTENSION);
433:
434: if (val == null) {
435: val = "xml";
436: }
437:
438: eb.setValue(ConfigData.OUTPUTEXTENSION, val);
439: }
440:
441: /**
442: * Sets the output file prefix.
443: *
444: * @param map Node map.
445: * @param eb bean attributes.
446: */
447: private void setOutputPrefix(NamedNodeMap map, EndpointBean eb) {
448: String val = getValue(map, ConfigData.OUTPUTPREFIX);
449:
450: if (val == null) {
451: val = "out";
452: }
453:
454: eb.setValue(ConfigData.OUTPUTPREFIX, val);
455: }
456:
457: /**
458: * Gets the valus specified by attr from the Map.
459: *
460: * @param map Node map.
461: * @param attr attribute.
462: *
463: * @return value.
464: */
465: private String getValue(NamedNodeMap map, String attr) {
466: Node attrnode = map.getNamedItem(attr);
467: String value = null;
468:
469: try {
470: value = attrnode.getNodeValue();
471: } catch (Exception e) {
472: ;
473: }
474:
475: return value;
476: }
477:
478: /**
479: * Loads the endpoint information.
480: *
481: * @param ep Endpoint object
482: * @param eb endpoint bean.
483: */
484: private void loadEndpointBean(Port ep, EndpointBean eb) {
485: List extList = ep.getExtensibilityElements();
486:
487: if (extList != null) {
488: for (int i = 0; i < extList.size(); i++) {
489: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
490: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
491: .get(i);
492:
493: if (sb.getElementType().getNamespaceURI().equals(
494: ConfigData.FILENAMESPACE)
495: && sb.getElementType().getLocalPart()
496: .equals("artifacts")) {
497: Element epnode = sb.getElement();
498: NamedNodeMap epattributes = epnode
499: .getAttributes();
500: setDirection(epattributes, eb);
501: if (eb.getRole() == ConfigData.CONSUMER) {
502: setLocation(epattributes, eb,
503: ConfigData.WSDL_INPUT_LOCATION,
504: ConfigData.INPUTDIR);
505: setLocation(epattributes, eb,
506: ConfigData.WSDL_PROCESSED_LOCATION,
507: ConfigData.PROCESSEDDIR);
508: }
509: setLocation(epattributes, eb,
510: ConfigData.WSDL_OUTPUT_LOCATION,
511: ConfigData.OUTPUTDIR);
512: setInputPattern(epattributes, eb);
513: if (!isValid()) {
514: setError("\n\tEndpoint : " + ep.getName()
515: + getError());
516: }
517: }
518: }
519: }
520: }
521: }
522:
523: /**
524: * Parses the config files and loads them into bean objects.
525: */
526: private void loadServicesList() {
527: java.util.Map servicemap = mDefinition.getServices();
528: List eplist = new LinkedList();
529: Collection services = servicemap.values();
530:
531: Iterator iter = services.iterator();
532:
533: while (iter.hasNext()) {
534: try {
535: javax.wsdl.Service ser = (javax.wsdl.Service) iter
536: .next();
537:
538: Map endpointsmap = ser.getPorts();
539: Collection endpoints = endpointsmap.values();
540:
541: Iterator epiter = endpoints.iterator();
542:
543: while (epiter.hasNext()) {
544: javax.wsdl.Port ep = (javax.wsdl.Port) epiter
545: .next();
546:
547: javax.wsdl.Binding b = ep.getBinding();
548:
549: if (!isFileBinding(b)) {
550: continue;
551: }
552:
553: EndpointBean eb = new EndpointBean();
554: loadEndpointBean(ep, eb);
555: eb.setValue(ConfigData.SERVICE_NAMESPACE, ser
556: .getQName().getNamespaceURI());
557: eb.setValue(ConfigData.SERVICE_LOCALNAME, ser
558: .getQName().getLocalPart());
559: eb.setValue(ConfigData.SERVICENAME, ser.getQName()
560: .toString());
561: eb.setValue(ConfigData.BINDING_NAME, b.getQName()
562: .toString());
563: eb.setValue(ConfigData.ENDPOINTNAME, ep.getName());
564: eb.setValue(ConfigData.INTERFACE_LOCALNAME, ep
565: .getBinding().getPortType().getQName()
566: .getLocalPart());
567: eb.setValue(ConfigData.INTERFACE_NAMESPACE, ep
568: .getBinding().getPortType().getQName()
569: .getNamespaceURI());
570: eb.setWsdlDefintion(mDefinition);
571: eb.setDeploymentType("WSDL11");
572: setOperations(eb, b);
573:
574: if (!isValid()) {
575: setError(("\n\t" + "Service : "
576: + ser.getQName().toString() + getError()));
577: eplist.clear();
578:
579: return;
580: }
581:
582: eplist.add(eb);
583: mResolver.addEndpointDoc(ser.getQName().toString()
584: + ep.getName(), mWsdlFile);
585:
586: }
587: } catch (Exception e) {
588: setError(mTranslator.getString(FBC_WSDL_ENDPOINT_ERROR,
589: e.getMessage()));
590: setException(e);
591: setValid(false);
592: e.printStackTrace();
593:
594: return;
595: }
596: }
597:
598: mEndpointInfoList = new EndpointBean[eplist.size()];
599:
600: for (int x = 0; x < eplist.size(); x++) {
601: mEndpointInfoList[x] = (EndpointBean) eplist.get(x);
602: }
603:
604: mTotalEndpoints = eplist.size();
605: }
606:
607: /**
608: *
609: *
610: * @param opstyle
611: *
612: * @return
613: */
614: private String resolveMep(OperationType opstyle) {
615: String mep = null;
616:
617: if (opstyle.equals(OperationType.ONE_WAY)) {
618: mep = MessageProcessor.IN_ONLY;
619: } else if (opstyle.equals(OperationType.REQUEST_RESPONSE)) {
620: mep = MessageProcessor.IN_OUT;
621: } else if (opstyle.equals(OperationType.NOTIFICATION)) {
622: mep = MessageProcessor.IN_ONLY;
623: } else if (opstyle.equals(OperationType.SOLICIT_RESPONSE)) {
624: mep = MessageProcessor.IN_OUT;
625: } else {
626: setError("Unknown Style of operation " + opstyle);
627: }
628:
629: return mep;
630: }
631:
632: }
|