Python Hessian


Serialization Classes

As part of the process for sending objects between the Hessian client and service, serialization must occur. The Java Class Library includes functionality for serialization, but it is specific to the Java language. For this reason, the developers at Caucho wrote their own code to support Hessian as well as for general use in any implementation language. An object can be serialized in Java but deserialized in Python, as long as the Hessian specification is used in the serialization code.

In serialization of objects, two actions are required: serialization and deserialization. Both operations require the use of a stream object from the OutputStream and InputStream class hierarchies. Serialization is simple to do, as shown in Listing 9.7.

Listing 9.7: Hessian serialization example.

start example
 Line 1: import com.caucho.hessian.io.HessianOutput; import com.caucho.hessian.io.HessianInput; import java.util.Vector; import java.util.Random; import java.io.*; public class Serialization {   public static void main(String[] args) { Line 10:    Vector vec = new Vector();     Random rand = new Random();     for(int i=0;i<10;i++) {       vec.add(new Integer(rand.nextInt(50)));       System.out.print(vec.elementAt(i) + " ");     }     System.out.printIn();     try { Line 20:      OutputStream os = new         FileOutputStream("test.txt");       HessianOutput out = new HessianOutput(os);       out.writeObject((Object)vec);       os.close();     } catch(Exception e) {}     vec.clear();     try {       InputStream is = new FilelnputStreamC'test.txt"); Line 30:      Hessianlnput in = new Hessianlnput(is);       vec = (Vector)in.readobject(null);       is.close();     } catch(Exception e) {}     forfint i=0;i<10;i++) {       System.out.print(vec.elementAt(i) + " ");     }     System.out.printIn();   } Line 40:} 
end example

The code in Listing 9.7 begins by creating a Vector object and populating it with 10 Integer objects, each containing a random integer value. At the same time the objects are added to the vector, the integer values are displayed on the console. Once the Vector has been populated, the serialization process begins. The Object is serialized and saved in a file called test.txt on the local drive.

Line 20 begins the process by creating a FileOutputStream object with the filename you want to use. On line 22, a HessianOutput object is created, using the FileOutputStream object as a parameter. The HessianOutput object performs the serialization of an object using its writeObject() method, as shown in line 23. The Vector object is cast to an Object and supplied as an argument to the writeObject() method. This method converts the provided Object into a format that can be easily saved in a file, transmitted on the Internet, or put into a database. Line 24 closes the previously open file.

Next, the Vector object is cleared of all stored values. The code begins the deserialization process by creating a FileInputStream object to the file just saved with the serialized Vector object. On line 30, a HessianInput object is instantiated to handle the deserialization process. The Vector object is pulled from the file and brought back to its original state using the readObject() method. The file you were using is closed, and the contents of the new object are displayed. As you can see in Figure 9.5, the original Vector and the object pulled from the file are identical. The figure also shows the original compile and execution statements used in the process.

click to expand
Figure 9.5: Hessian serialization.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net