1 package org.sapia.ubik.rmi.server.transport; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.Externalizable ; 5 import java.io.IOException ; 6 import java.io.ObjectInput ; 7 import java.io.ObjectOutput ; 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 20 public class MarshalledObject implements Externalizable { 21 private byte[] _bytes; 22 private String _codebase; 23 24 27 public MarshalledObject() { 28 } 29 30 33 public MarshalledObject(Object o, VmId id, String transportType, String codebase) 34 throws IOException { 35 if (o != null) { 36 _bytes = serialize(id, transportType, o); 37 } 38 _codebase = codebase; 39 } 40 41 52 public Object get(ClassLoader loader) 53 throws IOException , ClassNotFoundException { 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 68 public void readExternal(ObjectInput in) 69 throws IOException , ClassNotFoundException { 70 _bytes = (byte[]) in.readObject(); 71 _codebase = (String ) in.readObject(); 72 } 73 74 77 public void writeExternal(ObjectOutput out) throws IOException { 78 out.writeObject(_bytes); 79 out.writeObject(_codebase); 80 } 81 82 private static byte[] serialize(VmId vmid, String transportType, Object o) 83 throws IOException { 84 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 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 |