org/objectweb/proactive/core/body/ft/checkpointing/Checkpoint.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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     //id of the checkpointed body
00057     private UniqueID bodyID;
00058 
00059     //checkpointed body and infos
00060     private byte[] checkpointedBody;
00061 
00062     //additionnal infos
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     // GETTER SETTER
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             //System.out.println("[FT] Recovering body " + this.bodyID);
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             // Communcations are blocked until the activity is restarted
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      * The output stream is extended so as to add in the annotation of the
00143      * serialization stream the URL of the classserver of the checkpoint server.
00144      * The new codebase is the concatenation of the standard codebase and the
00145      * checkpoint server codebase.
00146      * @author cdelbe
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          * Write the codebase in the stream.
00160          */
00161         protected void annotateClass(Class cl) throws IOException {
00162             writeObject(this.codebase);
00163         }
00164 
00165         /*
00166          * Checks for objects that are instances of java.rmi.Remote
00167          * that need to be serialized as RMI stubs !
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 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1