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

Last change on this file since 12119 was 12040, checked in by mdewsnip, 18 years ago

(FindBugs) Fixed a couple of places where "catch Exception" was used.

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