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

Last change on this file since 12350 was 12310, checked in by davidb, 18 years ago

Minor mods to Gatherer exit code to allow -- for the Windows case -- GLI
to restart in a different interface language. Windows case complicated
by threads used to monitor the external applications that have been launched
and still running.

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