oval


Centre International de Mathİmatiques Pures et Appliquİes
International Center for Pure and Applied Mathematics


A CIMPA-UNSA-INRIA-UNESCO-VIETNAM SCHOOL ON


Objects and Network
Persistence Courses


Emmanuel Chailloux
Emmanuel.Chailloux@lip6.fr
http://www-spi.lip6.fr/~emmanuel

SUMMARY
Persistence:

Persistence

To conserve an object outside of the current execution in a file or a stream. Goal: to use the object in another program (or the same later)

Difficulties: The jdk1.1 proposes a mechanism of serialization.

Serialization in Java

To serialize an object: just means to write the values of all its data fields into a stream of bytes.
What is stored?
Storage contains : The key allows to check the version of the class.

The keyword transient marks data fields, which will not saved during serialization.

Another way: is to implement its own serialization mechanism. It's then mandatory to write a class Externalizable which implements the following interfaces : readExternal and writeExternal.

Example : Towns List
indentation
import java.io.*;

class ExTL implements Serializable {
String nom;
ExTL others;

ExTL() {nom=
null;others=null;}
ExTL(String n, ExTL e) {
nom=n;others=e;
}

boolean isEmpty() {return (nom == null);}
void destroy() {nom=null;others=null;}

public String toString() {
if (isEmpty()) return "[]";
else if (others.isEmpty()) return nom;
else return nom+"::"+others.toString();
}

public void add (String unNom) {
System.out.println(
"*");
ExTL e =
new ExTL(nom,others);
others=e;
nom=unNom;
}

}

Example : Towns List (2)
indentation
import java.io.*;

class ExecuteTL {
public static void main (String[] args) {
ExTL e =
new ExTL();
ObjectOutputStream out;
ObjectInputStream in;

try {
e.add(
"Hanoi");
e.add(
"Paris");
System.out.println(
"1 : "+e);
out =
new ObjectOutputStream(new FileOutputStream("ExTL.ser"));
out.writeObject(e);
out.flush(); out.close();
e.destroy();
System.out.println(
"2 : "+e);
in =
new ObjectInputStream(new FileInputStream("ExTL.ser"));
e.add(
"London");
e.others = (ExTL)in.readObject();
in.close();
System.out.println(
"3 : "+e);
}
catch (java.lang.ClassNotFoundException exc){System.err.println(exc);}
catch (StreamCorruptedException exc) {System.err.println(exc);}
catch (IOException exc) {System.err.println(exc);}
}
}


Running TL
$ java ExecuteTL
*
*
1 : Paris::Hanoi
2 : []
*
3 : London::Paris::Hanoi
* : method add(..) is traced



Serialization and threads

A Thread is a class, so it can be serialized.

To implement Serialize for a Consumer

class Consumer extends Thread implements Serializable {

  transient Shop aShop;
  String name;
  
  Consumer(Shop s, String sn){aShop=s; name = sn;}

  void changeShop(Shop s) {
    aShop = s;
  }
...
}
To save a Consumer
public class ProdConP {

  static void saveConsumer(Consumer c, String filename) {
    try {
      ObjectOutputStream out =
        new ObjectOutputStream(new FileOutputStream(filename));
      out.writeObject(c);
      out.close();
    }
    catch (Exception e){ System.out.println(e);}
    
  }
To load a Consumer
static Consumer loadConsumer(String filename) {
      Consumer c = null;
      try {

      ObjectInputStream in =
        new ObjectInputStream(new FileInputStream(filename));
      c = (Consumer)in.readObject();
      in.close();
    }
    catch (Exception e) { System.out.println(e);}
    return c;
  }
...
}
To test the program
Consumer c2 = new Consumer(myShop,"Cl2");
     c2.start();
     Consumer c3 = new Consumer(myShop,"Cl3");
     c3.start();

     c1.stop();
     char c;
     c=(char)System.in.read();
     switch (c) {
       case '1': { saveConsumer(c1,"C1.ser");break;}
       case '4': { Consumer cl = loadConsumer("C1.ser");
                   cl.changeShop(myShop);
                   cl.start();break;}
     }
Remarks
After reading:
Serialization and applets
An Applet is a Thread, so it can be serialized.

Different behaviors between appletviewer and Java browsers.

Example : Game of Life

How can you create the first GraphGOL file?
Ş by using the "save" menu in appletviewer

Execution : to save a GraphGOL
save object is a TextField
gol is an Applet.
String filename = (String)(save.getText()) +".ser";
          System.out.println("Saving "+filename);
          ObjectOutputStream out =
            new ObjectOutputStream (
              new FileOutputStream(filename));
          out.writeObject(gol);
          out.close();
Execution : to load an old GraphGOL
load is another TextField
String filename =  (String)(load.getText()) +".ser";
          System.out.println("Loading "+filename);
          ObjectInputStream in =
            new ObjectInputStream (
                          new FileInputStream(filename));
          GraphGOL g = (GraphGOL)in.readObject();
          in.close();
          if (gol != null) {remove(gol);}
          gol=g;
          add("North",gol);
          gol.initEvents();
          gol.start();
Serialization and sockets
A Socket containts a Stream, so serializable Object can be sent to or received from a Socket!!!

Example 1 : GOL server

Example 2 : Consumer migration

Remarks

no mobile code: for processus: using sockets:
This document was translated from LATEX by HEVEA.