KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 package org.objectweb.joram.shared.messages;
25
26 import org.objectweb.joram.shared.excepts.*;
27 import org.objectweb.joram.shared.messages.MessageTracing;
28 import org.objectweb.util.monolog.api.BasicLevel;
29
30 import java.io.*;
31 import java.util.*;
32
33 /**
34  * The <code>MessageBody</code> class actually provides the mom message ref.
35  * <p>
36  */

37 public class MessageBody implements Cloneable JavaDoc, Serializable {
38
39   /** The message type (SIMPLE, TEXT, OBJECT, MAP, STREAM, BYTES). */
40   transient private int type;
41
42   /** The bytes body. */
43   transient private byte[] body_bytes = null;
44   /** The map body. */
45   transient private HashMap body_map = null;
46   /** The text body. */
47   transient private String JavaDoc body_text = null;
48   /** body is saved */
49   transient public boolean saved = false;
50
51   /**
52    * Constructs a <code>MessageBody</code> instance.
53    */

54   public MessageBody() {}
55
56   void setType(int type) {
57     this.type = type;
58   }
59
60   /** get bytes body. */
61   public byte[] getBodyBytes() {
62     return body_bytes;
63   }
64   
65   /** get map body. */
66   public HashMap getBodyMap() {
67     return body_map;
68   }
69
70   /** get text body. */
71   public String JavaDoc getBodyText() {
72     return body_text;
73   }
74   
75   /** set bytes body. */
76   public void setBodyBytes(byte[] bytes) {
77     body_bytes = bytes;
78   }
79   
80   /** set map body. */
81   public void setBodyMap(HashMap map) {
82     body_map = map;
83   }
84
85   /** set text body. */
86   public void setBodyText(String JavaDoc text) {
87     body_text = text;
88   }
89   
90   /** Clones the MessageBody. */
91   public Object JavaDoc clone() {
92       if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG))
93         MessageTracing.dbgMessage.log(BasicLevel.DEBUG,
94                                       "MessageBody.clone()");
95     try {
96       MessageBody clone = (MessageBody) super.clone();
97       if (getBodyMap() != null) {
98         clone.setBodyMap(new HashMap());
99         if (getBodyMap().keySet() != null) {
100           for (Iterator it = getBodyMap().keySet().iterator(); it.hasNext(); ) {
101             Object JavaDoc key = it.next();
102             clone.getBodyMap().put(key,getBodyMap().get(key));
103           }
104         }
105       }
106       return clone;
107     } catch (CloneNotSupportedException JavaDoc cE) {
108       return null;
109     }
110   }
111
112   private static void writeString(ObjectOutputStream os,
113                                   String JavaDoc s) throws IOException {
114     if (s == null) {
115       os.writeInt(-1);
116     } else if (s.length() == 0) {
117       os.writeInt(0);
118     } else {
119       byte[] bout = s.getBytes();
120       os.writeInt(bout.length);
121       os.write(bout);
122     }
123   }
124
125   private static String JavaDoc readString(ObjectInputStream is) throws IOException {
126     int length = is.readInt();
127     if (length == -1) {
128       return null;
129     } else if (length == 0) {
130       return "";
131     } else {
132       byte[] bin = new byte[length];
133       is.readFully(bin);
134       return new String JavaDoc(bin);
135     }
136   }
137
138   private void writeObject(ObjectOutputStream os)
139     throws IOException {
140     os.writeInt(type);
141     if (type == MessageType.TEXT) {
142       writeString(os, body_text);
143     } else if ((type == MessageType.OBJECT) ||
144                (type == MessageType.STREAM) ||
145                (type == MessageType.BYTES)) {
146       if (body_bytes == null) {
147         os.writeInt(-1);
148       } else {
149         os.writeInt(body_bytes.length);
150         os.write(body_bytes);
151       }
152     } else if (type == MessageType.MAP) {
153       os.writeObject(body_map);
154     }
155   }
156
157   private void readObject(ObjectInputStream is)
158     throws IOException, ClassNotFoundException JavaDoc{
159     type = is.readInt();
160     if (type == MessageType.TEXT) {
161       body_text = readString(is);
162     } else if ((type == MessageType.OBJECT) ||
163                (type == MessageType.STREAM) ||
164                (type == MessageType.BYTES)) {
165       int length = is.readInt();
166       if (length == -1) {
167         body_bytes = null;
168       } else {
169         body_bytes = new byte[length];
170         is.readFully(body_bytes);
171       }
172     } else if (type == MessageType.MAP) {
173       body_map = (HashMap) is.readObject();
174     }
175   }
176
177   public String JavaDoc toString() {
178     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
179     buff.append('(');
180     buff.append(super.toString());
181     buff.append(",type=");
182     buff.append(type);
183     buff.append(",body_map=");
184     buff.append(body_map);
185     buff.append(",body_text=");
186     buff.append(body_text);
187     buff.append(",body_bytes=");
188     buff.append(body_bytes);
189     buff.append(",saved=");
190     buff.append(saved);
191     buff.append(')');
192     return buff.toString();
193   }
194 }
195
Popular Tags