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

Last change on this file since 13177 was 13026, checked in by mdewsnip, 18 years ago

Removed all the old WGet stuff.

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