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

Last change on this file since 19306 was 19306, checked in by ak19, 15 years ago

The download panel should be available in client-gli if there's a gs2build folder inside the client-gli checkout. In such a case, the gsdl flag is set to the gs2build folder in client-gli.sh/bat scripts, and gli is launched with this. Still only partially complete: the download panel is available AND it no longer throws an exception when you try to download, but it doesn't download anything either at the moment.

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