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

Last change on this file since 24238 was 24238, checked in by sjm84, 13 years ago

Fixed a few problems with Tomcat auto-starting when GLI starts on Linux

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