1 23 24 package org.objectweb.jonas_ws.deployment.api; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.URL ; 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.Vector ; 37 import javax.xml.namespace.QName ; 38 import org.objectweb.jonas_ejb.lib.BeanNaming; 39 import org.objectweb.jonas_lib.I18n; 40 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 41 import org.objectweb.jonas_lib.deployment.api.HandlerDesc; 42 import org.objectweb.jonas_lib.deployment.xml.Handler; 43 import org.objectweb.jonas_lib.deployment.xml.JonasInitParam; 44 import org.objectweb.jonas_lib.deployment.xml.JonasPortComponentRef; 45 import org.objectweb.jonas_lib.deployment.xml.JonasServiceRef; 46 import org.objectweb.jonas_lib.deployment.xml.PortComponentRef; 47 import org.objectweb.jonas_lib.deployment.xml.ServiceRef; 48 import org.objectweb.jonas_ws.deployment.lib.MappingFileManager; 49 import org.objectweb.jonas_ws.deployment.lib.wrapper.MappingFileManagerWrapper; 50 import org.objectweb.jonas.common.Log; 51 import org.objectweb.jonas.server.Server; 52 import org.objectweb.util.monolog.api.BasicLevel; 53 import org.objectweb.util.monolog.api.Logger; 54 55 63 public class ServiceRefDesc { 64 65 68 private static final String JAVAX_XML_RPC_SERVICE = "javax.xml.rpc.Service"; 69 70 73 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 74 75 78 private static I18n i18n = I18n.getInstance(ServiceRefDesc.class); 79 80 81 private Hashtable params = new Hashtable (); 82 83 84 private Vector pcRefs = new Vector (); 85 86 87 private Vector hRefs = new Vector (); 88 89 90 private WSDLFile wsdl; 91 92 93 private WSDLFile altWsdl; 94 95 96 private MappingFile mapping; 97 98 99 private String name; 100 101 102 private Class serviceInterface; 103 104 105 private QName serviceQName = null; 106 107 108 private File moduleFile = null; 109 110 111 private String wsdlFileName = null; 112 113 114 private String mappingFileName = null; 115 116 117 private URL alternateWSDL = null; 118 119 120 private URL localWSDLURL = null; 121 122 123 private URL mappingFileURL = null; 124 125 135 public ServiceRefDesc(ClassLoader classLoader, ServiceRef sref, JonasServiceRef jsref, String filename) 136 throws WSDeploymentDescException { 137 138 if (filename != null) { 139 moduleFile = new File (filename); 140 } 141 142 ClassLoader loader = classLoader; 144 145 name = sref.getServiceRefName(); 147 148 if (sref.getServiceQname() != null) { 150 serviceQName = sref.getServiceQname().getQName(); 151 } 152 153 String si = sref.getServiceInterface().trim(); 155 156 try { 157 serviceInterface = loader.loadClass(si); 158 } catch (ClassNotFoundException cnf) { 159 throw new WSDeploymentDescException(getI18n().getMessage("ServiceRefDesc.serviceIntfNotFound", si), cnf); } 161 162 if (!javax.xml.rpc.Service .class.isAssignableFrom(serviceInterface)) { 164 String err = getI18n().getMessage("ServiceRefDesc.mustExtService", serviceInterface.getName()); throw new WSDeploymentDescException(err); 166 } 167 168 if (jsref != null) { 170 List jipl = jsref.getJonasInitParamList(); 171 172 for (int i = 0; i < jipl.size(); i++) { 173 JonasInitParam p = (JonasInitParam) jipl.get(i); 175 params.put(p.getParamName().trim(), p.getParamValue().trim()); 176 } 177 } 178 179 Map links = linkPCR2JPCR(sref, jsref); 181 List pcrl = sref.getPortComponentRefList(); 182 183 for (int i = 0; i < pcrl.size(); i++) { 184 PortComponentRef ref = (PortComponentRef) pcrl.get(i); 186 JonasPortComponentRef jref = (JonasPortComponentRef) links.get(ref.getServiceEndpointInterface()); 187 pcRefs.add(new PortComponentRefDesc(loader, ref, jref)); 188 } 189 190 List hl = sref.getHandlerList(); 192 Handler h = null; 193 194 for (int i = 0; i < hl.size(); i++) { 195 h = (Handler) hl.get(i); 198 try { 200 hRefs.add(new HandlerDesc(loader, h)); 201 } catch (DeploymentDescException dde) { 202 throw new WSDeploymentDescException(dde); 203 } 204 } 205 206 if (jsref != null && jsref.getAltWsdl() != null && isJonasRuntime()) { 208 if (logger.isLoggable(BasicLevel.DEBUG)) { 209 logger.log(BasicLevel.DEBUG, "loading alt-wsdl : " + jsref.getAltWsdl()); 210 } 211 try { 212 alternateWSDL = new URL (jsref.getAltWsdl()); 213 altWsdl = new WSDLFile(alternateWSDL, jsref.getAltWsdl()); 214 } catch (IOException e) { 215 String err = "Cannot load alternate WSDL Stream from " + jsref.getAltWsdl(); 216 throw new WSDeploymentDescException(err); 217 } 218 } 219 220 String wf = sref.getWsdlFile(); 222 if (wf != null) { 223 this.wsdlFileName = wf.trim(); 224 wsdl = new WSDLFile(loader, wsdlFileName); 225 226 localWSDLURL = loader.getResource(wsdlFileName); 227 } 228 229 if (getWSDLFile() != null) { 230 if ((getWSDLFile().getNbServices() > 1) && (serviceQName == null)) { 231 String err = getI18n().getMessage("ServiceRefDesc.serviceQnameNotDef", wsdlFileName, name); throw new WSDeploymentDescException(err); 233 } else if ((getWSDLFile().getNbServices() == 1) && (serviceQName == null)) { 234 serviceQName = getWSDLFile().getServiceQname(); 237 } else { 238 if (isJonasRuntime()) { 241 if (!getWSDLFile().hasService(serviceQName)) { 242 throw new WSDeploymentDescException(getI18n().getMessage( 243 "ServiceRefDesc.serviceNotFoundInWSDL", serviceQName, wsdlFileName)); } 245 } 246 } 247 248 } 249 250 String mf = sref.getJaxrpcMappingFile(); 252 253 if (mf != null) { 254 this.mappingFileName = mf.trim(); 255 this.mappingFileURL = loader.getResource(mappingFileName); 256 setMappingFile(loader); 257 } 258 259 if (!serviceInterface.getName().equals(JAVAX_XML_RPC_SERVICE) && (mapping == null)) { 261 throw new WSDeploymentDescException(getI18n().getMessage( 262 "ServiceRefDesc.mappingRequired", serviceInterface.getName())); } 264 265 validate(); 266 } 267 268 271 private boolean isJonasRuntime() { 272 273 if (Server.isStarted()) { 274 Throwable t = new Exception (); 277 StackTraceElement [] elem = t.getStackTrace(); 278 for (int i = 0; i < elem.length; i++) { 279 if ("org.objectweb.jonas_ws.wsgen.wrapper.WsGenWrapper".equals(elem[i].getClassName())) { 280 return false; 281 } 282 if ("org.objectweb.jonas_lib.genclientstub.wrapper.ClientGenStubWrapper".equals(elem[i].getClassName())) { 283 return false; 284 } 285 if ("org.objectweb.jonas_ejb.genic.wrapper.GenicServiceWrapper".equals(elem[i].getClassName())) { 286 return false; 287 } 288 } 289 return true; 290 291 } else { 292 ClassLoader cl = this.getClass().getClassLoader(); 294 try { 295 cl.loadClass("org.objectweb.jonas.server.Bootstrap"); 296 } catch (ClassNotFoundException e) { 297 return true; 300 } 301 return false; 303 } 304 } 305 306 311 private Map linkPCR2JPCR(ServiceRef sref, JonasServiceRef jsref) { 312 Map res = new HashMap (); 313 for (Iterator i = sref.getPortComponentRefList().iterator(); i.hasNext();) { 315 PortComponentRef pcr = (PortComponentRef) i.next(); 316 res.put(pcr.getServiceEndpointInterface(), null); 317 } 318 if (jsref != null) { 320 321 Set keys = res.keySet(); 323 324 for (Iterator i = jsref.getJonasPortComponentRefList().iterator(); i.hasNext();) { 326 JonasPortComponentRef jpcr = (JonasPortComponentRef) i.next(); 327 String sei = jpcr.getServiceEndpointInterface(); 328 329 if ((sei != null) && (keys.contains(sei))) { 330 res.put(sei, jpcr); 332 } else { 333 String err = "jonas-port-component-ref '" + sei + "' is not linked to any port-component-ref. It will be ignored."; 334 logger.log(BasicLevel.WARN, err); 335 } 336 } 337 } 338 return res; 339 } 340 341 345 public List getPortComponentRefs() { 346 return pcRefs; 347 } 348 349 353 public List getHandlerRefs() { 354 return hRefs; 355 } 356 357 361 public String getServiceRefName() { 362 return name; 363 } 364 365 369 public Class getServiceInterface() { 370 return serviceInterface; 371 } 372 373 377 public WSDLFile getWSDLFile() { 378 WSDLFile file = altWsdl; 380 if (file == null) { 381 file = wsdl; 382 } 383 return file; 384 } 385 386 390 public MappingFile getMappingFile() { 391 return mapping; 392 } 393 394 398 public Hashtable getParams() { 399 return params; 400 } 401 402 407 public String getParam(String name) { 408 return (String ) params.get(name); 409 } 410 411 415 public String getWsdlFileName() { 416 return wsdlFileName; 417 } 418 419 425 public QName getServiceQName() { 426 return serviceQName; 427 } 428 429 430 431 434 public int hashCode() { 435 return this.name.hashCode(); 436 } 437 443 public boolean equals(Object other) { 444 if (other == null) { 445 return false; 446 } 447 448 if (!(other instanceof ServiceRefDesc)) { 449 return false; 450 } 451 452 ServiceRefDesc sr = (ServiceRefDesc) other; 453 454 if (!mapping.equals(sr.getMappingFile())) { 455 return false; 456 } 457 458 if (!wsdl.equals(sr.getWSDLFile())) { 459 return false; 460 } 461 462 if (!params.equals(sr.getParams())) { 463 return false; 464 } 465 466 for (Iterator i = sr.getPortComponentRefs().iterator(); i.hasNext();) { 467 PortComponentRefDesc pcr = (PortComponentRefDesc) i.next(); 468 469 if (!pcRefs.contains(pcr)) { 470 return false; 471 } 472 } 473 474 for (Iterator i = sr.getHandlerRefs().iterator(); i.hasNext();) { 475 HandlerDesc hr = (HandlerDesc) i.next(); 476 477 if (!hRefs.contains(hr)) { 478 return false; 479 } 480 } 481 482 return true; 483 } 484 485 490 private void setMappingFile(ClassLoader loader) throws WSDeploymentDescException { 491 492 if (moduleFile != null) { 495 if (isRunningInClientContainer()) { 496 mapping = MappingFileManager.getInstance(moduleFile, mappingFileName); 497 } else { 498 mapping = MappingFileManagerWrapper.getMappingFile(moduleFile, mappingFileName); 499 } 500 } else { 501 InputStream is = null; 503 if (loader != null) { 504 is = loader.getResourceAsStream(mappingFileName); 505 if (is != null) { 506 if (isRunningInClientContainer()) { 508 mapping = MappingFileManager.getInstance(is, mappingFileName); 509 } else { 510 mapping = MappingFileManagerWrapper.getMappingFile(is, mappingFileName); 511 } 512 } else { 513 throw new WSDeploymentDescException(getI18n().getMessage( 514 "ServiceRefDesc.mappingFileNotFoundInLoader", mappingFileName)); } 516 } else { 517 throw new WSDeploymentDescException(getI18n().getMessage("ServiceRefDesc.mappingFileNotFound")); } 519 } 520 } 521 522 526 private boolean isRunningInClientContainer() { 527 return (System.getProperty("jonas.base") == null); 528 } 529 530 534 private void validate() throws WSDeploymentDescException { 535 538 if ((wsdlFileName != null) && ((mappingFileName == null) || ("".equals(mappingFileName)))) { 539 throw new WSDeploymentDescException(getI18n().getMessage( 541 "ServiceRefDesc.missingMappingFile", this.name)); } 543 544 for (int i = 0; i < hRefs.size(); i++) { 546 HandlerDesc myHRef = (HandlerDesc) hRefs.get(i); 547 List pnl = myHRef.getPortNames(); 548 549 for (int j = 0; j < pnl.size(); j++) { 550 if (!getWSDLFile().hasPort((String ) pnl.get(j))) { 551 throw new WSDeploymentDescException(getI18n().getMessage( 553 "ServiceRefDesc.undefinedPort", myHRef.getName(), pnl.get(j), wsdlFileName)); } 555 } 556 } 557 558 if (!serviceInterface.equals(javax.xml.rpc.Service .class)) { 561 if ((getWSDLFile() == null) || ((getWSDLFile() != null) && (getWSDLFile().getDefinition().getServices().values().size() == 0))) { 562 throw new WSDeploymentDescException(getI18n().getMessage( 563 "ServiceRefDesc.wsdlMissingInformation", serviceInterface.getName(), name)); } 565 566 String namespaceURI = serviceQName.getNamespaceURI(); 568 String realPackage = BeanNaming.getPackageName(serviceInterface.getName()); 569 570 if (!realPackage.equals(mapping.getMapping(namespaceURI))) { 572 throw new WSDeploymentDescException(getI18n().getMessage( 573 "ServiceRefDesc.needPackageMapping", mappingFileName, realPackage)); } 575 576 591 } 592 } 593 594 597 protected static I18n getI18n() { 598 return i18n; 599 } 600 601 604 public URL getAlternateWsdlURL() { 605 return alternateWSDL; 606 } 607 608 611 public URL getLocalWSDLURL() { 612 return localWSDLURL; 613 } 614 615 618 public URL getMappingFileURL() { 619 return mappingFileURL; 620 } 621 } | Popular Tags |