1 16 17 package org.apache.axis.encoding; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.AxisProperties; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.AxisEngine; 23 import org.apache.axis.handlers.soap.SOAPService; 24 import org.apache.axis.components.logger.LogFactory; 25 import org.apache.axis.encoding.ser.ArrayDeserializerFactory; 26 import org.apache.axis.encoding.ser.ArraySerializerFactory; 27 import org.apache.axis.encoding.ser.BeanDeserializerFactory; 28 import org.apache.axis.encoding.ser.BeanSerializerFactory; 29 import org.apache.axis.utils.ArrayUtil; 30 import org.apache.axis.utils.Messages; 31 import org.apache.axis.utils.ClassUtils; 32 import org.apache.axis.utils.JavaUtils; 33 import org.apache.axis.wsdl.fromJava.Namespaces; 34 import org.apache.axis.wsdl.fromJava.Types; 35 import org.apache.commons.logging.Log; 36 37 import javax.xml.namespace.QName ; 38 import javax.xml.rpc.JAXRPCException ; 39 40 import java.lang.reflect.Array ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 import java.util.List ; 44 import java.io.Serializable ; 45 46 74 public class TypeMappingImpl implements Serializable 75 { 76 protected static Log log = 77 LogFactory.getLog(TypeMappingImpl.class.getName()); 78 79 85 public static boolean dotnet_soapenc_bugfix = false; 86 87 public static class Pair implements Serializable { 88 public Class javaType; 89 public QName xmlType; 90 public Pair(Class javaType, QName xmlType) { 91 this.javaType = javaType; 92 this.xmlType = xmlType; 93 } 94 public boolean equals(Object o) { 95 if (o == null) return false; 96 Pair p = (Pair) o; 97 if (p.xmlType == this.xmlType && 99 p.javaType == this.javaType) { 100 return true; 101 } 102 return (p.xmlType.equals(this.xmlType) && 103 p.javaType.equals(this.javaType)); 104 } 105 public int hashCode() { 106 int hashcode = 0; 107 if (javaType != null) { 108 hashcode ^= javaType.hashCode(); 109 } 110 if (xmlType != null) { 111 hashcode ^= xmlType.hashCode(); 112 } 113 return hashcode; 114 } 115 } 116 117 private HashMap qName2Pair; private HashMap class2Pair; private HashMap pair2SF; private HashMap pair2DF; private ArrayList namespaces; 123 protected Boolean doAutoTypes = null; 124 125 128 public TypeMappingImpl() { 129 qName2Pair = new HashMap (); 130 class2Pair = new HashMap (); 131 pair2SF = new HashMap (); 132 pair2DF = new HashMap (); 133 namespaces = new ArrayList (); 134 } 135 136 private static boolean isArray(Class clazz) 137 { 138 return clazz.isArray() || java.util.Collection .class.isAssignableFrom(clazz); 139 } 140 141 142 143 144 150 public String [] getSupportedEncodings() { 151 String [] stringArray = new String [namespaces.size()]; 152 return (String []) namespaces.toArray(stringArray); 153 } 154 155 162 public void setSupportedEncodings(String [] namespaceURIs) { 163 namespaces.clear(); 164 for (int i =0; i< namespaceURIs.length; i++) { 165 if (!namespaces.contains(namespaceURIs[i])) { 166 namespaces.add(namespaceURIs[i]); 167 } 168 } 169 } 170 171 186 public boolean isRegistered(Class javaType, QName xmlType) { 187 if (javaType == null || xmlType == null) { 188 throw new JAXRPCException ( 191 Messages.getMessage(javaType == null ? 192 "badJavaType" : "badXmlType")); 193 } 194 if (pair2SF.keySet().contains(new Pair(javaType, xmlType))) { 195 return true; 196 } 197 return false; 198 } 199 200 211 public void register(Class javaType, QName xmlType, 212 javax.xml.rpc.encoding.SerializerFactory sf, 213 javax.xml.rpc.encoding.DeserializerFactory dsf) 214 throws JAXRPCException { 215 if (sf == null && dsf == null) { 217 throw new JAXRPCException (Messages.getMessage("badSerFac")); 218 } 219 220 internalRegister(javaType, xmlType, sf, dsf); 221 } 222 223 232 protected void internalRegister(Class javaType, QName xmlType, 233 javax.xml.rpc.encoding.SerializerFactory sf, 234 javax.xml.rpc.encoding.DeserializerFactory dsf) 235 throws JAXRPCException { 236 if (javaType == null || xmlType == null) { 238 throw new JAXRPCException ( 239 Messages.getMessage(javaType == null ? 240 "badJavaType" : "badXmlType")); 241 } 242 243 253 Pair pair = new Pair(javaType, xmlType); 254 255 qName2Pair.put(xmlType, pair); 263 class2Pair.put(javaType, pair); 264 265 if (sf != null) 266 pair2SF.put(pair, sf); 267 if (dsf != null) 268 pair2DF.put(pair, dsf); 269 } 270 271 285 public javax.xml.rpc.encoding.SerializerFactory 286 getSerializer(Class javaType, QName xmlType) 287 throws JAXRPCException { 288 289 javax.xml.rpc.encoding.SerializerFactory sf = null; 290 291 if (xmlType == null) { 293 xmlType = getTypeQName(javaType, null); 294 if (xmlType == null) { 297 return null; 298 } 299 } 300 301 Pair pair = new Pair(javaType, xmlType); 303 304 sf = (javax.xml.rpc.encoding.SerializerFactory ) pair2SF.get(pair); 306 307 if (sf == null && javaType.isArray()) { 311 int dimension = 1; 312 Class componentType = javaType.getComponentType(); 313 while (componentType.isArray()) { 314 dimension += 1; 315 componentType = componentType.getComponentType(); 316 } 317 int[] dimensions = new int[dimension]; 318 componentType = componentType.getSuperclass(); 319 Class superJavaType = null; 320 while (componentType != null) { 321 superJavaType = Array.newInstance(componentType, dimensions).getClass(); 322 pair = new Pair(superJavaType, xmlType); 323 sf = (javax.xml.rpc.encoding.SerializerFactory ) pair2SF.get(pair); 324 if (sf != null) { 325 break; 326 } 327 componentType = componentType.getSuperclass(); 328 } 329 } 330 331 if (sf == null && javaType.isArray() && xmlType != null) { 333 Pair pair2 = (Pair) qName2Pair.get(xmlType); 334 if (pair2 != null 335 && pair2.javaType != null 336 && !pair2.javaType.isPrimitive() 337 && ArrayUtil.isConvertable(pair2.javaType, javaType)) { 338 sf = (javax.xml.rpc.encoding.SerializerFactory ) pair2SF.get(pair2); 339 } 340 } 341 342 return sf; 343 } 344 345 public SerializerFactory finalGetSerializer(Class javaType) { 346 Pair pair; 347 if (isArray(javaType)) { 348 pair = (Pair) qName2Pair.get(Constants.SOAP_ARRAY); 349 } else { 350 pair = (Pair) class2Pair.get(javaType); 351 } 352 if (pair != null) { 353 return (SerializerFactory)pair2SF.get(pair); 354 } 355 356 return null; 357 } 358 359 376 public QName getXMLType(Class javaType, QName xmlType, boolean encoded) 377 throws JAXRPCException 378 { 379 javax.xml.rpc.encoding.SerializerFactory sf = null; 380 381 if (xmlType == null) { 383 xmlType = getTypeQNameRecursive(javaType); 384 385 if (xmlType == null) { 388 return null; 389 } 390 } 391 392 Pair pair = new Pair(javaType, xmlType); 394 395 sf = (javax.xml.rpc.encoding.SerializerFactory ) pair2SF.get(pair); 397 if (sf != null) 398 return xmlType; 399 400 if (isArray(javaType)) { 404 if (encoded) { 405 return Constants.SOAP_ARRAY; 406 } else { 407 pair = (Pair) qName2Pair.get(xmlType); 408 } 409 } 410 411 if (pair == null) { 412 pair = (Pair) class2Pair.get(javaType); 413 } 414 415 if (pair != null) { 416 xmlType = pair.xmlType; 417 } 418 return xmlType; 419 } 420 421 435 public javax.xml.rpc.encoding.DeserializerFactory 436 getDeserializer(Class javaType, QName xmlType, TypeMappingDelegate start) 437 throws JAXRPCException { 438 if (javaType == null) { 439 javaType = start.getClassForQName(xmlType); 440 if (javaType == null) { 443 return null; 444 } 445 } 446 447 Pair pair = new Pair(javaType, xmlType); 448 449 return (javax.xml.rpc.encoding.DeserializerFactory ) pair2DF.get(pair); 450 } 451 452 public DeserializerFactory finalGetDeserializer(Class javaType, 453 QName xmlType, 454 TypeMappingDelegate start) { 455 DeserializerFactory df = null; 456 if (javaType != null && javaType.isArray()) { 457 Class componentType = javaType.getComponentType(); 458 459 if (xmlType != null) { 465 Class actualClass = start.getClassForQName(xmlType); 466 if (actualClass == componentType || 467 (actualClass != null && componentType.isAssignableFrom(actualClass))) { 468 return null; 469 } 470 } 471 Pair pair = (Pair) qName2Pair.get(Constants.SOAP_ARRAY); 472 df = (DeserializerFactory) pair2DF.get(pair); 473 if (df instanceof ArrayDeserializerFactory && javaType.isArray()) { 474 QName componentXmlType = start.getTypeQName(componentType); 475 if (componentXmlType != null) { 476 df = new ArrayDeserializerFactory(componentXmlType); 477 } 478 } 479 } 480 return df; 481 } 482 483 493 public void removeSerializer(Class javaType, QName xmlType) 494 throws JAXRPCException { 495 if (javaType == null || xmlType == null) { 496 throw new JAXRPCException ( 497 Messages.getMessage(javaType == null ? 498 "badJavaType" : "badXmlType")); 499 } 500 501 Pair pair = new Pair(javaType, xmlType); 502 pair2SF.remove(pair); 503 } 504 505 515 public void removeDeserializer(Class javaType, QName xmlType) 516 throws JAXRPCException { 517 if (javaType == null || xmlType == null) { 518 throw new JAXRPCException ( 519 Messages.getMessage(javaType == null ? 520 "badJavaType" : "badXmlType")); 521 } 522 Pair pair = new Pair(javaType, xmlType); 523 pair2DF.remove(pair); 524 } 525 526 527 528 529 534 public QName getTypeQNameRecursive(Class javaType) { 535 QName ret = null; 536 while (javaType != null) { 537 ret = getTypeQName(javaType, null); 538 if (ret != null) 539 return ret; 540 541 Class [] interfaces = javaType.getInterfaces(); 543 if (interfaces != null) { 544 for (int i = 0; i < interfaces.length; i++) { 545 Class iface = interfaces[i]; 546 ret = getTypeQName(iface, null); 547 if (ret != null) 548 return ret; 549 } 550 } 551 552 javaType = javaType.getSuperclass(); 553 } 554 return null; 555 } 556 557 565 public QName getTypeQNameExact(Class javaType, TypeMappingDelegate next) { 566 if (javaType == null) 567 return null; 568 569 QName xmlType = null; 570 Pair pair = (Pair) class2Pair.get(javaType); 571 572 if (isDotNetSoapEncFixNeeded() && pair != null ) { 573 xmlType = pair.xmlType; 577 if (Constants.isSOAP_ENC(xmlType.getNamespaceURI()) && 578 !xmlType.getLocalPart().equals("Array")) { 579 pair = null; 580 } 581 } 582 583 if (pair == null && next != null) { 584 xmlType = next.delegate.getTypeQNameExact(javaType, 586 next.next); 587 } 588 589 if (pair != null) { 590 xmlType = pair.xmlType; 591 } 592 593 return xmlType; 594 } 595 596 601 private boolean isDotNetSoapEncFixNeeded() { 602 MessageContext msgContext = MessageContext.getCurrentContext(); 603 if (msgContext != null) { 604 SOAPService service = msgContext.getService(); 605 if (service != null) { 606 String dotNetSoapEncFix = (String ) service.getOption(AxisEngine.PROP_DOTNET_SOAPENC_FIX); 607 if (dotNetSoapEncFix != null) { 608 return JavaUtils.isTrue(dotNetSoapEncFix); 609 } 610 } 611 } 612 return TypeMappingImpl.dotnet_soapenc_bugfix; 613 } 614 615 public QName getTypeQName(Class javaType, TypeMappingDelegate next) { 616 QName xmlType = getTypeQNameExact(javaType, next); 617 618 623 if ( shouldDoAutoTypes() && 624 javaType != List .class && 625 !List .class.isAssignableFrom(javaType) && 626 xmlType != null && 627 xmlType.equals(Constants.SOAP_ARRAY) ) 628 { 629 xmlType = new QName ( 630 Namespaces.makeNamespace( javaType.getName() ), 631 Types.getLocalNameFromFullName( javaType.getName() ) ); 632 633 internalRegister( javaType, 634 xmlType, 635 new ArraySerializerFactory(), 636 new ArrayDeserializerFactory() ); 637 } 638 639 if (xmlType == null && isArray(javaType)) { 641 642 Pair pair = (Pair) class2Pair.get(Object [].class); 644 if (pair != null) { 648 xmlType = pair.xmlType; 649 } else { 650 xmlType = Constants.SOAP_ARRAY; 651 } 652 } 653 654 657 if (xmlType == null && shouldDoAutoTypes()) 658 { 659 xmlType = new QName ( 660 Namespaces.makeNamespace( javaType.getName() ), 661 Types.getLocalNameFromFullName( javaType.getName() ) ); 662 663 668 internalRegister( javaType, 669 xmlType, 670 new BeanSerializerFactory(javaType, xmlType), 671 new BeanDeserializerFactory(javaType, xmlType) ); 672 } 673 674 return xmlType; 676 } 677 678 public Class getClassForQName(QName xmlType, Class javaType, 679 TypeMappingDelegate next) { 680 if (xmlType == null) { 681 return null; 682 } 683 684 686 if (javaType != null) { 687 Pair pair = new Pair(javaType, xmlType); 689 if (pair2DF.get(pair) == null) { 690 if (next != null) { 691 javaType = next.getClassForQName(xmlType, javaType); 692 } 693 } 694 } 695 696 if (javaType == null) { 697 Pair pair = (Pair) qName2Pair.get(xmlType); 699 if (pair == null && next != null) { 700 javaType = next.getClassForQName(xmlType); 702 } else if (pair != null) { 703 javaType = pair.javaType; 704 } 705 } 706 707 if(javaType == null && shouldDoAutoTypes()) { 709 String pkg = Namespaces.getPackage(xmlType.getNamespaceURI()); 710 if (pkg != null) { 711 String className = xmlType.getLocalPart(); 712 if (pkg.length() > 0) { 713 className = pkg + "." + className; 714 } 715 try { 716 javaType = ClassUtils.forName(className); 717 internalRegister(javaType, 718 xmlType, 719 new BeanSerializerFactory(javaType, xmlType), 720 new BeanDeserializerFactory(javaType, xmlType)); 721 } catch (ClassNotFoundException e) { 722 } 723 } 724 } 725 return javaType; 726 } 727 728 public void setDoAutoTypes(boolean doAutoTypes) { 729 this.doAutoTypes = doAutoTypes ? Boolean.TRUE : Boolean.FALSE; 730 } 731 732 public boolean shouldDoAutoTypes() { 733 if(doAutoTypes != null) { 734 return doAutoTypes.booleanValue(); 735 } 736 MessageContext msgContext = MessageContext.getCurrentContext(); 737 if(msgContext != null) { 738 if (msgContext.isPropertyTrue("axis.doAutoTypes") || 739 (msgContext.getAxisEngine() != null && JavaUtils.isTrue(msgContext.getAxisEngine().getOption("axis.doAutoTypes")))) { 740 doAutoTypes = Boolean.TRUE; 741 } 742 } 743 if(doAutoTypes == null){ 744 doAutoTypes = AxisProperties.getProperty("axis.doAutoTypes", 745 "false") 746 .equals("true") ? 747 Boolean.TRUE : Boolean.FALSE; 748 } 749 return doAutoTypes.booleanValue(); 750 } 751 752 755 public Class [] getAllClasses(TypeMappingDelegate next) 756 { 757 java.util.HashSet temp = new java.util.HashSet (); 758 if (next != null) 759 { 760 temp.addAll(java.util.Arrays.asList(next.getAllClasses())); 761 } 762 temp.addAll(class2Pair.keySet()); 763 return (Class [])temp.toArray(new Class [temp.size()]); 764 } 765 } | Popular Tags |