KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > jaxrpc > support > AxisBeanMappingServicePostProcessor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.remoting.jaxrpc.support;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.rpc.Service JavaDoc;
27 import javax.xml.rpc.encoding.TypeMapping JavaDoc;
28 import javax.xml.rpc.encoding.TypeMappingRegistry JavaDoc;
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 /**
38  * Axis-specific {@link JaxRpcServicePostProcessor} that registers bean
39  * mappings for domain objects that follow the JavaBean pattern.
40  *
41  * <p>The same mappings are usually also registered at the server in
42  * Axis' "server-config.wsdd" file.
43  *
44  * <p>To be registered as a service post-processor on a
45  * {@link org.springframework.remoting.jaxrpc.LocalJaxRpcServiceFactoryBean} or
46  * {@link org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean},
47  * carrying appropriate configuration.
48  *
49  * <p>Note: Without such explicit bean mappings, a complex type like a
50  * domain object cannot be transferred via SOAP.
51  *
52  * @author Juergen Hoeller
53  * @since 2.0
54  * @see org.apache.axis.encoding.ser.BeanSerializerFactory
55  * @see org.apache.axis.encoding.ser.BeanDeserializerFactory
56  * @see org.springframework.remoting.jaxrpc.LocalJaxRpcServiceFactoryBean#setServicePostProcessors
57  * @see org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean#setServicePostProcessors
58  */

59 public class AxisBeanMappingServicePostProcessor implements JaxRpcServicePostProcessor, BeanClassLoaderAware {
60
61     private String JavaDoc encodingStyleUri;
62
63     private String JavaDoc typeNamespaceUri;
64
65     private Map JavaDoc beanMappings;
66
67     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
68
69
70     /**
71      * Set the encoding style URI to use for the type mapping.
72      * <p>A typical value is "http://schemas.xmlsoap.org/soap/encoding/",
73      * as suggested by the JAX-RPC javadoc. However, note that the default
74      * behavior of this post-processor is to register the type mapping
75      * as JAX-RPC default if no explicit encoding style URI is given.
76      * @see javax.xml.rpc.encoding.TypeMappingRegistry#register
77      * @see javax.xml.rpc.encoding.TypeMappingRegistry#registerDefault
78      */

79     public void setEncodingStyleUri(String JavaDoc encodingStyleUri) {
80         this.encodingStyleUri = encodingStyleUri;
81     }
82
83     /**
84      * Set the application-specific namespace to use for XML types,
85      * for example "urn:JPetStore".
86      * @see javax.xml.rpc.encoding.TypeMapping#register
87      */

88     public void setTypeNamespaceUri(String JavaDoc typeNamespaceUri) {
89         this.typeNamespaceUri = typeNamespaceUri;
90     }
91
92     /**
93      * Specify the bean mappings to register as String-String pairs,
94      * with the Java type name as key and the WSDL type name as value.
95      */

96     public void setBeanMappings(Properties JavaDoc beanMappingProps) {
97         if (beanMappingProps != null) {
98             this.beanMappings = new HashMap JavaDoc(beanMappingProps.size());
99             Enumeration JavaDoc propertyNames = beanMappingProps.propertyNames();
100             while (propertyNames.hasMoreElements()) {
101                 String JavaDoc javaTypeName = (String JavaDoc) propertyNames.nextElement();
102                 String JavaDoc wsdlTypeName = beanMappingProps.getProperty(javaTypeName);
103                 this.beanMappings.put(javaTypeName, wsdlTypeName);
104             }
105         }
106         else {
107             this.beanMappings = null;
108         }
109     }
110
111     /**
112      * Specify the bean mappings to register as Java types,
113      * with the WSDL type names inferred from the Java type names
114      * (using the short, that is, non-fully-qualified class name).
115      */

116     public void setBeanClasses(Class JavaDoc[] beanClasses) {
117         if (beanClasses != null) {
118             this.beanMappings = new HashMap JavaDoc(beanClasses.length);
119             for (int i = 0; i < beanClasses.length; i++) {
120                 Class JavaDoc beanClass = beanClasses[i];
121                 String JavaDoc 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 JavaDoc beanClassLoader) {
131         this.beanClassLoader = beanClassLoader;
132     }
133
134
135     /**
136      * Register the specified bean mappings on the given Service's
137      * {@link TypeMappingRegistry}.
138      * @see javax.xml.rpc.Service#getTypeMappingRegistry()
139      * @see #setBeanMappings
140      * @see #registerBeanMappings(javax.xml.rpc.encoding.TypeMapping)
141      */

142     public void postProcessJaxRpcService(Service JavaDoc service) {
143         TypeMappingRegistry JavaDoc registry = service.getTypeMappingRegistry();
144         TypeMapping JavaDoc 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     /**
157      * Perform the actual bean mapping registration.
158      * @param mapping the JAX-RPC {@link TypeMapping} to operate on
159      * @see #setBeanMappings
160      * @see #registerBeanMapping(javax.xml.rpc.encoding.TypeMapping, Class, String)
161      */

162     protected void registerBeanMappings(TypeMapping JavaDoc mapping) {
163         if (this.beanMappings != null) {
164             for (Iterator JavaDoc it = this.beanMappings.entrySet().iterator(); it.hasNext();) {
165                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
166                 Object JavaDoc key = entry.getKey();
167                 Class JavaDoc javaType = null;
168                 if (key instanceof Class JavaDoc) {
169                     javaType = (Class JavaDoc) key;
170                 }
171                 else {
172                     javaType = ClassUtils.resolveClassName((String JavaDoc) key, this.beanClassLoader);
173                 }
174                 String JavaDoc wsdlTypeName = (String JavaDoc) entry.getValue();
175                 registerBeanMapping(mapping, javaType, wsdlTypeName);
176             }
177         }
178     }
179
180     /**
181      * Register a bean mapping for the given Java type and WSDL type name.
182      * @param mapping the JAX-RPC {@link TypeMapping} to operate on
183      * @param javaType the Java type
184      * @param wsdlTypeName the WSDL type name (as a {@link String})
185      */

186     protected void registerBeanMapping(TypeMapping JavaDoc mapping, Class JavaDoc javaType, String JavaDoc wsdlTypeName) {
187         registerBeanMapping(mapping, javaType, getTypeQName(wsdlTypeName));
188     }
189
190     /**
191      * Register a bean mapping for the given Java type and WSDL type.
192      * @param mapping the JAX-RPC {@link TypeMapping} to operate on
193      * @param javaType the Java type
194      * @param wsdlType the WSDL type (as XML {@link QName})
195      */

196     protected void registerBeanMapping(TypeMapping JavaDoc mapping, Class JavaDoc javaType, QName JavaDoc wsdlType) {
197         mapping.register(javaType, wsdlType,
198             new BeanSerializerFactory(javaType, wsdlType),
199             new BeanDeserializerFactory(javaType, wsdlType));
200     }
201
202     /**
203      * Return a {@link QName} for the given name, relative to the
204      * {@link #setTypeNamespaceUri namespace URI} of this post-processor, if given.
205      */

206     protected final QName JavaDoc getTypeQName(String JavaDoc name) {
207         return (this.typeNamespaceUri != null ? new QName JavaDoc(this.typeNamespaceUri, name) : new QName JavaDoc(name));
208     }
209
210 }
211
Popular Tags