KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > ObjectMessage


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
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): Frederic Maistre (INRIA)
22  * Contributor(s):ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms;
25
26 import org.objectweb.joram.shared.excepts.*;
27
28 import java.io.*;
29
30 import javax.jms.JMSException JavaDoc;
31 import javax.jms.MessageFormatException JavaDoc;
32 import javax.jms.MessageNotWriteableException JavaDoc;
33
34 /**
35  * Implements the <code>javax.jms.ObjectMessage</code> interface.
36  */

37 public final class ObjectMessage extends Message implements javax.jms.ObjectMessage JavaDoc {
38   /**
39    * Instanciates a bright new <code>ObjectMessage</code>.
40    */

41   ObjectMessage() {
42     super();
43     momMsg.type = momMsg.OBJECT;
44   }
45
46   /**
47    * Instanciates an <code>ObjectMessage</code> wrapping a
48    * consumed MOM message containing an object.
49    *
50    * @param session The consuming session.
51    * @param momMsg The MOM message to wrap.
52    */

53   ObjectMessage(Session session,
54                 org.objectweb.joram.shared.messages.Message momMsg) {
55     super(session, momMsg);
56   }
57
58   /**
59    * API method.
60    *
61    * @exception MessageNotWriteableException When trying to set an object if
62    * the message body is read-only.
63    * @exception MessageFormatException If object serialization fails.
64    */

65   public void setObject(Serializable obj) throws JMSException JavaDoc {
66     if (RObody)
67       throw new MessageNotWriteableException JavaDoc("Can't set an object as the"
68                                              + " message body is read-only.");
69
70     try {
71       clearBody();
72       momMsg.setObject(obj);
73     } catch (Exception JavaDoc exc) {
74       throw new MessageFormatException JavaDoc("Object serialization failed: " + exc);
75     }
76   }
77
78   /**
79    * API method.
80    *
81    * @exception MessageFormatException In case of a problem when getting the
82    * body.
83    */

84   public Serializable getObject() throws MessageFormatException JavaDoc {
85     if (momMsg.body == null) return null;
86
87     try {
88       return momMsg.getObject();
89     } catch (ClassNotFoundException JavaDoc cnfexc) {
90       ByteArrayInputStream bais = null;
91       ObjectInputStream ois = null;
92
93       try {
94         // Could not build serialized object: reason could be linked to
95
// class loaders hierarchy in an application server.
96
class Specialized_OIS extends ObjectInputStream {
97           Specialized_OIS(InputStream is) throws IOException {
98             super(is);
99           }
100
101           protected Class JavaDoc resolveClass(ObjectStreamClass osc)
102             throws IOException, ClassNotFoundException JavaDoc {
103             String JavaDoc n = osc.getName();
104             return Class.forName(n, false,
105                                  Thread.currentThread().getContextClassLoader());
106           }
107         }
108
109         bais = new ByteArrayInputStream(momMsg.body);
110         ois = new Specialized_OIS(bais);
111         return (Serializable) ois.readObject();
112       } catch (Exception JavaDoc exc) {
113         MessageFormatException JavaDoc jE =
114           new MessageFormatException JavaDoc("Error while deserializing the wrapped object: " + exc);
115         throw jE;
116       } finally {
117         try {
118           ois.close();
119         } catch (Exception JavaDoc e) {}
120         try {
121           bais.close();
122         } catch (Exception JavaDoc e) {}
123       }
124     } catch (Exception JavaDoc exc) {
125       MessageFormatException JavaDoc jE =
126         new MessageFormatException JavaDoc("Error while deserializing the wrapped object: " + exc);
127       throw jE;
128     }
129   }
130 }
131
Popular Tags