KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > protocol > SelfSerializer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.protocol;
30
31 import com.caucho.ejb.RemoteExceptionWrapper;
32 import com.caucho.server.util.CauchoSystem;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.TempStream;
35 import com.caucho.vfs.WriteStream;
36
37 import java.io.*;
38 import java.rmi.RemoteException JavaDoc;
39 /**
40  * Serializer for transfers inside a single JVM.
41  */

42 public class SelfSerializer {
43   TempStream trs;
44
45   ReadStream is;
46   WriteStream os;
47   
48   ObjectInputStream ois;
49   ObjectOutputStream oos;
50   
51   public static SelfSerializer allocate()
52     throws RemoteException JavaDoc
53   {
54     SelfSerializer ser = new SelfSerializer();
55
56     ser.clear();
57
58     return ser;
59   }
60
61   /**
62    * Clears the buffers for the serializer
63    */

64   public void clear()
65     throws RemoteException JavaDoc
66   {
67     try {
68       trs = new TempStream(null);
69       os = new WriteStream(trs);
70       oos = new ObjectOutputStream(os);
71       is = null;
72       ois = null;
73     } catch (Exception JavaDoc e) {
74       throw new RemoteExceptionWrapper(e);
75     }
76   }
77
78   /**
79    * Serializes the object to the temp stream.
80    *
81    * @param obj the object to serialize
82    */

83   public void write(Object JavaDoc obj)
84     throws RemoteException JavaDoc
85   {
86     try {
87       oos.writeObject(obj);
88     } catch (Exception JavaDoc e) {
89       throw new RemoteExceptionWrapper(e);
90     }
91   }
92
93   /**
94    * Reads an object from the serialized stream.
95    */

96   public Object JavaDoc read()
97     throws RemoteException JavaDoc
98   {
99     try {
100       if (is == null) {
101         oos.flush();
102         is = trs.openRead(true);
103         ois = new LoaderObjectInputStream(is);
104       }
105       
106       return ois.readObject();
107     } catch (Exception JavaDoc e) {
108       throw new RemoteExceptionWrapper(e);
109     }
110   }
111
112   /**
113    * Closes the streams.
114    */

115   public void close()
116   {
117     try {
118       InputStream is = this.is;
119       this.is = null;
120       OutputStream os = this.os;
121       this.os = null;
122       if (is != null)
123         is.close();
124       if (os != null)
125         os.close();
126     } catch (IOException e) {
127     }
128   }
129   
130   /**
131    * Extension class so the classes will be loaded by the proper
132    * class loader.
133    */

134   static class LoaderObjectInputStream extends ObjectInputStream {
135     ClassLoader JavaDoc loader;
136     
137     LoaderObjectInputStream(InputStream is)
138       throws IOException, StreamCorruptedException
139     {
140       super(is);
141
142       loader = Thread.currentThread().getContextClassLoader();
143     }
144
145     /**
146      * Finds the specified class using the current class loader.
147      */

148     protected Class JavaDoc resolveClass(ObjectStreamClass v)
149       throws IOException, ClassNotFoundException JavaDoc
150     {
151       return CauchoSystem.loadClass(v.getName(), false, loader);
152     }
153   }
154 }
155
Popular Tags