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

Last change on this file since 16990 was 16990, checked in by ak19, 16 years ago

After changing the gliserver url or library path url, a restart is now required when running the GLI client (remote GS server).

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