KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyObjectMessage


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.Externalizable JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutput JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.ObjectStreamClass JavaDoc;
34 import java.io.Serializable JavaDoc;
35
36 import javax.jms.JMSException JavaDoc;
37 import javax.jms.MessageFormatException JavaDoc;
38 import javax.jms.MessageNotWriteableException JavaDoc;
39 import javax.jms.ObjectMessage JavaDoc;
40
41 import org.jboss.util.Classes;
42
43 /**
44  * This class implements javax.jms.ObjectMessage
45  *
46  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
47  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
48  * @version $Revision: 46403 $
49  */

50 public class SpyObjectMessage extends SpyMessage implements ObjectMessage JavaDoc, Externalizable JavaDoc
51 {
52    // Constants -----------------------------------------------------
53

54    /** The serialVersionUID */
55    private final static long serialVersionUID = 8809953915407712952L;
56    
57    // Attributes ----------------------------------------------------
58

59    /** Is it a byte array */
60    boolean isByteArray = false;
61    /** The bytes */
62    byte[] objectBytes = null;
63    
64    // Static --------------------------------------------------------
65

66    // Constructors --------------------------------------------------
67

68    // Public --------------------------------------------------------
69

70    // ObjectMessage implementation ----------------------------------
71

72    public void setObject(Serializable JavaDoc object) throws JMSException JavaDoc
73    {
74       if (header.msgReadOnly)
75         {
76          throw new MessageNotWriteableException JavaDoc("setObject");
77       }
78       if (object == null)
79       {
80          objectBytes = null;
81          return;
82       }
83       try
84       {
85          if (object instanceof byte[])
86            {
87             //cheat for byte arrays
88
isByteArray = true;
89             objectBytes = new byte[((byte[]) object).length];
90             System.arraycopy(object, 0, objectBytes, 0, objectBytes.length);
91          }
92          else
93            {
94             isByteArray = false;
95             ByteArrayOutputStream JavaDoc byteArray = new ByteArrayOutputStream JavaDoc();
96             ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(byteArray);
97             objectOut.writeObject(object);
98             objectBytes = byteArray.toByteArray();
99             objectOut.close();
100          }
101       }
102       catch (IOException JavaDoc e)
103       {
104          MessageFormatException JavaDoc mfe = new MessageFormatException JavaDoc("Object cannot be serialized: " + e.getMessage());
105          mfe.setLinkedException(e);
106          throw mfe;
107       }
108    }
109
110    public Serializable JavaDoc getObject() throws JMSException JavaDoc
111    {
112
113       Serializable JavaDoc retVal = null;
114       try
115       {
116          if (null != objectBytes)
117          {
118             if (isByteArray)
119             {
120                retVal = new byte[objectBytes.length];
121                System.arraycopy(objectBytes, 0, retVal, 0, objectBytes.length);
122             }
123             else
124             {
125
126                /**
127                 * Default implementation ObjectInputStream does not work well
128                 * when running an a micro kernal style app-server like JBoss.
129                 * We need to look for the Class in the context class loader and
130                 * not in the System classloader.
131                 *
132                 * Would this be done better by using a MarshaedObject??
133                 */

134                class ObjectInputStreamExt extends ObjectInputStream JavaDoc
135                {
136                   ObjectInputStreamExt(InputStream JavaDoc is) throws IOException JavaDoc
137                   {
138                      super(is);
139                   }
140
141                   protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc v) throws IOException JavaDoc, ClassNotFoundException JavaDoc
142                   {
143                      return Classes.loadClass(v.getName());
144                   }
145                }
146                ObjectInputStream JavaDoc input = new ObjectInputStreamExt(new ByteArrayInputStream JavaDoc(objectBytes));
147                retVal = (Serializable JavaDoc) input.readObject();
148                input.close();
149             }
150          }
151       }
152       catch (ClassNotFoundException JavaDoc e)
153       {
154          throw new MessageFormatException JavaDoc("ClassNotFoundException: " + e.getMessage());
155       }
156       catch (IOException JavaDoc e)
157       {
158          throw new MessageFormatException JavaDoc("IOException: " + e.getMessage());
159       }
160       return retVal;
161    }
162    
163    // SpyMessage overrides ------------------------------------------
164

165    public void clearBody() throws JMSException JavaDoc
166    {
167       objectBytes = null;
168       super.clearBody();
169    }
170
171    public SpyMessage myClone() throws JMSException JavaDoc
172    {
173       SpyObjectMessage result = MessagePool.getObjectMessage();
174       result.copyProps(this);
175       result.isByteArray = this.isByteArray;
176       if (objectBytes != null)
177         {
178          result.objectBytes = new byte[this.objectBytes.length];
179          System.arraycopy(this.objectBytes, 0, result.objectBytes, 0, this.objectBytes.length);
180       }
181       return result;
182    }
183    
184    // Externalizable implementation ---------------------------------
185

186    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
187    {
188       super.writeExternal(out);
189       out.writeBoolean(isByteArray);
190       if (objectBytes == null)
191       {
192          out.writeInt(-1);
193       }
194       else
195       {
196          out.writeInt(objectBytes.length);
197          out.write(objectBytes);
198       }
199    }
200
201    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
202    {
203       super.readExternal(in);
204       isByteArray = in.readBoolean();
205       int length = in.readInt();
206       if (length < 0)
207       {
208          objectBytes = null;
209       }
210       else
211       {
212          objectBytes = new byte[length];
213          in.readFully(objectBytes);
214       }
215    }
216    
217    // Package protected ---------------------------------------------
218

219    // Protected -----------------------------------------------------
220

221    // Private -------------------------------------------------------
222

223    // Inner classes -------------------------------------------------
224
}
225
Popular Tags