1 package org.objectweb.celtix.tools.processors.wsdl2; 2 3 import java.io.IOException ; 4 import java.io.Writer ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import javax.wsdl.Binding; 10 import javax.wsdl.BindingInput; 11 import javax.wsdl.BindingOperation; 12 import javax.wsdl.BindingOutput; 13 import javax.wsdl.Input; 14 import javax.wsdl.Operation; 15 import javax.wsdl.Output; 16 import javax.wsdl.Port; 17 import javax.wsdl.PortType; 18 import javax.wsdl.Service; 19 import javax.wsdl.WSDLException; 20 import javax.wsdl.extensions.ExtensionRegistry; 21 import javax.wsdl.xml.WSDLWriter; 22 import javax.xml.namespace.QName ; 23 24 import org.objectweb.celtix.common.i18n.Message; 25 import org.objectweb.celtix.tools.common.ToolConstants; 26 import org.objectweb.celtix.tools.common.ToolException; 27 import org.objectweb.celtix.tools.common.WSDLConstants; 28 import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormat; 29 import org.objectweb.celtix.tools.extensions.xmlformat.XMLFormatBinding; 30 import org.objectweb.celtix.tools.extensions.xmlformat.XMLHttpAddress; 31 32 public class WSDLToXMLProcessor extends WSDLToProcessor { 33 34 private static final String NEW_FILE_NAME_MODIFIER = "-xmlbinding"; 35 private static final String HTTP_PREFIX = "http://localhost:9000"; 36 37 private ExtensionRegistry extReg; 38 39 private Map services; 40 private Service service; 41 private Map ports; 42 private Port port; 43 44 private Map portTypes; 45 private PortType portType; 46 private Binding binding; 47 48 public void process() throws ToolException { 49 init(); 50 if (isBindingExisted()) { 51 Message msg = new Message("BINDING_ALREADY_EXIST", LOG); 52 throw new ToolException(msg); 53 } 54 if (!isPortTypeExisted()) { 55 Message msg = new Message("PORTTYPE_NOT_EXIST", LOG); 56 throw new ToolException(msg); 57 } 58 if (isServicePortExisted()) { 59 Message msg = new Message("SERVICE_PORT_EXIST", LOG); 60 throw new ToolException(msg); 61 } 62 extReg = this.wsdlReader.getExtensionRegistry(); 63 doAppendBinding(); 64 doAppendService(); 65 writeToWSDL(); 66 } 67 68 private boolean isServicePortExisted() { 69 return isServiceExisted() && isPortExisted(); 70 } 71 72 private boolean isServiceExisted() { 73 services = wsdlDefinition.getServices(); 74 if (services == null) { 75 return false; 76 } 77 Iterator it = services.keySet().iterator(); 78 while (it.hasNext()) { 79 QName serviceQName = (QName )it.next(); 80 String serviceName = serviceQName.getLocalPart(); 81 if (serviceName.equals(env.get(ToolConstants.CFG_SERVICE))) { 82 service = (Service)services.get(serviceQName); 83 break; 84 } 85 } 86 return (service == null) ? false : true; 87 } 88 89 private boolean isPortExisted() { 90 ports = service.getPorts(); 91 if (ports == null) { 92 return false; 93 } 94 Iterator it = ports.keySet().iterator(); 95 while (it.hasNext()) { 96 String portName = (String )it.next(); 97 if (portName.equals(env.get(ToolConstants.CFG_PORT))) { 98 port = (Port)ports.get(portName); 99 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.get(ToolConstants.CFG_PORTTYPE))) { 115 portType = (PortType)portTypes.get(existPortQName); 116 break; 117 } 118 } 119 return (portType == null) ? false : true; 120 } 121 122 private boolean isBindingExisted() { 123 Map bindings = wsdlDefinition.getBindings(); 124 if (bindings == null) { 125 return false; 126 } 127 Iterator it = bindings.keySet().iterator(); 128 while (it.hasNext()) { 129 QName existBindingQName = (QName )it.next(); 130 String existBindingName = existBindingQName.getLocalPart(); 131 String bindingName = (String )env.get(ToolConstants.CFG_BINDING); 132 if (bindingName.equals(existBindingName)) { 133 binding = (Binding)bindings.get(existBindingQName); 134 } 135 } 136 return (binding == null) ? false : true; 137 } 138 139 protected void init() throws ToolException { 140 parseWSDL((String )env.get(ToolConstants.CFG_WSDLURL)); 141 if (wsdlDefinition.getNamespace(ToolConstants.XML_FORMAT_PREFIX) == null) { 142 wsdlDefinition.addNamespace(ToolConstants.XML_FORMAT_PREFIX, ToolConstants.NS_XML_FORMAT); 143 } 144 if (wsdlDefinition.getNamespace(ToolConstants.XML_HTTP_PREFIX) == null) { 145 wsdlDefinition.addNamespace(ToolConstants.XML_HTTP_PREFIX, ToolConstants.NS_XML_HTTP); 146 } 147 } 148 149 private void doAppendBinding() throws ToolException { 150 if (binding == null) { 151 binding = wsdlDefinition.createBinding(); 152 binding.setQName(new QName (wsdlDefinition.getTargetNamespace(), (String )env 153 .get(ToolConstants.CFG_BINDING))); 154 binding.setUndefined(false); 155 binding.setPortType(portType); 156 } 157 setXMLBindingExtElement(); 158 addBindingOperation(); 159 wsdlDefinition.addBinding(binding); 160 } 161 162 private void setXMLBindingExtElement() throws ToolException { 163 if (extReg == null) { 164 extReg = wsdlFactory.newPopulatedExtensionRegistry(); 165 } 166 XMLFormatBinding xmlBinding = null; 167 try { 168 xmlBinding = (XMLFormatBinding)extReg.createExtension(Binding.class, 169 ToolConstants.XML_BINDING_FORMAT); 170 } catch (WSDLException wse) { 171 Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG); 172 throw new ToolException(msg); 173 } 174 binding.addExtensibilityElement(xmlBinding); 175 } 176 177 @SuppressWarnings ("unchecked") 178 private void addBindingOperation() throws ToolException { 179 List <Operation> ops = portType.getOperations(); 180 for (Operation op : ops) { 181 BindingOperation bindingOperation = wsdlDefinition.createBindingOperation(); 182 bindingOperation.setName(op.getName()); 183 if (op.getInput() != null) { 184 bindingOperation.setBindingInput(getBindingInput(op.getInput(), op.getName())); 185 } 186 if (op.getOutput() != null) { 187 bindingOperation.setBindingOutput(getBindingOutput(op.getOutput(), op.getName())); 188 } 189 if (op.getFaults() != null && op.getFaults().size() > 0) { 190 addXMLFaults(op, bindingOperation); 191 } 192 bindingOperation.setOperation(op); 193 binding.addBindingOperation(bindingOperation); 194 } 195 } 196 197 private BindingInput getBindingInput(Input input, String operationName) throws ToolException { 198 BindingInput bi = wsdlDefinition.createBindingInput(); 199 bi.setName(input.getName()); 200 bi.addExtensibilityElement(getXMLBody(BindingInput.class, operationName)); 202 return bi; 203 } 204 205 private BindingOutput getBindingOutput(Output output, String operationName) throws ToolException { 206 BindingOutput bo = wsdlDefinition.createBindingOutput(); 207 bo.setName(output.getName()); 208 bo.addExtensibilityElement(getXMLBody(BindingOutput.class, operationName)); 209 return bo; 210 } 211 212 private void addXMLFaults(Operation op, BindingOperation bo) { 213 } 215 216 private XMLFormat getXMLBody(Class clz, String operationName) throws ToolException { 217 if (extReg == null) { 218 extReg = wsdlFactory.newPopulatedExtensionRegistry(); 219 } 220 XMLFormat xmlFormat = null; 221 try { 222 xmlFormat = (XMLFormat)extReg.createExtension(clz, ToolConstants.XML_FORMAT); 223 } catch (WSDLException wse) { 224 Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG); 225 throw new ToolException(msg); 226 } 227 xmlFormat.setRootNode(new QName (wsdlDefinition.getTargetNamespace(), operationName)); 228 return xmlFormat; 229 } 230 231 private void doAppendService() throws ToolException { 232 if (service == null) { 233 service = wsdlDefinition.createService(); 234 service 235 .setQName(new QName (WSDLConstants.WSDL_PREFIX, (String )env.get(ToolConstants.CFG_SERVICE))); 236 } 237 if (port == null) { 238 port = wsdlDefinition.createPort(); 239 port.setName((String )env.get(ToolConstants.CFG_PORT)); 240 port.setBinding(binding); 241 } 242 setAddrElement(); 243 service.addPort(port); 244 wsdlDefinition.addService(service); 245 } 246 247 private void setAddrElement() throws ToolException { 248 extReg = this.wsdlReader.getExtensionRegistry(); 249 if (extReg == null) { 250 extReg = wsdlFactory.newPopulatedExtensionRegistry(); 251 } 252 XMLHttpAddress xmlHttpAddress = null; 253 try { 254 xmlHttpAddress = (XMLHttpAddress)extReg.createExtension(Port.class, 255 WSDLConstants.NS_XMLHTTP_BINDING_ADDRESS); 256 } catch (WSDLException wse) { 257 Message msg = new Message("FAIl_TO_CREATE_SOAPADDRESS", LOG); 258 throw new ToolException(msg); 259 } 260 if (env.get(ToolConstants.CFG_ADDRESS) != null) { 261 xmlHttpAddress.setLocation((String )env.get(ToolConstants.CFG_ADDRESS)); 262 } else { 263 xmlHttpAddress.setLocation(HTTP_PREFIX + "/" + env.get(ToolConstants.CFG_SERVICE) + "/" 264 + env.get(ToolConstants.CFG_PORT)); 265 } 266 port.addExtensibilityElement(xmlHttpAddress); 267 } 268 269 private void writeToWSDL() throws ToolException { 270 WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter(); 271 Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER); 272 try { 273 wsdlWriter.writeWSDL(wsdlDefinition, outputWriter); 274 } catch (WSDLException wse) { 275 Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG); 276 throw new ToolException(msg); 277 } 278 try { 279 outputWriter.close(); 280 } catch (IOException ioe) { 281 Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG); 282 throw new ToolException(msg); 283 } 284 } 285 286 } 287 | Popular Tags |