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

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

For FLI: Removed gsdl.xml file creation from java file Gatherer.java and moved the functionality into Perl (g2futil.pm, called by g2f-import.pl). Now it should work with a remote GS3 server. Tested when FLI is running on GS3 as well as GS2

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