1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18
19 package org.codehaus.activemq.message;
20
21 import org.codehaus.activemq.util.BitArray;
22
23 import java.io.DataOutput;
24 import java.io.IOException;
25
26 /***
27 * Writes a ProducerInfo object to a Stream
28 */
29 public class ActiveMQMessageWriter extends AbstractPacketWriter {
30 /***
31 * Return the type of Packet
32 *
33 * @return integer representation of the type of Packet
34 */
35 public int getPacketType() {
36 return Packet.ACTIVEMQ_MESSAGE;
37 }
38
39 /***
40 * Write a Packet instance to data output stream
41 *
42 * @param packet the instance to be seralized
43 * @param dataOut the output stream
44 * @throws IOException thrown if an error occurs
45 */
46 public void writePacket(Packet packet, DataOutput dataOut) throws IOException {
47 ActiveMQMessage msg = (ActiveMQMessage) packet;
48 writeMessage(msg, dataOut);
49 }
50
51 /***
52 * @param msg
53 * @param dataOut
54 * @throws IOException
55 */
56
57 protected void writeMessage(ActiveMQMessage msg, DataOutput dataOut) throws IOException {
58 byte[] payload = msg.getBodyAsBytes();
59
60 super.writeUTF(msg.getJMSMessageID(), dataOut);
61 super.writeUTF(msg.getJMSClientID(), dataOut);
62 super.writeUTF(msg.getProducerID(), dataOut);
63 ActiveMQDestination.writeToStream((ActiveMQDestination) msg.getJMSDestination(), dataOut);
64 dataOut.write(msg.getJMSDeliveryMode());
65 dataOut.write(msg.getJMSPriority());
66
67 BitArray ba = new BitArray();
68 ba.set(ActiveMQMessage.CORRELATION_INDEX, msg.getJMSCorrelationID() != null);
69 ba.set(ActiveMQMessage.TYPE_INDEX, msg.getJMSType() != null);
70 ba.set(ActiveMQMessage.BROKER_NAME_INDEX, msg.getEntryBrokerName() != null);
71 ba.set(ActiveMQMessage.CLUSTER_NAME_INDEX, msg.getEntryClusterName() != null);
72 ba.set(ActiveMQMessage.TRANSACTION_ID_INDEX, msg.getTransactionId() != null);
73 ba.set(ActiveMQMessage.REPLY_TO_INDEX, msg.getJMSReplyTo() != null);
74 ba.set(ActiveMQMessage.TIMESTAMP_INDEX, msg.getJMSTimestamp() > 0);
75 ba.set(ActiveMQMessage.EXPIRATION_INDEX, msg.getJMSExpiration() > 0);
76 ba.set(ActiveMQMessage.REDELIVERED_INDEX, msg.getJMSRedelivered());
77 ba.set(ActiveMQMessage.XA_TRANS_INDEX, msg.isXaTransacted());
78 ba.set(ActiveMQMessage.CID_INDEX, msg.getConsumerNos() != null);
79 ba.set(ActiveMQMessage.PROPERTIES_INDEX, msg.getProperties() != null && msg.getProperties().size() > 0);
80 ba.set(ActiveMQMessage.PAYLOAD_INDEX, payload != null);
81 ba.writeToStream(dataOut);
82
83 if (ba.get(ActiveMQMessage.CORRELATION_INDEX)) {
84 super.writeUTF(msg.getJMSCorrelationID(), dataOut);
85 }
86 if (ba.get(ActiveMQMessage.TYPE_INDEX)) {
87 super.writeUTF(msg.getJMSType(), dataOut);
88 }
89 if (ba.get(ActiveMQMessage.BROKER_NAME_INDEX)) {
90 super.writeUTF(msg.getEntryBrokerName(), dataOut);
91 }
92 if (ba.get(ActiveMQMessage.CLUSTER_NAME_INDEX)) {
93 super.writeUTF(msg.getEntryClusterName(), dataOut);
94 }
95 if (ba.get(ActiveMQMessage.TRANSACTION_ID_INDEX)) {
96 super.writeUTF(msg.getTransactionId(), dataOut);
97 }
98 if (ba.get(ActiveMQMessage.REPLY_TO_INDEX)) {
99 ActiveMQDestination.writeToStream((ActiveMQDestination) msg.getJMSReplyTo(), dataOut);
100 }
101 if (ba.get(ActiveMQMessage.TIMESTAMP_INDEX)) {
102 dataOut.writeLong(msg.getJMSTimestamp());
103 }
104 if (ba.get(ActiveMQMessage.EXPIRATION_INDEX)) {
105 dataOut.writeLong(msg.getJMSExpiration());
106 }
107 if (ba.get(ActiveMQMessage.CID_INDEX)) {
108
109 int[] cids = msg.getConsumerNos();
110 dataOut.writeShort(cids.length);
111 for (int i = 0; i < cids.length; i++) {
112 dataOut.writeShort(cids[i]);
113 }
114 }
115 if (ba.get(ActiveMQMessage.PROPERTIES_INDEX)) {
116 msg.writeMapProperties(msg.getProperties(), dataOut);
117 }
118 if (ba.get(ActiveMQMessage.PAYLOAD_INDEX)) {
119 dataOut.writeInt(payload.length);
120 dataOut.write(payload);
121 }
122 }
123 }