001/*
002 * Copyright 2002-2018 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;
022
023import javax.xml.namespace.QName;
024import javax.xml.ws.Service;
025import javax.xml.ws.WebServiceFeature;
026import javax.xml.ws.handler.HandlerResolver;
027
028import org.springframework.core.io.Resource;
029import org.springframework.lang.Nullable;
030import org.springframework.util.Assert;
031
032/**
033 * Factory for locally defined JAX-WS {@link javax.xml.ws.Service} references.
034 * Uses the JAX-WS {@link javax.xml.ws.Service#create} factory API underneath.
035 *
036 * <p>Serves as base class for {@link LocalJaxWsServiceFactoryBean} as well as
037 * {@link JaxWsPortClientInterceptor} and {@link JaxWsPortProxyFactoryBean}.
038 *
039 * @author Juergen Hoeller
040 * @since 2.5
041 * @see javax.xml.ws.Service
042 * @see LocalJaxWsServiceFactoryBean
043 * @see JaxWsPortClientInterceptor
044 * @see JaxWsPortProxyFactoryBean
045 */
046public class LocalJaxWsServiceFactory {
047
048        @Nullable
049        private URL wsdlDocumentUrl;
050
051        @Nullable
052        private String namespaceUri;
053
054        @Nullable
055        private String serviceName;
056
057        @Nullable
058        private WebServiceFeature[] serviceFeatures;
059
060        @Nullable
061        private Executor executor;
062
063        @Nullable
064        private HandlerResolver handlerResolver;
065
066
067        /**
068         * Set the URL of the WSDL document that describes the service.
069         * @see #setWsdlDocumentResource(Resource)
070         */
071        public void setWsdlDocumentUrl(@Nullable URL wsdlDocumentUrl) {
072                this.wsdlDocumentUrl = wsdlDocumentUrl;
073        }
074
075        /**
076         * Set the WSDL document URL as a {@link Resource}.
077         * @since 3.2
078         */
079        public void setWsdlDocumentResource(Resource wsdlDocumentResource) throws IOException {
080                Assert.notNull(wsdlDocumentResource, "WSDL Resource must not be null");
081                this.wsdlDocumentUrl = wsdlDocumentResource.getURL();
082        }
083
084        /**
085         * Return the URL of the WSDL document that describes the service.
086         */
087        @Nullable
088        public URL getWsdlDocumentUrl() {
089                return this.wsdlDocumentUrl;
090        }
091
092        /**
093         * Set the namespace URI of the service.
094         * Corresponds to the WSDL "targetNamespace".
095         */
096        public void setNamespaceUri(@Nullable String namespaceUri) {
097                this.namespaceUri = (namespaceUri != null ? namespaceUri.trim() : null);
098        }
099
100        /**
101         * Return the namespace URI of the service.
102         */
103        @Nullable
104        public String getNamespaceUri() {
105                return this.namespaceUri;
106        }
107
108        /**
109         * Set the name of the service to look up.
110         * Corresponds to the "wsdl:service" name.
111         */
112        public void setServiceName(@Nullable String serviceName) {
113                this.serviceName = serviceName;
114        }
115
116        /**
117         * Return the name of the service.
118         */
119        @Nullable
120        public String getServiceName() {
121                return this.serviceName;
122        }
123
124        /**
125         * Specify WebServiceFeature objects (e.g. as inner bean definitions)
126         * to apply to JAX-WS service creation.
127         * @since 4.0
128         * @see Service#create(QName, WebServiceFeature...)
129         */
130        public void setServiceFeatures(WebServiceFeature... serviceFeatures) {
131                this.serviceFeatures = serviceFeatures;
132        }
133
134        /**
135         * Set the JDK concurrent executor to use for asynchronous executions
136         * that require callbacks.
137         * @see javax.xml.ws.Service#setExecutor
138         */
139        public void setExecutor(Executor executor) {
140                this.executor = executor;
141        }
142
143        /**
144         * Set the JAX-WS HandlerResolver to use for all proxies and dispatchers
145         * created through this factory.
146         * @see javax.xml.ws.Service#setHandlerResolver
147         */
148        public void setHandlerResolver(HandlerResolver handlerResolver) {
149                this.handlerResolver = handlerResolver;
150        }
151
152
153        /**
154         * Create a JAX-WS Service according to the parameters of this factory.
155         * @see #setServiceName
156         * @see #setWsdlDocumentUrl
157         */
158        public Service createJaxWsService() {
159                Assert.notNull(this.serviceName, "No service name specified");
160                Service service;
161
162                if (this.serviceFeatures != null) {
163                        service = (this.wsdlDocumentUrl != null ?
164                                Service.create(this.wsdlDocumentUrl, getQName(this.serviceName), this.serviceFeatures) :
165                                Service.create(getQName(this.serviceName), this.serviceFeatures));
166                }
167                else {
168                        service = (this.wsdlDocumentUrl != null ?
169                                        Service.create(this.wsdlDocumentUrl, getQName(this.serviceName)) :
170                                        Service.create(getQName(this.serviceName)));
171                }
172
173                if (this.executor != null) {
174                        service.setExecutor(this.executor);
175                }
176                if (this.handlerResolver != null) {
177                        service.setHandlerResolver(this.handlerResolver);
178                }
179
180                return service;
181        }
182
183        /**
184         * Return a QName for the given name, relative to the namespace URI
185         * of this factory, if given.
186         * @see #setNamespaceUri
187         */
188        protected QName getQName(String name) {
189                return (getNamespaceUri() != null ? new QName(getNamespaceUri(), name) : new QName(name));
190        }
191
192}