home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / Data.java < prev    next >
Text File  |  1998-11-05  |  7KB  |  193 lines

  1. // Copyright (c) 1997, 1998 Symantec, Inc. All Rights Reserved.
  2.  
  3. import java.io.Serializable;
  4. import java.util.Vector;
  5.  
  6. import java.io.FileInputStream;
  7. import java.io.ObjectInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.ObjectOutputStream;
  10.  
  11. /*
  12. This class contains all of the non-UI program global data (notice: no import
  13. of "java.awt").
  14. Only one instance of this class is created.
  15. This class (minus some potentially large vectors/arrays) is saved to disk
  16. as the program's "ini" data. The Java serializable mechanism is used to
  17. do this.
  18. */
  19. public class Data implements Serializable {
  20.     // the filename of this program's "ini" file
  21.     static final String INI_FILENAME = "WLA.dat";
  22.     // The only data instance
  23.     static Data theData = null;
  24.     // Program Options
  25.     int userSessionIdleLimit = 30;  // Delay in minutes for a user session to be considered "over"
  26.     boolean ignoreUnexpectedLogFileErrors = false;
  27.     // All of the reports
  28.     Vector reports = new Vector();
  29.     // The index of the currently selected report, or -1 if non selected
  30.     int currentReportIndex = -1;
  31.  
  32.     /*
  33.     Constructs the default Data object.
  34.     */
  35.     public Data() {
  36.         theData = this;
  37.             //{{INIT_CONTROLS
  38.         //}}
  39. }
  40.  
  41.     // Returns the one-and-only instance of this class
  42.     public static Data getDataInstance() {
  43.         return theData;
  44.     }
  45.     // Notes the one-and-only instance of this class
  46.     public static void setDataInstance(Data data) {
  47.         theData = data;
  48.     }
  49.  
  50.     /*
  51.     Re-creates the data instance previously saved to disk using 
  52.     saveDataInstance(). If no data has been saved or an error occurs, a default
  53.     data object is created.
  54.     Call getDataInstance() after this method to get the loaded/created data.
  55.     Returns null if all OK, otherwise error message.
  56.     */
  57.     public static String loadDataInstance() {
  58.         String msg = null;
  59.         Data data = null;
  60.         try {
  61.             // get typical input stream
  62.             FileInputStream istream = istream = new FileInputStream(INI_FILENAME);
  63.             try {
  64.                 // use input stream as an object input stream (java serialization)
  65.                 ObjectInputStream p = new ObjectInputStream(istream);
  66.                 try {
  67.                     // read the saved Data object
  68.                     data = (Data)p.readObject();
  69.                 } catch(java.lang.ClassNotFoundException x) {
  70.                     msg = x.toString();
  71.                 } catch(java.io.OptionalDataException x) {
  72.                     msg = x.toString();
  73.                 }
  74.                 // close the object input stream
  75.                 p.close();
  76.             } catch(java.io.StreamCorruptedException x) {
  77.                 msg = x.toString();
  78.             }
  79.             istream.close();
  80.         } catch(java.io.FileNotFoundException x) {
  81.             // can't find the program data file...(OK)
  82.         } catch(java.io.IOException x) {
  83.             // trouble reading program data file...notify user
  84.             msg = x.toString();
  85.         }
  86.         // If couldn't load, create defaults
  87.         if(data == null) {
  88.             // Couldn't read data, create new with default values
  89.             data = new Data();
  90.         } else {
  91.             // Set static that points to the one instance of program data
  92.             Data.setDataInstance(data);
  93.         }
  94.         return msg;
  95.     }        
  96.  
  97.     /**
  98.     Saves the current data object to disk.
  99.     The program data is saved using the Java serialization feature.
  100.     Potentially large vectors/arrays are freed before saving.
  101.     Returns null if all OK, otherwise error message.
  102.     */
  103.     public static String saveDataInstance() {
  104.         // Don't save the logFile.records Vector, and other bulky non-essentials
  105.         freeUpMem();
  106.         // save program data
  107.         String msg = null;
  108.         try {
  109.             // get a typical output stream for writing to a file
  110.             FileOutputStream ostream = ostream = new FileOutputStream(INI_FILENAME);
  111.             try {
  112.                 // use output stream as an object output stream (java serialization)
  113.                 ObjectOutputStream p = new ObjectOutputStream(ostream);
  114.                 try {
  115.                     // write the data
  116.                     p.writeObject(getDataInstance());
  117.                 } catch(java.io.OptionalDataException x) {
  118.                     msg = x.toString();
  119.                 }
  120.                 // close the object output stream
  121.                 p.close();
  122.             } catch(java.io.StreamCorruptedException x) {
  123.                 msg = x.toString();
  124.             }
  125.             ostream.close();
  126.         } catch(java.io.IOException x) {
  127.             x.printStackTrace();
  128.             msg = x.toString();
  129.         }
  130.         return msg;
  131.     }
  132.  
  133.     /*
  134.     Frees up potentially large vectors/arrays in this object and child objects.
  135.     */
  136.     public static void freeUpMem() {
  137.         Data data = getDataInstance();
  138.         int r = data.reports.size();
  139.         while(--r >= 0) {
  140.             Report report = data.getReport(r);
  141.             report.freeUpMem();
  142.         }
  143.         System.gc();
  144.     }
  145.  
  146.     // Returns true if a report is currently selected.
  147.     public boolean isReportSelected() {
  148.         return currentReportIndex >= 0;
  149.     }
  150.  
  151.     // Returns the report at the specified index.
  152.     public Report getReport(int index) {
  153.         try {
  154.             Report report = (Report)reports.elementAt(index);
  155.             return report;
  156.         } catch(java.lang.ArrayIndexOutOfBoundsException x) {
  157.             WLAUtil.fatalProgramError(x);
  158.             return null;
  159.         }
  160.     }
  161.     
  162.     // Returns the current report, or null if none
  163.     public Report getCurrentReport() {
  164.         Report report = null;
  165.         try {
  166.             report = (Report)reports.elementAt(currentReportIndex);
  167.         } catch(java.lang.ArrayIndexOutOfBoundsException x) {
  168.         }
  169.         return report;
  170.     }
  171.     
  172.     // Creates a new Report object and adds it to the vector of reports.
  173.     public Report newReport() {
  174.         Report report = new Report();
  175.         reports.addElement(report);
  176.         return report;
  177.     }
  178.  
  179.     // Removes the report at the specified index from the vector of reports.
  180.     public void disposeReport(int idx) {
  181.         reports.removeElementAt(idx);
  182.     }
  183.     
  184.     // Removes the given report from the vector of reports.
  185.     // Returns true if removed OK.
  186.     public boolean disposeReport(Report report) {
  187.         boolean bRemoved = reports.removeElement(report);
  188.         return bRemoved;
  189.     }
  190.     //{{DECLARE_CONTROLS
  191.     //}}
  192. }
  193.