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

Last change on this file since 13594 was 13594, checked in by mdewsnip, 17 years ago

Moved the LocalGreenstone class into the "greenstone" package.

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