]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/MarshallerMessageConverter.java
Implement kill and process progress
[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.io.Serializable;
20 import java.util.Enumeration;
21
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;
27
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;
37
38 public class MarshallerMessageConverter implements MessageConverter {
39 private final static Log log = LogFactory
40 .getLog(MarshallerMessageConverter.class);
41
42 private Marshaller marshaller;
43 private Unmarshaller unmarshaller;
44
45 /** Use unmarshalled ObjectMessages instead of TextMessages */
46 private Boolean disableMarshalling = false;
47
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();
53 if (message == null)
54 return null;
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));
61 }
62 }
63
64 Object res;
65 if (message instanceof TextMessage) {
66 String text = ((TextMessage) message).getText();
67 if (text == null)
68 throw new SlcException(
69 "Cannot unmarshall message without body: " + message);
70 try {
71 res = unmarshaller.unmarshal(new StringSource(text));
72 } catch (Exception e) {
73 throw new SlcException("Could not unmarshall " + text, e);
74 }
75 } else if (message instanceof ObjectMessage) {
76 res = ((ObjectMessage) message).getObject();
77 if (res == null)
78 throw new SlcException("Message without body: " + message);
79 } else {
80 throw new SlcException("This type of messages is not supported: "
81 + message);
82 }
83 if (log.isTraceEnabled())
84 log.trace("'From' message processed in " + (System.currentTimeMillis() - begin)
85 + " ms");
86 return res;
87 }
88
89 public Message toMessage(Object object, Session session)
90 throws JMSException, MessageConversionException {
91 long begin = System.currentTimeMillis();
92 Message msg;
93 if (disableMarshalling) {
94 msg = session.createObjectMessage();
95 ((ObjectMessage) msg).setObject((Serializable) object);
96 } else {
97 StringResult result = new StringResult();
98 try {
99 marshaller.marshal(object, result);
100 } catch (Exception e) {
101 throw new SlcException("Could not marshall " + object, e);
102 }
103 msg = session.createTextMessage();
104 ((TextMessage) msg).setText(result.toString());
105 }
106 if (log.isTraceEnabled())
107 log.trace("'To' message processed in " + (System.currentTimeMillis() - begin)
108 + " ms");
109 return msg;
110 }
111
112 public void setMarshaller(Marshaller marshaller) {
113 this.marshaller = marshaller;
114 }
115
116 public void setUnmarshaller(Unmarshaller unmarshaller) {
117 this.unmarshaller = unmarshaller;
118 }
119
120 public void setDisableMarshalling(Boolean disableMarshalling) {
121 this.disableMarshalling = disableMarshalling;
122 }
123
124 }