]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/MarshallerMessageConverter.java
8df639aa336031ff9f4f36ff0a123f8a9bdddbba
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / MarshallerMessageConverter.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.argeo.slc.jms;
18
19 import java.util.Enumeration;
20
21 import javax.jms.JMSException;
22 import javax.jms.Message;
23 import javax.jms.Session;
24 import javax.jms.TextMessage;
25
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;
35
36 public class MarshallerMessageConverter implements MessageConverter {
37 private final static Log log = LogFactory
38 .getLog(MarshallerMessageConverter.class);
39
40 private Marshaller marshaller;
41 private Unmarshaller unmarshaller;
42
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) {
48 return null;
49 }
50
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));
57 }
58 }
59
60 if (message instanceof TextMessage) {
61
62 String text = ((TextMessage) message).getText();
63
64 if (text == null)
65 throw new SlcException(
66 "Cannot unmarshall message without body: " + message);
67
68 try {
69 return unmarshaller.unmarshal(new StringSource(text));
70 } catch (Exception e) {
71 throw new SlcException("Could not unmarshall " + text, e);
72 }
73 } else {
74 throw new SlcException("This type of messages is not supported: "
75 + message);
76 }
77 }
78
79 public Message toMessage(Object object, Session session)
80 throws JMSException, MessageConversionException {
81 StringResult result = new StringResult();
82 try {
83 marshaller.marshal(object, result);
84 } catch (Exception e) {
85 throw new SlcException("Could not marshall " + object, e);
86 }
87 TextMessage msg = session.createTextMessage();
88 msg.setText(result.toString());
89 return msg;
90 }
91
92 public void setMarshaller(Marshaller marshaller) {
93 this.marshaller = marshaller;
94 }
95
96 public void setUnmarshaller(Unmarshaller unmarshaller) {
97 this.unmarshaller = unmarshaller;
98 }
99
100 }