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

Last change on this file since 14974 was 14974, checked in by davidb, 16 years ago

Changes to GLI to support export into Fedora. New utility called flisvn diff gems/MetadataSetManager.java

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