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

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

Changes for when client FLI runs against a remote GS server and remote Fedora

  • Property svn:keywords set to Author Date Id Revision
File size: 43.3 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 = "svn-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
322 // Start up the local library server, if that's what we want
323 if (Utility.isWindows() && local_library_path != null && !isGsdlRemote && !GS3) {
324 LocalLibraryServer.start(gsdl_path, local_library_path);
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("general.open_collection", true);
447 }
448 if (no_load || open_collection_file_path.equals("")) {
449 open_collection_file_path = null;
450 }
451 }
452 catch (Exception exception) {
453 DebugStream.printStackTrace(exception);
454 }
455
456
457 // Create GUI Manager (last) or else suffer the death of a thousand NPE's
458 g_man = new GUIManager(size);
459
460 // Get a list of the core Greenstone classifiers and plugins
461 Classifiers.loadClassifiersList(null);
462 Plugins.loadPluginsList(null);
463
464 // If using a remote Greenstone we need to download the collection configurations now
465 if (Gatherer.isGsdlRemote) {
466 if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
467 // !! Something went wrong downloading the collection configurations
468 System.err.println("Error: Could not download collection configurations.");
469 System.exit(0);
470 }
471 }
472
473 }
474
475
476 /** Returns the correct version of the (local or remote) Greenstone server if init() has already been called. */
477 public static int serverVersionNumber() {
478 return GS3 ? 3 : 2;
479 }
480
481 /** Returns "Server: version number" if init() has already been called. */
482 public static String getServerVersionAsString() {
483 return "Server: v" + serverVersionNumber();
484 }
485
486 public void openGUI()
487 {
488 // Size and place the frame on the screen
489 Rectangle bounds = Configuration.getBounds("general.bounds", true);
490 if (bounds == null) {
491 // Choose a sensible default value
492 bounds = new Rectangle(0, 0, 640, 480);
493 }
494
495 // Ensure width and height are reasonable
496 size = bounds.getSize();
497 if (size.width < 640) {
498 size.width = 640;
499 }
500 else if (size.width > Configuration.screen_size.width && Configuration.screen_size.width > 0) {
501 size.width = Configuration.screen_size.width;
502 }
503 if (size.height < 480) {
504 size.height = 480;
505 }
506 else if (size.height > Configuration.screen_size.height && Configuration.screen_size.height > 0) {
507 size.height = Configuration.screen_size.height;
508 }
509
510 if (!g_man_built) {
511
512 g_man.display();
513
514 // 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).
515 g_man.setLocation(bounds.x, bounds.y);
516 g_man.setVisible(true);
517
518 // After the window has been made visible, check that it is in the correct place
519 // sometimes java places a window not in the correct place,
520 // but with an offset. If so, we work out what the offset is
521 // and change the desired location to take that into account
522 Point location = g_man.getLocation();
523 int x_offset = bounds.x - location.x;
524 int y_offset = bounds.y - location.y;
525 // If not, offset the window to move it into the correct location
526 if (x_offset > 0 || y_offset > 0) {
527 ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset));
528 g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset);
529 }
530
531 // 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.
532 g_man.afterDisplay();
533 g_man_built = true;
534 }
535 else {
536 g_man.setVisible(true);
537 }
538
539 // Get a list of the core Greenstone classifiers and plugins
540 /*Classifiers.loadClassifiersList(null);
541 Plugins.loadPluginsList(null);
542
543 // If using a remote Greenstone we need to download the collection configurations now
544 if (Gatherer.isGsdlRemote) {
545 if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
546 // !! Something went wrong downloading the collection configurations
547 System.err.println("Error: Could not download collection configurations.");
548 System.exit(0);
549 }
550 }*/
551
552 // If there was a collection left open last time, reopen it
553 if (open_collection_file_path == null) {
554
555 //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
556 setMenuBarEnabled(true);
557 } else {
558
559 // If there was a collection left open last time, reopen it
560 c_man.openCollectionFromLastTime();
561 }
562 }
563
564 public static void setMenuBarEnabled(boolean enabled) {
565 g_man.menu_bar.file.setEnabled(enabled);
566 g_man.menu_bar.edit.setEnabled(enabled);
567 }
568
569 /** Exits the Gatherer after ensuring that things needing saving are saved.
570 * @see java.io.FileOutputStream
571 * @see java.io.PrintStream
572 * @see java.lang.Exception
573 * @see javax.swing.JOptionPane
574 * @see org.greenstone.gatherer.Configuration
575 * @see org.greenstone.gatherer.collection.CollectionManager
576 * @see org.greenstone.gatherer.gui.GUIManager
577 */
578 static public void exit(int new_exit_status)
579 {
580 DebugStream.println("In Gatherer.exit()...");
581 exit = true;
582 if (new_exit_status != 0) {
583 // default exit_status is already 0
584 // only remember a new exit status if it is non-trivial
585 exit_status = new_exit_status;
586 }
587
588 // Save the file associations
589 if (assoc_man != null) {
590 assoc_man.save();
591 assoc_man = null;
592 }
593
594 // Get the gui to deallocate
595 if(g_man != null) {
596 g_man.destroy();
597 g_man_built = false;
598 }
599
600 // Flush debug
601 DebugStream.closeDebugStream();
602
603 // If we started a server, we should try to stop it.
604 if (LocalLibraryServer.isRunning() == true) {
605 LocalLibraryServer.stop();
606 }
607
608 // If we're using a remote Greenstone server we need to make sure that all jobs have completed first
609 if (isGsdlRemote) {
610 RemoteGreenstoneServer.exit();
611 }
612
613 // Make sure we haven't started up any processes that are still running
614 if (apps.size() == 0) {
615 // If we're running as an applet we don't actually quit (just hide the main GLI window)
616 if (!Gatherer.isApplet) {
617 // This is the end...
618 System.exit(exit_status);
619 }
620 }
621 else {
622 JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
623 g_man.setVisible(false);
624 }
625 }
626
627 static public void exit()
628 {
629 exit(0);
630 }
631
632 /** Returns the path of the Greenstone "collect" directory. */
633 static public String getCollectDirectoryPath()
634 {
635 if (non_standard_collect_directory_path != null) {
636 return non_standard_collect_directory_path;
637 }
638
639 if (!GS3) {
640 return Configuration.gsdl_path + "collect" + File.separator;
641 }
642 else {
643 return getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect" + File.separator;
644 }
645 }
646
647
648 /** Returns the path of the GLI directory. */
649 static public String getGLIDirectoryPath()
650 {
651 return gli_directory_path;
652 }
653
654
655 /** Returns the path of the GLI "metadata" directory. */
656 static public String getGLIMetadataDirectoryPath()
657 {
658 return getGLIDirectoryPath() + "metadata" + File.separator;
659 }
660
661
662 /** Returns the path of the GLI user directory. */
663 static public String getGLIUserDirectoryPath()
664 {
665 return gli_user_directory_path;
666 }
667
668
669 /** Returns the path of the GLI user "cache" directory. */
670 static public String getGLIUserCacheDirectoryPath()
671 {
672 return getGLIUserDirectoryPath() + "cache" + File.separator;
673 }
674
675
676 /** Returns the path of the GLI user "log" directory. */
677 static public String getGLIUserLogDirectoryPath()
678 {
679 return getGLIUserDirectoryPath() + "log" + File.separator;
680 }
681
682
683 static public String getSitesDirectoryPath()
684 {
685 return Configuration.gsdl3_path + "sites" + File.separator;
686 }
687
688
689 static public void setCollectDirectoryPath(String collect_directory_path)
690 {
691 non_standard_collect_directory_path = collect_directory_path;
692 if (!non_standard_collect_directory_path.endsWith(File.separator)) {
693 non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator;
694 }
695 }
696
697
698 static public void setGLIDirectoryPath(String gli_directory_path_arg)
699 {
700 gli_directory_path = gli_directory_path_arg;
701 }
702
703
704 static public void setGLIUserDirectoryPath(String gli_user_directory_path_arg)
705 {
706 gli_user_directory_path = gli_user_directory_path_arg;
707
708 // Ensure the GLI user directory exists
709 File gli_user_directory = new File(gli_user_directory_path);
710 if (!gli_user_directory.exists() && !gli_user_directory.mkdirs()) {
711 System.err.println("Error: Unable to make directory: " + gli_user_directory);
712 }
713 }
714
715
716 static public void refresh(int refresh_reason)
717 {
718 if (g_man != null) {
719
720 g_man.refresh(refresh_reason, c_man.ready());
721 }
722
723 // Now is a good time to force a garbage collect
724 System.gc();
725 }
726
727
728 // used to send reload coll messages to the tomcat server
729 static public void configGS3Server(String site, String command) {
730 if (Configuration.library_url == null){
731 System.out.println("Error: you have not provide the Greenstone Library address.");
732 return;
733
734 }
735
736 try {
737 // need to add the servlet name to the exec address
738 String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command;
739 URL url = new URL(raw_url);
740 DebugStream.println("Action: " + raw_url);
741 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
742 int response_code = library_connection.getResponseCode();
743 if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
744 DebugStream.println("200 - Complete.");
745 }
746 else {
747 DebugStream.println("404 - Failed.");
748 }
749 url = null;
750 }
751 catch (Exception exception) {
752 DebugStream.printStackTrace(exception);
753 }
754 }
755
756
757 /** Used to 'spawn' a new child application when a file is double clicked.
758 * @param file The file to open
759 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
760 */
761 static public void spawnApplication(File file) {
762 String [] commands = assoc_man.getCommand(file);
763 if(commands != null) {
764 ExternalApplication app = new ExternalApplication(commands);
765 apps.add(app);
766 app.start();
767 }
768 else {
769 ///ystem.err.println("No open command available.");
770 }
771 }
772
773
774 static public void spawnApplication(String command)
775 {
776 ExternalApplication app = new ExternalApplication(command);
777 apps.add(app);
778 app.start();
779 }
780
781
782 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
783 * @param url The url to open the browser at
784 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
785 */
786 static public void spawnBrowser(String url) {
787 String command = assoc_man.getBrowserCommand(url);
788 if (command != null) {
789 BrowserApplication app = new BrowserApplication(command, url);
790 apps.add(app);
791 app.start();
792 }
793 else {
794 ///ystem.err.println("No browser command available.");
795 }
796 }
797
798
799 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
800 */
801 static private void missingEXEC() {
802 WarningDialog dialog;
803 if (GS3) {
804 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), "general.library_url", false);
805 }else {
806 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), "general.library_url", false);
807 }
808 dialog.setValueField(new URLField.Text(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false)));
809 dialog.display();
810 dialog.dispose();
811 dialog = null;
812
813 String library_url_string = Configuration.getString("general.library_url", true);
814 if (!library_url_string.equals("")) {
815 try {
816 Configuration.library_url = new URL(library_url_string);
817 }
818 catch (MalformedURLException exception) {
819 DebugStream.printStackTrace(exception);
820 }
821 }
822 }
823
824
825
826 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
827 */
828 static private void popupFedoraInfo() {
829
830 FedoraLogin dialog = new FedoraLogin("Fedora Login", false);
831
832 if (Configuration.library_url == null) {
833
834 String library_url_string = dialog.getLibraryURL();
835 if (!library_url_string.equals("")) {
836 try {
837 Configuration.library_url = new URL(library_url_string);
838 }
839 catch (MalformedURLException exception) {
840 DebugStream.printStackTrace(exception);
841 }
842 }
843 }
844
845 boolean showLogin = true;
846
847 do {
848 if(!dialog.loginRequested()) { // user pressed cancel to exit the FedoraLogin dialog
849 System.exit(0);
850 } else {
851 showLogin = dialog.loginRequested();
852 String hostname = dialog.getHostname();
853 String port = dialog.getPort();
854 String username = dialog.getUsername();
855 String password = dialog.getPassword();
856 String protocol = dialog.getProtocol();
857
858 Configuration.fedora_info.setHostname(hostname);
859 Configuration.fedora_info.setPort(port);
860 Configuration.fedora_info.setUsername(username);
861 Configuration.fedora_info.setPassword(password);
862 Configuration.fedora_info.setProtocol(protocol);
863
864 String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora";
865 String login_str = username + ":" + password;
866
867 String login_encoding = new sun.misc.BASE64Encoder().encode(login_str.getBytes());
868
869 try {
870 URL ping_url = new URL(ping_url_str);
871 URLConnection uc = ping_url.openConnection();
872 uc.setRequestProperty ("Authorization", "Basic " + login_encoding);
873 // Attempt to access some content ...
874 InputStream content = (InputStream)uc.getInputStream();
875
876 // if no exception occurred in the above, we would have come here:
877 showLogin = false;
878 dialog.dispose();
879 }
880 catch (Exception exception) {
881 // TODO: move into dictionary
882 String[] errorMessage = {"Failed to connect to the Fedora server.", "It might not be running, or",
883 "incorrect username and/or password."};
884 dialog.setErrorMessage(errorMessage);
885 //DebugStream.printStackTrace(exception);
886 // exception occurred, show the dialog again (do this after printing to
887 // debugStream, else the above does not get done for some reason).
888 dialog.setVisible(true);
889 }
890 }
891 } while(showLogin);
892
893 dialog = null; // no more need of the dialog
894
895 // Now we are connected.
896 }
897
898
899
900 static private void requestGLIServerURL()
901 {
902 WarningDialog dialog;
903 String[] defaultURLs = {
904 "http://localhost:8080/greenstone3/cgi-bin/gliserver.pl",
905 "http://localhost:8080/gsdl/cgi-bin/gliserver.pl"
906 };
907
908 // Warning dialog with no cancel button and no "turn off warning" checkbox
909 // (since user-input of the gliserver script is mandatory)
910 dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false, false);
911
912 dialog.setValueField(new URLField.DropDown(Configuration.getColor("coloring.editable_foreground", false),
913 Configuration.getColor("coloring.editable_background", false),
914 defaultURLs, "general.gliserver_url", "general.open_collection",
915 "gliserver.pl"));
916
917 if (Gatherer.default_gliserver_url!=null){
918 dialog.setValueField(Gatherer.default_gliserver_url.toString());
919 }
920
921 // A WarningDialog cannot always be made to respond (let alone to exit the program) on close. We
922 // handle the response of this particular WarningDialog here: a URL for gliserver.pl is a crucial
923 // piece of user-provided data. Therefore, if no URL was entered for gliserver.pl, it'll exit safely.
924 dialog.addWindowListener(new WindowAdapter() {
925 public void windowClosing(WindowEvent e) {
926 Gatherer.exit();
927 }
928 });
929
930 dialog.display();
931 dialog.dispose();
932 dialog = null;
933
934
935 String gliserver_url_string = Configuration.getString("general.gliserver_url", true);
936 if (!gliserver_url_string.equals("")) {
937 try {
938 Configuration.gliserver_url = new URL(gliserver_url_string);
939 Configuration.setString("general.gliserver_url", true, gliserver_url_string);
940 }
941 catch (MalformedURLException exception) {
942 DebugStream.printStackTrace(exception);
943 }
944 }
945 }
946
947
948 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
949 */
950 static private void missingGSDL() {
951 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false);
952 dialog.display();
953 dialog.dispose();
954 dialog = null;
955 }
956
957 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
958 static private void missingImageMagick() {
959 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false);
960 dialog.display();
961 dialog.dispose();
962 dialog = null;
963 }
964
965 /** 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. */
966 static private void missingPERL() {
967 WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false);
968 dialog.display();
969 dialog.dispose();
970 dialog = null;
971 }
972
973
974 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
975 * @see java.lang.Exception
976 * @see java.lang.System
977 * @see java.net.Authenticator
978 * @see org.greenstone.gatherer.Configuration
979 * @see org.greenstone.gatherer.GAuthenticator
980 */
981 static public void setProxy() {
982 try {// Can throw several exceptions
983 if(Configuration.get("general.use_proxy", true)) {
984 System.setProperty("http.proxyType", "4");
985 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
986 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
987 System.setProperty("http.proxySet", "true");
988 } else {
989 System.setProperty("http.proxyHost", "");
990 System.setProperty("http.proxyPort", "");
991 System.setProperty("http.proxySet", "false");
992 }
993 } catch (Exception error) {
994 DebugStream.println("Error in Gatherer.initProxy(): " + error);
995 DebugStream.printStackTrace(error);
996 }
997 }
998
999
1000 /** 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. */
1001 static private class ExternalApplication
1002 extends Thread {
1003 private Process process = null;
1004 /** The initial command string given to this sub-process. */
1005 private String command = null;
1006 private String[] commands = null;
1007 /** Constructor.
1008 * @param command The initial command <strong>String</strong>.
1009 */
1010 public ExternalApplication(String command) {
1011 this.command = command;
1012 }
1013
1014 public ExternalApplication(String[] commands) {
1015 this.commands = commands;
1016 }
1017 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1018 * @see java.lang.Exception
1019 * @see java.lang.Process
1020 * @see java.lang.Runtime
1021 * @see java.lang.System
1022 * @see java.util.Vector
1023 */
1024 public void run() {
1025 // Call an external process using the args.
1026 try {
1027 if(commands != null) {
1028 StringBuffer whole_command = new StringBuffer();
1029 for(int i = 0; i < commands.length; i++) {
1030 whole_command.append(commands[i]);
1031 whole_command.append(" ");
1032 }
1033 DebugStream.println("Running " + whole_command.toString());
1034 Runtime rt = Runtime.getRuntime();
1035 process = rt.exec(commands);
1036 process.waitFor();
1037 }
1038 else {
1039 DebugStream.println("Running " + command);
1040 Runtime rt = Runtime.getRuntime();
1041 process = rt.exec(command);
1042 process.waitFor();
1043 }
1044 }
1045 catch (Exception exception) {
1046 DebugStream.printStackTrace(exception);
1047 }
1048 // Remove ourself from Gatherer list of threads.
1049 apps.remove(this);
1050 // Call exit if we were the last outstanding child process thread.
1051 if (apps.size() == 0 && exit == true) {
1052 // In my opinion (DB) there is no need to exit here,
1053 // the 'run' method ending naturally brings this
1054 // thread to an end. In fact it is potentially
1055 // dangerous to exit here, as the main thread in the
1056 // Gatherer class may be stopped prematurely. As it so
1057 // happens the point at which the ExternalApplication thread
1058 // is asked to stop (Back in the main Gatherer thread) is after
1059 // various configuration files have been saved.
1060 //
1061 // A similar argument holds for BrowserApplication thread below.
1062 System.exit(exit_status);
1063 }
1064 }
1065 public void stopExternalApplication() {
1066 if(process != null) {
1067 process.destroy();
1068 }
1069 }
1070 }
1071 /** 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. */
1072 static private class BrowserApplication
1073 extends Thread {
1074 private Process process = null;
1075 /** The initial command string given to this sub-process. */
1076 private String command = null;
1077 private String url = null;
1078 private String[] commands = null;
1079
1080 public BrowserApplication(String command, String url) {
1081 StringTokenizer st = new StringTokenizer(command);
1082 int num_tokens = st.countTokens();
1083 this.commands = new String [num_tokens];
1084 int i=0;
1085 while (st.hasMoreTokens()) {
1086 commands[i] = st.nextToken();
1087 i++;
1088 }
1089 //this.commands = commands;
1090 this.url = url;
1091 }
1092 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1093 * @see java.lang.Exception
1094 * @see java.lang.Process
1095 * @see java.lang.Runtime
1096 * @see java.lang.System
1097 * @see java.util.Vector
1098 */
1099 public void run() {
1100 // Call an external process using the args.
1101 if(commands == null) {
1102 apps.remove(this);
1103 return;
1104 }
1105 try {
1106 String prog_name = commands[0];
1107 String lower_name = prog_name.toLowerCase();
1108 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
1109 DebugStream.println("found mozilla or netscape, trying remote it");
1110 // mozilla and netscape, try using a remote command to get things in the same window
1111 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
1112 printArray(new_commands);
1113
1114 Runtime rt = Runtime.getRuntime();
1115 process = rt.exec(new_commands);
1116 int exitCode = process.waitFor();
1117 if (exitCode != 0) { // if Netscape or mozilla was not open
1118 DebugStream.println("couldn't do remote, trying original command");
1119 printArray(commands);
1120 process = rt.exec(commands); // try the original command
1121 }
1122 } else {
1123 // just run what we have been given
1124 StringBuffer whole_command = new StringBuffer();
1125 for(int i = 0; i < commands.length; i++) {
1126 whole_command.append(commands[i]);
1127 whole_command.append(" ");
1128 }
1129 DebugStream.println("Running " + whole_command.toString());
1130 Runtime rt = Runtime.getRuntime();
1131 process = rt.exec(commands);
1132 process.waitFor();
1133 }
1134 }
1135
1136 catch (Exception exception) {
1137 DebugStream.printStackTrace(exception);
1138 }
1139 // Remove ourself from Gatherer list of threads.
1140 apps.remove(this);
1141 // Call exit if we were the last outstanding child process thread.
1142 if (apps.size() == 0 && exit == true) {
1143 System.exit(exit_status);
1144 }
1145 }
1146 public void printArray(String [] array) {
1147 for(int i = 0; i < array.length; i++) {
1148 DebugStream.print(array[i]+" ");
1149 System.err.println(array[i]+" ");
1150 }
1151 }
1152 public void stopBrowserApplication() {
1153 if(process != null) {
1154 process.destroy();
1155 }
1156 }
1157 }
1158
1159
1160 private class ImageMagickTest
1161 {
1162 public boolean found()
1163 {
1164 try {
1165 String[] command = new String[2];
1166 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
1167 command[1] = "-version";
1168 Process image_magick_process = Runtime.getRuntime().exec(command);
1169 image_magick_process.waitFor();
1170
1171 //new way of detection of ImageMagick
1172 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
1173
1174 BufferedReader br = new BufferedReader(isr);
1175 // Capture the standard output stream and seach for two particular occurances: Version and ImageMagick.
1176
1177 String line = br.readLine();
1178 if (line == null) {
1179 return false;
1180 }
1181 String lc_line = line.toLowerCase();
1182 if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
1183 return true;
1184 } else {
1185 return false;
1186 }
1187
1188 //return (image_magick_process.exitValue() == 0);
1189 }
1190 catch (IOException exception) {
1191 return false;
1192 }
1193 catch (InterruptedException exception) {
1194 return false;
1195 }
1196 }
1197 }
1198
1199
1200 private class PerlTest
1201 {
1202 private String[] command = new String[2];
1203
1204 public PerlTest()
1205 {
1206 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
1207 command[1] = "-version";
1208 }
1209
1210 public boolean found()
1211 {
1212 try {
1213 Process perl_process = Runtime.getRuntime().exec(command);
1214 perl_process.waitFor();
1215 return (perl_process.exitValue() == 0);
1216 }
1217 catch (Exception exception) {
1218 return false;
1219 }
1220 }
1221
1222 public String toString() {
1223 return command[0];
1224 }
1225 }
1226}
Note: See TracBrowser for help on using the repository browser.