1 25 26 package org.objectweb.jonas.ws.axis; 27 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.StringWriter ; 31 import java.lang.reflect.Constructor ; 32 import java.lang.reflect.Proxy ; 33 import java.net.URL ; 34 import java.util.Hashtable ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 41 import javax.naming.BinaryRefAddr ; 42 import javax.naming.Context ; 43 import javax.naming.InitialContext ; 44 import javax.naming.Name ; 45 import javax.naming.NamingException ; 46 import javax.naming.RefAddr ; 47 import javax.naming.Reference ; 48 import javax.naming.StringRefAddr ; 49 import javax.wsdl.Definition; 50 import javax.wsdl.Port; 51 import javax.wsdl.Service; 52 import javax.wsdl.extensions.ExtensibilityElement; 53 import javax.wsdl.extensions.soap.SOAPAddress; 54 import javax.wsdl.factory.WSDLFactory; 55 import javax.wsdl.xml.WSDLReader; 56 import javax.xml.namespace.QName ; 57 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.NodeList ; 61 62 import org.apache.axis.EngineConfiguration; 63 import org.apache.axis.client.AxisClient; 64 import org.apache.axis.configuration.XMLStringProvider; 65 import org.apache.axis.deployment.wsdd.WSDDConstants; 66 import org.apache.axis.deployment.wsdd.WSDDProvider; 67 import org.apache.axis.utils.XMLUtils; 68 import org.apache.axis.wsdl.toJava.Utils; 69 70 import org.objectweb.jonas_lib.I18n; 71 import org.objectweb.jonas_lib.xml.XMLSerializer; 72 73 import org.objectweb.jonas_ws.deployment.api.MappingFile; 74 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc; 75 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc; 76 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 77 78 import org.objectweb.jonas.common.JNDIUtils; 79 import org.objectweb.jonas.common.Log; 80 import org.objectweb.jonas.ws.JServiceFactory; 81 import org.objectweb.jonas.ws.WSServiceException; 82 83 import org.objectweb.util.monolog.api.BasicLevel; 84 import org.objectweb.util.monolog.api.Logger; 85 86 92 public class JAxisServiceFactory implements JServiceFactory { 93 94 95 96 private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX); 97 98 99 private static I18n i18n = I18n.getInstance(JAxisServiceFactory.class); 100 101 102 private static final String AXIS_CLIENT_CONFIG_PARAM = "axis.clientConfigFile"; 103 104 105 private static final String CLIENT_CONFIG_WSDD = "org/objectweb/jonas/ws/axis/client-config.wsdd"; 106 107 108 private static final String JONAS_SERVICE_CLASSNAME = "org.objectweb.jonas.ws.axis.JService"; 109 110 112 115 public static final String REF_CLIENT_CONFIG = "client.config.wsdd"; 116 117 120 public static final String REF_SERVICE_WSDL = "service.wsdl.url"; 121 122 125 public static final String REF_SERVICE_QNAME = "service.qname"; 126 127 130 public static final String REF_SERVICE_PORT2WSDL = "port.2.wsdl.map"; 131 132 135 public static final String REF_SERVICE_CALL_PROPS = "service.call.properties"; 136 137 140 public static final String REF_SERVICE_STUB_PROPS = "service.stub.properties"; 141 142 145 public static final String REF_SERVICE_WSDL_PORT_LIST = "service.port.list"; 146 147 150 private static final Class [] SETENDPOINTADDRESS_SIG = new Class [] {java.lang.String .class, java.lang.String .class }; 151 152 154 157 public JAxisServiceFactory() { 158 QName javaURI = new QName (WSDDConstants.URI_WSDD_JAVA, WSDDNoopProvider.PROVIDER_NAME); 159 WSDDProvider.registerProvider(javaURI, new WSDDNoopProvider()); 160 } 161 162 169 public Reference getServiceReference(ServiceRefDesc sr, ClassLoader cl) throws WSServiceException { 170 171 String classname = createServiceClassname(sr); 172 173 logger.log(BasicLevel.DEBUG, "Service classname: '" + classname + "'"); 174 175 Reference ref = new Reference (classname, getClass().getName(), null); 177 178 Document base = loadAxisDeployment(CLIENT_CONFIG_WSDD, cl); 179 180 String clientConfig = sr.getParam(AXIS_CLIENT_CONFIG_PARAM); 182 183 if (clientConfig != null) { 185 Document doc = loadAxisDeployment(clientConfig, cl); 187 188 mergeAxisDeployment(base, doc); 190 } 191 192 String str = null; 194 try { 195 str = serializeDOM(base); 196 if (logger.isLoggable(BasicLevel.DEBUG)) { 197 logger.log(BasicLevel.DEBUG, "Client Descriptor file : \n" + str); 198 } 199 } catch (IOException ioe) { 200 throw new WSServiceException("Cannot serialize Document", ioe); 201 } 202 ref.add(new StringRefAddr (REF_CLIENT_CONFIG, str)); 203 204 for (Iterator i = sr.getPortComponentRefs().iterator(); i.hasNext();) { 206 PortComponentRefDesc pcr = (PortComponentRefDesc) i.next(); 207 PortComponentDesc referencedPortComponent = pcr.getPortComponentDesc(); 208 209 if (referencedPortComponent != null) { 211 212 logger.log(BasicLevel.DEBUG, "Find a port-component-link in port-component-ref" + pcr.getSEI()); 213 URL url = referencedPortComponent.getEndpointURL(); 215 if (url == null) { 216 try { 219 Context ic = new InitialContext (); 220 url = (URL ) ic.lookup(referencedPortComponent.getName()); 221 } catch (NamingException ne) { 222 throw new WSServiceException("Cannot find updated endpoint for port-component '" 223 + referencedPortComponent.getName() + "'", ne); 224 } 225 } 226 227 logger.log(BasicLevel.DEBUG, "Uptodate URL : '" + url + "?JWSDL' "); 228 ref.add(new StringRefAddr (REF_SERVICE_WSDL, url.toExternalForm() + "?JWSDL")); 229 } 230 } 231 232 if (sr.getAlternateWsdlURL() != null) { 234 logger.log(BasicLevel.DEBUG, "Using alternate WSDL URL : '" + sr.getAlternateWsdlURL() + "'"); 235 ref.add(new StringRefAddr (REF_SERVICE_WSDL, sr.getAlternateWsdlURL().toString())); 236 } else if (sr.getLocalWSDLURL() != null) { 237 logger.log(BasicLevel.DEBUG, "Using WSDL URL : '" + sr.getLocalWSDLURL() + "'"); 238 ref.add(new StringRefAddr (REF_SERVICE_WSDL, sr.getLocalWSDLURL().toExternalForm())); 239 } 240 241 if (sr.getServiceQName() != null) { 243 ref.add(new BinaryRefAddr (REF_SERVICE_QNAME, JNDIUtils.getBytesFromObject(sr.getServiceQName()))); 244 } 245 246 List ports = sr.getPortComponentRefs(); 248 if (!ports.isEmpty()) { 249 Map map = new Hashtable (); 250 for (Iterator i = ports.iterator(); i.hasNext();) { 251 PortComponentRefDesc pcrd = (PortComponentRefDesc) i.next(); 252 QName wsdlPort = pcrd.getWsdlPort(); 253 if (wsdlPort != null) { 254 map.put(pcrd.getSEI().getName(), wsdlPort); 255 } 256 } 257 if (!map.isEmpty()) { 258 ref.add(new BinaryRefAddr (REF_SERVICE_PORT2WSDL, JNDIUtils.getBytesFromObject(map))); 259 } 260 } 261 262 String portNames = null; 264 for (Iterator i = sr.getPortComponentRefs().iterator(); i.hasNext();) { 265 PortComponentRefDesc pcr = (PortComponentRefDesc) i.next(); 266 Properties cProps = pcr.getCallProperties(); 267 Properties sProps = pcr.getStubProperties(); 268 269 if (pcr.getWsdlPort() != null) { 270 String name = pcr.getWsdlPort().getLocalPart(); 273 if (!cProps.isEmpty()) { 274 ref.add(new BinaryRefAddr (REF_SERVICE_CALL_PROPS + "_" + name, JNDIUtils 275 .getBytesFromObject(cProps))); 276 } 277 if (!sProps.isEmpty()) { 278 ref.add(new BinaryRefAddr (REF_SERVICE_STUB_PROPS + "_" + name, JNDIUtils 279 .getBytesFromObject(sProps))); 280 } 281 if (!sProps.isEmpty() || !cProps.isEmpty()) { 282 if (portNames != null) { 283 portNames += "," + name; 285 } else { 286 portNames = name; 287 } 288 } 289 } 290 } 291 if (portNames != null) { 292 ref.add(new StringRefAddr (REF_SERVICE_WSDL_PORT_LIST, portNames)); 293 } 294 295 return ref; 296 } 297 298 303 private String serializeDOM(Document base) throws IOException { 304 StringWriter sw = new StringWriter (); 305 XMLSerializer ser = new XMLSerializer(base); 306 ser.serialize(sw); 307 return sw.getBuffer().toString(); 308 } 309 310 314 private void mergeAxisDeployment(Document base, Document doc) { 315 Element importedDeploymentElement = (Element) base.importNode(doc.getDocumentElement(), true); 316 NodeList list = importedDeploymentElement.getChildNodes(); 317 for (int i = 0; i < list.getLength(); i++) { 319 if (list.item(i) instanceof Element) { 320 base.getDocumentElement().appendChild(list.item(i)); 321 } 322 } 323 } 324 325 332 private String createServiceClassname(ServiceRefDesc sr) { 333 334 String intfName = sr.getServiceInterface().getName(); 335 336 if (intfName.equals("javax.xml.rpc.Service")) { 337 return JONAS_SERVICE_CLASSNAME; 339 } else { 340 QName qn = sr.getServiceQName(); 342 MappingFile mf = sr.getMappingFile(); 343 344 String p = (String ) mf.getMappings().get(qn.getNamespaceURI()); 345 346 String classname = ""; 347 if (p != null) { 348 classname = p + "." + Utils.xmlNameToJavaClass(qn.getLocalPart()) + "Locator"; 349 } else { 350 classname = Utils.xmlNameToJavaClass(qn.getLocalPart()) + "Locator"; 351 } 352 353 return classname; 354 } 355 356 } 357 358 366 private Document loadAxisDeployment(String filename, ClassLoader cl) throws WSServiceException { 367 368 InputStream is = cl.getResourceAsStream(filename); 369 370 372 if (is == null) { 373 String err = i18n.getMessage("JAxisServiceFactory.loadAxisDeployment.configNotFound", filename); 375 logger.log(BasicLevel.ERROR, err); 376 throw new WSServiceException(err); 377 } 378 379 Document doc = null; 380 try { 381 doc = XMLUtils.newDocument(is); 382 } catch (Exception e) { 383 String err = i18n.getMessage("JAxisServiceFactory.loadAxisDeployment.docCreation", filename); 384 logger.log(BasicLevel.DEBUG, err); 385 386 throw new WSServiceException(err, e); 387 } 388 389 return doc; 390 } 391 392 403 public Object getObjectInstance(Object refObject, Name name, Context nameCtx, Hashtable env) throws Exception { 404 405 JService instance = null; 406 Object proxy = null; 407 408 if (refObject instanceof Reference ) { 409 Reference ref = (Reference ) refObject; 410 411 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 413 logger.log(BasicLevel.DEBUG, "Context ClassLoader : " + cl); 414 415 Class serviceClass = cl.loadClass(ref.getClassName()); 417 418 BinaryRefAddr bRefQname = (BinaryRefAddr ) ref.get(REF_SERVICE_QNAME); 420 421 QName serviceQname = null; 422 if (bRefQname != null) { 423 serviceQname = (QName ) JNDIUtils.getObjectFromBytes((byte[]) bRefQname.getContent()); 424 } 425 426 RefAddr refServiceWSDL = ref.get(REF_SERVICE_WSDL); 427 428 String serviceWsdl = null; 429 if (refServiceWSDL != null) { 430 serviceWsdl = (String ) refServiceWSDL.getContent(); 431 } 432 433 if (JONAS_SERVICE_CLASSNAME.equals(serviceClass.getName())) { 435 436 logger.log(BasicLevel.DEBUG, "default service class"); 438 439 if ((serviceQname == null) && (serviceWsdl == null)) { 440 logger.log(BasicLevel.DEBUG, "Create a new Service instance without params"); 441 instance = new JService(); 443 444 } else if ((serviceQname != null) && (serviceWsdl == null)) { 445 logger.log(BasicLevel.DEBUG, "Create a new Service instance with only a QName " + serviceQname); 446 instance = new JService(serviceQname); 448 449 } else if ((serviceQname != null) && (serviceWsdl != null)) { 450 logger.log(BasicLevel.DEBUG, "Create a new Service instance with QName " + serviceQname 451 + "and WSDL " + serviceWsdl); 452 instance = new JService(serviceWsdl, serviceQname); 454 455 } else { 456 logger.log(BasicLevel.DEBUG, "Create a new Service instance with WSDL " + serviceWsdl); 457 logger.log(BasicLevel.DEBUG, "Should not occurs !!!"); 458 instance = new JService(); 461 } 462 463 } else { 464 logger.log(BasicLevel.DEBUG, "Create a new Generated Service instance"); 465 logger.log(BasicLevel.DEBUG, "serviceWSDL:" + serviceWsdl + " serviceQName:" + serviceQname); 466 467 Constructor ctr = serviceClass.getConstructor(new Class [] {String .class, QName .class}); 469 instance = (JService) ctr.newInstance(new Object [] {serviceWsdl, serviceQname}); 470 471 if (serviceWsdl != null) { 475 WSDLFactory factory = WSDLFactory.newInstance(); 476 WSDLReader reader = factory.newWSDLReader(); 477 reader.setFeature("javax.wsdl.importDocuments", true); 478 Definition def = reader.readWSDL(serviceWsdl); 479 480 Service service = def.getService(serviceQname); 481 Map ports = service.getPorts(); 482 java.lang.reflect.Method m = serviceClass.getMethod("setEndpointAddress", SETENDPOINTADDRESS_SIG); 483 for (Iterator i = ports.keySet().iterator(); i.hasNext();) { 484 String portName = (String ) i.next(); 485 Port port = service.getPort(portName); 486 String endpoint = getSOAPLocation(port); 487 m.invoke(instance, new Object [] {port.getName(), endpoint }); 488 } 489 } 490 } 491 492 BinaryRefAddr bRefp2w = (BinaryRefAddr ) ref.get(REF_SERVICE_PORT2WSDL); 494 495 if (bRefp2w != null) { 496 Map map = (Map ) JNDIUtils.getObjectFromBytes((byte[]) bRefp2w.getContent()); 497 instance.assignSEIClassnameToWSDLPort(map); 498 } 499 500 RefAddr portsRef = ref.get(REF_SERVICE_WSDL_PORT_LIST); 502 String listPorts = null; 503 if (portsRef != null) { 504 listPorts = (String ) portsRef.getContent(); 505 506 if (listPorts != null) { 507 StringTokenizer strPort = new StringTokenizer (listPorts, ","); 509 while (strPort.hasMoreTokens()) { 510 String port = strPort.nextToken(); 511 512 BinaryRefAddr bRefcp = (BinaryRefAddr ) ref.get(REF_SERVICE_CALL_PROPS + "_" + port); 514 515 if (bRefcp != null) { 516 Properties callProperties = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) bRefcp.getContent()); 517 instance.assignCallProperties(port, callProperties); 518 } 519 520 BinaryRefAddr bRefsp = (BinaryRefAddr ) ref.get(REF_SERVICE_STUB_PROPS + "_" + port); 522 523 if (bRefsp != null) { 524 Properties stubProperties = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) bRefsp.getContent()); 525 instance.assignStubProperties(port, stubProperties); 526 } 527 } 528 } 529 } 530 EngineConfiguration ec = getConfiguration(ref); 531 532 instance.setEngine(new AxisClient(ec)); 534 535 JServiceProxy handler = new JServiceProxy(instance); 536 Class [] serviceInterfaces = serviceClass.getInterfaces(); 537 Class [] interfaces = new Class [serviceInterfaces.length + 1]; 538 for (int i = 0; i < serviceInterfaces.length; i++) { 539 interfaces[i] = serviceInterfaces[i]; 540 } 541 interfaces[serviceInterfaces.length] = javax.xml.rpc.Service .class; 542 proxy = Proxy.newProxyInstance(cl, interfaces, handler); 543 544 } 545 546 return proxy; 547 } 548 549 553 private String getSOAPLocation(Port port) { 554 String endpoint = null; 555 List extensions = port.getExtensibilityElements(); 556 for (Iterator i = extensions.iterator(); i.hasNext();) { 557 ExtensibilityElement ext = (ExtensibilityElement) i.next(); 558 if (ext instanceof SOAPAddress) { 559 SOAPAddress addr = (SOAPAddress) ext; 560 endpoint = addr.getLocationURI(); 561 } 562 } 563 return endpoint; 564 } 565 566 572 private EngineConfiguration getConfiguration(Reference ref) throws Exception { 573 String conf = (String ) ref.get(REF_CLIENT_CONFIG).getContent(); 575 logger.log(BasicLevel.DEBUG, "loaded configuration : " + conf); 576 return new XMLStringProvider(conf); 577 } 578 579 } | Popular Tags |