KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > message > JmsTextMessage


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.message;
22
23 import java.io.Serializable JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import java.io.DataInput JavaDoc;
27 import java.io.DataOutput JavaDoc;
28
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.ObjectInputStream JavaDoc;
33
34 import javax.jms.TextMessage JavaDoc;
35 import javax.jms.JMSException JavaDoc;
36 import javax.jms.MessageFormatException JavaDoc;
37 import javax.jms.MessageNotWriteableException JavaDoc;
38
39 /**
40  * Implementation of the JMS interface TextMessage.
41  *
42  * @see javax.jms.TextMessage
43  * @author Dan Greff
44  */

45 public final class JmsTextMessage extends JmsMessage
46   implements TextMessage JavaDoc, java.io.Externalizable JavaDoc
47 {
48   private static final byte TEXT = 0;
49   private static final byte NO_TEXT = 1;
50
51   private String JavaDoc textMessage;
52
53     /////////////////////////////////////////////////////////////////////////
54
// Constructors //
55
/////////////////////////////////////////////////////////////////////////
56

57   public JmsTextMessage()
58   {
59     super();
60   }
61
62   public JmsTextMessage(String JavaDoc name)
63   {
64     super(name);
65   }
66
67   public JmsTextMessage(byte [] buf, int offset, int length)
68   {
69     super(buf, offset, length);
70   }
71   
72     /////////////////////////////////////////////////////////////////////////
73
// Public Methods //
74
/////////////////////////////////////////////////////////////////////////
75

76   /**
77    * @see javax.jms.TextMessage#getText()
78    */

79   public String JavaDoc getText() throws JMSException JavaDoc
80   {
81     return this.textMessage;
82   }
83
84   /**
85    * @see javax.jms.TextMessage#setText(String)
86    */

87   public void setText(String JavaDoc payload) throws JMSException JavaDoc
88   {
89     checkWrite();
90     this.textMessage = payload;
91   }
92
93   public void clearBody() throws JMSException JavaDoc
94   {
95     checkWrite();
96     super.clearBody();
97     this.textMessage = null;
98   }
99   
100   /**
101    * Retrieves the contents of the object stored in the given DataInput stream.
102    *
103    * This is similar to Externalizable.readObject(). Externalizable was not
104    * used to emphasize that the format written must be language independent
105    * and should not use serialization.
106    */

107   public void unmarshal(DataInput JavaDoc in) throws IOException JavaDoc
108   {
109     //
110
// Read in the message header and properties
111
//
112
super.unmarshal(in);
113     
114     byte sanityByte = in.readByte();
115     if (sanityByte == TEXT) {
116       this.textMessage = in.readUTF();
117     }
118     this.setReadOnly();
119   }
120   
121   /**
122    * Stores this object on the given DataOutput stream.
123    *
124    * This is similar to Externalizable.writeObject(). Externalizable was not
125    * used to emphasize that the format written must be language independent
126    * and should not use serialization.
127    */

128   public void marshal(DataOutput JavaDoc out) throws IOException JavaDoc
129   {
130     //
131
// Write out the message header and properties
132
//
133
super.marshal(out);
134     
135     if (this.textMessage == null || this.textMessage.length() == 0)
136       out.writeByte(NO_TEXT);
137     else {
138       out.writeByte(TEXT);
139       out.writeUTF(this.textMessage);
140     }
141   }
142
143   
144     /////////////////////////////////////////////////////////////////////////
145
// Package Methods //
146
/////////////////////////////////////////////////////////////////////////
147

148   /*
149    * @return byte identifier for this message type. Specifically
150    * MessageConverter#TEXT_MESSAGE
151    */

152   final byte getMarshalingID()
153   {
154     return MessageEncoder.TEXT_MESSAGE;
155   }
156    
157 }
158
159
Popular Tags