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