KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > jaxrpc > LocalJaxRpcServiceFactory


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;
18
19 import java.net.URL JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.xml.namespace.QName JavaDoc;
23 import javax.xml.rpc.Service JavaDoc;
24 import javax.xml.rpc.ServiceException JavaDoc;
25 import javax.xml.rpc.ServiceFactory JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.springframework.beans.BeanUtils;
31
32 /**
33  * Factory for locally defined JAX-RPC {@link javax.xml.rpc.Service} references.
34  * Uses a JAX-RPC {@link javax.xml.rpc.ServiceFactory} underneath.
35  *
36  * <p>Serves as base class for {@link LocalJaxRpcServiceFactoryBean} as well as
37  * {@link JaxRpcPortClientInterceptor} and {@link JaxRpcPortProxyFactoryBean}.
38  *
39  * @author Juergen Hoeller
40  * @since 15.12.2003
41  * @see javax.xml.rpc.ServiceFactory
42  * @see javax.xml.rpc.Service
43  * @see LocalJaxRpcServiceFactoryBean
44  * @see JaxRpcPortClientInterceptor
45  * @see JaxRpcPortProxyFactoryBean
46  */

47 public class LocalJaxRpcServiceFactory {
48
49     /** Logger that is available to subclasses */
50     protected final Log logger = LogFactory.getLog(getClass());
51
52     private ServiceFactory JavaDoc serviceFactory;
53
54     private Class JavaDoc serviceFactoryClass;
55
56     private URL JavaDoc wsdlDocumentUrl;
57
58     private String JavaDoc namespaceUri;
59
60     private String JavaDoc serviceName;
61
62     private Class JavaDoc jaxRpcServiceInterface;
63
64     private Properties JavaDoc jaxRpcServiceProperties;
65
66     private JaxRpcServicePostProcessor[] servicePostProcessors;
67
68
69     /**
70      * Set the ServiceFactory instance to use.
71      * <p>This is an alternative to the common "serviceFactoryClass" property,
72      * allowing for a pre-initialized ServiceFactory instance to be specified.
73      * @see #setServiceFactoryClass
74      */

75     public void setServiceFactory(ServiceFactory JavaDoc serviceFactory) {
76         this.serviceFactory = serviceFactory;
77     }
78
79     /**
80      * Return the specified ServiceFactory instance, if any.
81      */

82     public ServiceFactory JavaDoc getServiceFactory() {
83         return this.serviceFactory;
84     }
85
86     /**
87      * Set the ServiceFactory class to use, for example
88      * "org.apache.axis.client.ServiceFactory".
89      * <p>Does not need to be set if the JAX-RPC implementation has registered
90      * itself with the JAX-RPC system property "SERVICEFACTORY_PROPERTY".
91      * @see javax.xml.rpc.ServiceFactory
92      */

93     public void setServiceFactoryClass(Class JavaDoc serviceFactoryClass) {
94         if (serviceFactoryClass != null && !ServiceFactory JavaDoc.class.isAssignableFrom(serviceFactoryClass)) {
95             throw new IllegalArgumentException JavaDoc("'serviceFactoryClass' must implement [javax.xml.rpc.ServiceFactory]");
96         }
97         this.serviceFactoryClass = serviceFactoryClass;
98     }
99
100     /**
101      * Return the ServiceFactory class to use, or <code>null</code> if default.
102      */

103     public Class JavaDoc getServiceFactoryClass() {
104         return this.serviceFactoryClass;
105     }
106
107     /**
108      * Set the URL of the WSDL document that describes the service.
109      */

110     public void setWsdlDocumentUrl(URL JavaDoc wsdlDocumentUrl) {
111         this.wsdlDocumentUrl = wsdlDocumentUrl;
112     }
113
114     /**
115      * Return the URL of the WSDL document that describes the service.
116      */

117     public URL JavaDoc getWsdlDocumentUrl() {
118         return this.wsdlDocumentUrl;
119     }
120
121     /**
122      * Set the namespace URI of the service.
123      * Corresponds to the WSDL "targetNamespace".
124      */

125     public void setNamespaceUri(String JavaDoc namespaceUri) {
126         this.namespaceUri = namespaceUri;
127     }
128
129     /**
130      * Return the namespace URI of the service.
131      */

132     public String JavaDoc getNamespaceUri() {
133         return this.namespaceUri;
134     }
135
136     /**
137      * Set the name of the service to look up.
138      * Corresponds to the "wsdl:service" name.
139      * @see javax.xml.rpc.ServiceFactory#createService(javax.xml.namespace.QName)
140      * @see javax.xml.rpc.ServiceFactory#createService(java.net.URL, javax.xml.namespace.QName)
141      * @see javax.xml.rpc.ServiceFactory#loadService(java.net.URL, javax.xml.namespace.QName, java.util.Properties)
142      */

143     public void setServiceName(String JavaDoc serviceName) {
144         this.serviceName = serviceName;
145     }
146
147     /**
148      * Return the name of the service.
149      */

150     public String JavaDoc getServiceName() {
151         return this.serviceName;
152     }
153
154     /**
155      * Set the JAX-RPC service interface to use for looking up the service.
156      * If specified, this will override a "serviceName" setting.
157      * <p>The specified interface will usually be a generated JAX-RPC service
158      * interface that directly corresponds to the WSDL service declaration.
159      * Note that this is not a port interface or the application-level service
160      * interface to be exposed by a port proxy!
161      * <p>Only supported by JAX-RPC 1.1 providers.
162      * @see #setServiceName
163      * @see javax.xml.rpc.ServiceFactory#loadService(Class)
164      * @see javax.xml.rpc.ServiceFactory#loadService(java.net.URL, Class, java.util.Properties)
165      */

166     public void setJaxRpcServiceInterface(Class JavaDoc jaxRpcServiceInterface) {
167         this.jaxRpcServiceInterface = jaxRpcServiceInterface;
168     }
169
170     /**
171      * Return the JAX-RPC service interface to use for looking up the service.
172      */

173     public Class JavaDoc getJaxRpcServiceInterface() {
174         return this.jaxRpcServiceInterface;
175     }
176
177     /**
178      * Set JAX-RPC service properties to be passed to the ServiceFactory, if any.
179      * <p>Only supported by JAX-RPC 1.1 providers.
180      * @see javax.xml.rpc.ServiceFactory#loadService(java.net.URL, javax.xml.namespace.QName, java.util.Properties)
181      * @see javax.xml.rpc.ServiceFactory#loadService(java.net.URL, Class, java.util.Properties)
182      */

183     public void setJaxRpcServiceProperties(Properties JavaDoc jaxRpcServiceProperties) {
184         this.jaxRpcServiceProperties = jaxRpcServiceProperties;
185     }
186
187     /**
188      * Return JAX-RPC service properties to be passed to the ServiceFactory, if any.
189      */

190     public Properties JavaDoc getJaxRpcServiceProperties() {
191         return this.jaxRpcServiceProperties;
192     }
193
194     /**
195      * Set the JaxRpcServicePostProcessors to be applied to JAX-RPC Service
196      * instances created by this factory.
197      * <p>Such post-processors can, for example, register custom type mappings.
198      * They are reusable across all pre-built subclasses of this factory:
199      * LocalJaxRpcServiceFactoryBean, JaxRpcPortClientInterceptor,
200      * JaxRpcPortProxyFactoryBean.
201      * @see LocalJaxRpcServiceFactoryBean
202      * @see JaxRpcPortClientInterceptor
203      * @see JaxRpcPortProxyFactoryBean
204      */

205     public void setServicePostProcessors(JaxRpcServicePostProcessor[] servicePostProcessors) {
206         this.servicePostProcessors = servicePostProcessors;
207     }
208
209     /**
210      * Return the JaxRpcServicePostProcessors to be applied to JAX-RPC Service
211      * instances created by this factory.
212      */

213     public JaxRpcServicePostProcessor[] getServicePostProcessors() {
214         return this.servicePostProcessors;
215     }
216
217
218     /**
219      * Create a JAX-RPC Service according to the parameters of this factory.
220      * @see #setServiceName
221      * @see #setWsdlDocumentUrl
222      * @see #postProcessJaxRpcService
223      */

224     public Service JavaDoc createJaxRpcService() throws ServiceException JavaDoc {
225         ServiceFactory JavaDoc serviceFactory = getServiceFactory();
226         if (serviceFactory == null) {
227             serviceFactory = createServiceFactory();
228         }
229
230         // Create service based on this factory's settings.
231
Service JavaDoc service = createService(serviceFactory);
232
233         // Allow for custom post-processing in subclasses.
234
postProcessJaxRpcService(service);
235
236         return service;
237     }
238
239     /**
240      * Return a QName for the given name, relative to the namespace URI
241      * of this factory, if given.
242      * @see #setNamespaceUri
243      */

244     protected QName JavaDoc getQName(String JavaDoc name) {
245         return (getNamespaceUri() != null ? new QName JavaDoc(getNamespaceUri(), name) : new QName JavaDoc(name));
246     }
247
248     /**
249      * Create a JAX-RPC ServiceFactory, either of the specified class
250      * or the default.
251      * @throws ServiceException if thrown by JAX-RPC methods
252      * @see #setServiceFactoryClass
253      * @see javax.xml.rpc.ServiceFactory#newInstance()
254      */

255     protected ServiceFactory JavaDoc createServiceFactory() throws ServiceException JavaDoc {
256         if (getServiceFactoryClass() != null) {
257             return (ServiceFactory JavaDoc) BeanUtils.instantiateClass(getServiceFactoryClass());
258         }
259         else {
260             return ServiceFactory.newInstance();
261         }
262     }
263
264     /**
265      * Actually create the JAX-RPC Service instance,
266      * based on this factory's settings.
267      * @param serviceFactory the JAX-RPC ServiceFactory to use
268      * @return the newly created JAX-RPC Service
269      * @throws ServiceException if thrown by JAX-RPC methods
270      * @see javax.xml.rpc.ServiceFactory#createService
271      * @see javax.xml.rpc.ServiceFactory#loadService
272      */

273     protected Service JavaDoc createService(ServiceFactory JavaDoc serviceFactory) throws ServiceException JavaDoc {
274         if (getServiceName() == null && getJaxRpcServiceInterface() == null) {
275             throw new IllegalArgumentException JavaDoc("Either 'serviceName' or 'jaxRpcServiceInterface' is required");
276         }
277
278         if (getJaxRpcServiceInterface() != null) {
279             // Create service via generated JAX-RPC service interface.
280
// Only supported on JAX-RPC 1.1
281
if (getWsdlDocumentUrl() != null || getJaxRpcServiceProperties() != null) {
282                 return serviceFactory.loadService(
283                         getWsdlDocumentUrl(), getJaxRpcServiceInterface(), getJaxRpcServiceProperties());
284             }
285             return serviceFactory.loadService(getJaxRpcServiceInterface());
286         }
287
288         // Create service via specified JAX-RPC service name.
289
QName JavaDoc serviceQName = getQName(getServiceName());
290         if (getJaxRpcServiceProperties() != null) {
291             // Only supported on JAX-RPC 1.1
292
return serviceFactory.loadService(getWsdlDocumentUrl(), serviceQName, getJaxRpcServiceProperties());
293         }
294         if (getWsdlDocumentUrl() != null) {
295             return serviceFactory.createService(getWsdlDocumentUrl(), serviceQName);
296         }
297         return serviceFactory.createService(serviceQName);
298     }
299
300     /**
301      * Post-process the given JAX-RPC Service. Called by {@link #createJaxRpcService}.
302      * Useful, for example, to register custom type mappings.
303      * <p>The default implementation delegates to all registered
304      * {@link JaxRpcServicePostProcessor JaxRpcServicePostProcessors}.
305      * It is usually preferable to implement custom type mappings etc there rather
306      * than in a subclass of this factory, to allow for reuse of the post-processors.
307      * @param service the current JAX-RPC Service
308      * (can be cast to an implementation-specific class if necessary)
309      * @see #setServicePostProcessors
310      * @see javax.xml.rpc.Service#getTypeMappingRegistry()
311      */

312     protected void postProcessJaxRpcService(Service JavaDoc service) {
313         JaxRpcServicePostProcessor[] postProcessors = getServicePostProcessors();
314         if (postProcessors != null) {
315             for (int i = 0; i < postProcessors.length; i++) {
316                 postProcessors[i].postProcessJaxRpcService(service);
317             }
318         }
319     }
320
321 }
322
Popular Tags