source: trunk/gsdl3/src/java/org/greenstone/gsdl3/build/StatusDisplay.java@ 3694

Last change on this file since 3694 was 3694, checked in by kjdon, 21 years ago

changed comment

  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1package org.greenstone.gsdl3.build;
2
3import java.applet.Applet;
4import java.awt.*;
5import java.awt.event.*;
6
7import org.greenstone.gsdl3.util.*;
8import org.w3c.dom.Element;
9import org.w3c.dom.Document;
10import org.xml.sax.InputSource;
11import org.apache.xerces.parsers.DOMParser;
12import java.net.URL;
13import java.io.DataInputStream;
14
15
16/** StatusDisplay - a java applet thats sends a message at regular intervals
17 * to a pre specified address (the library param) and then displays the
18 * status messages received.
19 * to use, embed in a web page using something like: (note the cgi args may differ - see process.xsl for details )
20 * <applet code="org.greenstone.gsdl3.build.StatusDisplay.class" codebase='lib/java' archive='gsdl3.jar, xercesImpl.jar, jaxp.jar, xml-apis.jar' width='537' height='100'>The status display applet.<param name='library' value='mylibrary?a=pr&rt=s&c=build&o=xml&ro=1&s=ImportCollection&l=en&coll=demo'/></applet>
21 * the cgi params for the library param are dependent on what action/service
22 * is using the applet
23 */
24public class StatusDisplay extends Applet
25 implements Runnable {
26
27 /** the display area for the status messages */
28 protected TextArea display_;
29 /** how long to delay between requests */
30 protected int delay_=3000; // 3 secs
31 /** the thread used */
32 protected Thread updatorThread_;
33 /** the url for the requests */
34 protected URL status_cgi_ = null;
35 /** if true, stops sending requests, and cant be unset */
36 protected boolean completed_ = false;
37 // init is called if you leave the page and go back - need to somehow retrieve all the previous messages that have been obtained cos we may have left the page and gone back to it during an import or something. dont want to lose all the info.
38 public void init() {
39 //Create the text field and make it uneditable.
40 display_ = new TextArea();
41 display_.setEditable(false);
42 display_.setBackground(Color.white);
43 //Set the layout manager so that the text field will be
44 //as wide as possible.
45 setLayout(new java.awt.GridLayout(1,0));
46
47 //Add the text field to the applet.
48 add(display_);
49 validate();
50
51 // add the initial status text
52 String initial_text = parameterValue("initial_text");
53 addItem(initial_text);
54
55 // check the initial error state - there may be no more messages to come
56 completed_ = GSStatus.isCompleted(Integer.parseInt(parameterValue("initial_code")));
57
58 // we always do the same query
59 String library = parameterValue("library");
60 library = tidy_URL(library);
61 try {
62 status_cgi_ = new URL(library);
63 } catch (Exception e) {
64 System.out.println("Error creating URL for "+library+": "+e.getMessage());
65 }
66 }
67
68 public void start() {
69 if (completed_) {
70 // do nothing
71 } else {
72 if (updatorThread_ == null) {
73 updatorThread_ = new Thread(this);
74 }
75 updatorThread_.start();
76 }
77 }
78 public void stop() {
79 updatorThread_ = null;
80 }
81 public void run() {
82
83 if (completed_) { // do nothing. dont know if we need this here
84 return;
85 }
86 //Just to be nice, lower this thread's priority
87 //so it can't interfere with other processing going on.
88 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
89
90 //Remember which thread we are.
91 Thread currentThread = Thread.currentThread();
92
93 //This is the loop.
94 while (currentThread == updatorThread_ && !completed_) {
95 queryServer();
96 display_.repaint();
97
98 //Delay for the specified time
99 try {
100 Thread.sleep(delay_);
101 } catch (InterruptedException e) {
102 break;
103 }
104 }
105 }
106
107 /** add some more text to the display */
108 void addItem(String new_text) {
109 String t = display_.getText()+"\n"+new_text;
110 display_.setText(t);
111 display_.setCaretPosition(t.length()); // so that you always see the
112 // most recent entries - ie so that it scrolls down automatically
113 repaint();
114 }
115
116 void queryServer() {
117
118 // Send the query
119 System.out.println("sending query: " + status_cgi_); // this appears in the java console
120 try {
121 DataInputStream in = new DataInputStream(status_cgi_.openStream());
122 DOMParser parser = new DOMParser();
123 parser.parse(new InputSource(in));
124 Document data_doc = parser.getDocument();
125 Element data_elem = data_doc.getDocumentElement();
126 Element status_elem = (Element)GSXML.getChildByTagName(data_elem, GSXML.STATUS_ELEM);
127 if (status_elem != null) {
128 String status = status_elem.getAttribute(GSXML.STATUS_ERROR_CODE_ATT);
129 System.out.println("received status "+status);
130 completed_ = GSStatus.isCompleted(Integer.parseInt(status));
131 addItem(GSXML.getNodeText(status_elem));
132 } else {
133 System.out.println("Error - no status receieved");
134 completed_=true;
135 }
136 in.close();
137 } catch (Exception e) {
138 System.err.println( "Error sending query ("+status_cgi_+"): " + e);
139 }
140
141 }
142
143 // Get a REQUIRED string. Stop the applet if we cannot.
144 String parameterValue(String name) {
145 try {
146 return getParameter(name);
147 } catch (Exception e) {
148 System.err.println("StatusDisplay: you must give a parameter for \""+ name + "\". Stopping.");
149 stop();
150 }
151 return "";
152 }
153
154 /**Tidy up URLs
155 *
156 * Ensure a URL address (as string) has a protocol, host, and file.
157 */
158 String tidy_URL(String address) {
159
160 // make sure the URL has protocol, host, and file
161 if (address.startsWith("http")) {
162 // the address has all the necessary components
163 } else if (address.startsWith("/")) {
164 // there is not protocol and host
165 URL document = getDocumentBase();
166 String port = "";
167 if (document.getPort()!=-1) {
168 port = ":" + document.getPort();
169 }
170 address = "http://" + document.getHost() + port + address;
171 } else {
172 // this URL is relative to the directory the document is in
173 URL document = getDocumentBase();
174 String directory = document.getFile();
175 int end = directory.lastIndexOf('/');
176 String port = "";
177 if (document.getPort()!=-1) {
178 port = ":" + document.getPort();
179 }
180 directory = directory.substring(0,end + 1);
181 address = "http://" + document.getHost() + port + directory + address;
182
183 }
184
185 return address;
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.