View Javadoc
1   /*
2    * Copyright 2002-2014 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.jaxws;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.concurrent.Executor;
22  import javax.xml.namespace.QName;
23  import javax.xml.ws.Service;
24  import javax.xml.ws.WebServiceFeature;
25  import javax.xml.ws.handler.HandlerResolver;
26  
27  import org.springframework.core.io.Resource;
28  import org.springframework.lang.UsesJava7;
29  import org.springframework.util.Assert;
30  
31  /**
32   * Factory for locally defined JAX-WS {@link javax.xml.ws.Service} references.
33   * Uses the JAX-WS {@link javax.xml.ws.Service#create} factory API underneath.
34   *
35   * <p>Serves as base class for {@link LocalJaxWsServiceFactoryBean} as well as
36   * {@link JaxWsPortClientInterceptor} and {@link JaxWsPortProxyFactoryBean}.
37   *
38   * @author Juergen Hoeller
39   * @since 2.5
40   * @see javax.xml.ws.Service
41   * @see LocalJaxWsServiceFactoryBean
42   * @see JaxWsPortClientInterceptor
43   * @see JaxWsPortProxyFactoryBean
44   */
45  public class LocalJaxWsServiceFactory {
46  
47  	private URL wsdlDocumentUrl;
48  
49  	private String namespaceUri;
50  
51  	private String serviceName;
52  
53  	private WebServiceFeature[] serviceFeatures;
54  
55  	private Executor executor;
56  
57  	private HandlerResolver handlerResolver;
58  
59  
60  	/**
61  	 * Set the URL of the WSDL document that describes the service.
62  	 * @see #setWsdlDocumentResource(Resource)
63  	 */
64  	public void setWsdlDocumentUrl(URL wsdlDocumentUrl) {
65  		this.wsdlDocumentUrl = wsdlDocumentUrl;
66  	}
67  
68  	/**
69  	 * Set the WSDL document URL as a {@link Resource}.
70  	 * @throws IOException
71  	 * @since 3.2
72  	 */
73  	public void setWsdlDocumentResource(Resource wsdlDocumentResource) throws IOException {
74  		Assert.notNull(wsdlDocumentResource, "WSDL Resource must not be null.");
75  		this.wsdlDocumentUrl = wsdlDocumentResource.getURL();
76  	}
77  
78  	/**
79  	 * Return the URL of the WSDL document that describes the service.
80  	 */
81  	public URL getWsdlDocumentUrl() {
82  		return this.wsdlDocumentUrl;
83  	}
84  
85  	/**
86  	 * Set the namespace URI of the service.
87  	 * Corresponds to the WSDL "targetNamespace".
88  	 */
89  	public void setNamespaceUri(String namespaceUri) {
90  		this.namespaceUri = (namespaceUri != null ? namespaceUri.trim() : null);
91  	}
92  
93  	/**
94  	 * Return the namespace URI of the service.
95  	 */
96  	public String getNamespaceUri() {
97  		return this.namespaceUri;
98  	}
99  
100 	/**
101 	 * Set the name of the service to look up.
102 	 * Corresponds to the "wsdl:service" name.
103 	 */
104 	public void setServiceName(String serviceName) {
105 		this.serviceName = serviceName;
106 	}
107 
108 	/**
109 	 * Return the name of the service.
110 	 */
111 	public String getServiceName() {
112 		return this.serviceName;
113 	}
114 
115 	/**
116 	 * Specify WebServiceFeature objects (e.g. as inner bean definitions)
117 	 * to apply to JAX-WS service creation.
118 	 * <p>Note: This mechanism requires JAX-WS 2.2 or higher.
119 	 * @since 4.0
120 	 * @see Service#create(QName, WebServiceFeature...)
121 	 */
122 	public void setServiceFeatures(WebServiceFeature... serviceFeatures) {
123 		this.serviceFeatures = serviceFeatures;
124 	}
125 
126 	/**
127 	 * Set the JDK concurrent executor to use for asynchronous executions
128 	 * that require callbacks.
129 	 * @see javax.xml.ws.Service#setExecutor
130 	 */
131 	public void setExecutor(Executor executor) {
132 		this.executor = executor;
133 	}
134 
135 	/**
136 	 * Set the JAX-WS HandlerResolver to use for all proxies and dispatchers
137 	 * created through this factory.
138 	 * @see javax.xml.ws.Service#setHandlerResolver
139 	 */
140 	public void setHandlerResolver(HandlerResolver handlerResolver) {
141 		this.handlerResolver = handlerResolver;
142 	}
143 
144 
145 	/**
146 	 * Create a JAX-WS Service according to the parameters of this factory.
147 	 * @see #setServiceName
148 	 * @see #setWsdlDocumentUrl
149 	 */
150 	@UsesJava7  // optional use of Service#create with WebServiceFeature[]
151 	public Service createJaxWsService() {
152 		Assert.notNull(this.serviceName, "No service name specified");
153 		Service service;
154 
155 		if (this.serviceFeatures != null) {
156 			service = (this.wsdlDocumentUrl != null ?
157 				Service.create(this.wsdlDocumentUrl, getQName(this.serviceName), this.serviceFeatures) :
158 				Service.create(getQName(this.serviceName), this.serviceFeatures));
159 		}
160 		else {
161 			service = (this.wsdlDocumentUrl != null ?
162 					Service.create(this.wsdlDocumentUrl, getQName(this.serviceName)) :
163 					Service.create(getQName(this.serviceName)));
164 		}
165 
166 		if (this.executor != null) {
167 			service.setExecutor(this.executor);
168 		}
169 		if (this.handlerResolver != null) {
170 			service.setHandlerResolver(this.handlerResolver);
171 		}
172 
173 		return service;
174 	}
175 
176 	/**
177 	 * Return a QName for the given name, relative to the namespace URI
178 	 * of this factory, if given.
179 	 * @see #setNamespaceUri
180 	 */
181 	protected QName getQName(String name) {
182 		return (getNamespaceUri() != null ? new QName(getNamespaceUri(), name) : new QName(name));
183 	}
184 
185 }