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