1 16 17 package org.springframework.remoting.jaxrpc.support; 18 19 import java.util.Enumeration ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import javax.xml.namespace.QName ; 26 import javax.xml.rpc.Service ; 27 import javax.xml.rpc.encoding.TypeMapping ; 28 import javax.xml.rpc.encoding.TypeMappingRegistry ; 29 30 import org.apache.axis.encoding.ser.BeanDeserializerFactory; 31 import org.apache.axis.encoding.ser.BeanSerializerFactory; 32 33 import org.springframework.beans.factory.BeanClassLoaderAware; 34 import org.springframework.remoting.jaxrpc.JaxRpcServicePostProcessor; 35 import org.springframework.util.ClassUtils; 36 37 59 public class AxisBeanMappingServicePostProcessor implements JaxRpcServicePostProcessor, BeanClassLoaderAware { 60 61 private String encodingStyleUri; 62 63 private String typeNamespaceUri; 64 65 private Map beanMappings; 66 67 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 68 69 70 79 public void setEncodingStyleUri(String encodingStyleUri) { 80 this.encodingStyleUri = encodingStyleUri; 81 } 82 83 88 public void setTypeNamespaceUri(String typeNamespaceUri) { 89 this.typeNamespaceUri = typeNamespaceUri; 90 } 91 92 96 public void setBeanMappings(Properties beanMappingProps) { 97 if (beanMappingProps != null) { 98 this.beanMappings = new HashMap (beanMappingProps.size()); 99 Enumeration propertyNames = beanMappingProps.propertyNames(); 100 while (propertyNames.hasMoreElements()) { 101 String javaTypeName = (String ) propertyNames.nextElement(); 102 String wsdlTypeName = beanMappingProps.getProperty(javaTypeName); 103 this.beanMappings.put(javaTypeName, wsdlTypeName); 104 } 105 } 106 else { 107 this.beanMappings = null; 108 } 109 } 110 111 116 public void setBeanClasses(Class [] beanClasses) { 117 if (beanClasses != null) { 118 this.beanMappings = new HashMap (beanClasses.length); 119 for (int i = 0; i < beanClasses.length; i++) { 120 Class beanClass = beanClasses[i]; 121 String wsdlTypeName = ClassUtils.getShortName(beanClass); 122 this.beanMappings.put(beanClass, wsdlTypeName); 123 } 124 } 125 else { 126 this.beanMappings = null; 127 } 128 } 129 130 public void setBeanClassLoader(ClassLoader beanClassLoader) { 131 this.beanClassLoader = beanClassLoader; 132 } 133 134 135 142 public void postProcessJaxRpcService(Service service) { 143 TypeMappingRegistry registry = service.getTypeMappingRegistry(); 144 TypeMapping mapping = registry.createTypeMapping(); 145 146 registerBeanMappings(mapping); 147 148 if (this.encodingStyleUri != null) { 149 registry.register(this.encodingStyleUri, mapping); 150 } 151 else { 152 registry.registerDefault(mapping); 153 } 154 } 155 156 162 protected void registerBeanMappings(TypeMapping mapping) { 163 if (this.beanMappings != null) { 164 for (Iterator it = this.beanMappings.entrySet().iterator(); it.hasNext();) { 165 Map.Entry entry = (Map.Entry ) it.next(); 166 Object key = entry.getKey(); 167 Class javaType = null; 168 if (key instanceof Class ) { 169 javaType = (Class ) key; 170 } 171 else { 172 javaType = ClassUtils.resolveClassName((String ) key, this.beanClassLoader); 173 } 174 String wsdlTypeName = (String ) entry.getValue(); 175 registerBeanMapping(mapping, javaType, wsdlTypeName); 176 } 177 } 178 } 179 180 186 protected void registerBeanMapping(TypeMapping mapping, Class javaType, String wsdlTypeName) { 187 registerBeanMapping(mapping, javaType, getTypeQName(wsdlTypeName)); 188 } 189 190 196 protected void registerBeanMapping(TypeMapping mapping, Class javaType, QName wsdlType) { 197 mapping.register(javaType, wsdlType, 198 new BeanSerializerFactory(javaType, wsdlType), 199 new BeanDeserializerFactory(javaType, wsdlType)); 200 } 201 202 206 protected final QName getTypeQName(String name) { 207 return (this.typeNamespaceUri != null ? new QName (this.typeNamespaceUri, name) : new QName (name)); 208 } 209 210 } 211 | Popular Tags |