1 25 26 package org.objectweb.jonas_ws.deployment.api; 27 28 import java.io.StringWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Vector ; 35 36 import javax.wsdl.Definition; 37 import javax.wsdl.Message; 38 import javax.wsdl.Part; 39 import javax.wsdl.Port; 40 import javax.wsdl.Service; 41 import javax.wsdl.WSDLException; 42 import javax.wsdl.extensions.soap.SOAPAddress; 43 import javax.wsdl.extensions.soap.SOAPBinding; 44 import javax.wsdl.factory.WSDLFactory; 45 import javax.wsdl.xml.WSDLReader; 46 import javax.wsdl.xml.WSDLWriter; 47 import javax.xml.namespace.QName ; 48 49 import org.objectweb.jonas_lib.I18n; 50 51 56 public class WSDLFile { 57 58 59 60 private String name; 61 62 63 private List wsdlPorts = null; 64 65 66 private Definition def = null; 67 68 69 private static final boolean VERBOSE = false; 70 71 74 private static I18n i18n = I18n.getInstance(WSDLFile.class); 75 76 82 public WSDLFile(ClassLoader cl, String name) throws WSDeploymentDescException { 83 this(cl.getResource(name), name); 84 } 85 86 92 public WSDLFile(URL url, String name) throws WSDeploymentDescException { 93 this.name = name; 94 95 try { 96 WSDLFactory f = WSDLFactory.newInstance(); 97 WSDLReader r = f.newWSDLReader(); 98 99 r.setFeature("javax.wsdl.verbose", VERBOSE); r.setFeature("javax.wsdl.importDocuments", true); 102 if (url == null) { 103 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.notFound", name)); } 105 106 def = r.readWSDL(url.toExternalForm(), url.toExternalForm()); 107 } catch (WSDLException e) { 108 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.WSDLParsingError", name), e); } 110 111 wsdlPorts = new Vector (); 112 fillWsdlPorts(); 113 } 114 115 121 public boolean hasPort(QName portQName) { 122 return hasPort(portQName.getLocalPart()); 123 } 124 125 131 public boolean hasPort(String portName) { 132 return getPort(portName) != null; 133 } 134 135 142 public boolean hasService(QName srvQName) { 143 return def.getService(srvQName) != null; 144 } 145 146 151 public boolean hasSOAPHeader(QName shQName) { 152 Map msgs = def.getMessages(); 153 154 for (Iterator m = msgs.values().iterator(); m.hasNext();) { 155 Message msg = (Message) m.next(); 156 157 if (msg.getQName().getNamespaceURI() == shQName.getNamespaceURI()) { 158 Part p = msg.getPart(shQName.getLocalPart()); 159 160 if (p != null) { 161 return true; 162 } 163 } 164 } 165 166 return false; 167 } 168 169 175 public boolean hasPortsIncludedIn(List portList) { 176 return portList.containsAll(wsdlPorts); 177 } 178 179 185 public boolean hasSOAPBinding(QName portQName) { 186 boolean isSoapBinding = false; 187 Port port = getPort(portQName.getLocalPart()); 188 189 if (port != null) { 191 List ee = port.getBinding().getExtensibilityElements(); 192 Iterator eeIt = ee.iterator(); 193 194 while (eeIt.hasNext() && !isSoapBinding) { 195 Object elem = eeIt.next(); 197 198 if (elem != null) { 199 isSoapBinding = elem instanceof SOAPBinding; 200 } 201 } 202 } 203 204 return isSoapBinding; 205 } 206 207 211 public Definition getDefinition() { 212 return def; 213 } 214 215 219 public int getNbServices() { 220 return def.getServices().size(); 221 } 222 223 227 public QName getServiceQname() { 228 Map srvs = def.getServices(); 229 Iterator svcIt = srvs.values().iterator(); 230 QName res = null; 231 232 if (svcIt.hasNext()) { 233 Service svc = (Service) svcIt.next(); 234 res = svc.getQName(); 235 } 236 237 return res; 238 } 239 240 246 public URL getLocation(QName portQName) throws WSDeploymentDescException { 247 Port port = getPort(portQName.getLocalPart()); 248 249 if (port != null) { 251 List ee = port.getExtensibilityElements(); 252 Iterator eeIt = ee.iterator(); 253 254 while (eeIt.hasNext()) { 255 Object elem = eeIt.next(); 257 258 if ((elem != null) && elem instanceof SOAPAddress) { 259 try { 260 return new URL (((SOAPAddress) elem).getLocationURI()); 261 } catch (MalformedURLException e) { 262 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.MalformedPortLocation", portQName)); } 264 } 265 } 266 } 267 268 return null; 269 } 270 271 276 public void setLocation(QName portQName, URL loc) { 277 Port port = getPort(portQName.getLocalPart()); 278 279 if (port != null) { 280 List ee = port.getExtensibilityElements(); 281 Iterator eeIt = ee.iterator(); 282 283 while (eeIt.hasNext()) { 284 Object elem = eeIt.next(); 286 287 if ((elem != null) && elem instanceof SOAPAddress) { 288 ((SOAPAddress) elem).setLocationURI(loc.toString()); 289 } 290 } 291 } 292 } 293 294 297 private void fillWsdlPorts() { 298 Map svcs = def.getServices(); 299 Iterator svcsIt = svcs.values().iterator(); 300 301 while (svcsIt.hasNext()) { 302 Service svc = (Service) svcsIt.next(); 303 304 if (svc != null) { 305 for (Iterator j = svc.getPorts().values().iterator(); j.hasNext();) { 306 Port p = (Port) j.next(); 307 wsdlPorts.add(new QName (def.getTargetNamespace(), p.getName())); 308 } 309 } 310 } 311 } 312 313 319 private Port getPort(String portName) { 320 Map svcs = def.getServices(); 321 Iterator svcsIt = svcs.values().iterator(); 322 323 while (svcsIt.hasNext()) { 324 Service svc = (Service) svcsIt.next(); 325 326 if (svc != null) { 327 Port port = svc.getPort(portName); 328 329 return port; 331 } 332 } 333 334 return null; 335 } 336 337 341 public String getName() { 342 return name; 343 } 344 345 348 public String toString() { 349 StringBuffer sb = new StringBuffer (); 350 351 sb.append("\n" + getClass().getName()); sb.append("\ngetName()=" + getName()); 354 StringWriter sw = new StringWriter (); 355 try { 357 WSDLFactory factory = WSDLFactory.newInstance(); 358 WSDLWriter writer = factory.newWSDLWriter(); 359 360 writer.writeWSDL(def, sw); 361 362 } catch (WSDLException e) { 363 sb.append(getI18n().getMessage("WSDLFile.writeDefError")); } 365 366 sb.append(sw.getBuffer().toString()); 367 368 return sb.toString(); 369 } 370 371 379 public boolean equals(Object other) { 380 if (other == null) { 381 return false; 382 } 383 384 if (!(other instanceof WSDLFile)) { 385 return false; 386 } 387 388 WSDLFile ref = (WSDLFile) other; 389 Definition odef = ref.getDefinition(); 390 391 if (def.getServices().size() != odef.getServices().size()) { 392 return false; 393 } 394 395 if (def.getPortTypes().size() != odef.getPortTypes().size()) { 396 return false; 397 } 398 399 if (def.getMessages().size() != odef.getMessages().size()) { 400 return false; 401 } 402 403 if (def.getBindings().size() != odef.getBindings().size()) { 404 return false; 405 } 406 407 return true; 409 } 410 411 414 protected static I18n getI18n() { 415 return i18n; 416 } 417 } | Popular Tags |