KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > jythonWeb > Client


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.jythonWeb;
25
26 import java.awt.*;
27 import java.awt.event.*;
28
29 // TODO: don't be lazy and use swing
30

31 public class Client extends java.applet.Applet JavaDoc implements Runnable JavaDoc, ActionListener
32 {
33     public static int PORT = 80;
34     //public static int PORT = 443;
35
public static String JavaDoc USERNAME = "admin";
36     public static String JavaDoc PASSWORD = "admin";
37
38     private Thread JavaDoc m_thread;
39     private Button m_loadButton;
40     private TextArea m_textArea;
41
42     private synchronized void waitForLoadRequest()
43     {
44     try
45         {
46             wait();
47         }
48         catch (InterruptedException JavaDoc e)
49         {
50         }
51     }
52
53     private synchronized void loadScriptNotify()
54     {
55     notify();
56     }
57
58     public void init()
59     {
60         m_textArea = new TextArea(10, 80);
61         m_textArea.setEditable(false);
62
63     m_loadButton = new Button("Get & Run Jython Script from Server");
64     m_loadButton.addActionListener(this);
65
66         setLayout(new BorderLayout());
67     add("South", m_textArea);
68     add("North", m_loadButton);
69     }
70
71     public void run()
72     {
73         try
74         {
75             org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("myooweb://" + getCodeBase().getHost() + ":" + PORT, USERNAME, PASSWORD);
76             //org.myoodb.MyOodbDatabase db = org.myoodb.MyOodbDatabase.open("myoowebs://" + getCodeBase().getHost() + ":" + PORT, USERNAME, PASSWORD);
77

78             Script script = (Script) db.getRoot("Script");
79
80             if (script == null)
81             {
82                 script = (Script) db.createRoot(ScriptDbImpl.class, "Script");
83                 //script = (Script) db.createRoot(ScriptDbImpl, "Script");
84
}
85
86             // XXX: First teach Jython about the package that allows for dynamic package import
87
org.python.util.PythonInterpreter interpreter = new org.python.util.PythonInterpreter();
88             org.python.core.PySystemState.add_package("org.python.core"); // allows for "import org.python.core.PySystemState"
89

90             while (m_thread != null)
91             {
92                 waitForLoadRequest();
93
94                 String JavaDoc code = script.getCode("WebExample.py");
95                 m_textArea.setText(code);
96
97                 interpreter.exec(code);
98             }
99         }
100         catch (Exception JavaDoc e)
101         {
102             e.printStackTrace();
103         }
104     }
105
106     public void start()
107     {
108         if (m_thread == null)
109         {
110             m_thread = new Thread JavaDoc(this);
111             m_thread.setDaemon(true);
112             m_thread.start();
113         }
114     }
115
116     public void stop()
117     {
118         if (m_thread != null)
119         {
120             m_thread = null;
121             loadScriptNotify();
122         }
123     }
124
125     public void actionPerformed(ActionEvent evt)
126     {
127         loadScriptNotify();
128     }
129 }
130
Popular Tags