ResponseReceipt xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    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  package org.codehaus.activemq.message;
    19
    20  import javax.jms.JMSException;
    21  import java.io.ByteArrayInputStream;
    22  import java.io.ByteArrayOutputStream;
    23  import java.io.IOException;
    24  import java.io.ObjectInputStream;
    25  import java.io.ObjectOutputStream;
    26  import java.io.Serializable;
    27
    28  /***
    29   * A receipt that also carries a reponse object.
    30   */
    31  public class ResponseReceipt extends Receipt {
    32
    33      private Serializable result;
    34      private byte[] resultBytes;
    35
    36      /***
    37       * @see org.codehaus.activemq.message.Receipt#getPacketType()
    38       */
    39      public int getPacketType() {
    40          return RESPONSE_RECEIPT_INFO;
    41      }
    42
    43      /***
    44       * @return Returns the result.
    45       */
    46      public Serializable getResult() throws JMSException {
    47          if (result == null) {
    48              if (resultBytes == null) {
    49                  return null;
    50              }
    51              try {
    52                  result = (Serializable) new ObjectInputStream(new ByteArrayInputStream(resultBytes)).readObject();
    53              }
    54              catch (Exception e) {
    55                  throw ((JMSException) new JMSException("Invalid network mesage received.").initCause(e));
    56              }
    57          }
    58          return result;
    59      }
    60
    61      /***
    62       * @param result The result to set.
    63       */
    64      public void setResult(Serializable result) {
    65          this.result = result;
    66          this.resultBytes = null;
    67      }
    68
    69      /***
    70       * @param data
    71       */
    72      public void setResultBytes(byte[] resultBytes) {
    73          this.resultBytes = resultBytes;
    74          this.result = null;
    75      }
    76
    77      /***
    78       * @return Returns the resultBytes.
    79       */
    80      public byte[] getResultBytes() throws IOException {
    81          if (resultBytes == null) {
    82              if (result == null) {
    83                  return null;
    84              }
    85              ByteArrayOutputStream baos = new ByteArrayOutputStream();
    86              ObjectOutputStream os = new ObjectOutputStream(baos);
    87              os.writeObject(result);
    88              os.close();
    89              resultBytes = baos.toByteArray();
    90          }
    91          return resultBytes;
    92      }
    93  }