001    /** 
002     * 
003     * Copyright 2004 Hiram Chirino
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    package org.activemq.message;
019    
020    import java.io.IOException;
021    import java.io.Serializable;
022    
023    import javax.jms.JMSException;
024    
025    import org.activemq.util.SerializationHelper;
026    
027    /**
028     * A receipt that also carries a response object.
029     */
030    public class ResponseReceipt extends Receipt {
031    
032        private Serializable result;
033        private byte[] resultBytes;
034    
035        /**
036         * @see org.activemq.message.Receipt#getPacketType()
037         */
038        public int getPacketType() {
039            return RESPONSE_RECEIPT_INFO;
040        }
041    
042        /**
043         * @Transient
044         *
045         * @return Returns the result.
046         */
047        public Serializable getResult() throws JMSException {
048            if (result == null) {
049                if (resultBytes == null) {
050                    return null;
051                }
052                try {
053                    result = (Serializable) SerializationHelper.readObject(resultBytes);
054                }
055                catch (Exception e) {
056                    throw ((JMSException) new JMSException("Invalid network mesage received.").initCause(e));
057                }
058            }
059            return result;
060        }
061    
062        /**
063         * @param result The result to set.
064         */
065        public void setResult(Serializable result) {
066            this.result = result;
067            this.resultBytes = null;
068        }
069    
070        /**
071         * @param data
072         */
073        public void setResultBytes(byte[] resultBytes) {
074            this.resultBytes = resultBytes;
075            this.result = null;
076        }
077    
078        /**
079         * @return Returns the resultBytes.
080         */
081        public byte[] getResultBytes() throws IOException {
082            if (resultBytes == null) {
083                if (result == null) {
084                    return null;
085                }
086                resultBytes = SerializationHelper.writeObject(result);
087            }
088            return resultBytes;
089        }
090    
091        public String toString() {
092            return super.toString() + " ResponseReceipt{ " +
093                    "result = " + result +
094                    ", resultBytes = " + resultBytes +
095                    " }";
096        }
097    }