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

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

Gatherer ensures that the GLI user cache directory exists, so that the Downloaded Files folder in the WorkspaceTree view is visible. No refresh of the Workspace window is needed when a download is performed

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