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    package org.activemq.service.impl;
019    
020    import java.io.Externalizable;
021    import java.io.IOException;
022    import java.io.ObjectInput;
023    import java.io.ObjectOutput;
024    import javax.jms.JMSException;
025    import org.activemq.io.WireFormat;
026    import org.activemq.io.impl.DefaultWireFormat;
027    import org.activemq.message.ActiveMQMessage;
028    import org.activemq.util.JMSExceptionHelper;
029    
030    
031    /**
032     * An entry for a message in a container
033     *
034     * @version $Revision: 1.1.1.1 $
035     */
036    public class MessageEntry implements Externalizable {
037        private static final long serialVersionUID = -3590625465815936811L;
038        private static final WireFormat wireFormat = new DefaultWireFormat();
039    
040    
041        private ActiveMQMessage message;
042    
043        /**
044         * Only used by serialisation
045         */
046        public MessageEntry() {
047        }
048    
049        public MessageEntry(ActiveMQMessage msg) {
050            this.message = msg;
051        }
052    
053    
054        /**
055         * @return Returns the message.
056         */
057        public ActiveMQMessage getMessage() {
058            return message;
059        }
060    
061        /**
062         * @return a hashCode for this object
063         */
064        public int hashCode() {
065            return message != null ? message.hashCode() : super.hashCode();
066        }
067    
068        /**
069         * Tests equivalence with an other object
070         *
071         * @param obj the object to test against
072         * @return true/false
073         */
074    
075        public boolean equals(Object obj) {
076            boolean result = false;
077            if (obj != null && obj instanceof MessageEntry) {
078                MessageEntry other = (MessageEntry) obj;
079                result = (this.message != null && other.message != null && this.message.equals(other.message) ||
080                        this.message == null && other.message == null);
081            }
082            return result;
083        }
084    
085        public void writeExternal(ObjectOutput out) throws IOException {
086            try {
087                wireFormat.writePacket(message, out);
088            }
089            catch (JMSException e) {
090                throw JMSExceptionHelper.newIOException(e);
091            }
092        }
093    
094        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
095            message = (ActiveMQMessage) wireFormat.readPacket(in);
096        }
097    }