View Javadoc

1   /*
2    * Copyright 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.ws.client.core.support;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import org.springframework.beans.factory.InitializingBean;
23  import org.springframework.oxm.Marshaller;
24  import org.springframework.oxm.Unmarshaller;
25  import org.springframework.util.Assert;
26  import org.springframework.ws.WebServiceMessageFactory;
27  import org.springframework.ws.client.core.WebServiceTemplate;
28  import org.springframework.ws.client.support.destination.DestinationProvider;
29  import org.springframework.ws.client.support.interceptor.ClientInterceptor;
30  import org.springframework.ws.transport.WebServiceMessageSender;
31  
32  /**
33   * Convenient super class for application classes that need Web service access.
34   * <p/>
35   * Requires a [email protected] WebServiceMessageFactory} or a [email protected] WebServiceTemplate} instance to be set. It will create its
36   * own <code>WebServiceTemplate</code> if <code>WebServiceMessageFactory</code> is passed in.
37   * <p/>
38   * In addition to the message factory property, this gateway offers [email protected] Marshaller} and [email protected] Unmarshaller}
39   * properties. Setting these is required when the [email protected] WebServiceTemplate#marshalSendAndReceive(Object) marshalling
40   * methods} of the template are to be used.
41   * <p/>
42   * Note that when [email protected] #setWebServiceTemplate(WebServiceTemplate) injecting a <code>WebServiceTemplate</code>}
43   * directly, the convenience setters ([email protected] #setMarshaller(Marshaller)}, [email protected] #setUnmarshaller(Unmarshaller)}, [email protected]
44   * #setMessageSender(WebServiceMessageSender)}, [email protected] #setMessageSenders(WebServiceMessageSender[])}, and [email protected]
45   * #setDefaultUri(String)}) should not be used on this class, but on the template directly.
46   *
47   * @author Arjen Poutsma
48   * @see #setMessageFactory(WebServiceMessageFactory)
49   * @see WebServiceTemplate
50   * @see #setMarshaller(Marshaller)
51   * @since 1.0.0
52   */
53  public abstract class WebServiceGatewaySupport implements InitializingBean {
54  
55      /** Logger available to subclasses. */
56      protected final Log logger = LogFactory.getLog(getClass());
57  
58      private WebServiceTemplate webServiceTemplate;
59  
60      /**
61       * Creates a new instance of the <code>WebServiceGatewaySupport</code> class, with a default
62       * <code>WebServiceTemplate</code>.
63       */
64      protected WebServiceGatewaySupport() {
65          webServiceTemplate = new WebServiceTemplate();
66      }
67  
68      /**
69       * Creates a new <code>WebServiceGatewaySupport</code> instance based on the given message factory.
70       *
71       * @param messageFactory the message factory to use
72       */
73      protected WebServiceGatewaySupport(WebServiceMessageFactory messageFactory) {
74          webServiceTemplate = new WebServiceTemplate(messageFactory);
75      }
76  
77      /** Returns the <code>WebServiceMessageFactory</code> used by the gateway. */
78      public final WebServiceMessageFactory getMessageFactory() {
79          return webServiceTemplate.getMessageFactory();
80      }
81  
82      /** Set the <code>WebServiceMessageFactory</code> to be used by the gateway. */
83      public final void setMessageFactory(WebServiceMessageFactory messageFactory) {
84          webServiceTemplate.setMessageFactory(messageFactory);
85      }
86  
87      /** Returns the default URI used by the gateway. */
88      public final String getDefaultUri() {
89          return webServiceTemplate.getDefaultUri();
90      }
91  
92      /** Sets the default URI used by the gateway. */
93      public final void setDefaultUri(String uri) {
94          webServiceTemplate.setDefaultUri(uri);
95      }
96  
97      /** Returns the destination provider used by the gateway. */
98      public final DestinationProvider getDestinationProvider() {
99          return webServiceTemplate.getDestinationProvider();
100     }
101 
102     /** Set the destination provider URI used by the gateway. */
103     public final void setDestinationProvider(DestinationProvider destinationProvider) {
104         webServiceTemplate.setDestinationProvider(destinationProvider);
105     }
106 
107     /** Sets a single <code>WebServiceMessageSender</code> to be used by the gateway. */
108     public final void setMessageSender(WebServiceMessageSender messageSender) {
109         webServiceTemplate.setMessageSender(messageSender);
110     }
111 
112     /** Returns the <code>WebServiceMessageSender</code>s used by the gateway. */
113     public final WebServiceMessageSender[] getMessageSenders() {
114         return webServiceTemplate.getMessageSenders();
115     }
116 
117     /** Sets multiple <code>WebServiceMessageSender</code> to be used by the gateway. */
118     public final void setMessageSenders(WebServiceMessageSender[] messageSenders) {
119         webServiceTemplate.setMessageSenders(messageSenders);
120     }
121 
122     /** Returns the <code>WebServiceTemplate</code> for the gateway. */
123     public final WebServiceTemplate getWebServiceTemplate() {
124         return webServiceTemplate;
125     }
126 
127     /**
128      * Sets the <code>WebServiceTemplate</code> to be used by the gateway.
129      * <p/>
130      * When using this property, the convenience setters ([email protected] #setMarshaller(Marshaller)}, [email protected]
131      * #setUnmarshaller(Unmarshaller)}, [email protected] #setMessageSender(WebServiceMessageSender)}, [email protected]
132      * #setMessageSenders(WebServiceMessageSender[])}, and [email protected] #setDefaultUri(String)}) should not be set on this
133      * class, but on the template directly.
134      */
135     public final void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
136         Assert.notNull(webServiceTemplate, "'webServiceTemplate' must not be null");
137         this.webServiceTemplate = webServiceTemplate;
138     }
139 
140     /** Returns the <code>Marshaller</code> used by the gateway. */
141     public final Marshaller getMarshaller() {
142         return webServiceTemplate.getMarshaller();
143     }
144 
145     /**
146      * Sets the <code>Marshaller</code> used by the gateway. Setting this property is only required if the marshalling
147      * functionality of <code>WebServiceTemplate</code> is to be used.
148      *
149      * @see WebServiceTemplate#marshalSendAndReceive
150      */
151     public final void setMarshaller(Marshaller marshaller) {
152         webServiceTemplate.setMarshaller(marshaller);
153     }
154 
155     /** Returns the <code>Unmarshaller</code> used by the gateway. */
156     public final Unmarshaller getUnmarshaller() {
157         return webServiceTemplate.getUnmarshaller();
158     }
159 
160     /**
161      * Sets the <code>Unmarshaller</code> used by the gateway. Setting this property is only required if the marshalling
162      * functionality of <code>WebServiceTemplate</code> is to be used.
163      *
164      * @see WebServiceTemplate#marshalSendAndReceive
165      */
166     public final void setUnmarshaller(Unmarshaller unmarshaller) {
167         webServiceTemplate.setUnmarshaller(unmarshaller);
168     }
169 
170     /** Returns the <code>ClientInterceptors</code> used by the template. */
171     public final ClientInterceptor[] getInterceptors() {
172         return webServiceTemplate.getInterceptors();
173     }
174 
175     /** Sets the <code>ClientInterceptors</code> used by the gateway. */
176     public final void setInterceptors(ClientInterceptor[] interceptors) {
177         webServiceTemplate.setInterceptors(interceptors);
178     }
179 
180     public final void afterPropertiesSet() throws Exception {
181         webServiceTemplate.afterPropertiesSet();
182         initGateway();
183     }
184 
185     /**
186      * Subclasses can override this for custom initialization behavior. Gets called after population of this instance's
187      * bean properties.
188      *
189      * @throws java.lang.Exception if initialization fails
190      */
191     protected void initGateway() throws Exception {
192     }
193 
194 }