1 7 8 package org.jboss.webservice.client; 10 11 13 import org.jboss.axis.EngineConfiguration; 14 import org.jboss.axis.client.AxisClient; 15 import org.jboss.axis.wsdl.gen.Parser; 16 import org.jboss.logging.Logger; 17 import org.jboss.webservice.EngineConfigurationFinder; 18 import org.jboss.webservice.deployment.BeanXMLMetaData; 19 import org.jboss.webservice.deployment.ServiceDescription; 20 import org.jboss.webservice.deployment.TypeMappingDescription; 21 import org.jboss.webservice.deployment.WSDDGenerator; 22 import org.jboss.webservice.encoding.ser.MetaDataBeanDeserializerFactory; 23 import org.jboss.webservice.encoding.ser.MetaDataBeanSerializerFactory; 24 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping; 25 import org.jboss.webservice.metadata.jaxrpcmapping.ServiceEndpointInterfaceMapping; 26 27 import javax.wsdl.Definition; 28 import javax.wsdl.Port; 29 import javax.xml.namespace.QName ; 30 import javax.xml.rpc.Call ; 31 import javax.xml.rpc.ServiceException ; 32 import javax.xml.rpc.encoding.DeserializerFactory ; 33 import javax.xml.rpc.encoding.SerializerFactory ; 34 import javax.xml.rpc.encoding.TypeMapping ; 35 import javax.xml.rpc.encoding.TypeMappingRegistry ; 36 import javax.xml.rpc.handler.HandlerRegistry ; 37 import java.io.PrintWriter ; 38 import java.io.StringWriter ; 39 import java.lang.reflect.Constructor ; 40 import java.lang.reflect.InvocationHandler ; 41 import java.lang.reflect.InvocationTargetException ; 42 import java.lang.reflect.Proxy ; 43 import java.net.URL ; 44 import java.rmi.Remote ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 import java.util.Properties ; 49 50 57 public class ServiceImpl extends org.jboss.axis.client.Service 58 { 59 60 static final long serialVersionUID = -340351073471390974L; 61 private static final Logger log = Logger.getLogger(ServiceImpl.class); 63 64 private static final String DEFAULT_PORT = "DEFAULT_PORT"; 66 67 private String targetEndpointAddress; 69 private Map serviceDescMap = new HashMap (); 71 private JavaWsdlMapping javaWsdlMapping; 73 private Definition wsdlDefinition; 75 private Properties callProperties; 77 78 83 public ServiceImpl() 84 { 85 super(); 86 } 87 88 93 public ServiceImpl(QName serviceName) 94 { 95 super(serviceName); 96 if (serviceName == null) 97 throw new IllegalStateException ("service name cannot be null"); 98 } 99 100 108 public ServiceImpl(URL wsdlDoc, QName serviceName) throws ServiceException 109 { 110 super(wsdlDoc, serviceName); 111 if (serviceName == null) 112 throw new IllegalStateException ("service name cannot be null"); 113 } 114 115 121 public void initService(ServiceDescription serviceDesc, String portName) throws ServiceException 122 { 123 log.debug("initService: port=" + portName); 124 125 if (portName == null) 126 portName = DEFAULT_PORT; 127 128 if (wsdlDefinition != null && wsdlDefinition != serviceDesc.getWsdlDefinition()) 129 throw new IllegalArgumentException ("Cannot redefine the wsdl definition for this service"); 130 131 if (javaWsdlMapping != null && javaWsdlMapping != serviceDesc.getJavaWsdlMapping()) 132 throw new IllegalArgumentException ("Cannot redefine the jaxrpc-mapping definition for this service"); 133 134 if (serviceDescMap.get(portName) != null) 135 throw new IllegalArgumentException ("A service decription for this tport is already registered"); 136 137 wsdlDefinition = serviceDesc.getWsdlDefinition(); 138 javaWsdlMapping = serviceDesc.getJavaWsdlMapping(); 139 140 serviceDescMap.put(portName, serviceDesc); 141 142 if (log.isTraceEnabled()) 143 { 144 WSDDGenerator wsddGenerator = new WSDDGenerator(serviceDesc); 145 146 StringWriter sw = new StringWriter (); 147 PrintWriter pw = new PrintWriter (sw); 148 wsddGenerator.appendOperations(pw); 149 wsddGenerator.appendTypeMappings(pw); 150 151 log.trace("Service configuration:\n" + sw); 152 } 153 154 setupTypeMapping(serviceDesc); 155 } 156 157 162 public HandlerRegistry getHandlerRegistry() 163 { 164 throw new UnsupportedOperationException ("Components should not use the getHandlerRegistry() method."); 165 } 166 167 171 public TypeMappingRegistry getTypeMappingRegistry() 172 { 173 throw new UnsupportedOperationException ("Components should not use the getTypeMappingRegistry() method."); 174 } 175 176 178 public Remote getPort(Class seiClass) throws ServiceException 179 { 180 Remote port = (Remote )super.getPort(seiClass); 181 182 InvocationHandler handler = new PortProxy(port, seiClass); 184 ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); 185 Class [] ifaces = {seiClass, org.jboss.webservice.client.Stub.class}; 186 return (Remote )Proxy.newProxyInstance(contextCL, ifaces, handler); 187 } 188 189 191 public Remote getPort(QName portName, Class seiClass) throws ServiceException 192 { 193 Remote port = (Remote )super.getPort(portName, seiClass); 194 195 InvocationHandler handler = new PortProxy(port, seiClass); 197 ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); 198 Class [] ifaces = {seiClass, org.jboss.webservice.client.Stub.class}; 199 return (Remote )Proxy.newProxyInstance(contextCL, ifaces, handler); 200 } 201 202 protected Remote getGeneratedStub(QName portName, Class proxyInterface) throws ServiceException 203 { 204 Remote stub = super.getGeneratedStub(portName, proxyInterface); 207 if (stub != null) 208 throw new ServiceException ("Axis generated stubs not supported in WS4EE clients: " + stub.getClass().getName()); 209 210 return null; 211 } 212 213 219 public void setCallProperties(Properties callProperties) 220 { 221 this.callProperties = callProperties; 222 } 223 224 225 public ServiceDescription getServiceDescription(String portName) 226 { 227 ServiceDescription serviceDesc = null; 228 229 if (serviceDescMap.size() == 1 && DEFAULT_PORT.equals(serviceDescMap.keySet().iterator().next())) 230 serviceDesc = (ServiceDescription)serviceDescMap.values().iterator().next(); 231 232 else if (portName != null) 233 serviceDesc = (ServiceDescription)serviceDescMap.get(portName); 234 235 if (serviceDesc == null) 236 log.warn("Cannot get ServiceDescription for: portName=" + portName + " we have " + serviceDescMap.keySet()); 237 238 return serviceDesc; 239 } 240 241 public Definition getWsdlDefinition() 242 { 243 return wsdlDefinition; 244 } 245 246 public void setWsdlDefinition(Definition wsdlDefinition) 247 { 248 this.wsdlDefinition = wsdlDefinition; 249 } 250 251 public JavaWsdlMapping getJavaWsdlMapping() 252 { 253 return javaWsdlMapping; 254 } 255 256 public void setJavaWsdlMapping(JavaWsdlMapping javaWsdlMapping) 257 { 258 this.javaWsdlMapping = javaWsdlMapping; 259 } 260 261 267 public Call createCall() throws ServiceException 268 { 269 CallImpl call = new CallImpl(this); 270 if (callProperties != null) 271 { 272 Iterator keys = callProperties.keySet().iterator(); 274 while (keys.hasNext()) 275 { 276 String key = (String )keys.next(); 277 String value = callProperties.getProperty(key); 278 call.setProperty(key, value); 279 } 280 } 281 return call; 282 } 283 284 292 public Iterator getPorts() throws ServiceException 293 { 294 if (wsdlService == null) 297 throw new UnsupportedOperationException ("wsdl service is not available"); 298 299 return wsdlService.getPorts().keySet().iterator(); 300 } 301 302 305 public QName getServiceName() 306 { 307 if (serviceName != null) 308 return serviceName; 309 310 if (wsdlService == null) 313 throw new UnsupportedOperationException ("wsdl service is not available"); 314 315 return wsdlService.getQName(); 316 } 317 318 321 protected AxisClient getAxisClient() 322 { 323 if (engine == null) 324 { 325 engine = new ClientEngine(getEngineConfiguration()); 326 } 327 return (AxisClient)engine; 328 } 329 330 334 protected EngineConfiguration getEngineConfiguration() 335 { 336 if (config == null) 337 { 338 config = EngineConfigurationFinder.getClientEngineConfiguration(); 339 if (config == null) 340 throw new IllegalStateException ("Cannot obtain client config"); 341 } 342 343 return config; 344 } 345 346 349 protected Parser getParser() 350 { 351 Parser parser = super.getParser(); 352 parser.setNowrap(true); 353 return parser; 354 } 355 356 361 protected String getTargetEnpointAddress() 362 { 363 return targetEndpointAddress; 364 } 365 366 public void setTargetEndpointAddress(String targetEndpointAddress) 367 { 368 this.targetEndpointAddress = targetEndpointAddress; 369 } 370 371 383 protected Port getWSDLPort(Class seiClass) throws ServiceException 384 { 385 if (wsdlService == null) 387 return null; 388 389 Map wsdlPorts = wsdlService.getPorts(); 390 if (wsdlPorts == null || wsdlPorts.size() == 0) 391 throw new ServiceException ("Cannot obtain wsdl wsdlPorts for service: " + wsdlService.getQName()); 392 393 Port wsdlPort = null; 394 395 if (wsdlPorts.values().size() == 1) 397 { 398 wsdlPort = (Port)wsdlPorts.values().iterator().next(); 399 return wsdlPort; 400 } 401 402 if (javaWsdlMapping != null) 403 { 404 log.debug("Trying to get jaxrpc port mapping for: " + seiClass.getName()); 405 ServiceEndpointInterfaceMapping[] seiMappings = javaWsdlMapping.getServiceEndpointInterfaceMappings(); 406 for (int i = 0; wsdlPort == null && i < seiMappings.length; i++) 407 { 408 ServiceEndpointInterfaceMapping seiMapping = seiMappings[i]; 409 if (seiClass.getName().equals(seiMapping.getServiceEndpointInterface())) 410 { 411 QName bindingName = seiMapping.getWsdlBinding(); 412 Iterator it = wsdlPorts.values().iterator(); 413 while (wsdlPort == null && it.hasNext()) 414 { 415 Port auxPort = (Port)it.next(); 416 if (auxPort.getBinding().getQName().equals(bindingName)) 417 wsdlPort = auxPort; 418 } 419 } 420 } 421 } 422 423 if (wsdlPort == null) 424 { 425 log.warn("Cannot obtain jaxrpc port mapping for: " + seiClass.getName()); 426 wsdlPort = super.getWSDLPort(seiClass); 427 } 428 429 return wsdlPort; 430 } 431 432 435 private void setupTypeMapping(ServiceDescription serviceDesc) throws ServiceException 436 { 437 TypeMappingRegistry tmRegistry = super.getTypeMappingRegistry(); 438 if (tmRegistry == null) 439 throw new IllegalStateException ("Cannot obtain TypeMappingRegistry"); 440 441 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 443 444 Iterator it = serviceDesc.getTypMappings(); 445 while (it.hasNext()) 446 { 447 TypeMappingDescription typeMapping = (TypeMappingDescription)it.next(); 448 QName typeQName = typeMapping.getTypeQName(); 449 String javaType = typeMapping.getJavaType(); 450 String encodingURI = typeMapping.getEncodingURI(); 451 BeanXMLMetaData metaData = typeMapping.getMetaData(); 452 453 TypeMapping tm = tmRegistry.getTypeMapping(encodingURI); 454 455 SerializerFactory serFactory = null; 456 DeserializerFactory desFactory = null; 457 try 458 { 459 Class typeClass = typeMapping.loadJavaType(cl); 461 462 if (typeClass != null) 463 { 464 String serFactoryName = typeMapping.getSerializerFactoryName(); 465 String desFactoryName = typeMapping.getDeserializerFactoryName(); 466 467 if (tm.isRegistered(typeClass, typeQName) == false || typeMapping.isUserDefined()) 468 { 469 if (serFactoryName != null) 470 { 471 Class serFactoryClass = cl.loadClass(serFactoryName); 472 if (hasQualifiedConstructor(serFactoryClass)) 473 { 474 Constructor ctor = serFactoryClass.getConstructor(new Class []{Class .class, QName .class}); 475 serFactory = (SerializerFactory)ctor.newInstance(new Object []{typeClass, typeQName}); 476 477 if (serFactory instanceof MetaDataBeanSerializerFactory) 479 ((MetaDataBeanSerializerFactory)serFactory).setMetaData(metaData); 480 } 481 else 482 { 483 serFactory = (SerializerFactory)serFactoryClass.newInstance(); 484 } 485 } 486 487 if (desFactoryName != null) 488 { 489 Class desFactoryClass = cl.loadClass(desFactoryName); 490 if (hasQualifiedConstructor(desFactoryClass)) 491 { 492 Constructor ctor = desFactoryClass.getConstructor(new Class []{Class .class, QName .class}); 493 desFactory = (DeserializerFactory)ctor.newInstance(new Object []{typeClass, typeQName}); 494 495 if (desFactory instanceof MetaDataBeanDeserializerFactory) 497 ((MetaDataBeanDeserializerFactory)desFactory).setMetaData(metaData); 498 } 499 else 500 { 501 desFactory = (DeserializerFactory)desFactoryClass.newInstance(); 502 } 503 } 504 505 if (serFactory != null && desFactory != null) 506 { 507 log.debug("Register type mapping [qname=" + typeQName + ",class=" + javaType + "," + serFactoryName + "," + desFactoryName + "]"); 508 tm.register(typeClass, typeQName, serFactory, desFactory); 509 } 510 } 511 else 512 { 513 log.debug("Ignore type mapping [qname=" + typeQName + ",class=" + javaType + "," + serFactoryName + "," + desFactoryName + "]"); 514 } 515 } 516 } 517 catch (InvocationTargetException e) 518 { 519 log.error("Cannot setup type mapping", e.getTargetException()); 520 throw new ServiceException (e.getTargetException()); 521 } 522 catch (Exception e) 523 { 524 log.error("Cannot setup type mapping", e); 525 throw new ServiceException (e); 526 } 527 528 } 529 } 530 531 private boolean hasQualifiedConstructor(Class factoryClass) 532 { 533 try 534 { 535 Constructor ctor = factoryClass.getConstructor(new Class []{Class .class, QName .class}); 536 return ctor != null; 537 } 538 catch (Exception ignore) 539 { 540 } 541 542 return false; 543 } 544 } 545 | Popular Tags |