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
.util
.Enumeration
;
21 import javax
.jms
.JMSException
;
22 import javax
.jms
.Message
;
23 import javax
.jms
.Session
;
24 import javax
.jms
.TextMessage
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.springframework
.jms
.support
.converter
.MessageConversionException
;
30 import org
.springframework
.jms
.support
.converter
.MessageConverter
;
31 import org
.springframework
.oxm
.Marshaller
;
32 import org
.springframework
.oxm
.Unmarshaller
;
33 import org
.springframework
.xml
.transform
.StringResult
;
34 import org
.springframework
.xml
.transform
.StringSource
;
36 public class MarshallerMessageConverter
implements MessageConverter
{
37 private final static Log log
= LogFactory
38 .getLog(MarshallerMessageConverter
.class);
40 private Marshaller marshaller
;
41 private Unmarshaller unmarshaller
;
43 /** @return the converted message or null if the message itself is null */
44 @SuppressWarnings("unchecked")
45 public Object
fromMessage(Message message
) throws JMSException
,
46 MessageConversionException
{
47 if (message
== null) {
51 if (log
.isTraceEnabled()) {
52 Enumeration
<String
> names
= message
.getPropertyNames();
53 while (names
.hasMoreElements()) {
54 String name
= names
.nextElement();
55 log
.trace("JMS Property: " + name
+ "="
56 + message
.getObjectProperty(name
));
60 if (message
instanceof TextMessage
) {
62 String text
= ((TextMessage
) message
).getText();
65 throw new SlcException(
66 "Cannot unmarshall message without body: " + message
);
69 return unmarshaller
.unmarshal(new StringSource(text
));
70 } catch (Exception e
) {
71 throw new SlcException("Could not unmarshall " + text
, e
);
74 throw new SlcException("This type of messages is not supported: "
79 public Message
toMessage(Object object
, Session session
)
80 throws JMSException
, MessageConversionException
{
81 StringResult result
= new StringResult();
83 marshaller
.marshal(object
, result
);
84 } catch (Exception e
) {
85 throw new SlcException("Could not marshall " + object
, e
);
87 TextMessage msg
= session
.createTextMessage();
88 msg
.setText(result
.toString());
92 public void setMarshaller(Marshaller marshaller
) {
93 this.marshaller
= marshaller
;
96 public void setUnmarshaller(Unmarshaller unmarshaller
) {
97 this.unmarshaller
= unmarshaller
;