001: package org.objectweb.celtix.tools.processors.wsdl2;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.wsdl.Binding;
010: import javax.wsdl.BindingInput;
011: import javax.wsdl.BindingOperation;
012: import javax.wsdl.BindingOutput;
013: import javax.wsdl.Input;
014: import javax.wsdl.Operation;
015: import javax.wsdl.Output;
016: import javax.wsdl.Port;
017: import javax.wsdl.PortType;
018: import javax.wsdl.Service;
019: import javax.wsdl.WSDLException;
020: import javax.wsdl.extensions.ExtensionRegistry;
021: import javax.wsdl.xml.WSDLWriter;
022: import javax.xml.namespace.QName;
023:
024: import org.objectweb.celtix.common.i18n.Message;
025: import org.objectweb.celtix.tools.common.ToolConstants;
026: import org.objectweb.celtix.tools.common.ToolException;
027: import org.objectweb.celtix.tools.common.WSDLConstants;
028: import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormat;
029: import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormatBinding;
030: import org.objectweb.celtix.tools.extensions.xmlformat.XMLHttpAddress;
031:
032: public class WSDLToXMLProcessor extends WSDLToProcessor {
033:
034: private static final String NEW_FILE_NAME_MODIFIER = "-xmlbinding";
035: private static final String HTTP_PREFIX = "http://localhost:9000";
036:
037: private ExtensionRegistry extReg;
038:
039: private Map services;
040: private Service service;
041: private Map ports;
042: private Port port;
043:
044: private Map portTypes;
045: private PortType portType;
046: private Binding binding;
047:
048: public void process() throws ToolException {
049: init();
050: if (isBindingExisted()) {
051: Message msg = new Message("BINDING_ALREADY_EXIST", LOG);
052: throw new ToolException(msg);
053: }
054: if (!isPortTypeExisted()) {
055: Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
056: throw new ToolException(msg);
057: }
058: if (isServicePortExisted()) {
059: Message msg = new Message("SERVICE_PORT_EXIST", LOG);
060: throw new ToolException(msg);
061: }
062: extReg = this .wsdlReader.getExtensionRegistry();
063: doAppendBinding();
064: doAppendService();
065: writeToWSDL();
066: }
067:
068: private boolean isServicePortExisted() {
069: return isServiceExisted() && isPortExisted();
070: }
071:
072: private boolean isServiceExisted() {
073: services = wsdlDefinition.getServices();
074: if (services == null) {
075: return false;
076: }
077: Iterator it = services.keySet().iterator();
078: while (it.hasNext()) {
079: QName serviceQName = (QName) it.next();
080: String serviceName = serviceQName.getLocalPart();
081: if (serviceName.equals(env.get(ToolConstants.CFG_SERVICE))) {
082: service = (Service) services.get(serviceQName);
083: break;
084: }
085: }
086: return (service == null) ? false : true;
087: }
088:
089: private boolean isPortExisted() {
090: ports = service.getPorts();
091: if (ports == null) {
092: return false;
093: }
094: Iterator it = ports.keySet().iterator();
095: while (it.hasNext()) {
096: String portName = (String) it.next();
097: if (portName.equals(env.get(ToolConstants.CFG_PORT))) {
098: port = (Port) ports.get(portName);
099: break;
100: }
101: }
102: return (port == null) ? false : true;
103: }
104:
105: private boolean isPortTypeExisted() {
106: portTypes = wsdlDefinition.getPortTypes();
107: if (portTypes == null) {
108: return false;
109: }
110: Iterator it = portTypes.keySet().iterator();
111: while (it.hasNext()) {
112: QName existPortQName = (QName) it.next();
113: String existPortName = existPortQName.getLocalPart();
114: if (existPortName.equals(env
115: .get(ToolConstants.CFG_PORTTYPE))) {
116: portType = (PortType) portTypes.get(existPortQName);
117: break;
118: }
119: }
120: return (portType == null) ? false : true;
121: }
122:
123: private boolean isBindingExisted() {
124: Map bindings = wsdlDefinition.getBindings();
125: if (bindings == null) {
126: return false;
127: }
128: Iterator it = bindings.keySet().iterator();
129: while (it.hasNext()) {
130: QName existBindingQName = (QName) it.next();
131: String existBindingName = existBindingQName.getLocalPart();
132: String bindingName = (String) env
133: .get(ToolConstants.CFG_BINDING);
134: if (bindingName.equals(existBindingName)) {
135: binding = (Binding) bindings.get(existBindingQName);
136: }
137: }
138: return (binding == null) ? false : true;
139: }
140:
141: protected void init() throws ToolException {
142: parseWSDL((String) env.get(ToolConstants.CFG_WSDLURL));
143: if (wsdlDefinition
144: .getNamespace(ToolConstants.XML_FORMAT_PREFIX) == null) {
145: wsdlDefinition.addNamespace(
146: ToolConstants.XML_FORMAT_PREFIX,
147: ToolConstants.NS_XML_FORMAT);
148: }
149: if (wsdlDefinition.getNamespace(ToolConstants.XML_HTTP_PREFIX) == null) {
150: wsdlDefinition.addNamespace(ToolConstants.XML_HTTP_PREFIX,
151: ToolConstants.NS_XML_HTTP);
152: }
153: }
154:
155: private void doAppendBinding() throws ToolException {
156: if (binding == null) {
157: binding = wsdlDefinition.createBinding();
158: binding.setQName(new QName(wsdlDefinition
159: .getTargetNamespace(), (String) env
160: .get(ToolConstants.CFG_BINDING)));
161: binding.setUndefined(false);
162: binding.setPortType(portType);
163: }
164: setXMLBindingExtElement();
165: addBindingOperation();
166: wsdlDefinition.addBinding(binding);
167: }
168:
169: private void setXMLBindingExtElement() throws ToolException {
170: if (extReg == null) {
171: extReg = wsdlFactory.newPopulatedExtensionRegistry();
172: }
173: XMLFormatBinding xmlBinding = null;
174: try {
175: xmlBinding = (XMLFormatBinding) extReg.createExtension(
176: Binding.class, ToolConstants.XML_BINDING_FORMAT);
177: } catch (WSDLException wse) {
178: Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG);
179: throw new ToolException(msg);
180: }
181: binding.addExtensibilityElement(xmlBinding);
182: }
183:
184: @SuppressWarnings("unchecked")
185: private void addBindingOperation() throws ToolException {
186: List<Operation> ops = portType.getOperations();
187: for (Operation op : ops) {
188: BindingOperation bindingOperation = wsdlDefinition
189: .createBindingOperation();
190: bindingOperation.setName(op.getName());
191: if (op.getInput() != null) {
192: bindingOperation.setBindingInput(getBindingInput(op
193: .getInput(), op.getName()));
194: }
195: if (op.getOutput() != null) {
196: bindingOperation.setBindingOutput(getBindingOutput(op
197: .getOutput(), op.getName()));
198: }
199: if (op.getFaults() != null && op.getFaults().size() > 0) {
200: addXMLFaults(op, bindingOperation);
201: }
202: bindingOperation.setOperation(op);
203: binding.addBindingOperation(bindingOperation);
204: }
205: }
206:
207: private BindingInput getBindingInput(Input input,
208: String operationName) throws ToolException {
209: BindingInput bi = wsdlDefinition.createBindingInput();
210: bi.setName(input.getName());
211: //This ext element in some scenario is optional, but if provided, won't cause error
212: bi.addExtensibilityElement(getXMLBody(BindingInput.class,
213: operationName));
214: return bi;
215: }
216:
217: private BindingOutput getBindingOutput(Output output,
218: String operationName) throws ToolException {
219: BindingOutput bo = wsdlDefinition.createBindingOutput();
220: bo.setName(output.getName());
221: bo.addExtensibilityElement(getXMLBody(BindingOutput.class,
222: operationName));
223: return bo;
224: }
225:
226: private void addXMLFaults(Operation op, BindingOperation bo) {
227: // TODO
228: }
229:
230: private XMLFormat getXMLBody(Class clz, String operationName)
231: throws ToolException {
232: if (extReg == null) {
233: extReg = wsdlFactory.newPopulatedExtensionRegistry();
234: }
235: XMLFormat xmlFormat = null;
236: try {
237: xmlFormat = (XMLFormat) extReg.createExtension(clz,
238: ToolConstants.XML_FORMAT);
239: } catch (WSDLException wse) {
240: Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG);
241: throw new ToolException(msg);
242: }
243: xmlFormat.setRootNode(new QName(wsdlDefinition
244: .getTargetNamespace(), operationName));
245: return xmlFormat;
246: }
247:
248: private void doAppendService() throws ToolException {
249: if (service == null) {
250: service = wsdlDefinition.createService();
251: service.setQName(new QName(WSDLConstants.WSDL_PREFIX,
252: (String) env.get(ToolConstants.CFG_SERVICE)));
253: }
254: if (port == null) {
255: port = wsdlDefinition.createPort();
256: port.setName((String) env.get(ToolConstants.CFG_PORT));
257: port.setBinding(binding);
258: }
259: setAddrElement();
260: service.addPort(port);
261: wsdlDefinition.addService(service);
262: }
263:
264: private void setAddrElement() throws ToolException {
265: extReg = this .wsdlReader.getExtensionRegistry();
266: if (extReg == null) {
267: extReg = wsdlFactory.newPopulatedExtensionRegistry();
268: }
269: XMLHttpAddress xmlHttpAddress = null;
270: try {
271: xmlHttpAddress = (XMLHttpAddress) extReg.createExtension(
272: Port.class,
273: WSDLConstants.NS_XMLHTTP_BINDING_ADDRESS);
274: } catch (WSDLException wse) {
275: Message msg = new Message("FAIl_TO_CREATE_SOAPADDRESS", LOG);
276: throw new ToolException(msg);
277: }
278: if (env.get(ToolConstants.CFG_ADDRESS) != null) {
279: xmlHttpAddress.setLocation((String) env
280: .get(ToolConstants.CFG_ADDRESS));
281: } else {
282: xmlHttpAddress.setLocation(HTTP_PREFIX + "/"
283: + env.get(ToolConstants.CFG_SERVICE) + "/"
284: + env.get(ToolConstants.CFG_PORT));
285: }
286: port.addExtensibilityElement(xmlHttpAddress);
287: }
288:
289: private void writeToWSDL() throws ToolException {
290: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
291: Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
292: try {
293: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
294: } catch (WSDLException wse) {
295: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
296: throw new ToolException(msg);
297: }
298: try {
299: outputWriter.close();
300: } catch (IOException ioe) {
301: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
302: throw new ToolException(msg);
303: }
304: }
305:
306: }
|