KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > binding > remote > RemoteSerializerImpl


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.resource.binding.remote;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.Serializable JavaDoc;
30
31 import org.jboss.serial.io.JBossObjectInputStream;
32 import org.jboss.serial.io.JBossObjectOutputStream;
33
34 /**
35  * A RemoteSerializerImpl.
36  *
37  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
38  * @version $Revision: 44967 $
39  */

40 public class RemoteSerializerImpl implements RemoteSerializer, Serializable JavaDoc
41 {
42    
43    private static final RemoteSerializer serializer = new RemoteSerializerImpl();
44    
45    /** The serialVersionUID */
46    private static final long serialVersionUID = 6386719587282465130L;
47
48    public byte[] serializeToByte(final Object JavaDoc target) throws Exception JavaDoc
49    {
50       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
51       OutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
52       JBossObjectOutputStream jbos = new JBossObjectOutputStream(oos);
53       jbos.writeObject(target);
54       jbos.close();
55       
56       return baos.toByteArray();
57       
58    }
59    
60    public Object JavaDoc serialize(final Object JavaDoc target) throws Exception JavaDoc{
61       
62       return shouldSerialize(target) ? serialize(target) : target;
63       
64    }
65    
66    public Object JavaDoc deserialize(Object JavaDoc target) throws Exception JavaDoc{
67       
68       SerializableWrapper wrapper = (SerializableWrapper)target;
69       byte[] payload = wrapper.getPayload();
70       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(payload);
71       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
72       JBossObjectInputStream jbis = new JBossObjectInputStream(ois);
73       Object JavaDoc result = jbis.readObject();
74       return result;
75       
76    }
77    public void serialize(final Object JavaDoc[] targets) throws Exception JavaDoc
78    {
79       
80       for (int i = 0; i < targets.length; i++)
81       {
82          final Object JavaDoc target = targets[i];
83          targets[i] = serialize(target);
84            
85       }
86             
87    }
88    
89    public boolean shouldSerialize(Object JavaDoc target)
90    {
91       return !(target instanceof Serializable JavaDoc);
92       
93    }
94    
95    static RemoteSerializer getInstance(){
96       
97       return serializer;
98    
99    }
100 }
101
Popular Tags