source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/admin/gui/ThreadControl.java@ 9972

Last change on this file since 9972 was 9972, checked in by chi, 19 years ago

The initial version of GAI applications.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the GAI application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: Chi-Yu Huang, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.admin.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import java.net.*;
43import javax.swing.*;
44import javax.swing.event.*;
45
46import org.greenstone.admin.GAI;
47import org.greenstone.admin.gui.LogPane;
48import org.greenstone.core.ParsingProgress;
49import org.apache.tools.ant.*;
50import org.apache.tools.ant.helper.ProjectHelperImpl;
51
52
53public class ThreadControl
54 extends Thread {
55
56 //boolean ant_success = false;
57
58 public ThreadControl(){
59 }
60
61 public void run(){
62 }
63
64
65 public void executeAntTarget(String command_string) throws IOException {
66 Project ant = new Project();
67 File buildFile = new File (GAI.gsdl3_path + File.separator + "build.xml");
68
69 //Set up a PrintStream pointing at a file
70 //FileOutputStream ant_out =
71 // new FileOutputStream(GAI.gsdl3_path + File.separator + "ant_out.log");
72 //FileOutputStream ant_err =
73 // new FileOutputStream(GAI.gsdl3_path + File.separator + "ant_err.log");
74
75 // BufferedOutputStream ant_out_buffer =
76 // new BufferedOutputStream(ant_out, 1024);
77 //BufferedOutputStream ant_err_buffer =
78 /// new BufferedOutputStream(ant_err, 1024);
79
80 //PrintStream ant_out_ps =
81 // new PrintStream(ant_out_buffer, false);
82 //PrintStream ant_err_ps =
83 // new PrintStream(ant_err_buffer, false);
84
85 DefaultLogger log = new DefaultLogger();
86 //log.setErrorPrintStream(ant_err_ps);
87 //log.setOutputPrintStream(ant_out_ps);
88
89 log.setErrorPrintStream(System.err);
90 log.setOutputPrintStream(System.out);
91 log.setMessageOutputLevel(Project.MSG_INFO);
92 ant.addBuildListener(log);
93 try {
94 ant.init();
95 ant.fireBuildStarted();
96 ProjectHelper helper = ProjectHelper.getProjectHelper();
97 helper.parse(ant, buildFile);
98 ant.executeTarget(command_string);
99 ant.fireBuildFinished(null);
100 }
101 catch (BuildException e) {
102 e.printStackTrace(System.err);
103 ant.fireBuildFinished(e);
104 }
105 /*finally {
106 System.setOut(ant_out_ps);
107 System.setErr(ant_err_ps);
108 }*/
109
110 //boolean ant_success = checkAntResult();
111 //ant_out_ps.close();
112 //ant_err_ps.close();
113 /*if (!ant_success){
114 int result = JOptionPane.showConfirmDialog((Component) null, "Ant building call was failed, do you want to see the log file?", "Exit Confirmation", JOptionPane.YES_NO_OPTION);
115 if ( result == JOptionPane.YES_OPTION) {
116 //System.exit(1);
117 // Showing ant_out.log content
118 } else if (result == JOptionPane.NO_OPTION) {
119 JOptionPane.showMessageDialog((Component) null,"You can see the log file from Log Pane!");
120 }
121 } else {
122 //System.err.println("****Ant build successed");
123 //JOptionPane.showMessageDialog((Component) null,"You can see the log file from Log Pane!");
124
125 }*/
126 }
127
128 /*public boolean checkAntResult(){
129 try {
130 String filename = GAI.gsdl3_path + File.separator +"ant_out.log";
131 String fileLine;
132
133 BufferedReader ant_result = new BufferedReader(new FileReader(filename));
134
135 while ((fileLine = ant_result.readLine()) != null) {
136 if (fileLine.matches("BUILD SUCCESSFUL")){
137 //return true;
138 return false;
139 } else if (fileLine.matches("BUILD FAILED")){
140 //System.err.println("***Ant result"+fileLine);
141 return false;
142 }
143 }
144 } catch (Exception e) {
145 e.printStackTrace();
146
147 }
148 return false;
149 }*/
150
151
152 public class restartTomcatServer
153 implements Runnable {
154 private String command_string = null;
155 /* Construct a restartTomcatServer Object*/
156 public restartTomcatServer(String command_string) {
157 this.command_string = command_string;
158 }
159
160 public void run() {
161 try {
162 executeAntTarget(this.command_string);
163 } catch(Exception e) {
164 System.out.println(e.getMessage());
165 }
166 }
167 }
168
169 public class startupTomcatServer
170 implements Runnable {
171 private String command_string = null;
172 /* Construct a startupTomcatServer Object*/
173 public startupTomcatServer(String command_string) {
174 this.command_string = command_string;
175 }
176 public void run() {
177 try {
178 executeAntTarget(this.command_string);
179 } catch(Exception e) {
180 System.out.println(e.getMessage());
181 }
182 }
183 }
184
185 public class shutdownTomcatServer
186 implements Runnable {
187 private String command_string = null;
188 /* Construct a shutdownTomcatServer Object*/
189 public shutdownTomcatServer(String command_string) {
190 this.command_string = command_string;
191 }
192 public void run() {
193 try {
194 executeAntTarget(this.command_string);
195 } catch(Exception e) {
196 System.out.println(e.getMessage());
197 }
198 }
199 }
200
201 public class startupMysqlServer
202 implements Runnable {
203 private String command_string = null;
204
205 /* Construct a startupMysqlServer Object*/
206 public startupMysqlServer(String command_string) {
207 this.command_string = command_string;
208 }
209 public void run() {
210 try {
211 executeAntTarget(this.command_string);
212 // ant_success;
213 } catch(Exception e) {
214 System.out.println(e.getMessage());
215 }
216 }
217 }
218
219 public class shutdownMysqlServer
220 implements Runnable {
221 private String command_string = null;
222 /* Construct a shutdownMysqlServer Object*/
223 public shutdownMysqlServer(String command_string) {
224 this.command_string = command_string;
225 }
226 public void run() {
227 try {
228 executeAntTarget(this.command_string);
229 } catch(Exception e) {
230 System.out.println(e.getMessage());
231 }
232 }
233 }
234
235 public class restartMysqlServer
236 implements Runnable {
237 private String command_string = null;
238 /* Construct a restartMysqlServer Object*/
239 public restartMysqlServer(String command_string) {
240 this.command_string = command_string;
241 }
242 public void run() {
243 try {
244 executeAntTarget(this.command_string);
245 } catch(Exception e) {
246 System.out.println(e.getMessage());
247 }
248 }
249 }
250
251 public void destroy(){
252 }
253}
254
255
256/***
257 * This program shows how you can time a set of threads. The main()
258 * program demonstrates the timing classes by spawning and timing a
259 * set of threads that just execute a dummy loop.
260 ***/
261/*public class ThreadTiming {
262
263 /** Times a group of threads. Works with TimingWrapper. */
264// public static class ThreadTimer {
265// private boolean __resultReady;
266// private int __monitor[], __numThreads;
267// private long __begin, __end;
268
269 /** Creates a ThreadTimer that can time a given number of threads. */
270// public ThreadTimer(int numThreads) {
271// __monitor = new int[1];
272// __monitor[0] = 0;
273// __numThreads = numThreads;
274// __resultReady = false;
275// }
276
277 /** Starts the timer when all the threads are ready to start. */
278// public void start() {
279// synchronized(__monitor) {
280// if(++__monitor[0] >= __numThreads) {
281// __begin = System.currentTimeMillis();
282// __monitor.notifyAll();
283// } else {
284// while(true) {
285// try {
286// __monitor.wait();
287// } catch(InterruptedException e) {
288// continue;
289// }
290// break;
291// }
292// }
293// }
294// }
295
296 /** Stops the timer when the last thread is done. */
297// public void stop() {
298// synchronized(__monitor) {
299// if(--__monitor[0] <= 0) {
300// __end = System.currentTimeMillis();
301// synchronized(this) {
302// __resultReady = true;
303// notify();
304// }
305// }
306// }
307//}/
308
309 /** Calling thread waits until timing result is ready. */
310// public synchronized void waitForResult() {
311// while(!__resultReady) {
312// try {
313/// wait();
314// } catch(InterruptedException e) {
315// }
316// }
317// __resultReady = false;
318//}
319//
320///** Returns elapsed time in milliseconds. */
321//public long getElapsedTime() {
322// return (__end - __begin);
323//}
324// }
325//
326 /** Wraps a thread so it can be timed with a group. */
327// public static class TimingWrapper implements Runnable {
328// private Runnable __runMe;
329//private ThreadTimer __timer;
330
331// public TimingWrapper(Runnable runMe, ThreadTimer timer) {
332// __runMe = runMe;
333// __timer = timer;
334//}
335
336//public void run() {
337// // Note, this timing approach does not measure thread creation
338// // and thread joining overhead.
339// __timer.start();
340// __runMe.run();
341// __timer.stop();
342//}
343// }
344
345
346//public static final int NUM_THREADS = 10;
347//public static final int NUM_LOOPS = 10000000;
348
349// public static void main(String[] args) {
350//int thread;
351//ThreadTimer timer;
352//
353// timer = new ThreadTimer(NUM_THREADS);
354
355 // Start a number of threads that iterate a dummy loop
356// for(thread = 0; thread < NUM_THREADS; thread++)
357// new Thread(new TimingWrapper(new Runnable() {
358// public void run() {
359// for(int i=0; i < NUM_LOOPS; i++);
360// }
361// }, timer)).start();
362//
363//timer.waitForResult();
364
365// System.out.println("Elapsed Seconds: " +
366// ((double)timer.getElapsedTime())/1000.0);
367// }
368
369//}*/
Note: See TracBrowser for help on using the repository browser.