00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.body.ft.checkpointing;
00032 
00033 import java.io.ByteArrayInputStream;
00034 import java.io.ByteArrayOutputStream;
00035 import java.io.IOException;
00036 import java.io.ObjectOutputStream;
00037 import java.io.OutputStream;
00038 import java.rmi.server.RemoteObject;
00039 import java.rmi.server.RemoteStub;
00040 
00041 import org.objectweb.proactive.Body;
00042 import org.objectweb.proactive.core.UniqueID;
00043 
00044 import sun.rmi.server.MarshalInputStream;
00045 
00046 
00055 public class Checkpoint implements java.io.Serializable {
00056     
00057     private UniqueID bodyID;
00058 
00059     
00060     private byte[] checkpointedBody;
00061 
00062     
00063     private CheckpointInfo ci;
00064 
00070     public Checkpoint(Body bodyToCheckpoint, String additionalCodebase) {
00071         try {
00072             this.bodyID = bodyToCheckpoint.getID();
00073             String codebase = System.getProperty("java.rmi.server.codebase");
00074             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
00075             CheckpointingOutputStream objectOutputStream = new CheckpointingOutputStream(byteArrayOutputStream,
00076                     codebase + " " + additionalCodebase);
00077             objectOutputStream.writeObject(bodyToCheckpoint);
00078             objectOutputStream.flush();
00079             objectOutputStream.close();
00080             byteArrayOutputStream.close();
00081             this.checkpointedBody = byteArrayOutputStream.toByteArray();
00082         } catch (IOException e) {
00083             System.err.println("Error while checkpointing the body " +
00084                 bodyToCheckpoint);
00085             e.printStackTrace();
00086         }
00087     }
00088 
00089     
00090     public UniqueID getBodyID() {
00091         return bodyID;
00092     }
00093 
00094     public void setBodyID(UniqueID uniqueID) {
00095         bodyID = uniqueID;
00096     }
00097 
00098     public void setCheckpointInfo(CheckpointInfo ci) {
00099         this.ci = ci;
00100     }
00101 
00102     public CheckpointInfo getCheckpointInfo() {
00103         return ci;
00104     }
00105 
00110     public Body recover() {
00111         try {
00112             
00113             ByteArrayInputStream bais = new ByteArrayInputStream(this.checkpointedBody);
00114             MarshalInputStream mis = new MarshalInputStream(bais);
00115             Body recoveredBody = (Body) (mis.readObject());
00116             mis.close();
00117             bais.close();
00118             
00119             recoveredBody.blockCommunication();
00120             return recoveredBody;
00121         } catch (IOException e) {
00122             System.err.println("Error while recovering the body with ID = " +
00123                 this.bodyID);
00124             e.printStackTrace();
00125             return null;
00126         } catch (ClassNotFoundException e) {
00127             System.err.println("Error while recovering the body with ID = " +
00128                 this.bodyID);
00129             e.printStackTrace();
00130             return null;
00131         }
00132     }
00133 
00137     public String toString() {
00138         return "Checkpoint for body " + this.bodyID;
00139     }
00140 
00141     
00142 
00143 
00144 
00145 
00146 
00147 
00148     private static class CheckpointingOutputStream extends ObjectOutputStream {
00149         private String codebase;
00150 
00151         public CheckpointingOutputStream(OutputStream out, String codebase)
00152             throws IOException {
00153             super(out);
00154             this.enableReplaceObject(true);
00155             this.codebase = codebase;
00156         }
00157 
00158         
00159 
00160 
00161         protected void annotateClass(Class cl) throws IOException {
00162             writeObject(this.codebase);
00163         }
00164 
00165         
00166 
00167 
00168 
00169         protected final Object replaceObject(Object obj)
00170             throws IOException {
00171             if ((obj instanceof RemoteObject) && !(obj instanceof RemoteStub)) {
00172                 return RemoteObject.toStub((RemoteObject) obj);
00173             } else {
00174                 return obj;
00175             }
00176         }
00177     }
00178 }