2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.jms
;
19 import java
.io
.Serializable
;
20 import java
.util
.Enumeration
;
22 import javax
.jms
.JMSException
;
23 import javax
.jms
.Message
;
24 import javax
.jms
.ObjectMessage
;
25 import javax
.jms
.Session
;
26 import javax
.jms
.TextMessage
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.springframework
.jms
.support
.converter
.MessageConversionException
;
32 import org
.springframework
.jms
.support
.converter
.MessageConverter
;
33 import org
.springframework
.oxm
.Marshaller
;
34 import org
.springframework
.oxm
.Unmarshaller
;
35 import org
.springframework
.xml
.transform
.StringResult
;
36 import org
.springframework
.xml
.transform
.StringSource
;
38 public class MarshallerMessageConverter
implements MessageConverter
{
39 private final static Log log
= LogFactory
40 .getLog(MarshallerMessageConverter
.class);
42 private Marshaller marshaller
;
43 private Unmarshaller unmarshaller
;
45 /** Use unmarshalled ObjectMessages instead of TextMessages */
46 private Boolean disableMarshalling
= false;
48 /** @return the converted message or null if the message itself is null */
49 @SuppressWarnings("unchecked")
50 public Object
fromMessage(Message message
) throws JMSException
,
51 MessageConversionException
{
52 long begin
= System
.currentTimeMillis();
55 if (log
.isTraceEnabled()) {
56 Enumeration
<String
> names
= message
.getPropertyNames();
57 while (names
.hasMoreElements()) {
58 String name
= names
.nextElement();
59 log
.trace("JMS Property: " + name
+ "="
60 + message
.getObjectProperty(name
));
65 if (message
instanceof TextMessage
) {
66 String text
= ((TextMessage
) message
).getText();
68 throw new SlcException(
69 "Cannot unmarshall message without body: " + message
);
71 res
= unmarshaller
.unmarshal(new StringSource(text
));
72 } catch (Exception e
) {
73 throw new SlcException("Could not unmarshall " + text
, e
);
75 } else if (message
instanceof ObjectMessage
) {
76 res
= ((ObjectMessage
) message
).getObject();
78 throw new SlcException("Message without body: " + message
);
80 throw new SlcException("This type of messages is not supported: "
83 if (log
.isTraceEnabled())
84 log
.trace("'From' message processed in " + (System
.currentTimeMillis() - begin
)
89 public Message
toMessage(Object object
, Session session
)
90 throws JMSException
, MessageConversionException
{
91 long begin
= System
.currentTimeMillis();
93 if (disableMarshalling
) {
94 msg
= session
.createObjectMessage();
95 ((ObjectMessage
) msg
).setObject((Serializable
) object
);
97 StringResult result
= new StringResult();
99 marshaller
.marshal(object
, result
);
100 } catch (Exception e
) {
101 throw new SlcException("Could not marshall " + object
, e
);
103 msg
= session
.createTextMessage();
104 ((TextMessage
) msg
).setText(result
.toString());
106 if (log
.isTraceEnabled())
107 log
.trace("'To' message processed in " + (System
.currentTimeMillis() - begin
)
112 public void setMarshaller(Marshaller marshaller
) {
113 this.marshaller
= marshaller
;
116 public void setUnmarshaller(Unmarshaller unmarshaller
) {
117 this.unmarshaller
= unmarshaller
;
120 public void setDisableMarshalling(Boolean disableMarshalling
) {
121 this.disableMarshalling
= disableMarshalling
;