001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.remoting.jaxws;
018
019import java.io.IOException;
020import java.net.URL;
021import java.util.concurrent.Executor;
022import javax.xml.namespace.QName;
023import javax.xml.ws.Service;
024import javax.xml.ws.WebServiceFeature;
025import javax.xml.ws.handler.HandlerResolver;
026
027import org.springframework.core.io.Resource;
028import org.springframework.lang.UsesJava7;
029import org.springframework.util.Assert;
030
031/**
032 * Factory for locally defined JAX-WS {@link javax.xml.ws.Service} references.
033 * Uses the JAX-WS {@link javax.xml.ws.Service#create} factory API underneath.
034 *
035 * <p>Serves as base class for {@link LocalJaxWsServiceFactoryBean} as well as
036 * {@link JaxWsPortClientInterceptor} and {@link JaxWsPortProxyFactoryBean}.
037 *
038 * @author Juergen Hoeller
039 * @since 2.5
040 * @see javax.xml.ws.Service
041 * @see LocalJaxWsServiceFactoryBean
042 * @see JaxWsPortClientInterceptor
043 * @see JaxWsPortProxyFactoryBean
044 */
045public class LocalJaxWsServiceFactory {
046
047        private URL wsdlDocumentUrl;
048
049        private String namespaceUri;
050
051        private String serviceName;
052
053        private WebServiceFeature[] serviceFeatures;
054
055        private Executor executor;
056
057        private HandlerResolver handlerResolver;
058
059
060        /**
061         * Set the URL of the WSDL document that describes the service.
062         * @see #setWsdlDocumentResource(Resource)
063         */
064        public void setWsdlDocumentUrl(URL wsdlDocumentUrl) {
065                this.wsdlDocumentUrl = wsdlDocumentUrl;
066        }
067
068        /**
069         * Set the WSDL document URL as a {@link Resource}.
070         * @since 3.2
071         */
072        public void setWsdlDocumentResource(Resource wsdlDocumentResource) throws IOException {
073                Assert.notNull(wsdlDocumentResource, "WSDL Resource must not be null.");
074                this.wsdlDocumentUrl = wsdlDocumentResource.getURL();
075        }
076
077        /**
078         * Return the URL of the WSDL document that describes the service.
079         */
080        public URL getWsdlDocumentUrl() {
081                return this.wsdlDocumentUrl;
082        }
083
084        /**
085         * Set the namespace URI of the service.
086         * Corresponds to the WSDL "targetNamespace".
087         */
088        public void setNamespaceUri(String namespaceUri) {
089                this.namespaceUri = (namespaceUri != null ? namespaceUri.trim() : null);
090        }
091
092        /**
093         * Return the namespace URI of the service.
094         */
095        public String getNamespaceUri() {
096                return this.namespaceUri;
097        }
098
099        /**
100         * Set the name of the service to look up.
101         * Corresponds to the "wsdl:service" name.
102         */
103        public void setServiceName(String serviceName) {
104                this.serviceName = serviceName;
105        }
106
107        /**
108         * Return the name of the service.
109         */
110        public String getServiceName() {
111                return this.serviceName;
112        }
113
114        /**
115         * Specify WebServiceFeature objects (e.g. as inner bean definitions)
116         * to apply to JAX-WS service creation.
117         * <p>Note: This mechanism requires JAX-WS 2.2 or higher.
118         * @since 4.0
119         * @see Service#create(QName, WebServiceFeature...)
120         */
121        public void setServiceFeatures(WebServiceFeature... serviceFeatures) {
122                this.serviceFeatures = serviceFeatures;
123        }
124
125        /**
126         * Set the JDK concurrent executor to use for asynchronous executions
127         * that require callbacks.
128         * @see javax.xml.ws.Service#setExecutor
129         */
130        public void setExecutor(Executor executor) {
131                this.executor = executor;
132        }
133
134        /**
135         * Set the JAX-WS HandlerResolver to use for all proxies and dispatchers
136         * created through this factory.
137         * @see javax.xml.ws.Service#setHandlerResolver
138         */
139        public void setHandlerResolver(HandlerResolver handlerResolver) {
140                this.handlerResolver = handlerResolver;
141        }
142
143
144        /**
145         * Create a JAX-WS Service according to the parameters of this factory.
146         * @see #setServiceName
147         * @see #setWsdlDocumentUrl
148         */
149        @UsesJava7  // optional use of Service#create with WebServiceFeature[]
150        public Service createJaxWsService() {
151                Assert.notNull(this.serviceName, "No service name specified");
152                Service service;
153
154                if (this.serviceFeatures != null) {
155                        service = (this.wsdlDocumentUrl != null ?
156                                Service.create(this.wsdlDocumentUrl, getQName(this.serviceName), this.serviceFeatures) :
157                                Service.create(getQName(this.serviceName), this.serviceFeatures));
158                }
159                else {
160                        service = (this.wsdlDocumentUrl != null ?
161                                        Service.create(this.wsdlDocumentUrl, getQName(this.serviceName)) :
162                                        Service.create(getQName(this.serviceName)));
163                }
164
165                if (this.executor != null) {
166                        service.setExecutor(this.executor);
167                }
168                if (this.handlerResolver != null) {
169                        service.setHandlerResolver(this.handlerResolver);
170                }
171
172                return service;
173        }
174
175        /**
176         * Return a QName for the given name, relative to the namespace URI
177         * of this factory, if given.
178         * @see #setNamespaceUri
179         */
180        protected QName getQName(String name) {
181                return (getNamespaceUri() != null ? new QName(getNamespaceUri(), name) : new QName(name));
182        }
183
184}