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.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.wsdl2.Binding;
036: import com.sun.jbi.wsdl2.BindingOperation;
037: import com.sun.jbi.wsdl2.Description;
038: import com.sun.jbi.wsdl2.Endpoint;
039: import com.sun.jbi.wsdl2.Interface;
040: import com.sun.jbi.wsdl2.InterfaceOperation;
041:
042: import org.w3c.dom.DocumentFragment;
043: import org.w3c.dom.NamedNodeMap;
044: import org.w3c.dom.Node;
045: import org.w3c.dom.NodeList;
046:
047: import java.io.File;
048:
049: import java.net.URI;
050: import java.net.URISyntaxException;
051:
052: import java.util.LinkedList;
053: import java.util.List;
054: import java.util.logging.Logger;
055:
056: import javax.xml.namespace.QName;
057:
058: /**
059: * Reads the configuration WSDL file and loads the data into the EndpointBean
060: * objects.
061: *
062: * @author Sun Microsystems, Inc.
063: */
064: public class WSDLFileReader extends UtilBase implements
065: FileBindingResources {
066: /**
067: * WSDL Description object
068: */
069: private Description mDefinition;
070:
071: /**
072: * Resolver object.
073: */
074: private FileBindingResolver mResolver;
075:
076: /**
077: * Helper class for i18n.
078: */
079: private StringTranslator mTranslator;
080:
081: /**
082: * The list of endpoints as configured in the config file. This list
083: * contains all the endpoints and their attibutes
084: */
085: private EndpointBean[] mEndpointInfoList = null;
086:
087: /**
088: * The total number of end points in the config file
089: */
090: private int mTotalEndpoints = 0;
091:
092: /**
093: * Creates a new WSDLFileReader object.
094: *
095: * @param rslv resolver object
096: */
097: public WSDLFileReader(FileBindingResolver rslv) {
098: setValid(true);
099: mResolver = rslv;
100: mTranslator = new StringTranslator();
101: }
102:
103: /**
104: * Returns the Bean object corresponding to the endpoint.
105: *
106: * @param endpoint endpoint name.
107: *
108: * @return endpoint Bean object.
109: */
110: public EndpointBean getBean(String endpoint) {
111: /*Search for the bean corresponding to the service and endpoint name
112: */
113: for (int j = 0; j < mEndpointInfoList.length; j++) {
114: String tmp = mEndpointInfoList[j].getUniqueName();
115:
116: if (tmp.trim().equals(endpoint)) {
117: return mEndpointInfoList[j];
118: }
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Gets the endpoint list corresponding to a config file.
126: *
127: * @return End point Bean list
128: */
129: public EndpointBean[] getEndpoint() {
130: return mEndpointInfoList;
131: }
132:
133: /**
134: * Returns the total number of endopoints in the wsld file.
135: *
136: * @return int number of endpoints.
137: */
138: public int getEndpointCount() {
139: return mTotalEndpoints;
140: }
141:
142: /**
143: * Initializes the config file and loads services.
144: *
145: * @param doc Name of the config file.
146: */
147: public void init(Description doc) {
148: mDefinition = doc;
149: loadServicesList();
150: }
151:
152: /**
153: * Util method that gets the attribute value.
154: *
155: * @param map attribute node map
156: * @param attr attribute name
157: *
158: * @return attribute value
159: */
160: private String getAttrValue(NamedNodeMap map, String attr) {
161: Node attrnode = map.getNamedItem(attr);
162: String value = null;
163:
164: try {
165: value = attrnode.getNodeValue();
166: } catch (Exception e) {
167: ;
168: }
169:
170: return value;
171: }
172:
173: /**
174: * Sets the direction to inbound or outbound.
175: *
176: * @param map node map.
177: * @param eb endpoint bean.
178: */
179: private void setDirection(NamedNodeMap map, EndpointBean eb) {
180: String val = getValue(map, ConfigData.ENDPOINT_TYPE);
181:
182: if (val.trim().equalsIgnoreCase(ConfigData.PROVIDER_STRING)) {
183: eb.setRole(ConfigData.PROVIDER);
184: eb.setValue(ConfigData.ENDPOINT_TYPE, val);
185: } else if (val.trim().equalsIgnoreCase(
186: ConfigData.CONSUMER_STRING)) {
187: eb.setRole(ConfigData.CONSUMER);
188: eb.setValue(ConfigData.ENDPOINT_TYPE, val);
189: } else {
190: setError(getError()
191: + "\n\t"
192: + mTranslator.getString(FBC_WSDL_INVALID_DIRECTION,
193: val));
194: eb.setValue(ConfigData.ENDPOINT_TYPE, "");
195: setValid(false);
196:
197: return;
198: }
199: }
200:
201: /**
202: * Gets a node array corresponding to a node name.
203: *
204: * @param nodeList node list
205: * @param name node name
206: *
207: * @return Node array
208: */
209: private Node[] getElementNodeArrayByName(NodeList nodeList,
210: String name) {
211: Node[] retNode = null;
212: List nodeArr = new LinkedList();
213: NodeList tmpNodeList = nodeList.item(0).getChildNodes();
214:
215: for (int g = 0; g < tmpNodeList.getLength(); g++) {
216: Node n = tmpNodeList.item(g);
217:
218: if ((n != null) && (n.getNodeType() == Node.ELEMENT_NODE)) {
219: if ((n.getLocalName().equals(name))) {
220: nodeArr.add(n);
221: }
222: }
223: }
224:
225: // for loop
226: retNode = new Node[nodeArr.size()];
227:
228: for (int k = 0; k < nodeArr.size(); k++) {
229: retNode[k] = (Node) nodeArr.get(k);
230: }
231:
232: return retNode;
233: }
234:
235: /**
236: * Method to determine if the binding information in the Document fragment
237: * is a file binding or not
238: *
239: * @param df document fragment
240: *
241: * @return true if file binding.
242: */
243: private boolean isFileBinding(DocumentFragment df) {
244: NamedNodeMap n = df.getFirstChild().getAttributes();
245:
246: for (int g = 0; g < n.getLength(); g++) {
247: if (n.item(g).getLocalName().equals(
248: ConfigData.WSDL_BINDING_TYPE)) {
249: if (n.item(g).getNodeValue().equals(
250: ConfigData.FILENAMESPACE)) {
251: return true;
252: }
253: }
254: }
255:
256: return false;
257: }
258:
259: /**
260: * Sets the input pattern.
261: *
262: * @param map Map containing endpoint info.
263: * @param eb Bean attributes.
264: */
265: private void setInputPattern(NamedNodeMap map, EndpointBean eb) {
266: String val = getValue(map, ConfigData.INPUTPATTERN);
267:
268: if (val == null) {
269: val = "";
270: }
271:
272: eb.setValue(ConfigData.INPUTPATTERN, val);
273: }
274:
275: /**
276: * Sets the input, output and processed location.
277: *
278: * @param map Map containing enpoint info
279: * @param eb bean attributes.
280: * @param attribute attribute name.
281: * @param configdata config data.
282: */
283: private void setLocation(NamedNodeMap map, EndpointBean eb,
284: String attribute, String configdata) {
285: String val = getValue(map, attribute);
286:
287: File f = null;
288:
289: try {
290: f = new File(new URI(val));
291: } catch (IllegalArgumentException ie) {
292: setValid(false);
293: setError(getError() + " "
294: + mTranslator.getString(FBC_NOT_VALID, val) + "\n"
295: + ie.getMessage());
296: setException(ie);
297:
298: return;
299: } catch (URISyntaxException use) {
300: setValid(false);
301: setError(getError() + "\n\t"
302: + mTranslator.getString(FBC_WSDL_INVALID_URI, val)
303: + "\n" + use.getMessage());
304: setException(use);
305:
306: return;
307: }
308:
309: eb.setValue(configdata, f.getAbsolutePath());
310: }
311:
312: /**
313: * Sets operations from the WSLD doc.
314: *
315: * @param eb Endpoint bean
316: * @param b binding
317: */
318: private void setOperations(EndpointBean eb, Binding b) {
319: Interface bindingif = b.getInterface();
320: Node[] allOpNodes = getElementNodeArrayByName(b
321: .toXmlDocumentFragment().getChildNodes(), "operation");
322:
323: for (int oper = 0; oper < b.getOperationsLength(); oper++) {
324: BindingOperation boper = b.getOperation(oper);
325: QName bindingopername = boper.getInterfaceOperation();
326: String input = null;
327: String output = null;
328: String mep = null;
329: String ext = null;
330: String prefix = null;
331:
332: for (int k = 0; k < allOpNodes.length; k++) {
333: NamedNodeMap nm = allOpNodes[k].getAttributes();
334:
335: if (nm != null) {
336: String val = getAttrValue(nm, "ref");
337:
338: if ((val != null)
339: && val.equals(bindingopername
340: .getLocalPart().trim())) {
341: input = getValue(nm,
342: ConfigData.INPUT_MESSAGE_TYPE);
343: output = getValue(nm,
344: ConfigData.OUTPUT_MESSAGE_TYPE);
345:
346: ext = getValue(nm, ConfigData.OUTPUTEXTENSION);
347:
348: prefix = getValue(nm, ConfigData.OUTPUTPREFIX);
349:
350: break;
351: }
352: }
353: }
354:
355: if ((input == null) || (input.trim().equals(""))) {
356: input = ConfigData.DEFAULT_MESSAGE_TYPE;
357: }
358:
359: if ((output == null) || (output.trim().equals(""))) {
360: output = ConfigData.DEFAULT_MESSAGE_TYPE;
361: }
362:
363: int ifoperlength = bindingif.getOperationsLength();
364:
365: for (int j = 0; j < ifoperlength; j++) {
366: InterfaceOperation op = bindingif.getOperation(j);
367: QName ifopername = op.getQualifiedName();
368:
369: if (bindingopername.getLocalPart().trim().equals(
370: ifopername.getLocalPart().trim())) {
371: mep = op.getPattern();
372:
373: if (mep != null) {
374: eb.addOperation(ifopername.getNamespaceURI(),
375: ifopername.getLocalPart(), mep, input,
376: output, ext, prefix);
377: }
378: }
379: }
380: }
381: }
382:
383: /**
384: * Sets the output extension.
385: *
386: * @param map Node map.
387: * @param eb endpoint bean.
388: */
389: private void setOutputExtension(NamedNodeMap map, EndpointBean eb) {
390: String val = getValue(map, ConfigData.OUTPUTEXTENSION);
391:
392: if (val == null) {
393: val = "xml";
394: }
395:
396: eb.setValue(ConfigData.OUTPUTEXTENSION, val);
397: }
398:
399: /**
400: * Sets the output file prefix.
401: *
402: * @param map Node map.
403: * @param eb bean attributes.
404: */
405: private void setOutputPrefix(NamedNodeMap map, EndpointBean eb) {
406: String val = getValue(map, ConfigData.OUTPUTPREFIX);
407:
408: if (val == null) {
409: val = "out";
410: }
411:
412: eb.setValue(ConfigData.OUTPUTPREFIX, val);
413: }
414:
415: /**
416: * Gets the valus specified by attr from the Map.
417: *
418: * @param map Node map.
419: * @param attr attribute.
420: *
421: * @return value.
422: */
423: private String getValue(NamedNodeMap map, String attr) {
424: Node attrnode = map.getNamedItemNS(ConfigData.FILENAMESPACE,
425: attr);
426: String value = null;
427:
428: try {
429: value = attrnode.getNodeValue();
430: } catch (Exception e) {
431: ;
432: }
433:
434: return value;
435: }
436:
437: /**
438: * Loads the endpoint information.
439: *
440: * @param ep Endpoint object
441: * @param eb endpoint bean.
442: */
443: private void loadEndpointBean(Endpoint ep, EndpointBean eb) {
444: DocumentFragment docfrag = ep.toXmlDocumentFragment();
445: Node epnode = docfrag.getFirstChild();
446: NamedNodeMap epattributes = epnode.getAttributes();
447: setDirection(epattributes, eb);
448:
449: if (eb.getRole() == ConfigData.CONSUMER) {
450: setLocation(epattributes, eb,
451: ConfigData.WSDL_INPUT_LOCATION, ConfigData.INPUTDIR);
452: setLocation(epattributes, eb,
453: ConfigData.WSDL_PROCESSED_LOCATION,
454: ConfigData.PROCESSEDDIR);
455: }
456:
457: setLocation(epattributes, eb, ConfigData.WSDL_OUTPUT_LOCATION,
458: ConfigData.OUTPUTDIR);
459: setInputPattern(epattributes, eb);
460:
461: if (!isValid()) {
462: setError("\n\tEndpoint : " + ep.getName() + getError());
463: }
464: }
465:
466: /**
467: * Parses the config files and loads them into bean objects.
468: */
469: private void loadServicesList() {
470: int services = mDefinition.getServicesLength();
471: List eplist = new LinkedList();
472:
473: for (int i = 0; i < services; i++) {
474: try {
475: com.sun.jbi.wsdl2.Service ser = mDefinition
476: .getService(i);
477: int endpoints = ser.getEndpointsLength();
478:
479: for (int k = 0; k < endpoints; k++) {
480: Endpoint ep = ser.getEndpoint(k);
481: Binding b = ep.getBinding();
482:
483: if (!isFileBinding(b.toXmlDocumentFragment())) {
484: continue;
485: }
486:
487: EndpointBean eb = new EndpointBean();
488: loadEndpointBean(ep, eb);
489: eb.setValue(ConfigData.SERVICE_NAMESPACE, ser
490: .getQName().getNamespaceURI());
491: eb.setValue(ConfigData.SERVICE_LOCALNAME, ser
492: .getQName().getLocalPart());
493: eb.setValue(ConfigData.SERVICENAME, ser.getQName()
494: .toString());
495: eb.setValue(ConfigData.BINDING_NAME, b.getName());
496: eb.setValue(ConfigData.ENDPOINTNAME, ep.getName());
497: eb.setValue(ConfigData.INTERFACE_LOCALNAME, ep
498: .getBinding().getInterface().getQName()
499: .getLocalPart());
500: eb.setValue(ConfigData.INTERFACE_NAMESPACE, ep
501: .getBinding().getInterface().getQName()
502: .getNamespaceURI());
503: // eb.setWsdlDefintion(mDefinition);
504: eb.setDeploymentType("WSDL20");
505: setOperations(eb, b);
506:
507: if (!isValid()) {
508: setError(("\n\t" + "Service : "
509: + ser.getQName().toString() + getError()));
510: eplist.clear();
511:
512: return;
513: }
514:
515: eplist.add(eb);
516: mResolver
517: .addEndpointDoc(ser.getQName().toString()
518: + ep.getName(), mDefinition
519: .toXmlDocument());
520: }
521: } catch (Exception e) {
522: setError(mTranslator.getString(FBC_WSDL_ENDPOINT_ERROR,
523: e.getMessage()));
524: setException(e);
525: setValid(false);
526: e.printStackTrace();
527:
528: return;
529: }
530: }
531:
532: mEndpointInfoList = new EndpointBean[eplist.size()];
533:
534: for (int x = 0; x < eplist.size(); x++) {
535: mEndpointInfoList[x] = (EndpointBean) eplist.get(x);
536: }
537:
538: mTotalEndpoints = eplist.size();
539: }
540: }
|