1 57 58 package org.apache.soap.server; 59 60 import java.io.*; 61 import java.util.*; 62 import javax.xml.parsers.*; 63 import org.w3c.dom.*; 64 import org.xml.sax.*; 65 import org.apache.soap.Constants; 66 import org.apache.soap.encoding.*; 67 import org.apache.soap.rpc.*; 68 import org.apache.soap.server.http.*; 69 import org.apache.soap.util.xml.*; 70 71 77 public class DeploymentDescriptor implements Serializable { 78 public static final int SERVICE_TYPE_RPC = 0; 80 public static final int SERVICE_TYPE_MESSAGE = 1; 81 82 public static final int SCOPE_REQUEST = 0; 84 public static final int SCOPE_SESSION = 1; 85 public static final int SCOPE_APPLICATION = 2; 86 87 public static final byte PROVIDER_JAVA = (byte) 0; 89 public static final byte PROVIDER_SCRIPT_FILE = (byte) 1; 90 public static final byte PROVIDER_SCRIPT_STRING = (byte) 2; 91 public static final byte PROVIDER_USER_DEFINED = (byte) 3; 92 93 protected String id; 94 protected int serviceType = SERVICE_TYPE_RPC; 95 protected int scope; 96 protected byte providerType = -1; 97 protected String providerClass; 98 99 protected String serviceClass; protected Hashtable props ; 101 102 protected boolean isStatic; 103 protected String scriptFilenameOrString; 104 protected String scriptLanguage; 105 protected String [] methods; 106 protected TypeMapping[] mappings; 107 transient SOAPMappingRegistry cachedSMR; 108 protected String [] faultListener; 109 private transient SOAPFaultRouter fr; 110 111 protected boolean checkMustUnderstands = false; 117 118 private String defaultSMRClass = null; 119 124 public DeploymentDescriptor () { 125 } 126 127 130 131 public void setID (String id) { 132 this.id = id; 133 } 134 135 public String getID () { 136 return id; 137 } 138 139 public boolean getCheckMustUnderstands() 140 { 141 return checkMustUnderstands; 142 } 143 144 public void setCheckMustUnderstands(boolean doIt) 145 { 146 checkMustUnderstands = doIt; 147 } 148 149 157 public void setServiceType (int serviceType) { 158 this.serviceType = serviceType; 159 } 160 161 public int getServiceType () { 162 return serviceType; 163 } 164 165 168 public void setScope (int scope) { 169 this.scope = scope; 170 } 171 172 public int getScope () { 173 return scope; 174 } 175 176 public void setDefaultSMRClass(String _defaultSMRClass) { 177 defaultSMRClass = _defaultSMRClass; 178 } 179 180 public String getDefaultSMRClass() { 181 return defaultSMRClass; 182 } 183 184 187 public void setMethods (String [] methods) { 188 this.methods = methods; 189 } 190 191 public String [] getMethods () { 192 return methods; 193 } 194 195 198 public void setProviderType (byte providerType) { 199 this.providerType = providerType; 200 } 201 202 public byte getProviderType () { 203 return providerType; 204 } 205 206 209 public void setServiceClass (String serviceClass) { 210 this.serviceClass = serviceClass; 211 } 212 213 public String getServiceClass () { 214 return serviceClass; 215 } 216 217 public Hashtable getProps() { 218 return props ; 219 } 220 221 public void setProps(Hashtable props) { 222 this.props = props ; 223 } 224 225 228 public void setProviderClass (String providerClass) { 229 this.providerClass = providerClass; 230 } 231 232 public String getProviderClass () { 233 return providerClass; 234 } 235 236 239 public void setIsStatic (boolean isStatic) { 240 this.isStatic = isStatic; 241 } 242 243 public boolean getIsStatic () { 244 return isStatic; 245 } 246 247 public void setScriptLanguage (String scriptLanguage) { 248 this.scriptLanguage = scriptLanguage; 249 } 250 251 public String getScriptLanguage () { 252 return scriptLanguage; 253 } 254 255 public void setScriptFilenameOrString (String scriptFilenameOrString) { 256 this.scriptFilenameOrString = scriptFilenameOrString; 257 } 258 259 public String getScriptFilenameOrString () { 260 return scriptFilenameOrString; 261 } 262 263 269 public void setMappings (TypeMapping[] mappings) { 270 this.mappings = mappings; 271 } 272 273 276 public TypeMapping[] getMappings () { 277 return mappings; 278 } 279 280 284 private void setCachedSMR (SOAPMappingRegistry cachedSMR) { 285 this.cachedSMR = cachedSMR; 286 } 287 288 private SOAPMappingRegistry getCachedSMR () { 289 return cachedSMR; 290 } 291 292 293 public String [] getFaultListener() {return faultListener;} 294 295 public void setFaultListener(String [] _faultListener) {faultListener = _faultListener;} 296 297 298 public SOAPFaultRouter buildFaultRouter(SOAPContext ctxt) { 299 if (fr != null) return fr; 300 301 fr = new SOAPFaultRouter(); 302 303 if (faultListener == null) return fr; 304 305 306 SOAPFaultListener[] lis = new SOAPFaultListener[faultListener.length]; 307 try { 308 for (int i = 0; i < faultListener.length; i++) { 309 Class c = ctxt.loadClass( faultListener[i] ); 310 lis[i] = (SOAPFaultListener)c.newInstance(); 311 } 312 } 313 catch (Exception e) {} 314 315 fr.setFaultListener(lis); 316 return fr; 317 } 318 322 public void toXML(Writer pr) { 323 PrintWriter pw = new PrintWriter (pr); 324 325 pw.println ("<isd:service xmlns:isd=\"" + 326 Constants.NS_URI_XML_SOAP_DEPLOYMENT + "\" id=\"" + id + "\"" + 327 (serviceType != SERVICE_TYPE_RPC ? " type=\"message\"" : "") + 328 " checkMustUnderstands=\"" + (checkMustUnderstands ? "true" : 329 "false") + 330 "\"" + 331 ">"); 332 333 byte pt = providerType; 334 String [] scopes = {"Request", "Session", "Application"}; 335 String providerString = null ; 336 337 if ( pt == DeploymentDescriptor.PROVIDER_JAVA ) 338 providerString = "java" ; 339 else if ( pt == DeploymentDescriptor.PROVIDER_USER_DEFINED ) 340 providerString = serviceClass ; 341 else 342 providerString = "script" ; 343 344 pw.print (" <isd:provider type=\"" + providerString + 345 "\" scope=\"" + scopes[scope] + "\" methods=\""); 346 for (int i = 0; i < methods.length; i++) { 347 pw.print (methods[i]); 348 if (i < methods.length-1) { 349 pw.print (" "); 350 } 351 } 352 pw.println ("\">"); 353 if (pt == DeploymentDescriptor.PROVIDER_JAVA) { 354 pw.println (" <isd:java class=\"" + providerClass + 355 "\" static=\"" + (isStatic ? "true" : "false") + "\"/>"); 356 } else if (pt == DeploymentDescriptor.PROVIDER_SCRIPT_FILE || 357 pt == DeploymentDescriptor.PROVIDER_SCRIPT_STRING ) { 358 pw.print (" <isd:script language=\"" + scriptLanguage + "\""); 359 if (pt == DeploymentDescriptor.PROVIDER_SCRIPT_FILE) { 360 pw.println (" source=\"" + scriptFilenameOrString + "\"/>"); 361 } else { 362 pw.println (">"); 363 pw.println (" <![CDATA["); 364 pw.println (scriptFilenameOrString); 365 pw.println (" ]]>"); 366 pw.println (" </isd:script>"); 367 } 368 } 369 else if ( pt == DeploymentDescriptor.PROVIDER_USER_DEFINED && 370 providerClass != null ) { 371 pw.println (" <isd:java class=\"" + providerClass + 372 "\" static=\"" + (isStatic ? "true" : "false") + "\"/>"); 373 } 374 375 if ( props != null ) 376 for ( Enumeration e = props.keys() ; e.hasMoreElements(); ) { 377 String key = (String ) e.nextElement() ; 378 String value = (String ) props.get(key); 379 pw.println(" <isd:option key=\"" + key + "\" value=\"" + 380 value + "\" />" ); 381 } 382 383 pw.println (" </isd:provider>"); 384 385 if (faultListener != null) { 386 for (int i = 0; i < faultListener.length; i++) { 387 pw.println(" <isd:faultListener>" + 388 faultListener[i] + 389 "</isd:faultListener>"); 390 } 391 } 392 393 if (mappings != null) { 394 pw.print(" <isd:mappings"); 395 if (defaultSMRClass != null) { 396 pw.println(" defaultRegistryClass=\"" + defaultSMRClass + "\">"); 397 } else { 398 pw.println(">"); 399 } 400 401 for (int i = 0; i < mappings.length; i++) { 402 TypeMapping tm = mappings[i]; 403 404 pw.print (" <isd:map"); 405 406 if (tm.encodingStyle != null) { 407 pw.print (" encodingStyle=\"" + tm.encodingStyle +"\""); 408 } 409 410 if (tm.elementType != null) { 411 pw.print (" xmlns:x=\"" + tm.elementType.getNamespaceURI () + 412 "\" qname=\"x:" + tm.elementType.getLocalPart () + "\""); 413 } 414 415 if (tm.javaType != null) { 416 pw.print (" javaType=\"" + tm.javaType + "\""); 417 } 418 419 if (tm.xml2JavaClassName != null) { 420 pw.print (" xml2JavaClassName=\"" + tm.xml2JavaClassName + "\""); 421 } 422 423 if (tm.java2XMLClassName != null) { 424 pw.print (" java2XMLClassName=\"" + tm.java2XMLClassName + "\""); 425 } 426 427 pw.println ("/>"); 428 } 429 430 pw.println (" </isd:mappings>"); 431 } 432 433 pw.println ("</isd:service>"); 434 pw.flush (); 435 } 436 437 public static DeploymentDescriptor fromXML(Reader rd) 438 throws IllegalArgumentException { 439 if (rd == null) { 440 throw new IllegalArgumentException ("Reader passed to " + 441 "DeploymentDescriptor.fromXML(...) " + 442 "must not be null."); 443 } 444 445 try { 446 DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder(); 447 Document doc = xdb.parse(new InputSource(rd)); 448 449 if (doc != null) { 450 Element root = doc.getDocumentElement(); 451 452 return fromXML(root); 453 } else { 454 throw new Exception ("Document came back null."); 455 } 456 } catch (Exception e) { 457 throw new IllegalArgumentException ("Problem parsing " + 458 "deployment descriptor: " + 459 e); 460 } 461 } 462 463 467 public static DeploymentDescriptor fromXML(Element root) 468 throws IllegalArgumentException { 469 if ((root == null) || 470 !root.getNamespaceURI().equals (Constants.NS_URI_XML_SOAP_DEPLOYMENT) || 471 !root.getLocalName().equals ("service")) { 472 throw new IllegalArgumentException ("root is null or document element " + 473 "is not {" + 474 Constants.NS_URI_XML_SOAP_DEPLOYMENT + 475 "}service"); 476 } 477 478 DeploymentDescriptor dd = new DeploymentDescriptor (); 479 NodeList nl; 480 Element e; 481 482 String id = DOMUtils.getAttribute (root, "id"); 483 if (id == null) { 484 throw new IllegalArgumentException ("required 'id' attribute " + 485 "missing in deployment descriptor"); 486 } 487 dd.setID (id); 488 489 String checkMustUnderstands = DOMUtils.getAttribute(root, "checkMustUnderstands"); 491 if (checkMustUnderstands != null) { 492 if (checkMustUnderstands.equals("true")) 493 dd.checkMustUnderstands = true; 494 } 495 496 String serviceTypeStr = DOMUtils.getAttribute (root, "type"); 498 if (serviceTypeStr != null) { 499 if (serviceTypeStr.equals ("message")) { 500 dd.setServiceType (SERVICE_TYPE_MESSAGE); 501 } else { 502 throw new IllegalArgumentException ("unknown value for 'type' " + 503 "attribute: '" + serviceTypeStr + 504 "': bad deployment descriptor"); 505 } 506 } 507 508 nl = root.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 509 "provider"); 510 if ((nl == null) || nl.getLength () != 1) { 511 throw new IllegalArgumentException ("exactly one 'provider' element " + 512 "missing in deployment descriptor"); 513 } 514 e = (Element) nl.item (0); 515 String typeStr = DOMUtils.getAttribute (e, "type"); 516 String scopeStr = DOMUtils.getAttribute (e, "scope"); 517 String methodsStr = DOMUtils.getAttribute (e, "methods"); 518 if ((typeStr == null) || 519 (scopeStr == null) || 521 (!scopeStr.equals ("Request") && 522 !scopeStr.equals ("Session") && !scopeStr.equals ("Application")) || 523 (methodsStr == null) || methodsStr.equals ("")) { 524 throw new IllegalArgumentException ("invalid value for type or scope " + 525 "or methods attribute in provider " + 526 "element of deployment descriptor"); 527 } 528 529 int scope = -1; 530 String [] methods; 531 532 Element saved_E = e ; 533 nl = e.getElementsByTagNameNS(Constants.NS_URI_XML_SOAP_DEPLOYMENT, 534 "option" ); 535 for ( int i = 0 ; nl != null && i < nl.getLength() ; i++ ) { 536 String key, value ; 537 538 e = (Element) nl.item(i); 539 key = DOMUtils.getAttribute( e, "key" ); 540 value = DOMUtils.getAttribute( e, "value" ); 541 542 if ( key == null || key.equals("") ) 543 throw new IllegalArgumentException ("Missing 'key' attribute on " + 544 "'option' element in deployment " + 545 "desriptor" ); 546 if ( dd.props == null ) dd.props = new Hashtable(); 547 dd.props.put( key, value ); 548 } 549 e = saved_E ; 550 551 if (typeStr.equals ("java")) { 552 dd.setProviderType (DeploymentDescriptor.PROVIDER_JAVA); 553 nl = e.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 554 "java"); 555 if ((nl == null) || nl.getLength () != 1) { 556 throw new IllegalArgumentException ("exactly one 'java' element " + 557 "missing in deployment " + 558 "descriptor"); 559 } 560 e = (Element) nl.item (0); 561 String className = DOMUtils.getAttribute (e, "class"); 562 if (className == null) { 563 throw new IllegalArgumentException ("<java> element requires " + 564 "'class' attribute"); 565 } 566 dd.setProviderClass (className); 567 String isStatic = DOMUtils.getAttribute (e, "static"); 568 boolean isStaticBool = false; 569 if (isStatic != null) { 570 if (isStatic.equals ("false")) { 571 isStaticBool = false; 572 } else if (isStatic.equals ("true")) { 573 isStaticBool = true; 574 } else { 575 throw new IllegalArgumentException ("'static' attribute of " + 576 "<java> element must be " + 577 "true or false"); 578 } 579 } 580 dd.setIsStatic (isStaticBool); 581 582 } else if (typeStr.equals ("script")) { 583 nl = e.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 584 "script"); 585 if ((nl == null) || nl.getLength () != 1) { 586 throw new IllegalArgumentException ("exactly one 'script' element " + 587 "missing in deployment " + 588 "descriptor"); 589 } 590 e = (Element) nl.item (0); 591 dd.setScriptLanguage (DOMUtils.getAttribute (e, "language")); 592 String source = DOMUtils.getAttribute (e, "source"); 593 if (source != null) { 594 dd.setProviderType (DeploymentDescriptor.PROVIDER_SCRIPT_FILE); 595 dd.setScriptFilenameOrString (source); 596 } else { 597 dd.setProviderType (DeploymentDescriptor.PROVIDER_SCRIPT_STRING); 598 dd.setScriptFilenameOrString (DOMUtils.getChildCharacterData (e)); 599 } 600 } else { 601 dd.setProviderType (DeploymentDescriptor.PROVIDER_USER_DEFINED); 604 dd.setServiceClass (typeStr); 605 606 nl = e.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 608 "java"); 609 if ( nl != null ) { 610 if ( nl.getLength () > 1) { 611 throw new IllegalArgumentException ("exactly one 'java' element " + 612 "missing in deployment " + 613 "descriptor"); 614 } 615 if ( nl.getLength() != 0 ) { 616 e = (Element) nl.item (0); 617 String className = DOMUtils.getAttribute (e, "class"); 618 if (className == null) { 619 throw new IllegalArgumentException ("<java> element requires " + 620 "'class' attribute"); 621 } 622 dd.setProviderClass (className); 623 String isStatic = DOMUtils.getAttribute (e, "static"); 624 boolean isStaticBool = false; 625 if (isStatic != null) { 626 if (isStatic.equals ("false")) { 627 isStaticBool = false; 628 } else if (isStatic.equals ("true")) { 629 isStaticBool = true; 630 } else { 631 throw new IllegalArgumentException ("'static' attribute of " + 632 "<java> element must be " + 633 "true or false"); 634 } 635 } 636 dd.setIsStatic (isStaticBool); 637 } 639 } 640 } 641 642 if (scopeStr.equals ("Request")) { 643 scope = DeploymentDescriptor.SCOPE_REQUEST; 644 } else if (scopeStr.equals ("Session")) { 645 scope = DeploymentDescriptor.SCOPE_SESSION; 646 } else { scope = DeploymentDescriptor.SCOPE_APPLICATION; 648 } 649 dd.setScope (scope); 650 651 StringTokenizer st = new StringTokenizer (methodsStr); 652 int nTokens = st.countTokens (); 653 methods = new String [nTokens]; 654 for (int i = 0; i < nTokens; i++) { 655 methods[i] = st.nextToken (); 656 } 657 dd.setMethods (methods); 658 659 nl = root.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 661 "faultListener"); 662 String [] lis = new String [nl.getLength()]; 663 664 try { 665 for (int i = 0; i < nl.getLength(); i++) { 666 lis[i] = DOMUtils.getChildCharacterData((Element)nl.item(i)); 668 } 669 } 670 catch (Exception ex) { 671 throw new IllegalArgumentException (ex.getMessage()); 672 } 673 674 dd.setFaultListener(lis); 675 676 nl = root.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 678 "mappings"); 679 if ((nl == null) || nl.getLength () > 1) { 680 throw new IllegalArgumentException ("at most one 'mappings' element " + 681 "allowed in deployment descriptor"); 682 } 683 if (nl.getLength () == 1) { 684 e = (Element) nl.item (0); 685 686 String className = DOMUtils.getAttribute(e, "defaultRegistryClass"); 687 688 if (className != null) { 689 dd.setDefaultSMRClass(className); 690 } 691 692 nl = e.getElementsByTagNameNS (Constants.NS_URI_XML_SOAP_DEPLOYMENT, 693 "map"); 694 int nmaps = nl.getLength (); 695 if (nmaps > 0) { 696 TypeMapping[] tms = new TypeMapping[nmaps]; 697 dd.setMappings (tms); 698 for (int i = 0; i < nmaps; i++) { 699 e = (Element) nl.item (i); 700 QName qname = DOMUtils.getQualifiedAttributeValue(e, "qname"); 701 tms[i] = 702 new TypeMapping (DOMUtils.getAttribute (e, "encodingStyle"), 703 qname, 704 DOMUtils.getAttribute (e, "javaType"), 705 DOMUtils.getAttribute (e, "java2XMLClassName"), 706 DOMUtils.getAttribute (e, "xml2JavaClassName")); 707 } 708 } 709 } 710 711 return dd; 712 } 713 714 717 public String toString () { 718 StringBuffer methodsStrbuf = new StringBuffer ("["); 719 for (int i = 0; i < methods.length; i++) { 720 methodsStrbuf.append (methods[i]); 721 if (i < methods.length-1) { 722 methodsStrbuf.append (","); 723 } 724 } 725 methodsStrbuf.append ("]"); 726 String header = "[DeploymentDescriptor id='" + id + "', " + 727 ((serviceType != SERVICE_TYPE_RPC) ? "type='message', " : "") + 728 "scope='" + scope + "', "; 729 String body = null; 730 if (providerType == PROVIDER_JAVA) { 731 body = "class='" + providerClass + "', static='" + isStatic + "', "; 732 } else if (providerType == PROVIDER_SCRIPT_FILE) { 733 body = "source='" + scriptFilenameOrString + "', "; 734 body += "language='" + scriptLanguage + "', "; 735 } else if (providerType == PROVIDER_USER_DEFINED) { 736 body = "type='" + serviceClass + "', class='" + providerClass ; 737 body += "', static='" + isStatic + "', "; 738 } 739 740 StringBuffer lis = new StringBuffer ("["); 741 if (faultListener != null) 742 for (int i = 0; i < faultListener.length; 743 lis.append(faultListener[i]), lis.append(" "), i++); 744 lis.append("]"); 745 746 StringBuffer opts = new StringBuffer (); 747 if (props != null) 748 opts.append( props.toString() ); 749 750 return header + body + "methods='" + methodsStrbuf + "', " + 751 "faultListener='" + lis + "', " + "mappings='" + 752 mappingsToString(mappings) + "'], " + 753 "opts='" + opts ; 754 } 755 756 private static String mappingsToString(TypeMapping[] mappings) { 757 if (mappings != null) { 758 StringBuffer strBuf = new StringBuffer (); 759 760 for (int i = 0; i < mappings.length; i++) { 761 strBuf.append((i > 0 ? " " : "") + mappings[i]); 762 } 763 764 return strBuf.toString(); 765 } else { 766 return null; 767 } 768 } 769 770 777 public static SOAPMappingRegistry 778 buildSOAPMappingRegistry (DeploymentDescriptor dd, SOAPContext ctx) { 779 TypeMapping[] maps = dd.getMappings (); 780 SOAPMappingRegistry smr = dd.getCachedSMR (); 781 782 if (smr != null) { 783 return smr; 784 } else { 785 String defaultSMRClassName = dd.getDefaultSMRClass(); 786 787 if (defaultSMRClassName != null) { 788 try { 789 Class defaultSMRClass = ctx.loadClass( defaultSMRClassName ); 790 791 smr = (SOAPMappingRegistry)defaultSMRClass.newInstance(); 792 } 793 catch (Exception e) { 794 } 796 } 797 798 if (smr == null) { 799 SOAPMappingRegistry baseReg = SOAPMappingRegistry.getBaseRegistry ( 800 Constants.NS_URI_CURRENT_SCHEMA_XSD); 801 if (maps == null) { 802 dd.setCachedSMR (baseReg); 803 return baseReg; 804 } else { 805 smr = new SOAPMappingRegistry (baseReg); 806 } 807 } 808 } 809 810 if (maps != null) { 811 for (int i = 0; i < maps.length; i++) { 812 TypeMapping tm = maps[i]; 813 int step = 0; 814 try { 815 step = 0; 816 Class javaType = null; 817 if (tm.javaType != null) 818 javaType = ctx.loadClass( tm.javaType ); 819 step = 1; 820 Serializer s = null; 821 if (tm.java2XMLClassName != null) { 822 Class c = ctx.loadClass( tm.java2XMLClassName ); 823 s = (Serializer) c.newInstance (); 824 } 825 step = 2; 826 Deserializer d = null; 827 if (tm.xml2JavaClassName != null) { 828 Class c = ctx.loadClass( tm.xml2JavaClassName ); 829 d = (Deserializer) c.newInstance (); 830 } 831 smr.mapTypes (tm.encodingStyle, tm.elementType, javaType, s, d); 832 } catch (Exception e2) { 833 String m = "Deployment error in SOAP service '" + dd.getID () + 834 "': "; 835 if (step == 0) { 836 m += "class name '" + tm.javaType + "' could not be resolved: "; 837 } else if (step == 1) { 838 m += "class name '" + tm.java2XMLClassName + "' could not be " + 839 "resolved as a serializer: "; 840 } else { 841 m += "class name '" + tm.xml2JavaClassName + "' could not be " + 842 "resolved as a deserializer: "; 843 } 844 throw new IllegalArgumentException (m + e2.getMessage ()); 845 } 846 } 847 } 848 dd.setCachedSMR (smr); 849 return smr; 850 } 851 } 852 | Popular Tags |