1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.jms; |
18 |
|
|
19 |
|
import java.io.ByteArrayOutputStream; |
20 |
|
import java.io.DataOutputStream; |
21 |
|
import java.io.ObjectOutputStream; |
22 |
|
import java.nio.ByteBuffer; |
23 |
|
import java.util.Enumeration; |
24 |
|
|
25 |
|
import javax.jms.BytesMessage; |
26 |
|
import javax.jms.MapMessage; |
27 |
|
import javax.jms.Message; |
28 |
|
import javax.jms.MessageEOFException; |
29 |
|
import javax.jms.ObjectMessage; |
30 |
|
import javax.jms.StreamMessage; |
31 |
|
import javax.jms.TextMessage; |
32 |
|
|
33 |
|
import org.apache.camel.Converter; |
34 |
|
import org.apache.camel.converter.NIOConverter; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@Converter |
44 |
|
public final class JmsIOConverter { |
45 |
|
|
46 |
0 |
private JmsIOConverter() { |
47 |
0 |
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@Converter |
55 |
|
public static ByteBuffer toByteBuffer(final Message message) throws Exception { |
56 |
|
|
57 |
0 |
if (message instanceof TextMessage) { |
58 |
0 |
final String text = ((TextMessage)message).getText(); |
59 |
0 |
return NIOConverter.toByteBuffer(text); |
60 |
|
} |
61 |
0 |
if (message instanceof BytesMessage) { |
62 |
0 |
final BytesMessage bmsg = (BytesMessage)message; |
63 |
0 |
final int len = (int)bmsg.getBodyLength(); |
64 |
0 |
final byte[] data = new byte[len]; |
65 |
0 |
bmsg.readBytes(data, len); |
66 |
0 |
return NIOConverter.toByteBuffer(data); |
67 |
|
|
68 |
|
} |
69 |
0 |
if (message instanceof StreamMessage) { |
70 |
0 |
final StreamMessage msg = (StreamMessage)message; |
71 |
0 |
final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
72 |
0 |
final DataOutputStream dataOut = new DataOutputStream(bytesOut); |
73 |
|
try { |
74 |
|
while (true) { |
75 |
0 |
final Object obj = msg.readObject(); |
76 |
0 |
writeData(dataOut, obj); |
77 |
0 |
} |
78 |
0 |
} catch (MessageEOFException e) { |
79 |
|
|
80 |
|
} |
81 |
0 |
dataOut.close(); |
82 |
0 |
return NIOConverter.toByteBuffer(bytesOut.toByteArray()); |
83 |
|
} |
84 |
0 |
if (message instanceof MapMessage) { |
85 |
0 |
final MapMessage msg = (MapMessage)message; |
86 |
0 |
final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
87 |
0 |
final DataOutputStream dataOut = new DataOutputStream(bytesOut); |
88 |
0 |
for (final Enumeration en = msg.getMapNames(); en.hasMoreElements();) { |
89 |
0 |
final Object obj = msg.getObject(en.nextElement().toString()); |
90 |
0 |
writeData(dataOut, obj); |
91 |
0 |
} |
92 |
0 |
dataOut.close(); |
93 |
0 |
return NIOConverter.toByteBuffer(bytesOut.toByteArray()); |
94 |
|
} |
95 |
0 |
if (message instanceof ObjectMessage) { |
96 |
0 |
ObjectMessage objMessage = (ObjectMessage)message; |
97 |
0 |
Object object = objMessage.getObject(); |
98 |
0 |
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
99 |
0 |
ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut); |
100 |
0 |
objectOut.writeObject(object); |
101 |
0 |
objectOut.close(); |
102 |
0 |
return NIOConverter.toByteBuffer(bytesOut.toByteArray()); |
103 |
|
} |
104 |
0 |
return null; |
105 |
|
|
106 |
|
} |
107 |
|
|
108 |
|
private static void writeData(DataOutputStream dataOut, Object data) throws Exception { |
109 |
|
|
110 |
0 |
if (data instanceof byte[]) { |
111 |
0 |
dataOut.write((byte[])data); |
112 |
0 |
} else if (data instanceof String) { |
113 |
0 |
dataOut.writeUTF(data.toString()); |
114 |
0 |
} else if (data instanceof Double) { |
115 |
0 |
dataOut.writeDouble(((Double)data).doubleValue()); |
116 |
0 |
} else if (data instanceof Float) { |
117 |
0 |
dataOut.writeFloat(((Float)data).floatValue()); |
118 |
0 |
} else if (data instanceof Long) { |
119 |
0 |
dataOut.writeLong(((Long)data).longValue()); |
120 |
0 |
} else if (data instanceof Integer) { |
121 |
0 |
dataOut.writeInt(((Integer)data).intValue()); |
122 |
0 |
} else if (data instanceof Short) { |
123 |
0 |
dataOut.writeShort(((Short)data).shortValue()); |
124 |
0 |
} else if (data instanceof Character) { |
125 |
0 |
dataOut.writeChar(((Character)data).charValue()); |
126 |
0 |
} else if (data instanceof Byte) { |
127 |
0 |
dataOut.writeByte(((Byte)data).byteValue()); |
128 |
0 |
} else if (data instanceof Boolean) { |
129 |
0 |
dataOut.writeBoolean(((Boolean)data).booleanValue()); |
130 |
|
} |
131 |
|
|
132 |
0 |
} |
133 |
|
} |