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 19 package org.codehaus.activemq.message; 20 21 /*** 22 * Abstract class for a transportable Packet 23 * 24 * @version $Revision: 1.10 $ 25 */ 26 public abstract class AbstractPacket implements Packet { 27 28 private String id; 29 private boolean receiptRequired; 30 private transient int memoryUsage = 2048; 31 private transient int memoryUsageReferenceCount; 32 33 /*** 34 * @return the unique id for this Packet 35 */ 36 37 public String getId() { 38 return this.id; 39 } 40 41 42 /*** 43 * Set the unique id for this Packet 44 * 45 * @param newId 46 */ 47 48 public void setId(String newId) { 49 this.id = newId; 50 } 51 52 53 /*** 54 * @return true if a Recipt is required 55 */ 56 57 public boolean isReceiptRequired() { 58 return this.receiptRequired; 59 } 60 61 /*** 62 * @return false since most packets are not receipt packets 63 */ 64 public boolean isReceipt() { 65 return false; 66 } 67 68 /*** 69 * Set if a Recipt if required on receiving this Packet 70 * 71 * @param value 72 */ 73 74 public void setReceiptRequired(boolean value) { 75 this.receiptRequired = value; 76 } 77 78 /*** 79 * Retrieve if a JMS Message type or not 80 * 81 * @return true if it is a JMS Message 82 */ 83 public boolean isJMSMessage() { 84 return false; 85 } 86 87 /*** 88 * Tests equality with another instance 89 * 90 * @param obj - the other instance to test equality with 91 * @return Returns true if the objects are equilvant 92 */ 93 94 public boolean equals(Object obj) { 95 boolean result = this == obj; 96 if (!result && obj != null && obj instanceof AbstractPacket) { 97 AbstractPacket other = (AbstractPacket) obj; 98 result = other.getId().equals(this.getId()); 99 } 100 101 return result; 102 } 103 104 /*** 105 * @return Returns hash code for this instance 106 */ 107 108 public int hashCode() { 109 return this.id != null ? this.id.hashCode() : super.hashCode(); 110 } 111 112 /*** 113 * Get a hint about how much memory this Packet is consuming 114 * @return an aproximation of the current memory used by this instance 115 */ 116 public int getMemoryUsage(){ 117 return memoryUsage; 118 } 119 120 /*** 121 * Set a hint about how mujch memory this packet is consuming 122 * @param newMemoryUsage 123 */ 124 public void setMemoryUsage(int newMemoryUsage){ 125 this.memoryUsage = newMemoryUsage; 126 } 127 128 /*** 129 * Increment reference count for bounded memory collections 130 * @return the incremented reference value 131 * @see org.codehaus.activemq.message.util.MemoryBoundedQueue 132 */ 133 public synchronized int incrementMemoryReferenceCount(){ 134 return ++memoryUsageReferenceCount; 135 } 136 137 138 /*** 139 * Decrement reference count for bounded memory collections 140 * @return the decremented reference value 141 * @see org.codehaus.activemq.message.util.MemoryBoundedQueue 142 */ 143 public synchronized int decrementMemoryReferenceCount(){ 144 return --memoryUsageReferenceCount; 145 } 146 147 148 /*** 149 * @return the current reference count for bounded memory collections 150 * @see org.codehaus.activemq.message.util.MemoryBoundedQueue 151 */ 152 public synchronized int getMemoryUsageReferenceCount(){ 153 return memoryUsageReferenceCount; 154 } 155 156 /*** 157 * @return pretty print of this Packet 158 */ 159 public String toString() { 160 return getPacketTypeAsString(getPacketType()) + ": " + getId(); 161 } 162 163 protected static String getPacketTypeAsString(int type) { 164 String packetTypeStr = ""; 165 switch (type) { 166 case ACTIVEMQ_MESSAGE: 167 packetTypeStr = "ACTIVEMQ_MESSAGE"; 168 break; 169 case ACTIVEMQ_TEXT_MESSAGE: 170 packetTypeStr = "ACTIVEMQ_TEXT_MESSAGE"; 171 break; 172 case ACTIVEMQ_OBJECT_MESSAGE: 173 packetTypeStr = "ACTIVEMQ_OBJECT_MESSAGE"; 174 break; 175 case ACTIVEMQ_BYTES_MESSAGE: 176 packetTypeStr = "ACTIVEMQ_BYTES_MESSAGE"; 177 break; 178 case ACTIVEMQ_STREAM_MESSAGE: 179 packetTypeStr = "ACTIVEMQ_STREAM_MESSAGE"; 180 break; 181 case ACTIVEMQ_MAP_MESSAGE: 182 packetTypeStr = "ACTIVEMQ_MAP_MESSAGE"; 183 break; 184 case ACTIVEMQ_MSG_ACK: 185 packetTypeStr = "ACTIVEMQ_MSG_ACK"; 186 break; 187 case RECEIPT_INFO: 188 packetTypeStr = "RECEIPT_INFO"; 189 break; 190 case CONSUMER_INFO: 191 packetTypeStr = "CONSUMER_INFO"; 192 break; 193 case PRODUCER_INFO: 194 packetTypeStr = "PRODUCER_INFO"; 195 break; 196 case TRANSACTION_INFO: 197 packetTypeStr = "TRANSACTION_INFO"; 198 break; 199 case XA_TRANSACTION_INFO: 200 packetTypeStr = "XA_TRANSACTION_INFO"; 201 break; 202 case ACTIVEMQ_BROKER_INFO: 203 packetTypeStr = "ACTIVEMQ_BROKER_INFO"; 204 break; 205 case ACTIVEMQ_CONNECTION_INFO: 206 packetTypeStr = "ACTIVEMQ_CONNECTION_INFO"; 207 break; 208 case SESSION_INFO: 209 packetTypeStr = "SESSION_INFO"; 210 break; 211 default: 212 packetTypeStr = "UNKNOWN PACKET TYPE: " + type; 213 } 214 return packetTypeStr; 215 } 216 217 218 /*** 219 * A helper method used when implementing equals() which returns 220 * true if the objects are identical or equal handling nulls properly 221 * 222 * @return true if the objects are the same or equal or both null 223 */ 224 protected boolean equals(Object left, Object right) { 225 return left == right || (left != null && left.equals(right)); 226 } 227 }