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

Last change on this file since 14372 was 14372, checked in by qq6, 17 years ago

set the general.gliserver_url if Configuration.gliserver_url is defined

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