1 55 56 package org.jboss.axis.providers.java; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.AxisFault; 60 import org.jboss.axis.Constants; 61 import org.jboss.axis.Handler; 62 import org.jboss.axis.Message; 63 import org.jboss.axis.MessageContext; 64 import org.jboss.axis.description.ServiceDesc; 65 import org.jboss.axis.encoding.TypeMapping; 66 import org.jboss.axis.enums.Scope; 67 import org.jboss.axis.handlers.soap.SOAPService; 68 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 69 import org.jboss.axis.providers.BasicProvider; 70 import org.jboss.axis.session.Session; 71 import org.jboss.axis.utils.ClassUtils; 72 import org.jboss.axis.utils.Messages; 73 import org.jboss.axis.utils.cache.ClassCache; 74 import org.jboss.axis.utils.cache.JavaClass; 75 import org.jboss.axis.wsdl.fromJava.Emitter; 76 import org.jboss.logging.Logger; 77 import org.w3c.dom.Document ; 78 import org.xml.sax.SAXException ; 79 80 import javax.xml.rpc.JAXRPCException ; 81 import javax.xml.rpc.holders.IntHolder ; 82 import javax.xml.rpc.server.ServiceLifecycle ; 83 import java.util.ArrayList ; 84 import java.util.StringTokenizer ; 85 86 94 public abstract class JavaProvider extends BasicProvider 95 { 96 private static Logger log = Logger.getLogger(JavaProvider.class.getName()); 97 98 public static final String OPTION_CLASSNAME = "className"; 99 public static final String OPTION_ALLOWEDMETHODS = "allowedMethods"; 100 public static final String OPTION_IS_STATIC = "isStatic"; 101 public static final String OPTION_CLASSPATH = "classPath"; 102 public static final String OPTION_WSDL_PORTTYPE = "wsdlPortType"; 103 public static final String OPTION_WSDL_SERVICEELEMENT = "wsdlServiceElement"; 104 public static final String OPTION_WSDL_SERVICEPORT = "wsdlServicePort"; 105 public static final String OPTION_WSDL_TARGETNAMESPACE = "wsdlTargetNamespace"; 106 public static final String OPTION_WSDL_INPUTSCHEMA = "wsdlInputSchema"; 107 108 public static final String OPTION_SCOPE = "scope"; 109 110 114 public Object getServiceObject(MessageContext msgContext, 115 Handler service, 116 String clsName, 117 IntHolder scopeHolder) 118 throws Exception 119 { 120 String serviceName = msgContext.getService().getName(); 121 122 Scope scope = Scope.getScope((String )service.getOption(OPTION_SCOPE), Scope.DEFAULT); 125 126 scopeHolder.value = scope.getValue(); 127 128 if (scope == Scope.REQUEST) 129 { 130 return getNewServiceObject(msgContext, clsName); 132 } 133 else if (scope == Scope.SESSION) 134 { 135 if (serviceName == null) 137 serviceName = msgContext.getService().toString(); 138 139 Session session = msgContext.getSession(); 141 if (session != null) 142 { 143 return getSessionServiceObject(session, serviceName, 144 msgContext, clsName); 145 } 146 else 147 { 148 scopeHolder.value = Scope.DEFAULT.getValue(); 150 return getNewServiceObject(msgContext, clsName); 151 } 152 } 153 else if (scope == Scope.APPLICATION) 154 { 155 AxisEngine engine = msgContext.getAxisEngine(); 157 Session appSession = engine.getApplicationSession(); 158 if (appSession != null) 159 { 160 return getSessionServiceObject(appSession, serviceName, 161 msgContext, clsName); 162 } 163 else 164 { 165 scopeHolder.value = Scope.DEFAULT.getValue(); 168 return getNewServiceObject(msgContext, clsName); 169 } 170 } 171 else 172 { 173 return null; 175 } 176 } 177 178 181 class LockObject 182 { 183 private boolean completed = false; 184 185 synchronized void waitUntilComplete() throws InterruptedException 186 { 187 while (!completed) 188 { 189 wait(); 190 } 191 } 192 193 synchronized void complete() 194 { 195 completed = true; 196 notifyAll(); 197 } 198 } 199 200 206 private Object getSessionServiceObject(Session session, 207 String serviceName, 208 MessageContext msgContext, 209 String clsName) throws Exception 210 { 211 Object obj = null; 212 boolean makeNewObject = false; 213 214 synchronized (session.getLockObject()) 216 { 217 obj = session.get(serviceName); 219 220 if (obj == null) 224 { 225 obj = new LockObject(); 226 makeNewObject = true; 227 session.set(serviceName, obj); 228 } 229 } 230 231 235 if (LockObject.class == obj.getClass()) 236 { 237 LockObject lock = (LockObject)obj; 238 239 if (makeNewObject) 243 { 244 try 245 { 246 obj = getNewServiceObject(msgContext, clsName); 247 session.set(serviceName, obj); 248 } 249 finally 250 { 251 lock.complete(); 252 } 253 } 254 else 255 { 256 lock.waitUntilComplete(); 259 260 obj = session.get(serviceName); 263 } 264 } 265 266 return obj; 267 } 268 269 277 private Object getNewServiceObject(MessageContext msgContext, 278 String clsName) throws Exception 279 { 280 Object serviceObject = makeNewServiceObject(msgContext, clsName); 281 if (serviceObject != null && 282 serviceObject instanceof ServiceLifecycle ) 283 { 284 ((ServiceLifecycle )serviceObject).init(msgContext.getProperty(Constants.MC_SERVLET_ENDPOINT_CONTEXT)); 285 } 286 return serviceObject; 287 } 288 289 297 public abstract void processMessage(MessageContext msgContext, 298 SOAPEnvelopeAxisImpl reqEnv, 299 SOAPEnvelopeAxisImpl resEnv, 300 Object obj) 301 throws Exception ; 302 303 304 309 public void invoke(MessageContext msgContext) throws AxisFault 310 { 311 if (log.isDebugEnabled()) 312 log.debug("Enter: JavaProvider::invoke (" + this + ")"); 313 314 315 316 String serviceName = msgContext.getTargetService(); 317 Handler service = msgContext.getService(); 318 319 320 321 String clsName = getServiceClassName(service); 322 323 if ((clsName == null) || clsName.equals("")) 324 { 325 throw new AxisFault("Server.NoClassForService", 326 Messages.getMessage("noOption00", getServiceClassNameOptionName(), serviceName), 327 null, null); 328 } 329 330 IntHolder scope = new IntHolder (); 331 Object serviceObject = null; 332 333 try 334 { 335 serviceObject = getServiceObject(msgContext, service, clsName, scope); 336 337 Message resMsg = msgContext.getResponseMessage(); 338 SOAPEnvelopeAxisImpl resEnv; 339 340 if (resMsg == null) 343 { 344 resEnv = new SOAPEnvelopeAxisImpl(msgContext.getSOAPConstants(), 345 msgContext.getSchemaVersion()); 346 347 resMsg = new Message(resEnv); 348 msgContext.setResponseMessage(resMsg); 349 } 350 else 351 { 352 resEnv = resMsg.getSOAPEnvelope(); 353 } 354 355 Message reqMsg = msgContext.getRequestMessage(); 356 SOAPEnvelopeAxisImpl reqEnv = reqMsg.getSOAPEnvelope(); 357 358 processMessage(msgContext, reqEnv, resEnv, serviceObject); 359 } 360 catch (Exception ex) 361 { 362 processException(ex); 363 log.error("Detect unprocessed exception", ex); 364 throw AxisFault.makeFault(ex); 365 } 366 finally 367 { 368 if (serviceObject != null && 371 scope.value == Scope.REQUEST.getValue() && 372 serviceObject instanceof ServiceLifecycle ) 373 { 374 ((ServiceLifecycle )serviceObject).destroy(); 375 } 376 } 377 378 if (log.isDebugEnabled()) 379 log.debug("Exit: JavaProvider::invoke (" + this + ")"); 380 } 381 382 387 protected void processException(Exception ex) throws AxisFault 388 { 389 if (ex instanceof AxisFault) 390 { 391 throw (AxisFault)ex; 392 } 393 394 if (ex instanceof JAXRPCException ) 398 { 399 JAXRPCException rte = (JAXRPCException )ex; 400 if (rte.getLinkedCause() instanceof AxisFault) 401 throw (AxisFault)rte.getLinkedCause(); 402 403 AxisFault fault = AxisFault.makeFault(ex); 404 fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true"); 405 throw fault; 406 } 407 408 if (ex instanceof SAXException ) 409 { 410 SAXException exp = (SAXException )ex; 411 Exception real = exp.getException(); 412 if (real != null) 413 throw AxisFault.makeFault(real); 414 } 415 416 AxisFault fault = AxisFault.makeFault(ex); 417 throw fault; 418 } 419 420 426 public void generateWSDL(MessageContext msgContext) throws AxisFault 427 { 428 if (log.isDebugEnabled()) 429 log.debug("Enter: JavaProvider::generateWSDL (" + this + ")"); 430 431 432 433 SOAPService service = msgContext.getService(); 434 ServiceDesc serviceDesc = service.getInitializedServiceDesc(msgContext); 435 436 446 try 447 { 448 String locationUrl = 450 msgContext.getStrProp(MessageContext.WSDLGEN_SERV_LOC_URL); 451 452 if (locationUrl == null) 453 { 454 locationUrl = serviceDesc.getEndpointURL(); 456 } 457 458 if (locationUrl == null) 459 { 460 locationUrl = msgContext.getStrProp(MessageContext.TRANS_URL); 462 } 463 464 String interfaceNamespace = 466 msgContext.getStrProp(MessageContext.WSDLGEN_INTFNAMESPACE); 467 468 if (interfaceNamespace == null) 469 { 470 interfaceNamespace = serviceDesc.getDefaultNamespace(); 472 } 473 474 if (interfaceNamespace == null) 475 { 476 interfaceNamespace = locationUrl; 478 } 479 480 500 Emitter emitter = new Emitter(); 501 502 508 String alias = (String )service.getOption("alias"); 511 if (alias != null) emitter.setServiceElementName(alias); 512 513 emitter.setStyle(serviceDesc.getStyle()); 515 emitter.setUse(serviceDesc.getUse()); 516 517 emitter.setClsSmart(serviceDesc.getImplClass(), locationUrl); 518 519 String targetNamespace = (String )service.getOption(OPTION_WSDL_TARGETNAMESPACE); 522 if (targetNamespace == null || 523 targetNamespace.length() == 0) 524 { 525 targetNamespace = interfaceNamespace; 526 } 527 emitter.setIntfNamespace(targetNamespace); 528 529 emitter.setLocationUrl(locationUrl); 530 emitter.setServiceDesc(serviceDesc); 531 emitter.setTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry() 532 .getTypeMapping(serviceDesc.getUse().getEncoding())); 533 emitter.setDefaultTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry(). 534 getDefaultTypeMapping()); 535 536 String wsdlPortType = (String )service.getOption(OPTION_WSDL_PORTTYPE); 537 String wsdlServiceElement = (String )service.getOption(OPTION_WSDL_SERVICEELEMENT); 538 String wsdlServicePort = (String )service.getOption(OPTION_WSDL_SERVICEPORT); 539 540 if (wsdlPortType != null && wsdlPortType.length() > 0) 541 { 542 emitter.setPortTypeName(wsdlPortType); 543 } 544 if (wsdlServiceElement != null && wsdlServiceElement.length() > 0) 545 { 546 emitter.setServiceElementName(wsdlServiceElement); 547 } 548 if (wsdlServicePort != null && wsdlServicePort.length() > 0) 549 { 550 emitter.setServicePortName(wsdlServicePort); 551 } 552 553 String wsdlInputSchema = (String ) 554 service.getOption(OPTION_WSDL_INPUTSCHEMA); 555 if (null != wsdlInputSchema && wsdlInputSchema.length() > 0) 556 { 557 emitter.setInputSchema(wsdlInputSchema); 558 } 559 560 Document doc = emitter.emit(Emitter.MODE_ALL); 561 562 msgContext.setProperty("WSDL", doc); 563 } 564 catch (NoClassDefFoundError e) 565 { 566 log.info(Messages.getMessage("toAxisFault00"), e); 567 throw new AxisFault(e.toString(), e); 568 } 569 catch (Exception e) 570 { 571 log.info(Messages.getMessage("toAxisFault00"), e); 572 throw AxisFault.makeFault(e); 573 } 574 575 if (log.isDebugEnabled()) 576 log.debug("Exit: JavaProvider::generateWSDL (" + this + ")"); 577 } 578 579 private String getAllowedMethods(Handler service) 580 { 581 String val = (String )service.getOption(OPTION_ALLOWEDMETHODS); 582 if (val == null || val.length() == 0) 583 { 584 val = (String )service.getOption("methodName"); 586 } 587 return val; 588 } 589 590 597 601 protected Object makeNewServiceObject(MessageContext msgContext, String clsName) 602 throws Exception 603 { 604 605 Object serviceObject = null; 606 607 ClassLoader cl = msgContext.getClassLoader(); 608 609 ClassCache cache = msgContext.getAxisEngine().getClassCache(); 610 if (cache != null) 611 { 612 log.debug("makeNewServiceObject from cache: " + clsName); 613 JavaClass jc = cache.lookup(clsName, cl); 614 serviceObject = jc.getJavaClass().newInstance(); 615 } 616 else 617 { 618 log.debug("makeNewServiceObject for name: " + clsName); 619 Class serviceClass = ClassUtils.forName(clsName, true, cl); 620 serviceObject = serviceClass.newInstance(); 621 } 622 623 return serviceObject; 624 } 625 626 629 protected String getServiceClassName(Handler service) 630 { 631 return (String )service.getOption(getServiceClassNameOptionName()); 632 } 633 634 638 protected String getServiceClassNameOptionName() 639 { 640 return OPTION_CLASSNAME; 641 } 642 643 646 protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext) 647 throws AxisFault 648 { 649 650 ClassLoader cl = null; 651 Class serviceClass = null; 652 AxisEngine engine = service.getEngine(); 653 654 if (msgContext != null) 657 { 658 cl = msgContext.getClassLoader(); 659 } 660 else 661 { 662 cl = Thread.currentThread().getContextClassLoader(); 663 } 664 665 if (engine != null && engine.getClassCache() != null) 667 { 668 try 669 { 670 JavaClass jc = engine.getClassCache().lookup(clsName, cl); 671 serviceClass = jc.getJavaClass(); 672 } 673 catch (ClassNotFoundException e) 674 { 675 log.error(Messages.getMessage("exception00"), e); 676 throw new AxisFault(Messages.getMessage("noClassForService00", clsName), e); 677 } 678 } 679 680 if (serviceClass == null) 682 { 683 try 684 { 685 serviceClass = ClassUtils.forName(clsName, true, cl); 686 } 687 catch (ClassNotFoundException e) 688 { 689 log.error(Messages.getMessage("exception00"), e); 690 throw new AxisFault(Messages.getMessage("noClassForService00", clsName), e); 691 } 692 } 693 694 return serviceClass; 695 } 696 697 703 public void initServiceDesc(SOAPService service, MessageContext msgContext) 704 throws AxisFault 705 { 706 708 String clsName = getServiceClassName(service); 709 if (clsName == null) 710 { 711 throw new AxisFault(Messages.getMessage("noServiceClass")); 712 } 713 Class cls = getServiceClass(clsName, service, msgContext); 714 ServiceDesc serviceDescription = service.getServiceDescription(); 715 716 if (serviceDescription.getAllowedMethods() == null && service != null) 718 { 719 String allowedMethods = getAllowedMethods(service); 720 if (allowedMethods != null && !"*".equals(allowedMethods)) 721 { 722 ArrayList methodList = new ArrayList (); 723 StringTokenizer tokenizer = new StringTokenizer (allowedMethods, " ,"); 724 while (tokenizer.hasMoreTokens()) 725 { 726 methodList.add(tokenizer.nextToken()); 727 } 728 serviceDescription.setAllowedMethods(methodList); 729 } 730 } 731 732 serviceDescription.loadServiceDescByIntrospection(cls); 733 } 734 735 } 736 | Popular Tags |