source: trunk/gli/src/org/greenstone/gatherer/Gatherer.java@ 9869

Last change on this file since 9869 was 9869, checked in by mdewsnip, 19 years ago

Dealing with the user config.xml files is now much smarter. Instead of throwing away all the user preferences every time the version of GLI is updated, they are now intelligently merged into the updated (core) config.xml file.

  • Property svn:keywords set to Author Date Id Revision
File size: 28.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer 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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.lang.*;
33import java.net.*;
34import java.util.*;
35import javax.swing.*;
36import javax.swing.plaf.*;
37import javax.swing.text.*;
38import org.greenstone.gatherer.Configuration;
39import org.greenstone.gatherer.GAuthenticator;
40import org.greenstone.gatherer.cdm.ClassifierManager;
41import org.greenstone.gatherer.cdm.PluginManager;
42import org.greenstone.gatherer.collection.CollectionManager;
43import org.greenstone.gatherer.feedback.ActionRecorderDialog;
44import org.greenstone.gatherer.file.FileManager;
45import org.greenstone.gatherer.file.FileAssociationManager;
46import org.greenstone.gatherer.gui.GUIManager;
47import org.greenstone.gatherer.gui.URLField;
48import org.greenstone.gatherer.gui.WarningDialog;
49import org.greenstone.gatherer.util.StaticStrings;
50import org.greenstone.gatherer.util.Utility;
51
52
53/** Containing the top-level "core" for the Gatherer, this class is the
54 * common core for the GLI application and applet. It first parses the
55 * command line arguments, preparing to update the configuration as
56 * required. Next it loads several important support classes such as the
57 * Configuration and Dictionary. Finally it creates the other important
58 * managers and sends them on their way.
59 * @author John Thompson, Greenstone Digital Library, University of Waikato
60 * @version 2.3
61 */
62public class Gatherer
63{
64 /** Has the exit flag been set? */
65 public boolean exit = false;
66
67 /** All of the external applications that must exit before we close the Gatherer. */
68 public Vector apps = new Vector();
69 static private String non_standard_collect_directory_path = null;
70 static public String open_collection_file_path = null;
71 /** A public reference to the FileAssociationManager. */
72 static public FileAssociationManager assoc_man;
73 /** A public reference to the CollectionManager. */
74 static public CollectionManager c_man;
75 /** a reference to the Servlet Configuration is GS3 */
76 static public ServletConfiguration servlet_config;
77 /** A public reference to the FileManager. */
78 static public FileManager f_man;
79 /** A public reference to the GUIManager. */
80 static public GUIManager g_man;
81 static private boolean g_man_built = false;
82
83 /** A static reference to ourselves. */
84 static public Gatherer self;
85 /** We are using the GLI for GS3 */
86 static public boolean GS3 = false;
87
88 static public boolean isGsdlRemote = false;
89 static public String cgiBase = "";
90
91 // feedback stuff
92 /** is the feedback feature enabled? */
93 static public boolean feedback_enabled = true;
94 /** the action recorder dialog */
95 static public ActionRecorderDialog feedback_dialog = null;
96
97 // Refresh reasons
98 static public final int COLLECTION_OPENED = 0;
99 static public final int COLLECTION_CLOSED = 1;
100 static public final int COLLECTION_REBUILT = 2;
101 static public final int PREFERENCES_CHANGED = 3;
102
103
104 /** Magic to allow Enter to fire the default button. */
105 static {
106 KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
107 Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
108 map.removeKeyStrokeBinding(enter);
109 }
110
111
112 public Gatherer()
113 {
114 this.self = this;
115
116 Utility.initImages(this);
117 }
118
119
120 public GUIManager init(Dimension size, String gsdl_path, String gsdl3_path, String local_library_path,
121 String library_url_string, boolean debug_enabled, String perl_path,
122 boolean no_load, String open_collection,
123 String site_name, String servlet_path, String wget_path)
124 {
125 if (gsdl3_path != null && !gsdl3_path.equals("")) {
126 this.GS3 = true;
127 } else {
128 gsdl3_path = null;
129 }
130
131 // Create the debug stream if required
132 if (debug_enabled) {
133 DebugStream.enableDebugging();
134
135 Calendar now = Calendar.getInstance();
136 String debug_file_path = "debug" + now.get(Calendar.DATE) + "-" + now.get(Calendar.MONTH) + "-" + now.get(Calendar.YEAR) + ".txt";
137
138 // Debug file is created in the user's GLI directory
139 debug_file_path = Utility.getGLIUserFolder().toString() + File.separator + debug_file_path;
140 DebugStream.println("Debug file path: " + debug_file_path);
141 DebugStream.setDebugFile(debug_file_path);
142 DebugStream.print(System.getProperties());
143 }
144
145 try {
146 // Load GLI config file
147 new Configuration(gsdl_path, gsdl3_path, site_name);
148 if (Configuration.just_updated_config_xml_file) {
149 // Delete the plugins.dat and classifiers.dat files
150 PluginManager.clearPluginCache();
151 ClassifierManager.clearClassifierCache();
152 }
153
154 if (GS3) {
155 // Load Greenstone 3 servelt configuration
156 servlet_config = new ServletConfiguration(gsdl3_path);
157 }
158
159 // Check we know where Perl is
160 Configuration.perl_path = perl_path;
161 if (isGsdlRemote && Utility.isWindows() && Configuration.perl_path != null) {
162 if (Configuration.perl_path.toLowerCase().endsWith("perl.exe")) {
163 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - "perl.exe".length());
164 }
165 if (Configuration.perl_path.endsWith(File.separator)) {
166 Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - File.separator.length());
167 }
168 }
169
170 if (GS3 && Configuration.servlet_path == null) {
171 Configuration.servlet_path = servlet_config.getServletPath(Configuration.site_name);
172 }
173
174 // the feedback dialog has been loaded with a default locale,
175 // now set the user specified one
176 if (feedback_enabled && feedback_dialog != null) {
177 feedback_dialog.setLocale(Configuration.getLocale("general.locale", true));
178 }
179
180 // Read Dictionary
181 new Dictionary(Configuration.getLocale("general.locale", true), Configuration.getFont("general.font", true));
182
183 // check that we are using Sun Java
184 String java_vendor = System.getProperty("java.vendor");
185 if (!java_vendor.equals("Sun Microsystems Inc.")) {
186 System.err.println(Dictionary.get("General.NotSunJava", java_vendor));
187 }
188
189 if (gsdl_path == null) {
190 missingGSDL();
191 }
192
193 // Start up the local library server, if that's what we want
194 if (Utility.isWindows() && local_library_path != null && !GS3) {
195 LocalLibraryServer.start(gsdl_path, local_library_path);
196 }
197
198 // The "-library_url" option overwrites anything in the config files
199 if (library_url_string != null && library_url_string.length() > 0) {
200 try {
201 System.err.println("Setting library_url to " + library_url_string + "...");
202 Configuration.library_url = new URL(library_url_string);
203 }
204 catch (MalformedURLException error) {
205 DebugStream.printStackTrace(error);
206 }
207 }
208
209 // Check that we now know the Greenstone library URL, since we need this for previewing collections
210 DebugStream.println("Configuration.library_url = " + Configuration.library_url);
211 if (Configuration.library_url == null) {
212 missingEXEC();
213 }
214
215 // Check for ImageMagick
216 if (Gatherer.isGsdlRemote) {
217 DebugStream.println("Not checking for ImageMagick.");
218 }
219 else if (!(new ImageMagickTest()).found()) {
220 // Time for a warning message
221 missingImageMagick();
222 }
223
224 if (Gatherer.isGsdlRemote) {
225 DebugStream.println("Not checking for perl path/exe");
226 }
227 else {
228 // Perl path is a little different as it is perfectly ok to
229 // start the GLI without providing a perl path
230 boolean found_perl = false;
231 if (Configuration.perl_path != null) {
232 // See if the file pointed to actually exists
233 File perl_file = new File(Configuration.perl_path);
234 found_perl = perl_file.exists();
235 perl_file = null;
236 }
237 if (Configuration.perl_path == null || !found_perl) {
238 // Run test to see if we can run perl as is.
239 PerlTest perl_test = new PerlTest();
240 if (perl_test.found()) {
241 // If so replace the perl path with the system
242 // default (or null for unix).
243 Configuration.perl_path = perl_test.toString();
244 found_perl = true;
245 }
246 }
247 if (!found_perl) {
248 // Time for an error message.
249 missingPERL();
250 }
251 }
252
253 boolean download_workflow = Configuration.get("workflow.download", false);
254 if (download_workflow) {
255 // If the WGet path hasn't been specified by the user, try the Greenstone one
256 if (wget_path == null) {
257 File wget_file = new File(Utility.getWGetPath(gsdl_path));
258 if (wget_file.exists()) {
259 wget_path = Utility.getWGetPath(gsdl_path);
260 }
261 }
262
263 // If we still don't have a WGet path, give up (there isn't anything else we can do)
264 String wget_version_str = null;
265 if (wget_path == null) {
266 wget_version_str = StaticStrings.NO_WGET_STR;
267 missingWGET();
268 }
269 // Otherwise check the version of WGet
270 else {
271 wget_version_str = testWGetVersion(wget_path);
272 if (wget_version_str.equals(StaticStrings.WGET_OLD_STR)) {
273 oldWGET();
274 }
275 }
276
277 // Store the new values in the configuration
278 DebugStream.println("WGet path: " + wget_path);
279 Configuration.setWGetPath(wget_path);
280 Configuration.setWGetVersion(wget_version_str);
281 }
282
283 // Set the default font for all Swing components.
284 FontUIResource default_font = Configuration.getFont("general.font", true);
285 Enumeration keys = UIManager.getDefaults().keys();
286 while (keys.hasMoreElements()) {
287 Object key = keys.nextElement();
288 Object value = UIManager.get(key);
289 if (value instanceof FontUIResource) {
290 UIManager.put(key, default_font);
291 }
292 }
293
294 // Set up proxy
295 setProxy();
296 // Now we set up an Authenticator
297 Authenticator.setDefault(new GAuthenticator());
298
299 assoc_man = new FileAssociationManager();
300 // Create File Manager
301 f_man = new FileManager();
302 // Create Collection Manager
303 c_man = new CollectionManager();
304
305 if (GS3) {
306 if (site_name==null) {
307 site_name = Configuration.site_name;
308 servlet_path = null; // need to reset this
309 }
310 if (servlet_path == null) {
311 servlet_path = Configuration.getServletPath();
312 }
313 }
314
315 open_collection_file_path = open_collection;
316 if (open_collection_file_path == null) {
317 open_collection_file_path = Configuration.getString("general.open_collection", true);
318 }
319 if (no_load || open_collection_file_path.equals("")) {
320 open_collection_file_path = null;
321 }
322 }
323 catch (Exception exception) {
324 DebugStream.printStackTrace(exception);
325 }
326
327 // Create GUI Manager (last) or else suffer the death of a thousand NPE's
328 g_man = new GUIManager(size);
329 return g_man;
330 }
331
332
333 public void run(Dimension size, GUIManager g_man)
334 {
335 // Size and place the frame on the screen
336 Rectangle bounds = Configuration.getBounds("general.bounds", true);
337 if (bounds == null) {
338 // Choose a sensible default value
339 bounds = new Rectangle(0, 0, 640, 480);
340 }
341
342 // Ensure width and height are reasonable
343 size = bounds.getSize();
344 if (size.width < 640) {
345 size.width = 640;
346 }
347 else if (size.width > Configuration.screen_size.width) {
348 size.width = Configuration.screen_size.width;
349 }
350 if (size.height < 480) {
351 size.height = 480;
352 }
353 else if (size.height > Configuration.screen_size.height) {
354 size.height = Configuration.screen_size.height;
355 }
356
357 if (!g_man_built) {
358
359 g_man.display();
360
361 // Place the window in the desired location on the screen, if this is do-able (not under most linux window managers apparently. In fact you're lucky if they listen to any of your screen size requests).
362 g_man.setLocation(bounds.x, bounds.y);
363 g_man.setVisible(true);
364
365 // After the window has been made visible, check that it is in the correct place
366 // sometimes java places a window not in the correct place,
367 // but with an offset. If so, we work out what the offset is
368 // and change the desired location to take that into account
369 Point location = g_man.getLocation();
370 int x_offset = bounds.x - location.x;
371 int y_offset = bounds.y - location.y;
372 // If not, offset the window to move it into the correct location
373 if (x_offset > 0 || y_offset > 0) {
374 ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset));
375 g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset);
376 }
377
378 // The 'after-display' triggers several events which don't occur until after the visual components are actually available on screen. Examples of these would be the various html renderings, as they can't happen offscreen.
379 g_man.afterDisplay();
380 g_man_built = true;
381 }
382 else {
383 g_man.setVisible(true);
384 }
385 }
386
387
388 /** Exits the Gatherer after ensuring that things needing saving are saved.
389 * @see java.io.FileOutputStream
390 * @see java.io.PrintStream
391 * @see java.lang.Exception
392 * @see javax.swing.JOptionPane
393 * @see org.greenstone.gatherer.Configuration
394 * @see org.greenstone.gatherer.collection.CollectionManager
395 * @see org.greenstone.gatherer.gui.GUIManager
396 */
397 public void exit() {
398 exit = true;
399 // If we have an open collection make note of it.
400 Configuration.setString("general.open_collection", true, null);
401 if(c_man.ready()) {
402 ///ystem.err.println("Collection open.");
403 if(c_man.saved()) {
404 ///ystem.err.println("Collection has been recently saved, so I'll remember it for next time.");
405 Configuration.setString("general.open_collection", true, c_man.getCollectionFilePath());
406 }
407 c_man.closeCollection();
408 }
409 if(assoc_man != null) {
410 assoc_man.save();
411 assoc_man = null;
412 }
413
414 // Store the current position and size (if reasonable) of the Gatherer for next time
415 Rectangle bounds = g_man.getBounds();
416 Configuration.setBounds("general.bounds", true, bounds);
417
418 // Save configuration.
419 Configuration.save();
420
421 // Get the gui to deallocate
422 g_man.setVisible(false);
423 g_man.destroy();
424 g_man_built = false;
425
426 // Flush debug
427 DebugStream.closeDebugStream();
428
429 // If we started a server, we should try to stop it.
430 if (LocalLibraryServer.isRunning() == true) {
431 LocalLibraryServer.stop();
432 }
433
434 if(apps.size() == 0) {
435 if (!Gatherer.isGsdlRemote) {
436 System.exit(0);
437 }
438 }
439 else {
440 JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
441 g_man.setVisible(false);
442 }
443 }
444
445
446 static public String getCollectDirectoryPath()
447 {
448 if (non_standard_collect_directory_path != null) {
449 return non_standard_collect_directory_path;
450 }
451
452 if (!GS3) {
453 return Configuration.gsdl_path + "collect" + File.separator;
454 }
455 else {
456 return getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect" + File.separator;
457 }
458 }
459
460
461 static public String getSitesDirectoryPath()
462 {
463 return Configuration.gsdl3_path + File.separator + "web" + File.separator + "sites" + File.separator;
464 }
465
466
467 static public void setCollectDirectoryPath(String collect_directory_path)
468 {
469 non_standard_collect_directory_path = collect_directory_path;
470 if (!non_standard_collect_directory_path.endsWith(File.separator)) {
471 non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator;
472 }
473 }
474
475
476 static public void refresh(int refresh_reason)
477 {
478 if (g_man != null) {
479 g_man.refresh(refresh_reason, c_man.ready());
480 }
481
482 // Now is a good time to force a garbage collect
483 System.gc();
484 }
485
486
487 // used to send reload coll messages to the tomcat server
488 static public void configGS3Server(String site, String command) {
489
490 try {
491 // need to add the servlet name to the exec address
492 String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command;
493 URL url = new URL(raw_url);
494 DebugStream.println("Action: " + raw_url);
495 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
496 int response_code = library_connection.getResponseCode();
497 if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
498 DebugStream.println("200 - Complete.");
499 }
500 else {
501 DebugStream.println("404 - Failed.");
502 }
503 url = null;
504 }
505 catch (Exception exception) {
506 DebugStream.printStackTrace(exception);
507 }
508 }
509
510
511 /** Used to 'spawn' a new child application when a file is double clicked.
512 * @param file The file to open
513 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
514 */
515 public void spawnApplication(File file) {
516 String [] commands = assoc_man.getCommand(file);
517 if(commands != null) {
518 ExternalApplication app = new ExternalApplication(commands);
519 apps.add(app);
520 app.start();
521 }
522 else {
523 ///ystem.err.println("No open command available.");
524 }
525 }
526
527
528 public void spawnApplication(String command)
529 {
530 ExternalApplication app = new ExternalApplication(command);
531 apps.add(app);
532 app.start();
533 }
534
535
536 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
537 * @param url The url to open the browser at
538 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
539 */
540 public void spawnBrowser(String url) {
541 String command = assoc_man.getBrowserCommand(url);
542 if (command != null) {
543 BrowserApplication app = new BrowserApplication(command, url);
544 apps.add(app);
545 app.start();
546 }
547 else {
548 ///ystem.err.println("No browser command available.");
549 }
550 }
551
552
553 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
554 */
555 static public void missingEXEC() {
556 WarningDialog dialog;
557 if (GS3) {
558 dialog = new WarningDialog("warning.MissingEXEC_GS3", "MissingEXEC_GS3.Title", Dictionary.get("MissingEXEC_GS3.Message"), "general.library_url", false);
559 }
560 else {
561 dialog = new WarningDialog("warning.MissingEXEC", "MissingEXEC.Title", Dictionary.get("MissingEXEC.Message"), "general.library_url", false);
562 }
563 dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false), Configuration.getColor("coloring.error_foreground", false), Configuration.getColor("coloring.error_background", false)));
564 dialog.display();
565 dialog.dispose();
566 dialog = null;
567
568 String library_url_string = Configuration.getString("general.library_url", true);
569 if (!library_url_string.equals("")) {
570 try {
571 Configuration.library_url = new URL(library_url_string);
572 }
573 catch (MalformedURLException exception) {
574 DebugStream.printStackTrace(exception);
575 }
576 }
577 }
578
579
580 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
581 */
582 static public void missingGSDL() {
583 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", "MissingGSDL.Title", Dictionary.get("MissingGSDL.Message"), null, false);
584 dialog.display();
585 dialog.dispose();
586 dialog = null;
587 }
588
589 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
590 static public void missingImageMagick() {
591 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", "MissingImageMagick.Title", Dictionary.get("MissingImageMagick.Message"), null, false);
592 dialog.display();
593 dialog.dispose();
594 dialog = null;
595 }
596
597 /** Prints a warning message about missing a valid PERL path, which although not fatal pretty much ensures no collection creation/building will work properly in the GLI. */
598 static public void missingPERL() {
599 WarningDialog dialog = new WarningDialog("warning.MissingPERL", "MissingPERL.Title", Dictionary.get("MissingPERL.Message"), null, false);
600 dialog.display();
601 dialog.dispose();
602 dialog = null;
603 }
604
605 /** Prints a warning message about missing a valid WGet path, which although not fatal means mirroring won't work */
606 static public void missingWGET() {
607 WarningDialog dialog = new WarningDialog("warning.MissingWGET", "MissingWGET.Title", Dictionary.get("MissingWGET.Message"), null, false);
608 dialog.display();
609 dialog.dispose();
610 dialog = null;
611 }
612
613 /** Prints a warning message about having an old version of WGet. not fatal, but mirroring may not work properly */
614 static public void oldWGET() {
615 WarningDialog dialog = new WarningDialog("warning.OldWGET", "OldWGET.Title", Dictionary.get("OldWGET.Message"), null, false);
616 dialog.display();
617 dialog.dispose();
618 dialog = null;
619 }
620
621
622 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
623 * @see java.lang.Exception
624 * @see java.lang.System
625 * @see java.net.Authenticator
626 * @see org.greenstone.gatherer.Configuration
627 * @see org.greenstone.gatherer.GAuthenticator
628 */
629 static public void setProxy() {
630 try {// Can throw several exceptions
631 if(Configuration.get("general.use_proxy", true)) {
632 System.setProperty("http.proxyType", "4");
633 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
634 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
635 System.setProperty("http.proxySet", "true");
636 } else {
637 System.setProperty("http.proxySet", "false");
638 }
639 } catch (Exception error) {
640 DebugStream.println("Error in Gatherer.initProxy(): " + error);
641 DebugStream.printStackTrace(error);
642 }
643 }
644
645
646 // TODO fill this in
647 private String testWGetVersion(String wget_path) {
648 return StaticStrings.WGET_STR;
649 }
650
651 /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
652 private class ExternalApplication
653 extends Thread {
654 private Process process = null;
655 /** The initial command string given to this sub-process. */
656 private String command = null;
657 private String[] commands = null;
658 /** Constructor.
659 * @param command The initial command <strong>String</strong>.
660 */
661 public ExternalApplication(String command) {
662 this.command = command;
663 }
664
665 public ExternalApplication(String[] commands) {
666 this.commands = commands;
667 }
668 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
669 * @see java.lang.Exception
670 * @see java.lang.Process
671 * @see java.lang.Runtime
672 * @see java.lang.System
673 * @see java.util.Vector
674 */
675 public void run() {
676 // Call an external process using the args.
677 try {
678 if(commands != null) {
679 StringBuffer whole_command = new StringBuffer();
680 for(int i = 0; i < commands.length; i++) {
681 whole_command.append(commands[i]);
682 whole_command.append(" ");
683 }
684 DebugStream.println("Running " + whole_command.toString());
685 Runtime rt = Runtime.getRuntime();
686 process = rt.exec(commands);
687 process.waitFor();
688 }
689 else {
690 DebugStream.println("Running " + command);
691 Runtime rt = Runtime.getRuntime();
692 process = rt.exec(command);
693 process.waitFor();
694 }
695 }
696 catch (Exception exception) {
697 DebugStream.printStackTrace(exception);
698 }
699 // Remove ourself from Gatherer list of threads.
700 apps.remove(this);
701 // Call exit if we were the last outstanding child process thread.
702 if (apps.size() == 0 && exit == true) {
703 System.exit(0);
704 }
705 }
706 public void stopExternalApplication() {
707 if(process != null) {
708 process.destroy();
709 }
710 }
711 }
712 /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
713 private class BrowserApplication
714 extends Thread {
715 private Process process = null;
716 /** The initial command string given to this sub-process. */
717 private String command = null;
718 private String url = null;
719 private String[] commands = null;
720
721 public BrowserApplication(String command, String url) {
722 StringTokenizer st = new StringTokenizer(command);
723 int num_tokens = st.countTokens();
724 this.commands = new String [num_tokens];
725 int i=0;
726 while (st.hasMoreTokens()) {
727 commands[i] = st.nextToken();
728 i++;
729 }
730 //this.commands = commands;
731 this.url = url;
732 }
733 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
734 * @see java.lang.Exception
735 * @see java.lang.Process
736 * @see java.lang.Runtime
737 * @see java.lang.System
738 * @see java.util.Vector
739 */
740 public void run() {
741 // Call an external process using the args.
742 if(commands == null) {
743 apps.remove(this);
744 return;
745 }
746 try {
747 String prog_name = commands[0];
748 String lower_name = prog_name.toLowerCase();
749 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
750 DebugStream.println("found mozilla or netscape, trying remote it");
751 // mozilla and netscape, try using a remote command to get things in the same window
752 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
753 printArray(new_commands);
754
755 Runtime rt = Runtime.getRuntime();
756 process = rt.exec(new_commands);
757 int exitCode = process.waitFor();
758 if (exitCode != 0) { // if Netscape or mozilla was not open
759 DebugStream.println("couldn't do remote, trying original command");
760 printArray(commands);
761 process = rt.exec(commands); // try the original command
762 }
763 } else {
764 // just run what we have been given
765 StringBuffer whole_command = new StringBuffer();
766 for(int i = 0; i < commands.length; i++) {
767 whole_command.append(commands[i]);
768 whole_command.append(" ");
769 }
770 DebugStream.println("Running " + whole_command.toString());
771 Runtime rt = Runtime.getRuntime();
772 process = rt.exec(commands);
773 process.waitFor();
774 }
775 }
776
777 catch (Exception exception) {
778 DebugStream.printStackTrace(exception);
779 }
780 // Remove ourself from Gatherer list of threads.
781 apps.remove(this);
782 // Call exit if we were the last outstanding child process thread.
783 if (apps.size() == 0 && exit == true) {
784 System.exit(0);
785 }
786 }
787 public void printArray(String [] array) {
788 for(int i = 0; i < array.length; i++) {
789 DebugStream.print(array[i]+" ");
790 System.err.println(array[i]+" ");
791 }
792 }
793 public void stopBrowserApplication() {
794 if(process != null) {
795 process.destroy();
796 }
797 }
798 }
799
800
801 private class ImageMagickTest
802 {
803 public boolean found()
804 {
805 try {
806 String[] command = new String[2];
807 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
808 command[1] = "-version";
809 Process image_magick_process = Runtime.getRuntime().exec(command);
810 image_magick_process.waitFor();
811 return (image_magick_process.exitValue() == 0);
812 }
813 catch (Exception exception) {
814 return false;
815 }
816 }
817 }
818
819
820 private class PerlTest
821 {
822 private String[] command = new String[2];
823
824 public PerlTest()
825 {
826 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
827 command[1] = "-version";
828 }
829
830 public boolean found()
831 {
832 try {
833 Process perl_process = Runtime.getRuntime().exec(command);
834 perl_process.waitFor();
835 return (perl_process.exitValue() == 0);
836 }
837 catch (Exception exception) {
838 return false;
839 }
840 }
841
842 public String toString() {
843 return command[0];
844 }
845 }
846}
Note: See TracBrowser for help on using the repository browser.