// Copyright © 1998 Sun Microsystems, Inc. All Rights Reserved. // package examples.activation; import java.io.*; import java.rmi.*; import java.rmi.activation.*; import java.util.Vector; public class MyPersistentClass extends Activatable implements examples.activation.YetAnotherRemoteInterface { private Vector transactions; private File holder; // The constructor for activation and export; this constructor is // called by the method ActivationInstantiator.newInstance during // activation, to construct the object. // public MyPersistentClass(ActivationID id, MarshalledObject data) throws RemoteException, ClassNotFoundException, java.io.IOException { super(id, 0); // Extract the File object from the MarshalledObject that was // passed to the constructor // holder = (File)data.get(); if (holder.exists()) { // Use the MarshalledObject to restore my state // this.restoreState(); } else { transactions = new Vector(1,1); transactions.addElement("Initializing transaction vector"); } } // Define the method declared in AnotherRemoteInterface // public Vector calltheServer(Vector v) throws RemoteException { int limit = v.size(); for (int i = 0; i < limit; i++) { transactions.addElement(v.elementAt(i)); } // Save this object's data out to file // this.saveState(); return transactions; } public Vector getTransactions() { return transactions; } // If the MarshalledObject that was passed to the constructor was // a file, then use it to recover the vector of transaction data // private void restoreState() throws IOException, ClassNotFoundException { File f = holder; FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); transactions = (Vector)ois.readObject(); ois.close(); } private void saveState() { FileOutputStream fos = null; ObjectOutputStream oos = null; try { File f = holder; try { //FileOutputStream fos = new FileOutputStream(f); fos = new FileOutputStream(f); } catch (IOException e1) { e1.printStackTrace(); // NEW LINE throw new RuntimeException("Error creating FileOutputStream"); } try { //ObjectOutputStream oos = new ObjectOutputStream(fos); oos = new ObjectOutputStream(fos); } catch (IOException e2) { throw new RuntimeException("Error creating ObjectOutputStream"); } try { oos.writeObject(getTransactions()); } catch (IOException e3) { throw new RuntimeException("Error writing Vector"); } try { oos.close(); } catch (IOException e3) { throw new RuntimeException("Error closing stream"); } } catch (SecurityException e4) { throw new RuntimeException("Security Problem"); // } catch (Exception e) { // throw new RuntimeException("Error saving vector of data"); } } }