KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaTextMessage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Nimo.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms;
47
48 import java.io.IOException JavaDoc;
49
50 import javax.jms.JMSException JavaDoc;
51 import javax.jms.MessageNotWriteableException JavaDoc;
52 import javax.jms.TextMessage JavaDoc;
53 import org.mr.core.util.byteable.Byteable;
54 import org.mr.core.util.byteable.ByteableInputStream;
55 import org.mr.core.util.byteable.ByteableOutputStream;
56 import org.mr.core.util.byteable.ByteableRegistry;
57
58
59 /**
60  * This class implements the javax.jms.TextMessage interface.
61  * A text message is used to pass text from the producer to the destination.
62  *
63  * @version 1.0
64  * @since Jan 15, 2004
65  * @author Nimo
66  *
67  */

68 public class MantaTextMessage extends MantaMessage implements TextMessage JavaDoc{
69     
70     
71     
72     /**
73      * only for makeCopy method use
74      *
75      */

76     public MantaTextMessage(){
77         
78     }
79     /**
80      * Construct a Text message with no text attached.
81      *
82      * @param sess - the creating session
83      * @throws JMSException
84      */

85     MantaTextMessage(MantaSession sess) throws JMSException JavaDoc {
86         this(sess,null);
87         
88     }//MantaTextMessage
89

90     /**
91      * Constructs a Text Message with the given text.
92      * @param sess - the creating session
93      * @param message - the message text to create.
94      *
95      * @throws JMSException
96      */

97     MantaTextMessage(MantaSession sess, String JavaDoc msg) throws JMSException JavaDoc {
98         flags = (flags & ONLY_MSG_TYPES) | TEXT_MESSAGE;
99         message = msg;
100         creatingSession = sess;
101     }//MantaTextMessage
102

103     
104     /**
105      * Sets the text for the message.
106      */

107     public void setText(String JavaDoc message) throws JMSException JavaDoc {
108         if (isWriteable())
109             this.message = message;
110         else
111             throw new MessageNotWriteableException JavaDoc("MNJMS00025 : THE MESSAGE IS NOT IN A WRITEABLE STATE. FAILED ON METHOD setText().");
112            
113             
114     }//setText
115

116     /**
117      * Gets the text of the message.
118      */

119     public String JavaDoc getText() throws JMSException JavaDoc {
120         return message;
121     }//getText
122

123     /**
124      * Clear out the message body.
125      */

126     public final void clearBody() throws JMSException JavaDoc {
127         super.clearBody();
128 // if ((flags&ORIG_BODY_SAVED)==0) {
129
// backupMessage=message;
130
// flags=flags|ORIG_BODY_SAVED;
131
// }
132
message = null;
133     }//clearBody
134

135     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
136         super.toBytes(out);
137         out.writeUTF(message);
138         out.flush();
139     }
140     
141     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
142         
143         MantaTextMessage ctm;
144         try {
145             
146             ctm = new MantaTextMessage(null);
147             createHeadersAndProperties(ctm,in);
148             ctm.message=in.readUTF();
149         }
150         catch (JMSException JavaDoc ee) {
151             throw new IOException JavaDoc("MNJMS00FFA : METHOD createInstance() FAILED ON MantaTextMessage. EXCEPTION TEXT : "+ee.getMessage());
152         }
153         
154         return ctm;
155     }
156     
157     public void registerToByteableRegistry() {
158         ByteableRegistry.registerByteableFactory(getByteableName() , this);
159     }
160     
161     public final static String JavaDoc BYTEABLENAME = "MantaTextMsg";
162     public String JavaDoc getByteableName() {
163         
164         return BYTEABLENAME;
165     }
166     
167     public static void register() throws JMSException JavaDoc{
168         MantaTextMessage instance = new MantaTextMessage(null);
169         instance.registerToByteableRegistry();
170     }
171     
172     public String JavaDoc toString(){
173         return message;
174     }
175                    
176 // protected void retrieveOldBody() {
177
//
178
// if ((flags&ORIG_BODY_SAVED)>0) {
179
// //someone did actually clearBody()
180
//
181
// if (backupMessage==null)
182
// message=null;
183
// else
184
// message = new String(backupMessage.toString());
185
// }
186
// //else - message was not touched.
187
// }
188

189     public MantaMessage makeCopy() throws JMSException JavaDoc{
190         MantaTextMessage copy = new MantaTextMessage();
191         initCopy(copy);
192         copy.message = message;
193         return copy;
194     }
195     
196     
197     private String JavaDoc message = null;
198     //private String backupMessage;
199
}//MantaTextMessage
200
Popular Tags