KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > messaging > NormalizedMessageImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: NormalizedMessageImpl.java 1644 2007-01-24 09:52:50Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.messaging;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.activation.DataHandler JavaDoc;
33 import javax.jbi.messaging.MessagingException;
34 import javax.jbi.messaging.NormalizedMessage;
35 import javax.security.auth.Subject JavaDoc;
36 import javax.xml.transform.Source JavaDoc;
37
38 /**
39  * JBI <code>NormalizedMessage</code> implementation.
40  *
41  * @version $Rev: 1644 $ $Date: 2007-01-24 10:52:50 +0100 (mer, 24 jan 2007) $
42  * @since Petals 1.0
43  * @author Adrien LOUIS, <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
44  */

45 public class NormalizedMessageImpl implements NormalizedMessage, Serializable JavaDoc {
46
47     /**
48      *
49      */

50     private static final long serialVersionUID = 1L;
51
52     /**
53      * Attachments to the main message: id + content.
54      */

55     protected transient Map JavaDoc<String JavaDoc, DataHandler JavaDoc> attachments;
56
57     /**
58      * The XML "payload" message.
59      */

60     protected transient Source JavaDoc content;
61
62     /**
63      * Metadata associated with the message content. Map: property + value.
64      */

65     protected Map JavaDoc<String JavaDoc, Object JavaDoc> properties;
66
67     /**
68      * JAAS Security Subject.
69      */

70     protected Subject JavaDoc subject;
71
72     protected MessageExchangeSerializer messageExchangeSerializer;
73
74     /**
75      * This default constructor creates a new empty
76      * <code>NormalizedMessage</code>.
77      */

78     public NormalizedMessageImpl() {
79         super();
80
81         content = null;
82         properties = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
83         attachments = new HashMap JavaDoc<String JavaDoc, DataHandler JavaDoc>();
84
85         messageExchangeSerializer = MessageExchangeSerializer.instance();
86     }
87
88     /*
89      * ---------------------------------------------------------------------- N
90      * o r m a l i z e d M e s s a g e M e t h o d s
91      * ----------------------------------------------------------------------
92      */

93
94     /**
95      * @inheritDoc
96      */

97     public void addAttachment(String JavaDoc id, DataHandler JavaDoc attachment)
98         throws MessagingException {
99         attachments.put(id, attachment);
100     }
101
102     /**
103      * @inheritDoc
104      */

105     public DataHandler JavaDoc getAttachment(String JavaDoc id) {
106         return (DataHandler JavaDoc) attachments.get(id);
107     }
108
109     /**
110      * @inheritDoc
111      */

112     public Set JavaDoc getAttachmentNames() {
113         return attachments.keySet();
114     }
115
116     /**
117      * @inheritDoc
118      */

119     public Source JavaDoc getContent() {
120         return content;
121     }
122
123     /**
124      * @inheritDoc
125      */

126     public Object JavaDoc getProperty(String JavaDoc name) {
127         return properties.get(name);
128     }
129
130     /**
131      * @inheritDoc
132      */

133     public Set JavaDoc getPropertyNames() {
134         return properties.keySet();
135     }
136
137     /**
138      * @inheritDoc
139      */

140     public Subject JavaDoc getSecuritySubject() {
141         return subject;
142     }
143
144     /**
145      * @inheritDoc
146      */

147     public void removeAttachment(String JavaDoc id) throws MessagingException {
148         Object JavaDoc att = attachments.remove(id);
149
150         if (att == null) {
151             throw new MessagingException(id + " attachment does not exist.");
152         }
153     }
154
155     /**
156      * @inheritDoc
157      */

158     public void setContent(Source JavaDoc content) throws MessagingException {
159         this.content = content;
160     }
161
162     /**
163      * @inheritDoc
164      */

165     public void setProperty(String JavaDoc name, Object JavaDoc value) {
166         if (value == null) {
167             // case to remove properties not defined (null)
168
properties.remove(name);
169
170         } else {
171             // define this property name with a new value
172
properties.put(name, value);
173         }
174     }
175
176     /**
177      * @inheritDoc
178      */

179     public void setSecuritySubject(Subject JavaDoc securitySubject) {
180         this.subject = securitySubject;
181     }
182
183     /**
184      * @see NormalizedMessageImpl#readObject(ObjectInputStream)
185      */

186     protected void readObjectDelegate(ObjectInputStream JavaDoc s) throws IOException JavaDoc {
187         try {
188             s.defaultReadObject();
189
190             if (s.available() > 0) {
191                 attachments = messageExchangeSerializer
192                     .deserializeAttachments(s);
193                 content = messageExchangeSerializer.deserializeContent(s);
194             }
195         } catch (ClassNotFoundException JavaDoc e) {
196             throw new IOException JavaDoc(e.getClass() + ":" + e.getMessage());
197         }
198     }
199
200     /**
201      * Serialization process : <br/> - Serialization of all serializable fields
202      * of the NormalizedMessage <br/> - Serialization of all attachement <br/> -
203      * Finally, serialization of the content. Content must always be serialized
204      * at the end of the serialization process. Because in the deserialization
205      * process, a new content will be created with all ending bytes of the
206      * Stream.
207      *
208      * @see NormalizedMessageImpl#writeObject(ObjectOutputStream)
209      */

210     protected void writeObjectDelegate(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
211         s.defaultWriteObject();
212         try {
213             messageExchangeSerializer.serializeAttachments(attachments, s);
214             messageExchangeSerializer.serializeContent(content, s);
215         } catch (Exception JavaDoc e) {
216             throw new IOException JavaDoc(e.getMessage());
217         }
218     }
219
220     /**
221      * Used to deserialize Content object
222      *
223      * @param s
224      * @throws IOException
225      */

226     private void readObject(ObjectInputStream JavaDoc s) throws IOException JavaDoc {
227         readObjectDelegate(s);
228     }
229
230     /**
231      * Used to serialize Content object
232      *
233      * @param s
234      * @throws IOException
235      */

236     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
237         writeObjectDelegate(s);
238     }
239 }
240
Popular Tags