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

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

When GS is local, it will use library_url_gs3 or library_url_gs2 and similarly open_collection_gs3 or open_collection_gs2 properties from the config file. (It still uses the default library_url and open_collection when GS is remote or if GLI is used with Fedora.) Method Configuration.gliPropertyNameSuffix() is added to return either the empty string or _gs2 or _gs3 in order to work out the property name in the situation. This change helps when running local GS2 and GS3 alternatively with GLI, as it preserves the library url and last opened collection for each separately.

  • Property svn:keywords set to Author Date Id Revision
File size: 43.5 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 (Exception exception) {
753 DebugStream.printStackTrace(exception);
754 }
755 }
756
757
758 /** Used to 'spawn' a new child application when a file is double clicked.
759 * @param file The file to open
760 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
761 */
762 static public void spawnApplication(File file) {
763 String [] commands = assoc_man.getCommand(file);
764 if(commands != null) {
765 ExternalApplication app = new ExternalApplication(commands);
766 apps.add(app);
767 app.start();
768 }
769 else {
770 ///ystem.err.println("No open command available.");
771 }
772 }
773
774
775 static public void spawnApplication(String command)
776 {
777 ExternalApplication app = new ExternalApplication(command);
778 apps.add(app);
779 app.start();
780 }
781
782
783 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
784 * @param url The url to open the browser at
785 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
786 */
787 static public void spawnBrowser(String url) {
788 String command = assoc_man.getBrowserCommand(url);
789 if (command != null) {
790 BrowserApplication app = new BrowserApplication(command, url);
791 apps.add(app);
792 app.start();
793 }
794 else {
795 ///ystem.err.println("No browser command available.");
796 }
797 }
798
799
800 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
801 */
802 static private void missingEXEC() {
803 WarningDialog dialog;
804 String configPropertyName = "general.library_url"+Configuration.gliPropertyNameSuffix();
805
806 if (GS3) {
807 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), configPropertyName, false);
808 } else { // local case
809 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), configPropertyName, false);
810 }
811
812 dialog.setValueField(new URLField.Text(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
813 dialog.display();
814 dialog.dispose();
815 dialog = null;
816
817 String library_url_string = Configuration.getString(configPropertyName, true);
818 if (!library_url_string.equals("")) {
819 try {
820 Configuration.library_url = new URL(library_url_string);
821 }
822 catch (MalformedURLException exception) {
823 DebugStream.printStackTrace(exception);
824 }
825 }
826 }
827
828
829
830 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
831 */
832 static private void popupFedoraInfo() {
833
834 FedoraLogin dialog = new FedoraLogin("Fedora Login", false);
835
836 if (Configuration.library_url == null) {
837
838 String library_url_string = dialog.getLibraryURL();
839 if (!library_url_string.equals("")) {
840 try {
841 Configuration.library_url = new URL(library_url_string);
842 }
843 catch (MalformedURLException exception) {
844 DebugStream.printStackTrace(exception);
845 }
846 }
847 }
848
849 boolean showLogin = true;
850
851 do {
852 if(!dialog.loginRequested()) { // user pressed cancel to exit the FedoraLogin dialog
853 System.exit(0);
854 } else {
855 showLogin = dialog.loginRequested();
856 String hostname = dialog.getHostname();
857 String port = dialog.getPort();
858 String username = dialog.getUsername();
859 String password = dialog.getPassword();
860 String protocol = dialog.getProtocol();
861
862 Configuration.fedora_info.setHostname(hostname);
863 Configuration.fedora_info.setPort(port);
864 Configuration.fedora_info.setUsername(username);
865 Configuration.fedora_info.setPassword(password);
866 Configuration.fedora_info.setProtocol(protocol);
867
868 String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora";
869 String login_str = username + ":" + password;
870
871 String login_encoding = new sun.misc.BASE64Encoder().encode(login_str.getBytes());
872
873 try {
874 URL ping_url = new URL(ping_url_str);
875 URLConnection uc = ping_url.openConnection();
876 uc.setRequestProperty ("Authorization", "Basic " + login_encoding);
877 // Attempt to access some content ...
878 InputStream content = (InputStream)uc.getInputStream();
879
880 // if no exception occurred in the above, we would have come here:
881 showLogin = false;
882 dialog.dispose();
883 }
884 catch (Exception exception) {
885 // TODO: move into dictionary
886 String[] errorMessage = {"Failed to connect to the Fedora server.", "It might not be running, or",
887 "incorrect username and/or password."};
888 dialog.setErrorMessage(errorMessage);
889 //DebugStream.printStackTrace(exception);
890 // exception occurred, show the dialog again (do this after printing to
891 // debugStream, else the above does not get done for some reason).
892 dialog.setVisible(true);
893 }
894 }
895 } while(showLogin);
896
897 dialog = null; // no more need of the dialog
898
899 // Now we are connected.
900 }
901
902
903
904 static private void requestGLIServerURL()
905 {
906 WarningDialog dialog;
907 String[] defaultURLs = {
908 "http://localhost:8080/greenstone3/cgi-bin/gliserver.pl",
909 "http://localhost:8080/gsdl/cgi-bin/gliserver.pl"
910 };
911
912 // Warning dialog with no cancel button and no "turn off warning" checkbox
913 // (since user-input of the gliserver script is mandatory)
914 dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false, false);
915
916 dialog.setValueField(new URLField.DropDown(Configuration.getColor("coloring.editable_foreground", false),
917 Configuration.getColor("coloring.editable_background", false),
918 defaultURLs, "general.gliserver_url",
919 "general.open_collection"+Configuration.gliPropertyNameSuffix(),
920 "gliserver.pl"));
921
922 if (Gatherer.default_gliserver_url!=null){
923 dialog.setValueField(Gatherer.default_gliserver_url.toString());
924 }
925
926 // A WarningDialog cannot always be made to respond (let alone to exit the program) on close. We
927 // handle the response of this particular WarningDialog here: a URL for gliserver.pl is a crucial
928 // piece of user-provided data. Therefore, if no URL was entered for gliserver.pl, it'll exit safely.
929 dialog.addWindowListener(new WindowAdapter() {
930 public void windowClosing(WindowEvent e) {
931 Gatherer.exit();
932 }
933 });
934
935 dialog.display();
936 dialog.dispose();
937 dialog = null;
938
939
940 String gliserver_url_string = Configuration.getString("general.gliserver_url", true);
941 if (!gliserver_url_string.equals("")) {
942 try {
943 Configuration.gliserver_url = new URL(gliserver_url_string);
944 Configuration.setString("general.gliserver_url", true, gliserver_url_string);
945 }
946 catch (MalformedURLException exception) {
947 DebugStream.printStackTrace(exception);
948 }
949 }
950 }
951
952
953 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
954 */
955 static private void missingGSDL() {
956 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false);
957 dialog.display();
958 dialog.dispose();
959 dialog = null;
960 }
961
962 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
963 static private void missingImageMagick() {
964 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false);
965 dialog.display();
966 dialog.dispose();
967 dialog = null;
968 }
969
970 /** 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. */
971 static private void missingPERL() {
972 WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false);
973 dialog.display();
974 dialog.dispose();
975 dialog = null;
976 }
977
978
979 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
980 * @see java.lang.Exception
981 * @see java.lang.System
982 * @see java.net.Authenticator
983 * @see org.greenstone.gatherer.Configuration
984 * @see org.greenstone.gatherer.GAuthenticator
985 */
986 static public void setProxy() {
987 try {// Can throw several exceptions
988 if(Configuration.get("general.use_proxy", true)) {
989 System.setProperty("http.proxyType", "4");
990 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
991 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
992 System.setProperty("http.proxySet", "true");
993 } else {
994 System.setProperty("http.proxyHost", "");
995 System.setProperty("http.proxyPort", "");
996 System.setProperty("http.proxySet", "false");
997 }
998 } catch (Exception error) {
999 DebugStream.println("Error in Gatherer.initProxy(): " + error);
1000 DebugStream.printStackTrace(error);
1001 }
1002 }
1003
1004
1005 /** 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. */
1006 static private class ExternalApplication
1007 extends Thread {
1008 private Process process = null;
1009 /** The initial command string given to this sub-process. */
1010 private String command = null;
1011 private String[] commands = null;
1012 /** Constructor.
1013 * @param command The initial command <strong>String</strong>.
1014 */
1015 public ExternalApplication(String command) {
1016 this.command = command;
1017 }
1018
1019 public ExternalApplication(String[] commands) {
1020 this.commands = commands;
1021 }
1022 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1023 * @see java.lang.Exception
1024 * @see java.lang.Process
1025 * @see java.lang.Runtime
1026 * @see java.lang.System
1027 * @see java.util.Vector
1028 */
1029 public void run() {
1030 // Call an external process using the args.
1031 try {
1032 if(commands != null) {
1033 StringBuffer whole_command = new StringBuffer();
1034 for(int i = 0; i < commands.length; i++) {
1035 whole_command.append(commands[i]);
1036 whole_command.append(" ");
1037 }
1038 DebugStream.println("Running " + whole_command.toString());
1039 Runtime rt = Runtime.getRuntime();
1040 process = rt.exec(commands);
1041 process.waitFor();
1042 }
1043 else {
1044 DebugStream.println("Running " + command);
1045 Runtime rt = Runtime.getRuntime();
1046 process = rt.exec(command);
1047 process.waitFor();
1048 }
1049 }
1050 catch (Exception exception) {
1051 DebugStream.printStackTrace(exception);
1052 }
1053 // Remove ourself from Gatherer list of threads.
1054 apps.remove(this);
1055 // Call exit if we were the last outstanding child process thread.
1056 if (apps.size() == 0 && exit == true) {
1057 // In my opinion (DB) there is no need to exit here,
1058 // the 'run' method ending naturally brings this
1059 // thread to an end. In fact it is potentially
1060 // dangerous to exit here, as the main thread in the
1061 // Gatherer class may be stopped prematurely. As it so
1062 // happens the point at which the ExternalApplication thread
1063 // is asked to stop (Back in the main Gatherer thread) is after
1064 // various configuration files have been saved.
1065 //
1066 // A similar argument holds for BrowserApplication thread below.
1067 System.exit(exit_status);
1068 }
1069 }
1070 public void stopExternalApplication() {
1071 if(process != null) {
1072 process.destroy();
1073 }
1074 }
1075 }
1076 /** 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. */
1077 static private class BrowserApplication
1078 extends Thread {
1079 private Process process = null;
1080 /** The initial command string given to this sub-process. */
1081 private String command = null;
1082 private String url = null;
1083 private String[] commands = null;
1084
1085 public BrowserApplication(String command, String url) {
1086 StringTokenizer st = new StringTokenizer(command);
1087 int num_tokens = st.countTokens();
1088 this.commands = new String [num_tokens];
1089 int i=0;
1090 while (st.hasMoreTokens()) {
1091 commands[i] = st.nextToken();
1092 i++;
1093 }
1094 //this.commands = commands;
1095 this.url = url;
1096 }
1097 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1098 * @see java.lang.Exception
1099 * @see java.lang.Process
1100 * @see java.lang.Runtime
1101 * @see java.lang.System
1102 * @see java.util.Vector
1103 */
1104 public void run() {
1105 // Call an external process using the args.
1106 if(commands == null) {
1107 apps.remove(this);
1108 return;
1109 }
1110 try {
1111 String prog_name = commands[0];
1112 String lower_name = prog_name.toLowerCase();
1113 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
1114 DebugStream.println("found mozilla or netscape, trying remote it");
1115 // mozilla and netscape, try using a remote command to get things in the same window
1116 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
1117 printArray(new_commands);
1118
1119 Runtime rt = Runtime.getRuntime();
1120 process = rt.exec(new_commands);
1121 int exitCode = process.waitFor();
1122 if (exitCode != 0) { // if Netscape or mozilla was not open
1123 DebugStream.println("couldn't do remote, trying original command");
1124 printArray(commands);
1125 process = rt.exec(commands); // try the original command
1126 }
1127 } else {
1128 // just run what we have been given
1129 StringBuffer whole_command = new StringBuffer();
1130 for(int i = 0; i < commands.length; i++) {
1131 whole_command.append(commands[i]);
1132 whole_command.append(" ");
1133 }
1134 DebugStream.println("Running " + whole_command.toString());
1135 Runtime rt = Runtime.getRuntime();
1136 process = rt.exec(commands);
1137 process.waitFor();
1138 }
1139 }
1140
1141 catch (Exception exception) {
1142 DebugStream.printStackTrace(exception);
1143 }
1144 // Remove ourself from Gatherer list of threads.
1145 apps.remove(this);
1146 // Call exit if we were the last outstanding child process thread.
1147 if (apps.size() == 0 && exit == true) {
1148 System.exit(exit_status);
1149 }
1150 }
1151 public void printArray(String [] array) {
1152 for(int i = 0; i < array.length; i++) {
1153 DebugStream.print(array[i]+" ");
1154 System.err.println(array[i]+" ");
1155 }
1156 }
1157 public void stopBrowserApplication() {
1158 if(process != null) {
1159 process.destroy();
1160 }
1161 }
1162 }
1163
1164
1165 private class ImageMagickTest
1166 {
1167 public boolean found()
1168 {
1169 try {
1170 String[] command = new String[2];
1171 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
1172 command[1] = "-version";
1173 Process image_magick_process = Runtime.getRuntime().exec(command);
1174 image_magick_process.waitFor();
1175
1176 //new way of detection of ImageMagick
1177 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
1178
1179 BufferedReader br = new BufferedReader(isr);
1180 // Capture the standard output stream and seach for two particular occurances: Version and ImageMagick.
1181
1182 String line = br.readLine();
1183 if (line == null) {
1184 return false;
1185 }
1186 String lc_line = line.toLowerCase();
1187 if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
1188 return true;
1189 } else {
1190 return false;
1191 }
1192
1193 //return (image_magick_process.exitValue() == 0);
1194 }
1195 catch (IOException exception) {
1196 return false;
1197 }
1198 catch (InterruptedException exception) {
1199 return false;
1200 }
1201 }
1202 }
1203
1204
1205 private class PerlTest
1206 {
1207 private String[] command = new String[2];
1208
1209 public PerlTest()
1210 {
1211 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
1212 command[1] = "-version";
1213 }
1214
1215 public boolean found()
1216 {
1217 try {
1218 Process perl_process = Runtime.getRuntime().exec(command);
1219 perl_process.waitFor();
1220 return (perl_process.exitValue() == 0);
1221 }
1222 catch (Exception exception) {
1223 return false;
1224 }
1225 }
1226
1227 public String toString() {
1228 return command[0];
1229 }
1230 }
1231}
Note: See TracBrowser for help on using the repository browser.