KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
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: MessageExchangeSerializer.java 154 2006-03-27 15:30:10Z alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.messaging;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.activation.DataHandler JavaDoc;
36 import javax.mail.util.ByteArrayDataSource;
37 import javax.xml.transform.Source JavaDoc;
38 import javax.xml.transform.Transformer JavaDoc;
39 import javax.xml.transform.stream.StreamResult JavaDoc;
40 import javax.xml.transform.stream.StreamSource JavaDoc;
41
42 import org.objectweb.petals.util.SourceHelper;
43
44 /**
45  *
46  * @author alouis - EBM Websourcing
47  *
48  */

49 public class MessageExchangeSerializer implements Serializable JavaDoc {
50
51     private static final long serialVersionUID = 1L;
52
53     private MessageExchangeSerializer() {
54         super();
55     }
56
57     protected static MessageExchangeSerializer instance = new MessageExchangeSerializer();
58
59     public static MessageExchangeSerializer instance() {
60         return instance;
61     }
62
63     public void serializeContent(Source content, ObjectOutputStream JavaDoc s)
64         throws Exception JavaDoc {
65         synchronized (content) {
66             StreamResult JavaDoc sresult = new StreamResult JavaDoc(s);
67             Transformer JavaDoc transform = SourceHelper.getTransformer();
68             transform.transform(content, sresult);
69         }
70     }
71
72     public Source deserializeContent(ObjectInputStream JavaDoc s) throws IOException JavaDoc {
73         StreamSource JavaDoc source = new StreamSource JavaDoc();
74
75         byte[] buff = new byte[1024];
76         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
77         int count = 0;
78         int n = 0;
79         while (-1 != (n = s.read(buff))) {
80             outputStream.write(buff, 0, n);
81             count += n;
82         }
83
84         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(outputStream
85             .toByteArray());
86         source.setInputStream(in);
87         return source;
88     }
89
90     /**
91      * Serialize all datahandler in the Map. <br>
92      * Serialized information is : <br>
93      * -attachmentName <br>
94      * -attachmentContentType <br>
95      * -name <br>
96      * -inputStream available bytes count <br>
97      * -inputStream as a byteArray
98      *
99      * @param attachments
100      * @param s
101      * @throws Exception
102      */

103     public void serializeAttachments(Map JavaDoc<String JavaDoc, DataHandler JavaDoc> attachments,
104         ObjectOutputStream JavaDoc s) throws Exception JavaDoc {
105         s.writeInt(attachments.size());
106
107         for (Iterator JavaDoc iter = attachments.keySet().iterator(); iter.hasNext();) {
108
109             String JavaDoc attachmentName = (String JavaDoc) iter.next();
110             DataHandler JavaDoc attachment = attachments.get(attachmentName);
111
112             InputStream JavaDoc binary = attachment.getInputStream();
113             synchronized (binary) {
114
115                 int available = binary.available();
116
117                 // write information about the attachment
118
s.writeUTF(attachmentName);
119                 s.writeUTF(attachment.getContentType());
120                 s.writeUTF(attachment.getName());
121                 s.writeInt(available);
122
123                 // write attachment content
124
byte[] content = new byte[available];
125                 binary.read(content);
126                 s.write(content);
127                 s.flush();
128             }
129         }
130     }
131
132     /**
133      * Deserialize all datahandlers in the stream and return a Map. <br>
134      * Deserialized information is : <br>
135      * -attachmentName <br>
136      * -attachmentContentType <br>
137      * -name <br>
138      * -Stream available bytes count <br>
139      * -Stream as a byteArray
140      *
141      * @param s
142      * @return
143      * @throws IOException
144      */

145     public Map JavaDoc<String JavaDoc, DataHandler JavaDoc> deserializeAttachments(ObjectInputStream JavaDoc s)
146         throws IOException JavaDoc {
147         HashMap JavaDoc<String JavaDoc, DataHandler JavaDoc> attachments = new HashMap JavaDoc<String JavaDoc, DataHandler JavaDoc>();
148
149         int attachmentsCount = s.readInt();
150
151         for (int i = 0; i < attachmentsCount; i++) {
152             // read information
153
String JavaDoc attachmentName = s.readUTF();
154             String JavaDoc contentType = s.readUTF();
155             String JavaDoc name = s.readUTF();
156             int available = s.readInt();
157
158             // read content
159
byte[] content = new byte[available];
160             s.readFully(content);
161             ByteArrayDataSource dataSource = new ByteArrayDataSource(content,
162                 contentType);
163             dataSource.setName(name);
164
165             // create the dataHandler
166
DataHandler JavaDoc dataHandler = new DataHandler JavaDoc(dataSource);
167
168             attachments.put(attachmentName, dataHandler);
169         }
170         return attachments;
171     }
172
173 }
174
Popular Tags