KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > MarshalledObject


1 package org.sapia.ubik.rmi.server.transport;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.Externalizable JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.ObjectInput JavaDoc;
7 import java.io.ObjectOutput JavaDoc;
8
9 import org.sapia.ubik.rmi.Utils;
10 import org.sapia.ubik.rmi.server.RmiUtils;
11 import org.sapia.ubik.rmi.server.VmId;
12
13
14 /**
15  * An instance of this class keeps an object in byte form.
16  *
17  * @author Yanick Duchesne
18  * 17-Sep-2003
19  */

20 public class MarshalledObject implements Externalizable JavaDoc {
21   private byte[] _bytes;
22   private String JavaDoc _codebase;
23
24   /**
25    * Do not use. Meant for externalization.
26    */

27   public MarshalledObject() {
28   }
29
30   /**
31    * Constructor for MarshalledObject.
32    */

33   public MarshalledObject(Object JavaDoc o, VmId id, String JavaDoc transportType, String JavaDoc codebase)
34     throws IOException JavaDoc {
35     if (o != null) {
36       _bytes = serialize(id, transportType, o);
37     }
38     _codebase = codebase;
39   }
40
41   /**
42    * Returns the object that this instance wraps. Internally uses the
43    * given classloader to deserialize the wrapped object (kept as an array
44    * of bytes).
45    *
46    * @param loader the ClassLoader to use for deserialization.
47    * @return an <code>Object</code>
48    * @throws IOException if an error occurs while deserializing.
49    * @throws ClassNotFoundException if the class of the object to deserialize
50    * was not found.
51    */

52   public Object JavaDoc get(ClassLoader JavaDoc loader)
53     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
54     if (_bytes == null) {
55       return _bytes;
56     }
57     if(_codebase != null && RmiUtils.CODE_DOWNLOAD){
58       return Utils.deserialize(_bytes, loader, _codebase);
59     }
60     else{
61       return Utils.deserialize(_bytes, loader);
62     }
63   }
64
65   /**
66    * @see java.io.Externalizable#readExternal(ObjectInput)
67    */

68   public void readExternal(ObjectInput JavaDoc in)
69     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
70     _bytes = (byte[]) in.readObject();
71     _codebase = (String JavaDoc) in.readObject();
72   }
73
74   /**
75    * @see java.io.Externalizable#writeExternal(ObjectOutput)
76    */

77   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
78     out.writeObject(_bytes);
79     out.writeObject(_codebase);
80   }
81
82   private static byte[] serialize(VmId vmid, String JavaDoc transportType, Object JavaDoc o)
83     throws IOException JavaDoc {
84     ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
85     MarshalOutputStream ous = new MarshalOutputStream(bos);
86     ous.setUp(vmid, transportType);
87     ous.writeObject(o);
88     ous.flush();
89     ous.close();
90
91     return bos.toByteArray();
92   }
93 }
94
Popular Tags