View Javadoc

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  package org.codehaus.activemq.service.impl;
19  
20  import org.codehaus.activemq.message.ActiveMQMessage;
21  import org.codehaus.activemq.message.DefaultWireFormat;
22  import org.codehaus.activemq.message.WireFormat;
23  import org.codehaus.activemq.util.JMSExceptionHelper;
24  
25  import javax.jms.JMSException;
26  import java.io.Externalizable;
27  import java.io.IOException;
28  import java.io.ObjectInput;
29  import java.io.ObjectOutput;
30  
31  
32  /***
33   * An entry for a message in a container
34   *
35   * @version $Revision: 1.3 $
36   */
37  public class MessageEntry implements Externalizable {
38      private static final long serialVersionUID = -3590625465815936811L;
39      private static final WireFormat wireFormat = new DefaultWireFormat();
40  
41  
42      private ActiveMQMessage message;
43  
44      /***
45       * Only used by serialisation
46       */
47      public MessageEntry() {
48      }
49  
50      public MessageEntry(ActiveMQMessage msg) {
51          this.message = msg;
52      }
53  
54  
55      /***
56       * @return Returns the message.
57       */
58      public ActiveMQMessage getMessage() {
59          return message;
60      }
61  
62      /***
63       * @return a hashCode for this object
64       */
65      public int hashCode() {
66          return message != null ? message.hashCode() : super.hashCode();
67      }
68  
69      /***
70       * Tests equivalence with an other object
71       *
72       * @param obj the object to test against
73       * @return true/false
74       */
75  
76      public boolean equals(Object obj) {
77          boolean result = false;
78          if (obj != null && obj instanceof MessageEntry) {
79              MessageEntry other = (MessageEntry) obj;
80              result = (this.message != null && other.message != null && this.message.equals(other.message) ||
81                      this.message == null && other.message == null);
82          }
83          return result;
84      }
85  
86      public void writeExternal(ObjectOutput out) throws IOException {
87          try {
88              wireFormat.writePacket(message, out);
89          }
90          catch (JMSException e) {
91              throw JMSExceptionHelper.newIOException(e);
92          }
93      }
94  
95      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
96          message = (ActiveMQMessage) wireFormat.readPacket(in);
97      }
98  }