001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.simpleWeb;
025:
026: import java.awt.*;
027: import java.awt.event.*;
028:
029: import org.myoodb.objects.*;
030:
031: // TODO: don't be lazy and use swing
032:
033: public class Client extends java.applet.Applet implements Runnable,
034: ActionListener {
035: public static int PORT = 80;
036: //public static int PORT = 443;
037: public static String USERNAME = "admin";
038: public static String PASSWORD = "admin";
039:
040: private Thread m_thread;
041: private Button m_addButton;
042: private TextArea m_textArea;
043:
044: private synchronized void waitForAddPersonRequest() {
045: try {
046: wait();
047: } catch (InterruptedException e) {
048: }
049: }
050:
051: private synchronized void addPersonNotify() {
052: notify();
053: }
054:
055: public void init() {
056: m_textArea = new TextArea(60, 60);
057: m_textArea.setEditable(false);
058:
059: m_addButton = new Button("Add Person");
060: m_addButton.addActionListener(this );
061:
062: setLayout(new BorderLayout());
063:
064: add("Center", m_textArea);
065: add("South", m_addButton);
066: }
067:
068: public void run() {
069: try {
070: org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase
071: .open("http://" + getCodeBase().getHost() + ":"
072: + PORT, USERNAME, PASSWORD);
073: //org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("https://" + getCodeBase().getHost() + ":" + PORT, USERNAME, PASSWORD);
074:
075: org.myoodb.collectable.LinkedList root = (org.myoodb.collectable.LinkedList) db
076: .getRoot("Persons");
077: if (root == null) {
078: root = (org.myoodb.collectable.LinkedList) db
079: .createRoot("Persons",
080: "org.myoodb.collectable.LinkedListDbImpl");
081: //root = (org.myoodb.collectable.LinkedList) db.createRoot("Persons", org.myoodb.collectable.LinkedListDbImpl.class);
082: }
083:
084: while (m_thread != null) {
085: waitForAddPersonRequest();
086:
087: Person person = (Person) db
088: .createObject("org.myoodb.objects.PersonDbImpl");
089: //Person person = (Person) db.createObject(org.myoodb.objects.PersonDbImpl.class);
090:
091: person.setName("John Smith the " + root.size());
092: root.add(person);
093:
094: System.out
095: .println("New persons in database: " + person);
096: m_textArea.append("New persons in database: " + person
097: + "\n");
098:
099: java.util.Iterator iter = root.toArrayList().iterator();
100: while (iter.hasNext()) {
101: person = (Person) iter.next();
102:
103: long start = System.currentTimeMillis();
104: String name = person.getName();
105: long stop = System.currentTimeMillis();
106:
107: System.out
108: .println(" - Existing persons in database: "
109: + name
110: + " / getTime:"
111: + (stop - start));
112: m_textArea
113: .append(" - Existing persons in database: "
114: + name
115: + " / getTime: "
116: + (stop - start) + "\n");
117: }
118: }
119: } catch (Exception e) {
120: e.printStackTrace();
121: }
122: }
123:
124: public void start() {
125: if (m_thread == null) {
126: m_thread = new Thread(this );
127: m_thread.setDaemon(true);
128: m_thread.start();
129: }
130: }
131:
132: public void stop() {
133: if (m_thread != null) {
134: m_thread = null;
135: addPersonNotify();
136: }
137: }
138:
139: public void actionPerformed(ActionEvent evt) {
140: addPersonNotify();
141: }
142: }
|