00001
00026 package org.objectweb.jonas_ws.deployment.api;
00027
00028 import java.io.StringWriter;
00029 import java.net.MalformedURLException;
00030 import java.net.URL;
00031 import java.util.Iterator;
00032 import java.util.List;
00033 import java.util.Map;
00034 import java.util.Vector;
00035
00036 import javax.wsdl.Definition;
00037 import javax.wsdl.Message;
00038 import javax.wsdl.Part;
00039 import javax.wsdl.Port;
00040 import javax.wsdl.Service;
00041 import javax.wsdl.WSDLException;
00042 import javax.wsdl.extensions.soap.SOAPAddress;
00043 import javax.wsdl.extensions.soap.SOAPBinding;
00044 import javax.wsdl.factory.WSDLFactory;
00045 import javax.wsdl.xml.WSDLReader;
00046 import javax.wsdl.xml.WSDLWriter;
00047 import javax.xml.namespace.QName;
00048
00049 import org.objectweb.jonas_lib.I18n;
00050
00056 public class WSDLFile {
00057
00058
00060 private String name;
00061
00063 private List wsdlPorts = null;
00064
00066 private Definition def = null;
00067
00069 private static final boolean VERBOSE = false;
00070
00074 private static I18n i18n = I18n.getInstance(WSDLFile.class);
00075
00082 public WSDLFile(ClassLoader cl, String name) throws WSDeploymentDescException {
00083 this(cl.getResource(name), name);
00084 }
00085
00092 public WSDLFile(URL url, String name) throws WSDeploymentDescException {
00093 this.name = name;
00094
00095 try {
00096 WSDLFactory f = WSDLFactory.newInstance();
00097 WSDLReader r = f.newWSDLReader();
00098
00099 r.setFeature("javax.wsdl.verbose", VERBOSE);
00100 r.setFeature("javax.wsdl.importDocuments", true);
00101
00102 if (url == null) {
00103 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.notFound", name));
00104 }
00105
00106 def = r.readWSDL(url.toExternalForm(), url.toExternalForm());
00107 } catch (WSDLException e) {
00108 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.WSDLParsingError", name), e);
00109 }
00110
00111 wsdlPorts = new Vector();
00112 fillWsdlPorts();
00113 }
00114
00121 public boolean hasPort(QName portQName) {
00122 return hasPort(portQName.getLocalPart());
00123 }
00124
00131 public boolean hasPort(String portName) {
00132 return getPort(portName) != null;
00133 }
00134
00142 public boolean hasService(QName srvQName) {
00143 return def.getService(srvQName) != null;
00144 }
00145
00151 public boolean hasSOAPHeader(QName shQName) {
00152 Map msgs = def.getMessages();
00153
00154 for (Iterator m = msgs.values().iterator(); m.hasNext();) {
00155 Message msg = (Message) m.next();
00156
00157 if (msg.getQName().getNamespaceURI() == shQName.getNamespaceURI()) {
00158 Part p = msg.getPart(shQName.getLocalPart());
00159
00160 if (p != null) {
00161 return true;
00162 }
00163 }
00164 }
00165
00166 return false;
00167 }
00168
00175 public boolean hasPortsIncludedIn(List portList) {
00176 return portList.containsAll(wsdlPorts);
00177 }
00178
00185 public boolean hasSOAPBinding(QName portQName) {
00186 boolean isSoapBinding = false;
00187 Port port = getPort(portQName.getLocalPart());
00188
00189
00190 if (port != null) {
00191 List ee = port.getBinding().getExtensibilityElements();
00192 Iterator eeIt = ee.iterator();
00193
00194 while (eeIt.hasNext() && !isSoapBinding) {
00195
00196 Object elem = eeIt.next();
00197
00198 if (elem != null) {
00199 isSoapBinding = elem instanceof SOAPBinding;
00200 }
00201 }
00202 }
00203
00204 return isSoapBinding;
00205 }
00206
00211 public Definition getDefinition() {
00212 return def;
00213 }
00214
00219 public int getNbServices() {
00220 return def.getServices().size();
00221 }
00222
00227 public QName getServiceQname() {
00228 Map srvs = def.getServices();
00229 Iterator svcIt = srvs.values().iterator();
00230 QName res = null;
00231
00232 if (svcIt.hasNext()) {
00233 Service svc = (Service) svcIt.next();
00234 res = svc.getQName();
00235 }
00236
00237 return res;
00238 }
00239
00246 public URL getLocation(QName portQName) throws WSDeploymentDescException {
00247 Port port = getPort(portQName.getLocalPart());
00248
00249
00250 if (port != null) {
00251 List ee = port.getExtensibilityElements();
00252 Iterator eeIt = ee.iterator();
00253
00254 while (eeIt.hasNext()) {
00255
00256 Object elem = eeIt.next();
00257
00258 if ((elem != null) && elem instanceof SOAPAddress) {
00259 try {
00260 return new URL(((SOAPAddress) elem).getLocationURI());
00261 } catch (MalformedURLException e) {
00262 throw new WSDeploymentDescException(getI18n().getMessage("WSDLFile.MalformedPortLocation", portQName));
00263 }
00264 }
00265 }
00266 }
00267
00268 return null;
00269 }
00270
00276 public void setLocation(QName portQName, URL loc) {
00277 Port port = getPort(portQName.getLocalPart());
00278
00279 if (port != null) {
00280 List ee = port.getExtensibilityElements();
00281 Iterator eeIt = ee.iterator();
00282
00283 while (eeIt.hasNext()) {
00284
00285 Object elem = eeIt.next();
00286
00287 if ((elem != null) && elem instanceof SOAPAddress) {
00288 ((SOAPAddress) elem).setLocationURI(loc.toString());
00289 }
00290 }
00291 }
00292 }
00293
00297 private void fillWsdlPorts() {
00298 Map svcs = def.getServices();
00299 Iterator svcsIt = svcs.values().iterator();
00300
00301 while (svcsIt.hasNext()) {
00302 Service svc = (Service) svcsIt.next();
00303
00304 if (svc != null) {
00305 for (Iterator j = svc.getPorts().values().iterator(); j.hasNext();) {
00306 Port p = (Port) j.next();
00307 wsdlPorts.add(new QName(def.getTargetNamespace(), p.getName()));
00308 }
00309 }
00310 }
00311 }
00312
00319 private Port getPort(String portName) {
00320 Map svcs = def.getServices();
00321 Iterator svcsIt = svcs.values().iterator();
00322
00323 while (svcsIt.hasNext()) {
00324 Service svc = (Service) svcsIt.next();
00325
00326 if (svc != null) {
00327 Port port = svc.getPort(portName);
00328
00329
00330 return port;
00331 }
00332 }
00333
00334 return null;
00335 }
00336
00341 public String getName() {
00342 return name;
00343 }
00344
00348 public String toString() {
00349 StringBuffer sb = new StringBuffer();
00350
00351 sb.append("\n" + getClass().getName());
00352 sb.append("\ngetName()=" + getName());
00353
00354 StringWriter sw = new StringWriter();
00355
00356 try {
00357 WSDLFactory factory = WSDLFactory.newInstance();
00358 WSDLWriter writer = factory.newWSDLWriter();
00359
00360 writer.writeWSDL(def, sw);
00361
00362 } catch (WSDLException e) {
00363 sb.append(getI18n().getMessage("WSDLFile.writeDefError"));
00364 }
00365
00366 sb.append(sw.getBuffer().toString());
00367
00368 return sb.toString();
00369 }
00370
00379 public boolean equals(Object other) {
00380 if (other == null) {
00381 return false;
00382 }
00383
00384 if (!(other instanceof WSDLFile)) {
00385 return false;
00386 }
00387
00388 WSDLFile ref = (WSDLFile) other;
00389 Definition odef = ref.getDefinition();
00390
00391 if (def.getServices().size() != odef.getServices().size()) {
00392 return false;
00393 }
00394
00395 if (def.getPortTypes().size() != odef.getPortTypes().size()) {
00396 return false;
00397 }
00398
00399 if (def.getMessages().size() != odef.getMessages().size()) {
00400 return false;
00401 }
00402
00403 if (def.getBindings().size() != odef.getBindings().size()) {
00404 return false;
00405 }
00406
00407
00408 return true;
00409 }
00410
00414 protected static I18n getI18n() {
00415 return i18n;
00416 }
00417 }