How to use ObjectInputStream and AgoObjectInputStream to serialize and deserialize a business or utility object.
See the chapter on Business Object Basics in the Programmer's Guide |
If you need to de-serialize a SilverStream-specific object like a triggered or utility business object, a page, or a Hashtable that contains a utility business object, you should use the AgoObjectInputStream class instead of the standard Java ObjectInputStream.
You can use the regular java.io.ObjectOutputStream to save the Object, but you must use an AgoObjectInputStream to deserialize it. Otherwise you will experience problems loading the object classes.
The following example shows how to write a value to a database table called Objects, then how to deserialize it by calling AgoObjectInputStream. The example has the following components:
Objects Table
The table called Objects has these two columns:
Name VARCHAR(20) Object LONGVARBINARY
Serial utility object
Utility object called Serial. It has one member variable and two methods. The methods are defined in the User Code as follows:
int m_i=5; // default value of 5
public void setInt(int i) { m_i = i; } public int getInt() { return m_i; }
The page calls the setInt()
method to set a value to pass to the saveserarg business object, described in the next section.
saveserarg
This object is used to extract and save the data. It is passed a Hashtable from which it extracts the values and saves them in a row in the Objects table using an AgaData. The code uses ObjectOutputStream to save the object.
The saveserarg expects a Hashtable with two entries keyed by name and object. The name is a String and object is a Serial. All of its code is in the invoked()
method, as shown here:
Serializable s = evt.getParameter(); System.out.println("getParameter returns "+s); Hashtable ht = (Hashtable)s; String name = (String)ht.get("name"); Serial ser = (Serial)ht.get("object"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(ser); byte[] buf = baos.toByteArray(); try { AgaData obj = evt.getAgaData("obj"); obj.insertBefore(); obj.setProperty("name", name); obj.setProperty("object", buf); obj.updateRows(); } catch (Exception x) { x.printStackTrace(); } return;
getserarg
This object is used to get the data from the table and deserialize it. It is passed a String which it uses as a name to lookup in the Objects table via an AgaData. It gets the serialized object in the Object column, deserializes it, and prints the value.
The getserarg business object expects it's parameter to be a String. It uses the following import line:
import com.sssw.shr.util.AgoObjectInputStream;
The rest of the code is in the invoked()
method as follows:
Serializable s = evt.getParameter(); System.out.println("getParameter returns "+s); String name = (String)s; try { AgaData obj = evt.getAgaData("obj"); obj.query("Objects.Name = '"+name+"'"); if (obj.gotoFirst()) { byte[] buf = (byte[])obj.getProperty("object"); ByteArrayInputStream bais = newByteArrayInputStream(buf); AgoObjectInputStream in = null; in = new AgoObjectInputStream(bais, getClass().getClassLoader()); Object o = in.readObject(); Serial ser = (Serial)o; evt.setResult(ser); } } catch (Exception x) { x.printStackTrace(); } return;
For this example, a page is used to invoke the business objects. The page has two buttons on it (Button1 and Button2) called saveserarg and getserarg respectively. The pageActionPerformed event for Button1 is coded as follows:
{ Serial S = new Serial(); s.setInt(6); Hashtable ht = new Hashtable(); ht.put("name", "tom"); ht.put("object", s); invokeBusinessObject("saveserarg", ht); }
The pageActionPerformed event for Button2 is coded as follows:
{ Serial s = (Serial)invokeBusinessObject("getserarg", "tom"); }
To populate the Objects table, push Button1 once and the values "tom" and 6 are written to it. If you change the hard-coded values to "bob" and default integer value in the serial to 5 and push Button1 again, then a second row is added.
You can deserialize the values by pushing Button2. The first time you see 6 printed to the server console. If you change the hard coded value in Button2's code to "tom" and push Button2 again, you should see 5 printed to the server console.