KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQTextMessage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.command;
19
20 import org.apache.activemq.ActiveMQConnection;
21 import org.apache.activemq.util.ByteArrayInputStream;
22 import org.apache.activemq.util.ByteArrayOutputStream;
23 import org.apache.activemq.util.ByteSequence;
24 import org.apache.activemq.util.JMSExceptionSupport;
25 import org.apache.activemq.util.MarshallingSupport;
26 import org.apache.activemq.wireformat.WireFormat;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.MessageNotWriteableException JavaDoc;
30 import javax.jms.TextMessage JavaDoc;
31 import java.io.DataInputStream JavaDoc;
32 import java.io.DataOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.util.zip.DeflaterOutputStream JavaDoc;
37 import java.util.zip.InflaterInputStream JavaDoc;
38
39 /**
40  *
41  * @openwire:marshaller code="28"
42  * @version $Revision$
43  */

44 public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage JavaDoc {
45
46     public static final byte DATA_STRUCTURE_TYPE=CommandTypes.ACTIVEMQ_TEXT_MESSAGE;
47
48     protected String JavaDoc text;
49
50     public Message copy() {
51         ActiveMQTextMessage copy = new ActiveMQTextMessage();
52         copy(copy);
53         return copy;
54     }
55     
56     private void copy(ActiveMQTextMessage copy) {
57         super.copy(copy);
58         copy.text = text;
59     }
60     
61     public byte getDataStructureType() {
62         return DATA_STRUCTURE_TYPE;
63     }
64     
65     public String JavaDoc getJMSXMimeType() {
66         return "jms/text-message";
67     }
68
69     public void setText(String JavaDoc text) throws MessageNotWriteableException JavaDoc {
70         checkReadOnlyBody();
71         this.text = text;
72         setContent(null);
73     }
74
75     public String JavaDoc getText() throws JMSException JavaDoc {
76         if (text == null && getContent() != null) {
77             try {
78                 ByteSequence bodyAsBytes = getContent();
79                 if (bodyAsBytes != null) {
80                     InputStream is = new ByteArrayInputStream(bodyAsBytes);
81                     if( isCompressed() ) {
82                         is = new InflaterInputStream JavaDoc(is);
83                     }
84                     DataInputStream JavaDoc dataIn = new DataInputStream JavaDoc(is);
85                     text = MarshallingSupport.readUTF8(dataIn);
86                     dataIn.close();
87                     setContent(null);
88                 }
89             } catch (IOException JavaDoc ioe) {
90                 throw JMSExceptionSupport.create(ioe);
91             }
92         }
93         return text;
94     }
95
96     public void beforeMarshall(WireFormat wireFormat) throws IOException JavaDoc {
97         super.beforeMarshall(wireFormat);
98         
99         ByteSequence content = getContent();
100         if (content == null && text!=null ) {
101             ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
102             OutputStream os = bytesOut;
103             ActiveMQConnection connection = getConnection();
104             if( connection!=null && connection.isUseCompression() ) {
105                 compressed = true;
106                 os = new DeflaterOutputStream JavaDoc(os);
107             }
108             DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(os);
109             MarshallingSupport.writeUTF8(dataOut, text);
110             dataOut.close();
111             setContent(bytesOut.toByteSequence());
112         }
113     }
114     
115     /**
116      * Clears out the message body. Clearing a message's body does not clear its header values or property entries.
117      * <p/>
118      * <P>If this message body was read-only, calling this method leaves the message body in the same state as an empty
119      * body in a newly created message.
120      *
121      * @throws JMSException if the JMS provider fails to clear the message body due to some internal error.
122      */

123     public void clearBody() throws JMSException JavaDoc {
124         super.clearBody();
125         this.text = null;
126     }
127     
128     public int getSize() {
129         if( size == 0 && content==null && text!=null ) {
130             size = AVERAGE_MESSAGE_SIZE_OVERHEAD;
131             if( marshalledProperties!=null )
132                 size += marshalledProperties.getLength();
133             size = text.length()*2;
134         }
135         return super.getSize();
136     }
137 }
138
Popular Tags