home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 2.0 KB | 91 lines |
- // /os/dynamap/java/COM/odi/demo/pcount/PersistentCounter.java -- A simple
- // persistent counter to count the number of visits to a page. Can be run as
- // an application or an applet.
-
- package COM.odi.demo.pcount;
-
- import java.awt.Graphics;
- import java.applet.Applet;
- import COM.odi.*;
-
- final
- public class PersistentCounter extends Applet {
-
- // When run as an application.
-
- public static void main(String argv[]) {
- PersistentCounter counter = new PersistentCounter();
- counter.init(System.getProperty("server"), System.getProperty("db"));
- counter.noteVisit();
- System.out.println("Counter value = " + counter.visits);
- }
-
- // When run as an applet.
-
- public void init() {
- init(getParameter("server"), getParameter("db"));
- }
-
- public void start() {
- noteVisit();
- }
-
- public void destroy() {
- db.close();
- }
-
- public String[][] getParameterInfo() {
- return parameterInfo;
- }
-
- public void paint(Graphics g) {
-
- // Display the number of visits on the page.
-
- g.drawString("This page has been visited "
- + visits
- + ((visits == 1) ? " time." : " times."),
- 25,
- 25);
- }
-
- // Private methods.
-
- void init(String serverName, String dbName) {
- if (dbName == null)
- dbName = "pcount.odb";
-
- ObjectStore.initialize(serverName, null);
- try {
- db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
- } catch(DatabaseNotFoundException exception) {
- db = Database.create(dbName, 0664);
- }
- }
-
- void noteVisit() {
- Transaction transaction = Transaction.begin(ObjectStore.UPDATE);
- try {
- visits = ((Integer)db.getRoot("visits")).intValue();
- visits++;
- db.setRoot("visits", new Integer(visits));
- } catch(DatabaseRootNotFoundException exception) {
- visits = 1;
- db.createRoot("visits", new Integer(visits));
- }
- transaction.commit();
- }
-
- // Fields
-
- int visits;
- Database db;
-
- // Constants
-
- static String parameterInfo[][] = {
- {"server", "String", "The name of the DMA ObjectStore server"},
- {"db", "String", "The name of the database"}
- };
- }
-