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

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

Moved all of the ZIP functions out of Utility and into a new ZipTools class.

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