blob: c8ec2c2970b8b2542a97c82e29f4fe34f9b41d11 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*******************************************************************************
* Copyright (c) 2010 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.framework.messaging.internal.activemq;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.messaging.internal.JAXBUtil;
/**
* @author Andrew M. Finkbeiner
*/
class ActiveMqUtil {
ActiveMqUtil() {
}
Object translateMessage(Message message, Class<?> clazz) throws OseeCoreException, JMSException {
Object messageBody = message;
if (message instanceof TextMessage) {
String text = ((TextMessage) message).getText();
if (clazz != null) {
try {
messageBody = JAXBUtil.unmarshal(text, clazz);
} catch (UnsupportedEncodingException ex) {
OseeExceptions.wrapAndThrow(ex);
}
} else {
messageBody = text;
}
} else if (message instanceof BytesMessage) {
int length = (int) ((BytesMessage) message).getBodyLength();
byte[] bytes = new byte[length];
((BytesMessage) message).readBytes(bytes);
messageBody = bytes;
} else if (message instanceof ObjectMessage) {
messageBody = ((ObjectMessage) message).getObject();
}
return messageBody;
}
Message createMessage(Session session, Class<?> clazz, Object body) throws OseeCoreException, JMSException {
body = tryToGetSerialized(clazz, body);
if (body instanceof String) {
return session.createTextMessage((String) body);
} else if (body instanceof byte[]) {
BytesMessage byteMessage = session.createBytesMessage();
byteMessage.writeBytes((byte[]) body);
return byteMessage;
} else if (body instanceof Serializable) {
return session.createObjectMessage((Serializable) body);
} else {
throw new OseeCoreException("Unsupported java type [%s]", body.getClass().getName());
}
}
private Object tryToGetSerialized(Class<?> clazz, Object body) throws OseeCoreException {
if (clazz != null) {
try {
return JAXBUtil.marshal(body);
} catch (UnsupportedEncodingException ex) {
OseeExceptions.wrapAndThrow(ex);
}
}
return body;
}
}
|