Fixed SWS-106: Default message factory for WebServiceTemplate

This commit is contained in:
Arjen Poutsma
2007-04-12 14:53:33 +00:00
parent af847033f1
commit 74a880e1a5
5 changed files with 103 additions and 89 deletions

View File

@@ -23,9 +23,17 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.WebServiceClientException;
@@ -36,6 +44,7 @@ import org.springframework.ws.transport.TransportInputStream;
import org.springframework.ws.transport.TransportOutputStream;
import org.springframework.ws.transport.WebServiceConnection;
import org.springframework.ws.transport.WebServiceMessageSender;
import org.springframework.ws.transport.support.DefaultStrategiesHelper;
/**
* <strong>The central class for client-side Web services.</strong> It provides a message-driven approach to sending and
@@ -48,7 +57,7 @@ import org.springframework.ws.transport.WebServiceMessageSender;
*
* @author Arjen Poutsma
*/
public class WebServiceTemplate extends WebServiceAccessor implements WebServiceOperations {
public class WebServiceTemplate extends WebServiceAccessor implements WebServiceOperations, ApplicationContextAware {
private Marshaller marshaller;
@@ -56,16 +65,12 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
private FaultResolver faultResolver = new SimpleFaultResolver();
/**
* Creates a new <code>WebServiceTemplate</code>.
* <p/>
* <b>Note</b> that the message factory and message sender properties have to be set before this template can be
* used.
*
* @see #setMessageFactory(org.springframework.ws.WebServiceMessageFactory)
* @see #setMessageSender(org.springframework.ws.transport.WebServiceMessageSender)
*/
private DefaultStrategiesHelper defaultStrategiesHelper;
/** Creates a new <code>WebServiceTemplate</code> using default settings. */
public WebServiceTemplate() {
Resource resource = new ClassPathResource(ClassUtils.getShortName(getClass()) + ".properties", getClass());
defaultStrategiesHelper = new DefaultStrategiesHelper(resource);
}
/**
@@ -75,6 +80,7 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
* @param messageSender the message sender to use
*/
public WebServiceTemplate(WebServiceMessageFactory messageFactory, WebServiceMessageSender messageSender) {
this();
setMessageFactory(messageFactory);
setMessageSender(messageSender);
}
@@ -110,9 +116,33 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
this.faultResolver = faultResolver;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (getMessageFactory() == null) {
initWebServiceMessageFactory(applicationContext);
}
}
private void initWebServiceMessageFactory(ApplicationContext applicationContext)
throws BeanInitializationException {
WebServiceMessageFactory messageFactory = (WebServiceMessageFactory) defaultStrategiesHelper
.getDefaultStrategy(WebServiceMessageFactory.class, applicationContext);
if (logger.isInfoEnabled()) {
logger.info("Using default message factory [" + messageFactory + "]");
}
if (messageFactory instanceof InitializingBean) {
try {
((InitializingBean) messageFactory).afterPropertiesSet();
}
catch (Exception ex) {
throw new BeanInitializationException("Could not initialize message factory", ex);
}
}
setMessageFactory(messageFactory);
}
/*
* Marshalling methods
*/
* Marshalling methods
*/
public Object marshalSendAndReceive(final Object requestPayload) throws IOException {
return marshalSendAndReceive(requestPayload, null);
@@ -143,8 +173,8 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
}
/*
* Result-handling methods
*/
* Result-handling methods
*/
public void sendAndReceive(Source requestPayload, Result responseResult) throws IOException {
sendAndReceive(requestPayload, null, responseResult);
@@ -174,8 +204,8 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
}
/*
* Source-handling methods
*/
* Source-handling methods
*/
public Object sendAndReceive(final Source requestPayload, final SourceExtractor responseExtractor)
throws IOException {
@@ -215,8 +245,8 @@ public class WebServiceTemplate extends WebServiceAccessor implements WebService
}
/*
* WebServiceMessage-handling methods
*/
* WebServiceMessage-handling methods
*/
public void sendAndReceive(WebServiceMessageCallback requestCallback, WebServiceMessageCallback responseCallback)
throws IOException {

View File

@@ -18,8 +18,10 @@ package org.springframework.ws.client.core.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.ws.WebServiceMessageFactory;
@@ -45,67 +47,65 @@ import org.springframework.ws.transport.WebServiceMessageSender;
* @see org.springframework.ws.client.core.WebServiceTemplate
* @see #setMarshaller(org.springframework.oxm.Marshaller)
*/
public abstract class WebServiceGatewaySupport implements InitializingBean {
public abstract class WebServiceGatewaySupport implements InitializingBean, ApplicationContextAware {
/** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());
private WebServiceMessageFactory messageFactory;
private WebServiceMessageSender messageSender;
private WebServiceTemplate webServiceTemplate;
private Marshaller marshaller;
private Unmarshaller unmarshaller;
/**
* Creates a new instance of the <code>WebServiceGatewaySupport</code> class, with a default
* <code>WebServiceTemplate</code>.
*/
protected WebServiceGatewaySupport() {
webServiceTemplate = new WebServiceTemplate();
}
/**
* Returns the <code>WebServiceMessageFactory</code> used by the gateway.
* Creates a new <code>WebServiceGatewaySupport</code> instance based on the given message factory and message
* sender.
*
* @param messageFactory the message factory to use
* @param messageSender the message sender to use
*/
protected WebServiceGatewaySupport(WebServiceMessageFactory messageFactory, WebServiceMessageSender messageSender) {
webServiceTemplate = new WebServiceTemplate(messageFactory, messageSender);
}
/** Returns the <code>WebServiceMessageFactory</code> used by the gateway. */
public final WebServiceMessageFactory getMessageFactory() {
return messageFactory;
return webServiceTemplate.getMessageFactory();
}
/**
* Set the <code>WebServiceMessageFactory</code> to be used by the gateway.
*/
/** Set the <code>WebServiceMessageFactory</code> to be used by the gateway. */
public final void setMessageFactory(WebServiceMessageFactory messageFactory) {
this.messageFactory = messageFactory;
webServiceTemplate.setMessageFactory(messageFactory);
}
/**
* Returns the <code>WebServiceMessageSender</code> used by the gateway.
*/
/** Returns the <code>WebServiceMessageSender</code> used by the gateway. */
public final WebServiceMessageSender getMessageSender() {
return messageSender;
return webServiceTemplate.getMessageSender();
}
/**
* Sets the <code>WebServiceMessageSender</code> to be used by the gateway.
*/
/** Sets the <code>WebServiceMessageSender</code> to be used by the gateway. */
public final void setMessageSender(WebServiceMessageSender messageSender) {
this.messageSender = messageSender;
webServiceTemplate.setMessageSender(messageSender);
}
/**
* Returns the <code>WebServiceTemplate</code> for the gateway.
*/
/** Returns the <code>WebServiceTemplate</code> for the gateway. */
public final WebServiceTemplate getWebServiceTemplate() {
return webServiceTemplate;
}
/**
* Sets the <code>WebServiceTemplate</code> to be used by the gateway.
*/
/** Sets the <code>WebServiceTemplate</code> to be used by the gateway. */
public final void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
this.webServiceTemplate = webServiceTemplate;
}
/**
* Returns the <code>Marshaller</code> used by the gateway.
*/
/** Returns the <code>Marshaller</code> used by the gateway. */
public final Marshaller getMarshaller() {
return marshaller;
return webServiceTemplate.getMarshaller();
}
/**
@@ -115,14 +115,12 @@ public abstract class WebServiceGatewaySupport implements InitializingBean {
* @see org.springframework.ws.client.core.WebServiceTemplate#marshalSendAndReceive
*/
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
webServiceTemplate.setMarshaller(marshaller);
}
/**
* Returns the <code>Unmarshaller</code> used by the gateway.
*/
/** Returns the <code>Unmarshaller</code> used by the gateway. */
public final Unmarshaller getUnmarshaller() {
return unmarshaller;
return webServiceTemplate.getUnmarshaller();
}
/**
@@ -132,29 +130,16 @@ public abstract class WebServiceGatewaySupport implements InitializingBean {
* @see org.springframework.ws.client.core.WebServiceTemplate#marshalSendAndReceive
*/
public final void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
webServiceTemplate.setUnmarshaller(unmarshaller);
}
public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
if (webServiceTemplate == null && messageFactory != null && messageSender != null) {
webServiceTemplate = new WebServiceTemplate(messageFactory, messageSender);
if (marshaller != null) {
webServiceTemplate.setMarshaller(marshaller);
}
if (unmarshaller != null) {
webServiceTemplate.setUnmarshaller(unmarshaller);
}
}
if (webServiceTemplate == null) {
throw new IllegalArgumentException("messageFactory and messageSender or webServiceTemplate is required");
}
try {
initGateway();
}
catch (Exception ex) {
throw new BeanInitializationException("Initialization of Web service gateway failed: " + ex.getMessage(),
ex);
}
public final void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
webServiceTemplate.setApplicationContext(applicationContext);
}
public final void afterPropertiesSet() throws Exception {
webServiceTemplate.afterPropertiesSet();
initGateway();
}
/**

View File

@@ -41,23 +41,17 @@ import org.springframework.util.StringUtils;
*/
public class DefaultStrategiesHelper {
/**
* Keys are strategy interface names, values are implementation class names.
*/
/** Keys are strategy interface names, values are implementation class names. */
private Properties defaultStrategies = new Properties();
/**
* Initializes a new instance of the <code>DefaultStrategiesHelper</code> based on the given set of properties.
*/
/** Initializes a new instance of the <code>DefaultStrategiesHelper</code> based on the given set of properties. */
public DefaultStrategiesHelper(Properties defaultStrategies) {
Assert.notNull(defaultStrategies, "defaultStrategies must not be null");
this.defaultStrategies = defaultStrategies;
}
/**
* Initializes a new instance of the <code>DefaultStrategiesHelper</code> based on the given resource.
*/
public DefaultStrategiesHelper(Resource resource) {
/** Initializes a new instance of the <code>DefaultStrategiesHelper</code> based on the given resource. */
public DefaultStrategiesHelper(Resource resource) throws IllegalStateException {
try {
InputStream is = resource.getInputStream();
defaultStrategies = new Properties();
@@ -127,7 +121,7 @@ public class DefaultStrategiesHelper {
List result = getDefaultStrategies(strategyInterface, applicationContext);
if (result.size() != 1) {
throw new BeanInitializationException(
"DispatcherServlet needs exactly 1 strategy for interface [" + strategyInterface.getName() + "]");
"Could not find exactly 1 strategy for interface [" + strategyInterface.getName() + "]");
}
return result.get(0);
}

View File

@@ -0,0 +1,4 @@
# Default implementation classes for WebServiceTemplate's strategy interfaces.
# Not meant to be customized by application developers.
org.springframework.ws.WebServiceMessageFactory=org.springframework.ws.soap.saaj.SaajSoapMessageFactory

View File

@@ -6,7 +6,8 @@
</properties>
<body>
<release version="1.0-RC1">
<action dev="poutsma" type="add" issue="SWS-79">Add faults into dynamically created WSDL</action>
<action dev="poutsma" type="fix" issue="SWS-106">Default message factory for WebServiceTemplate</action>
<action dev="poutsma" type="fix" issue="SWS-79">Add faults into dynamically created WSDL</action>
<action dev="poutsma" type="add" issue="SWS-20">Added MethodEndpoint functionality, invoking methods for
incoming requests
</action>