org.springframework.oxm.support
Class MarshallingMessageConverter

java.lang.Object
  extended by org.springframework.oxm.support.MarshallingMessageConverter
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, org.springframework.jms.support.converter.MessageConverter

public class MarshallingMessageConverter
extends Object
implements org.springframework.jms.support.converter.MessageConverter, org.springframework.beans.factory.InitializingBean

Spring JMS MessageConverter that uses a Marshaller and Unmarshaller. Marshals an object to a BytesMessage, or to a TextMessage if the marshalTo is set to MARSHAL_TO_TEXT_MESSAGE. Unmarshals from a TextMessage or BytesMessage to an object.

Since:
1.5.1
Author:
Arjen Poutsma
See Also:
org.springframework.jms.core.JmsTemplate#convertAndSend, org.springframework.jms.core.JmsTemplate#receiveAndConvert

Field Summary
static int MARSHAL_TO_BYTES_MESSAGE
          Constant that indicates that toMessage(Object, Session) should marshal to a BytesMessage.
static int MARSHAL_TO_TEXT_MESSAGE
          Constant that indicates that toMessage(Object, Session) should marshal to a TextMessage.
 
Constructor Summary
MarshallingMessageConverter()
          Constructs a new MarshallingMessageConverter with no Marshaller set.
MarshallingMessageConverter(Marshaller marshaller)
          Constructs a new MarshallingMessageConverter with the given Marshaller set.
MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)
          Creates a new MarshallingMessageConverter with the given marshaller and unmarshaller.
 
Method Summary
 void afterPropertiesSet()
           
 Object fromMessage(javax.jms.Message message)
          Unmarshals the given Message into an object.
protected  javax.jms.BytesMessage marshalToBytesMessage(Object object, javax.jms.Session session, Marshaller marshaller)
          Marshals the given object to a BytesMessage.
protected  javax.jms.Message marshalToMessage(Object object, javax.jms.Session session, Marshaller marshaller)
          Template method that allows for custom message marshalling.
protected  javax.jms.TextMessage marshalToTextMessage(Object object, javax.jms.Session session, Marshaller marshaller)
          Marshals the given object to a TextMessage.
 void setMarshaller(Marshaller marshaller)
          Sets the Marshaller to be used by this message converter.
 void setMarshalTo(int marshalTo)
          Indicates whether toMessage(Object,Session) should marshal to a BytesMessage or a TextMessage.
 void setUnmarshaller(Unmarshaller unmarshaller)
          Sets the Unmarshaller to be used by this message converter.
 javax.jms.Message toMessage(Object object, javax.jms.Session session)
          Marshals the given object to a TextMessage or BytesMessage.
protected  Object unmarshalFromBytesMessage(javax.jms.BytesMessage message, Unmarshaller unmarshaller)
          Unmarshals the given BytesMessage into an object.
protected  Object unmarshalFromMessage(javax.jms.Message message, Unmarshaller unmarshaller)
          Template method that allows for custom message unmarshalling.
protected  Object unmarshalFromTextMessage(javax.jms.TextMessage message, Unmarshaller unmarshaller)
          Unmarshals the given TextMessage into an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MARSHAL_TO_BYTES_MESSAGE

public static final int MARSHAL_TO_BYTES_MESSAGE
Constant that indicates that toMessage(Object, Session) should marshal to a BytesMessage.

See Also:
Constant Field Values

MARSHAL_TO_TEXT_MESSAGE

public static final int MARSHAL_TO_TEXT_MESSAGE
Constant that indicates that toMessage(Object, Session) should marshal to a TextMessage.

See Also:
Constant Field Values
Constructor Detail

MarshallingMessageConverter

public MarshallingMessageConverter()
Constructs a new MarshallingMessageConverter with no Marshaller set. The marshaller must be set after construction by invoking setMarshaller(Marshaller).


MarshallingMessageConverter

public MarshallingMessageConverter(Marshaller marshaller)
Constructs a new MarshallingMessageConverter with the given Marshaller set. If the given Marshaller also implements the Unmarshaller interface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.

Note that all Marshaller implementations in Spring-WS also implement the Unmarshaller interface, so that you can safely use this constructor.

Parameters:
marshaller - object used as marshaller and unmarshaller
Throws:
IllegalArgumentException - when marshaller does not implement the Unmarshaller interface

MarshallingMessageConverter

public MarshallingMessageConverter(Marshaller marshaller,
                                   Unmarshaller unmarshaller)
Creates a new MarshallingMessageConverter with the given marshaller and unmarshaller.

Parameters:
marshaller - the marshaller to use
unmarshaller - the unmarshaller to use
Method Detail

setMarshalTo

public void setMarshalTo(int marshalTo)
Indicates whether toMessage(Object,Session) should marshal to a BytesMessage or a TextMessage. The default is MARSHAL_TO_BYTES_MESSAGE, i.e. this converter marshals to a BytesMessage.

See Also:
MARSHAL_TO_BYTES_MESSAGE, MARSHAL_TO_TEXT_MESSAGE

setMarshaller

public void setMarshaller(Marshaller marshaller)
Sets the Marshaller to be used by this message converter.


setUnmarshaller

public void setUnmarshaller(Unmarshaller unmarshaller)
Sets the Unmarshaller to be used by this message converter.


afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
Exception

toMessage

public javax.jms.Message toMessage(Object object,
                                   javax.jms.Session session)
                            throws javax.jms.JMSException,
                                   org.springframework.jms.support.converter.MessageConversionException
Marshals the given object to a TextMessage or BytesMessage. The desired message type can be defined by setting the marshalTo property.

Specified by:
toMessage in interface org.springframework.jms.support.converter.MessageConverter
Throws:
javax.jms.JMSException
org.springframework.jms.support.converter.MessageConversionException
See Also:
marshalToTextMessage(java.lang.Object, javax.jms.Session, org.springframework.oxm.Marshaller), marshalToBytesMessage(java.lang.Object, javax.jms.Session, org.springframework.oxm.Marshaller)

fromMessage

public Object fromMessage(javax.jms.Message message)
                   throws javax.jms.JMSException,
                          org.springframework.jms.support.converter.MessageConversionException
Unmarshals the given Message into an object.

Specified by:
fromMessage in interface org.springframework.jms.support.converter.MessageConverter
Throws:
javax.jms.JMSException
org.springframework.jms.support.converter.MessageConversionException
See Also:
unmarshalFromTextMessage(javax.jms.TextMessage, org.springframework.oxm.Unmarshaller), unmarshalFromBytesMessage(javax.jms.BytesMessage, org.springframework.oxm.Unmarshaller)

marshalToTextMessage

protected javax.jms.TextMessage marshalToTextMessage(Object object,
                                                     javax.jms.Session session,
                                                     Marshaller marshaller)
                                              throws javax.jms.JMSException,
                                                     IOException
Marshals the given object to a TextMessage.

Parameters:
object - the object to be marshalled
session - current JMS session
marshaller - the marshaller to use
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors
See Also:
Session.createTextMessage(), Marshaller.marshal(Object, Result)

marshalToBytesMessage

protected javax.jms.BytesMessage marshalToBytesMessage(Object object,
                                                       javax.jms.Session session,
                                                       Marshaller marshaller)
                                                throws javax.jms.JMSException,
                                                       IOException
Marshals the given object to a BytesMessage.

Parameters:
object - the object to be marshalled
session - current JMS session
marshaller - the marshaller to use
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors
See Also:
Session.createBytesMessage(), Marshaller.marshal(Object, Result)

marshalToMessage

protected javax.jms.Message marshalToMessage(Object object,
                                             javax.jms.Session session,
                                             Marshaller marshaller)
                                      throws javax.jms.JMSException,
                                             IOException
Template method that allows for custom message marshalling. Invoked when setMarshalTo(int) is not MARSHAL_TO_TEXT_MESSAGE or MARSHAL_TO_BYTES_MESSAGE.

Default implemenetation throws a MessageConversionException.

Parameters:
object - the object to marshal
session - the JMS session
marshaller - the marshaller to use
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors

unmarshalFromTextMessage

protected Object unmarshalFromTextMessage(javax.jms.TextMessage message,
                                          Unmarshaller unmarshaller)
                                   throws javax.jms.JMSException,
                                          IOException
Unmarshals the given TextMessage into an object.

Parameters:
message - the message
unmarshaller - the unmarshaller to use
Returns:
the unmarshalled object
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors
See Also:
Unmarshaller.unmarshal(Source)

unmarshalFromBytesMessage

protected Object unmarshalFromBytesMessage(javax.jms.BytesMessage message,
                                           Unmarshaller unmarshaller)
                                    throws javax.jms.JMSException,
                                           IOException
Unmarshals the given BytesMessage into an object.

Parameters:
message - the message
unmarshaller - the unmarshaller to use
Returns:
the unmarshalled object
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors
See Also:
Unmarshaller.unmarshal(Source)

unmarshalFromMessage

protected Object unmarshalFromMessage(javax.jms.Message message,
                                      Unmarshaller unmarshaller)
                               throws javax.jms.JMSException,
                                      IOException
Template method that allows for custom message unmarshalling. Invoked when fromMessage(Message) is invoked with a message that is not a TextMessage or BytesMessage.

Default implemenetation throws a MessageConversionException.

Parameters:
message - the message
unmarshaller - the unmarshaller to use
Returns:
the unmarshalled object
Throws:
javax.jms.JMSException - if thrown by JMS methods
IOException - in case of I/O errors


Copyright © 2010. All Rights Reserved.