1 23 package org.objectweb.joram.shared.messages; 24 25 import java.io.*; 26 import java.util.*; 27 28 31 public class MessagePersistent implements Serializable { 32 33 transient private Message message; 34 transient String msgClassName; 35 transient private boolean pin; 36 37 public MessagePersistent(Message message) { 38 this.message = message; 39 } 40 41 public void setPin(boolean pin) { 42 this.pin = pin; 43 } 44 45 public boolean getPin() { 46 return pin; 47 } 48 49 public Message getMessage() { 50 return message; 51 } 52 53 public MessageBody getMessageBody() { 54 return message.getMessageBody(); 55 } 56 57 public String getIdentifier() { 58 return message.getIdentifier(); 59 } 60 61 public String getSaveName() { 62 return message.getSaveName(); 63 } 64 65 private static void writeString(ObjectOutput os, 66 String s) throws IOException { 67 if (s == null) { 68 os.writeInt(-1); 69 } else if (s.length() == 0) { 70 os.writeInt(0); 71 } else { 72 byte[] bout = s.getBytes(); 73 os.writeInt(bout.length); 74 os.write(bout); 75 } 76 } 77 78 private static String readString(ObjectInput is) throws IOException { 79 int length = is.readInt(); 80 if (length == -1) { 81 return null; 82 } else if (length == 0) { 83 return ""; 84 } else { 85 byte[] bin = new byte[length]; 86 is.readFully(bin); 87 return new String (bin); 88 } 89 } 90 91 final static byte BOOLEAN = 0; 92 final static byte BYTE = 1; 93 final static byte DOUBLE = 2; 94 final static byte FLOAT = 3; 95 final static byte INTEGER = 4; 96 final static byte LONG = 5; 97 final static byte SHORT = 6; 98 final static byte STRING = 7; 99 100 private static void writeProperties(ObjectOutput os, 101 Hashtable h) 102 throws IOException { 103 if (h == null) { 104 os.writeInt(0); 105 return; 106 } 107 108 os.writeInt(h.size()); 109 for (Enumeration e = h.keys() ; e.hasMoreElements() ;) { 110 String key = (String ) e.nextElement(); 111 writeString(os, key); 112 Object value = h.get(key); 113 if (value instanceof Boolean ) { 114 os.writeByte(BOOLEAN); 115 os.writeBoolean(((Boolean ) value).booleanValue()); 116 } else if (value instanceof Byte ) { 117 os.writeByte(BYTE); 118 os.writeByte(((Byte ) value).byteValue()); 119 } else if (value instanceof Double ) { 120 os.writeByte(DOUBLE); 121 os.writeDouble(((Double ) value).doubleValue()); 122 } else if (value instanceof Float ) { 123 os.writeByte(FLOAT); 124 os.writeFloat(((Float ) value).floatValue()); 125 } else if (value instanceof Integer ) { 126 os.writeByte(INTEGER); 127 os.writeInt(((Integer ) value).intValue()); 128 } else if (value instanceof Long ) { 129 os.writeByte(LONG); 130 os.writeLong(((Long ) value).longValue()); 131 } else if (value instanceof Short ) { 132 os.writeByte(SHORT); 133 os.writeShort(((Short ) value).shortValue()); 134 } else if (value instanceof String ) { 135 os.writeByte(STRING); 136 writeString(os, (String ) value); 137 } 138 } 139 } 140 141 private static Hashtable readProperties(ObjectInput is) 142 throws IOException, ClassNotFoundException { 143 int size = is.readInt(); 144 if (size == 0) return null; 145 146 Hashtable h = new Hashtable(size); 147 148 for (int i = 0; i < size; i++) { 149 String key = readString(is); 150 byte type = is.readByte(); 151 if (type == BOOLEAN) { 152 h.put(key, new Boolean (is.readBoolean())); 153 } else if (type == BYTE) { 154 h.put(key, new Byte (is.readByte())); 155 } else if (type == DOUBLE) { 156 h.put(key, new Double (is.readDouble())); 157 } else if (type == FLOAT) { 158 h.put(key, new Float (is.readFloat())); 159 } else if (type == INTEGER) { 160 h.put(key, new Integer (is.readInt())); 161 } else if (type == LONG) { 162 h.put(key, new Long (is.readLong())); 163 } else if (type == SHORT) { 164 h.put(key, new Short (is.readShort())); 165 } else if (type == STRING) { 166 h.put(key, readString(is)); 167 } 168 } 169 return h; 170 } 171 172 private void writeObject(ObjectOutputStream os) 173 throws IOException { 174 writeString(os, message.getClass().getName()); 175 os.writeBoolean(pin); 176 os.writeInt(message.type); 177 os.writeBoolean(message.persistent); 178 os.writeBoolean(message.noBody); 179 writeString(os, message.id); 180 os.writeInt(message.priority); 181 os.writeLong(message.expiration); 182 os.writeLong(message.timestamp); 183 writeString(os, message.toId); 184 writeString(os, message.toType); 185 writeString(os, message.correlationId); 186 writeString(os, message.replyToId); 187 writeString(os, message.replyToType); 188 189 os.writeObject(message.optionalHeader); 190 191 os.writeBoolean(message.bodyRO); 192 193 writeProperties(os, message.properties); 194 195 os.writeLong(message.order); 196 os.writeInt(message.deliveryCount); 197 198 os.writeBoolean(message.denied); 199 os.writeBoolean(message.deletedDest); 200 os.writeBoolean(message.expired); 201 os.writeBoolean(message.notWriteable); 202 os.writeBoolean(message.undeliverable); 203 writeString(os, message.getSaveName()); 204 message.bodyRO = true; 205 } 206 207 private void readObject(ObjectInputStream is) 208 throws Exception { 209 msgClassName = readString(is); 211 message = (Message) Class.forName(msgClassName).newInstance(); 212 pin = is.readBoolean(); 213 message.type = is.readInt(); 214 message.persistent = is.readBoolean(); 215 message.noBody = is.readBoolean(); 216 message.id = readString(is); 217 message.priority = is.readInt(); 218 message.expiration = is.readLong(); 219 message.timestamp = is.readLong(); 220 message.toId = readString(is); 221 message.toType = readString(is); 222 message.correlationId = readString(is); 223 message.replyToId = readString(is); 224 message.replyToType = readString(is); 225 226 message.optionalHeader = (Hashtable) is.readObject(); 227 228 message.bodyRO = is.readBoolean(); 229 230 message.properties = readProperties(is); 231 232 message.order = is.readLong(); 233 message.deliveryCount = is.readInt(); 234 235 message.denied = is.readBoolean(); 236 message.deletedDest= is.readBoolean(); 237 message.expired = is.readBoolean(); 238 message.notWriteable = is.readBoolean(); 239 message.undeliverable = is.readBoolean(); 240 message.setSaveName(readString(is)); 241 message.acksCounter = 0; 242 message.durableAcksCounter = 0; 243 message.propertiesRO = true; 244 } 245 246 public String toString() { 247 StringBuffer buff = new StringBuffer (); 248 buff.append(super.toString()); 249 buff.append(",msgClassName="); 250 buff.append(msgClassName); 251 buff.append(",message.saveName="); 252 buff.append(message.getSaveName()); 253 buff.append(",pin="); 254 buff.append(pin); 255 buff.append(",message.type="); 256 buff.append(message.type); 257 buff.append(",message.id="); 258 buff.append(message.id); 259 buff.append(",message.toId="); 260 buff.append(message.toId); 261 buff.append(",message.toType="); 262 buff.append(message.toType); 263 buff.append(",message.replyToId="); 264 buff.append(message.replyToId); 265 buff.append(",message.replyToType="); 266 buff.append(message.replyToType); 267 buff.append(",message.body="); 268 buff.append(message.getMessageBody()); 269 buff.append(')'); 270 return buff.toString(); 271 } 272 } 273 | Popular Tags |