001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.protocol;
030:
031: import com.caucho.ejb.RemoteExceptionWrapper;
032: import com.caucho.server.util.CauchoSystem;
033: import com.caucho.vfs.ReadStream;
034: import com.caucho.vfs.TempStream;
035: import com.caucho.vfs.WriteStream;
036:
037: import java.io.*;
038: import java.rmi.RemoteException;
039:
040: /**
041: * Serializer for transfers inside a single JVM.
042: */
043: public class SelfSerializer {
044: TempStream trs;
045:
046: ReadStream is;
047: WriteStream os;
048:
049: ObjectInputStream ois;
050: ObjectOutputStream oos;
051:
052: public static SelfSerializer allocate() throws RemoteException {
053: SelfSerializer ser = new SelfSerializer();
054:
055: ser.clear();
056:
057: return ser;
058: }
059:
060: /**
061: * Clears the buffers for the serializer
062: */
063: public void clear() throws RemoteException {
064: try {
065: trs = new TempStream();
066: os = new WriteStream(trs);
067: oos = new ObjectOutputStream(os);
068: is = null;
069: ois = null;
070: } catch (Exception e) {
071: throw new RemoteExceptionWrapper(e);
072: }
073: }
074:
075: /**
076: * Serializes the object to the temp stream.
077: *
078: * @param obj the object to serialize
079: */
080: public void write(Object obj) throws RemoteException {
081: try {
082: oos.writeObject(obj);
083: } catch (Exception e) {
084: throw new RemoteExceptionWrapper(e);
085: }
086: }
087:
088: /**
089: * Reads an object from the serialized stream.
090: */
091: public Object read() throws RemoteException {
092: try {
093: if (is == null) {
094: oos.flush();
095: is = trs.openRead();
096: ois = new LoaderObjectInputStream(is);
097: }
098:
099: return ois.readObject();
100: } catch (Exception e) {
101: throw new RemoteExceptionWrapper(e);
102: }
103: }
104:
105: /**
106: * Closes the streams.
107: */
108: public void close() {
109: try {
110: InputStream is = this .is;
111: this .is = null;
112: OutputStream os = this .os;
113: this .os = null;
114: if (is != null)
115: is.close();
116: if (os != null)
117: os.close();
118: } catch (IOException e) {
119: }
120: }
121:
122: /**
123: * Extension class so the classes will be loaded by the proper
124: * class loader.
125: */
126: static class LoaderObjectInputStream extends ObjectInputStream {
127: ClassLoader loader;
128:
129: LoaderObjectInputStream(InputStream is) throws IOException,
130: StreamCorruptedException {
131: super (is);
132:
133: loader = Thread.currentThread().getContextClassLoader();
134: }
135:
136: /**
137: * Finds the specified class using the current class loader.
138: */
139: protected Class resolveClass(ObjectStreamClass v)
140: throws IOException, ClassNotFoundException {
141: return CauchoSystem.loadClass(v.getName(), false, loader);
142: }
143: }
144: }
|