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

Last change on this file since 12756 was 12646, checked in by mdewsnip, 18 years ago

Support for collection-specific plugins and classifiers with the new code.

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