KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > messages > MessagePersistent


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.shared.messages;
24
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  *
30  */

31 public class MessagePersistent implements Serializable {
32
33   transient private Message message;
34   transient String JavaDoc 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 JavaDoc getIdentifier() {
58    return message.getIdentifier();
59   }
60
61   public String JavaDoc getSaveName() {
62     return message.getSaveName();
63   }
64
65   private static void writeString(ObjectOutput os,
66                                   String JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc key = (String JavaDoc) e.nextElement();
111       writeString(os, key);
112       Object JavaDoc value = h.get(key);
113       if (value instanceof Boolean JavaDoc) {
114         os.writeByte(BOOLEAN);
115         os.writeBoolean(((Boolean JavaDoc) value).booleanValue());
116       } else if (value instanceof Byte JavaDoc) {
117         os.writeByte(BYTE);
118         os.writeByte(((Byte JavaDoc) value).byteValue());
119       } else if (value instanceof Double JavaDoc) {
120         os.writeByte(DOUBLE);
121         os.writeDouble(((Double JavaDoc) value).doubleValue());
122       } else if (value instanceof Float JavaDoc) {
123         os.writeByte(FLOAT);
124         os.writeFloat(((Float JavaDoc) value).floatValue());
125       } else if (value instanceof Integer JavaDoc) {
126         os.writeByte(INTEGER);
127         os.writeInt(((Integer JavaDoc) value).intValue());
128       } else if (value instanceof Long JavaDoc) {
129         os.writeByte(LONG);
130         os.writeLong(((Long JavaDoc) value).longValue());
131        } else if (value instanceof Short JavaDoc) {
132         os.writeByte(SHORT);
133         os.writeShort(((Short JavaDoc) value).shortValue());
134       } else if (value instanceof String JavaDoc) {
135         os.writeByte(STRING);
136         writeString(os, (String JavaDoc) value);
137       }
138      }
139   }
140
141   private static Hashtable readProperties(ObjectInput is)
142     throws IOException, ClassNotFoundException JavaDoc {
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 JavaDoc key = readString(is);
150       byte type = is.readByte();
151       if (type == BOOLEAN) {
152         h.put(key, new Boolean JavaDoc(is.readBoolean()));
153       } else if (type == BYTE) {
154         h.put(key, new Byte JavaDoc(is.readByte()));
155       } else if (type == DOUBLE) {
156         h.put(key, new Double JavaDoc(is.readDouble()));
157       } else if (type == FLOAT) {
158         h.put(key, new Float JavaDoc(is.readFloat()));
159       } else if (type == INTEGER) {
160         h.put(key, new Integer JavaDoc(is.readInt()));
161       } else if (type == LONG) {
162         h.put(key, new Long JavaDoc(is.readLong()));
163       } else if (type == SHORT) {
164         h.put(key, new Short JavaDoc(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 JavaDoc {
209     // read header part
210
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 JavaDoc toString() {
247     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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