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.jythonWeb;
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_loadButton;
042: private TextArea m_textArea;
043:
044: private synchronized void waitForLoadRequest() {
045: try {
046: wait();
047: } catch (InterruptedException e) {
048: }
049: }
050:
051: private synchronized void loadScriptNotify() {
052: notify();
053: }
054:
055: public void init() {
056: m_textArea = new TextArea(10, 80);
057: m_textArea.setEditable(false);
058:
059: m_loadButton = new Button("Get & Run Jython Script from Server");
060: m_loadButton.addActionListener(this );
061:
062: setLayout(new BorderLayout());
063: add("South", m_textArea);
064: add("North", m_loadButton);
065: }
066:
067: public void run() {
068: try {
069: org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase
070: .open("http://" + getCodeBase().getHost() + ":"
071: + PORT, USERNAME, PASSWORD);
072: //org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("https://" + getCodeBase().getHost() + ":" + PORT, USERNAME, PASSWORD);
073:
074: Script script = (Script) db.getRoot("Script");
075:
076: if (script == null) {
077: script = (Script) db.createRoot("Script",
078: "org.myoodb.objects.ScriptDbImpl");
079: //script = (Script) db.createRoot("Script", ScriptDbImpl.class);
080: }
081:
082: // XXX: First teach Jython about the package that allows for dynamic package import
083: org.python.util.PythonInterpreter interpreter = new org.python.util.PythonInterpreter();
084: org.python.core.PySystemState
085: .add_package("org.python.core"); // allows for "import org.python.core.PySystemState"
086:
087: while (m_thread != null) {
088: waitForLoadRequest();
089:
090: String code = script.getCode("WebExample.py");
091: m_textArea.setText(code);
092:
093: interpreter.exec(code);
094: }
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: }
099:
100: public void start() {
101: if (m_thread == null) {
102: m_thread = new Thread(this );
103: m_thread.setDaemon(true);
104: m_thread.start();
105: }
106: }
107:
108: public void stop() {
109: if (m_thread != null) {
110: m_thread = null;
111: loadScriptNotify();
112: }
113: }
114:
115: public void actionPerformed(ActionEvent evt) {
116: loadScriptNotify();
117: }
118: }
|