001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    
019    package org.activemq.message;
020    
021    import org.activemq.service.MessageIdentity;
022    
023    import java.io.DataInput;
024    import java.io.IOException;
025    import java.io.DataOutput;
026    
027    /**
028     * Denotes an object that can be serialized/deserailized using a PacketReader/PacketWriter
029     */
030    
031    public class MessageAck extends AbstractPacket implements BodyPacket{
032    
033        public static final int MESSAGE_READ_INDEX = 2;
034        public static final int XA_TRANS_INDEX = 3;
035        public static final int PERSISTENT_INDEX = 4;
036        public static final int EXPIRED_INDEX = 5;
037        public static final int TRANSACTION_ID_INDEX = 6;
038        public static final int EXTERNAL_MESSAGE_ID_INDEX = 7;
039        public static final int CACHED_VALUES_INDEX = 8;
040        public static final int LONG_SEQUENCE_INDEX = 9;
041        
042        private String consumerId;
043        private String messageID;
044        private ActiveMQDestination  destination;
045        private Object transactionId;
046        private boolean messageRead;
047        private boolean xaTransacted;
048        private boolean persistent;
049        private boolean expired;
050        private short sessionId;
051        private long sequenceNumber;
052        private String producerKey;
053        private boolean externalMessageId;
054        private transient MessageIdentity messageIdentity;
055    
056    
057        /**
058         * Return the type of Packet
059         *
060         * @return integer representation of the type of Packet
061         */
062    
063        public int getPacketType() {
064            return ACTIVEMQ_MSG_ACK;
065        }
066    
067        /**
068         * @return pretty print of this Packet
069         */
070        public String toString() {
071            return super.toString() + " MessageAck{ " +
072                    "consumerId = '" + consumerId + "' " +
073                    ", messageID = '" + messageID + "' " +
074                    ", destination = " + destination +
075                    ", transactionId = '" + transactionId + "' " +
076                    ", messageRead = " + messageRead +
077                    ", xaTransacted = " + xaTransacted +
078                    ", persistent = " + persistent +
079                    ", expired = " + expired +
080                    ", messageIdentity = " + messageIdentity +
081                    " }";
082        }
083    
084    
085        /**
086         * @return Returns the transactionId.
087         *
088         * @Transient
089         */
090        public Object getTransactionId() {
091            return this.transactionId;
092        }
093    
094        /**
095         * @param newTransactionId The transactionId to set.
096         *
097         * @Transient
098         */
099        public void setTransactionId(Object newTransactionId) {
100            this.transactionId = newTransactionId;
101            this.xaTransacted  = newTransactionId!=null && newTransactionId.getClass()==ActiveMQXid.class;
102        }
103    
104    
105        public void readBody(DataInput dataIn) throws IOException {
106            setTransactionIDString(dataIn.readUTF());
107        }
108    
109        public void writeBody(DataOutput dataOut) throws IOException {
110            dataOut.writeUTF(getTransactionIDString());
111        }
112    
113        /**
114         * @return Returns true if this message is part of a transaction
115         */
116    
117        public boolean isPartOfTransaction() {
118            return this.transactionId != null;
119        }
120    
121    
122        /**
123         * @return the messageId
124         */
125        
126        public String getMessageID() {
127            if (messageID == null && producerKey != null){
128                messageID = producerKey + sequenceNumber;
129            }
130            return messageID;
131        }
132    
133        /**
134         * @param messageID The messageID to set.
135         */
136        public void setMessageID(String messageID) {
137            this.messageID = messageID;
138            this.messageIdentity=null;
139        }
140    
141        /**
142         * @return Returns the messageRead.
143         */
144        public boolean isMessageRead() {
145            return messageRead;
146        }
147    
148        /**
149         * @param messageRead The messageRead to set.
150         */
151        public void setMessageRead(boolean messageRead) {
152            this.messageRead = messageRead;
153        }
154    
155        /**
156         * @return Returns the consumerId.
157         */
158        public String getConsumerId() {
159            return consumerId;
160        }
161    
162        /**
163         * @param consumerId The consumerId to set.
164         */
165        public void setConsumerId(String consumerId) {
166            this.consumerId = consumerId;
167        }
168    
169        /**
170         * @return Returns the xaTransacted.
171         */
172        public boolean isXaTransacted() {
173            return xaTransacted;
174        }
175    
176        public MessageIdentity getMessageIdentity() {
177            if (messageIdentity == null) {
178                messageIdentity = new MessageIdentity(getMessageID());
179            }
180            return messageIdentity;
181        }
182        /**
183         * @return Returns the destination.
184         */
185        public ActiveMQDestination getDestination() {
186            return destination;
187        }
188        /**
189         * @param destination The destination to set.
190         */
191        public void setDestination(ActiveMQDestination destination) {
192            this.destination = destination;
193        }
194        /**
195         * @return Returns the persistent.
196         */
197        public boolean isPersistent() {
198            return persistent;
199        }
200        /**
201         * @param persistent The persistent to set.
202         */
203        public void setPersistent(boolean persistent) {
204            this.persistent = persistent;
205        }
206        
207        /**
208         * @return true the delivered message was to a non-persistent destination
209         */
210        public boolean isTemporary(){
211            return persistent == false || (destination != null && destination.isTemporary());
212        }
213        
214        /**
215         * @return Returns the expired.
216         */
217        public boolean isExpired() {
218            return expired;
219        }
220        /**
221         * @param expired The expired to set.
222         */
223        public void setExpired(boolean expired) {
224            this.expired = expired;
225        }
226        
227        /**
228         * @return Returns the producerKey.
229         */
230        public String getProducerKey() {
231            return producerKey;
232        }
233        /**
234         * @param producerKey The producerKey to set.
235         */
236        public void setProducerKey(String producerKey) {
237            this.producerKey = producerKey;
238        }
239        
240        /**
241         * @return Returns the messageSequence.
242         */
243        public long getSequenceNumber() {
244            return sequenceNumber;
245        }
246        /**
247         * @param messageSequence The messageSequence to set.
248         */
249        public void setSequenceNumber(long messageSequence) {
250            this.sequenceNumber = messageSequence;
251        }
252        /**
253         * @return Returns the sessionId.
254         */
255        public short getSessionId() {
256            return sessionId;
257        }
258        /**
259         * @param sessionId The sessionId to set.
260         */
261        public void setSessionId(short sessionId) {
262            this.sessionId = sessionId;
263        }
264        /**
265         * @return Returns the externalMessageId.
266         */
267        public boolean isExternalMessageId() {
268            return externalMessageId;
269        }
270        /**
271         * @param externalMessageId The externalMessageId to set.
272         */
273        public void setExternalMessageId(boolean externalMessageId) {
274            this.externalMessageId = externalMessageId;
275        }
276    
277        /**
278         * A helper method for the OpenWire protocol
279         */
280        public String getTransactionIDString() throws IOException {
281            return ActiveMQXid.transactionIDToString(getTransactionId());
282        }
283    
284        /**
285         * A helper method for the OpenWire protocol
286         */
287        public void setTransactionIDString(String text) throws IOException {
288            setTransactionId(ActiveMQXid.transactionIDFromString(text));
289        }
290    }