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

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

Changes to work with the remote GS server knowing the server version and informing the GLI client application. 1. Changes to work with gliserver.pl's get-greenstone-server-version rather than being passed this from the client-gli scripts. Now the server will tell the client what version of Greenstone it is running (GS2 or GS3). However, this has made it compulsory now for the gliserver URL request dialog to always be displayed upon startup. It will remember upto 5 URLs entered previously along with the collection that was left open last time GLI ran against that gliserver URL. These changes have been made possible by merging config3.xml into config.xml and configRemote3.xml into configRemote.xml. 2. Minor bugfix: previously g_man was called to destroy itself even though it could be (and was occasionally) null.

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