home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / PersistentCounter.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  2.0 KB  |  91 lines

  1. // /os/dynamap/java/COM/odi/demo/pcount/PersistentCounter.java -- A simple
  2. // persistent counter to count the number of visits to a page.  Can be run as
  3. // an application or an applet.
  4.  
  5. package COM.odi.demo.pcount;
  6.  
  7. import java.awt.Graphics;
  8. import java.applet.Applet;
  9. import COM.odi.*;
  10.  
  11. final
  12. public class PersistentCounter extends Applet {
  13.  
  14.   // When run as an application.
  15.  
  16.   public static void main(String argv[]) {
  17.     PersistentCounter counter = new PersistentCounter();
  18.     counter.init(System.getProperty("server"), System.getProperty("db"));
  19.     counter.noteVisit();
  20.     System.out.println("Counter value = " + counter.visits);
  21.   }
  22.  
  23.   // When run as an applet.
  24.  
  25.   public void init() {
  26.     init(getParameter("server"), getParameter("db"));
  27.   }
  28.  
  29.   public void start() {
  30.     noteVisit();
  31.   }
  32.  
  33.   public void destroy() {
  34.     db.close();
  35.   }
  36.  
  37.   public String[][] getParameterInfo() {
  38.     return parameterInfo;
  39.   }
  40.  
  41.   public void paint(Graphics g) {
  42.  
  43.     // Display the number of visits on the page.
  44.  
  45.     g.drawString("This page has been visited "
  46.          + visits
  47.          + ((visits == 1) ? " time." : " times."),
  48.          25,
  49.          25);
  50.   }
  51.  
  52.   // Private methods.
  53.  
  54.   void init(String serverName, String dbName) {
  55.     if (dbName == null)
  56.       dbName = "pcount.odb";
  57.  
  58.     ObjectStore.initialize(serverName, null);
  59.     try {
  60.       db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
  61.     } catch(DatabaseNotFoundException exception) {
  62.       db = Database.create(dbName, 0664);
  63.     }
  64.   }
  65.  
  66.   void noteVisit() {
  67.     Transaction transaction = Transaction.begin(ObjectStore.UPDATE);
  68.     try {
  69.       visits = ((Integer)db.getRoot("visits")).intValue();
  70.       visits++;
  71.       db.setRoot("visits", new Integer(visits));
  72.     } catch(DatabaseRootNotFoundException exception) {
  73.       visits = 1;
  74.       db.createRoot("visits", new Integer(visits));
  75.     }
  76.     transaction.commit();
  77.   }
  78.  
  79.   // Fields
  80.  
  81.   int visits;
  82.   Database db;
  83.  
  84.   // Constants
  85.  
  86.   static String parameterInfo[][] = {
  87.     {"server", "String", "The name of the DMA ObjectStore server"},
  88.     {"db", "String", "The name of the database"}
  89.   };
  90. }
  91.