home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / rmi / activation / code / MyPersistentClass.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  4.4 KB  |  134 lines

  1. /*
  2.  * Copyright (c) 1998, 1999 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  5.  * modify and redistribute this software in source and binary code form,
  6.  * provided that i) this copyright notice and license appear on all copies of
  7.  * the software; and ii) Licensee does not utilize the software in a manner
  8.  * which is disparaging to Sun.
  9.  *
  10.  * This software is provided "AS IS," without a warranty of any kind. ALL
  11.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  12.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  13.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  14.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  15.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  16.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  17.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  18.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  19.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  20.  * POSSIBILITY OF SUCH DAMAGES.
  21.  *
  22.  * This software is not designed or intended for use in on-line control of
  23.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  24.  * the design, construction, operation or maintenance of any nuclear
  25.  * facility. Licensee represents and warrants that it will not use or
  26.  * redistribute the Software for such purposes.
  27.  */
  28. package examples.activation; 
  29.  
  30. import java.io.*;
  31. import java.rmi.*;
  32. import java.rmi.activation.*;
  33. import java.util.Vector;
  34.  
  35. public class MyPersistentClass extends Activatable
  36.     implements examples.activation.YetAnotherRemoteInterface {
  37.  
  38.     private Vector transactions;
  39.     private File holder;
  40.  
  41.     // The constructor for activation and export; this constructor is
  42.     // called by the method ActivationInstantiator.newInstance during
  43.     // activation, to construct the object.
  44.     //
  45.     public MyPersistentClass(ActivationID id, MarshalledObject data) 
  46.     throws RemoteException, ClassNotFoundException, java.io.IOException {
  47.  
  48.     super(id, 0);
  49.  
  50.         // Extract the File object from the MarshalledObject that was 
  51.         // passed to the constructor 
  52.     //
  53.     holder = (File)data.get();
  54.  
  55.     if (holder.exists()) {
  56.         // Use the MarshalledObject to restore my state
  57.         //
  58.         this.restoreState();
  59.     } else {
  60.         transactions = new Vector(1,1);
  61.         transactions.addElement("Initializing transaction vector");
  62.     }
  63.     }
  64.  
  65.     // Define the method declared in AnotherRemoteInterface
  66.     //
  67.     public Vector calltheServer(Vector v) throws RemoteException {
  68.  
  69.     int limit = v.size();
  70.     for (int i = 0; i < limit; i++) {
  71.         transactions.addElement(v.elementAt(i));
  72.     }
  73.  
  74.     // Save this object's data out to file
  75.     //
  76.     this.saveState();
  77.     return transactions;
  78.     }
  79.  
  80.     public Vector getTransactions() {
  81.     return transactions;
  82.     }
  83.  
  84.  
  85.     // If the MarshalledObject that was passed to the constructor was
  86.     // a file, then use it to recover the vector of transaction data
  87.     //
  88.     private void restoreState() throws IOException, ClassNotFoundException {
  89.     File f = holder;
  90.     FileInputStream fis = new FileInputStream(f);
  91.         ObjectInputStream ois = new ObjectInputStream(fis);
  92.         transactions = (Vector)ois.readObject();
  93.         ois.close();
  94.     }
  95.  
  96.     private void saveState() {
  97.  
  98.         FileOutputStream fos = null;
  99.         ObjectOutputStream oos = null;
  100.  
  101.     try {
  102.         File f  = holder;
  103.         try {
  104.             //FileOutputStream fos = new FileOutputStream(f);
  105.          fos = new FileOutputStream(f);
  106.         } catch (IOException e1) {
  107.         e1.printStackTrace(); // NEW LINE
  108.             throw new RuntimeException("Error creating FileOutputStream");
  109.         }
  110.         try {
  111.             //ObjectOutputStream oos = new ObjectOutputStream(fos);
  112.         oos = new ObjectOutputStream(fos);
  113.         } catch (IOException e2) {
  114.             throw new RuntimeException("Error creating ObjectOutputStream");
  115.         }
  116.         try {
  117.         oos.writeObject(getTransactions());
  118.         } catch (IOException e3) {
  119.             throw new RuntimeException("Error writing Vector");
  120.         }
  121.         try {
  122.         oos.close();
  123.         } catch (IOException e3) {
  124.             throw new RuntimeException("Error closing stream");
  125.         }
  126.  
  127.     } catch (SecurityException e4) {
  128.         throw new RuntimeException("Security Problem");
  129. //    } catch (Exception e) {
  130. //        throw new RuntimeException("Error saving vector of data");
  131.     }
  132.     }
  133. }
  134.