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

Last change on this file since 12572 was 12490, checked in by kjdon, 18 years ago

updated version number for 2.71 release

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