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

Last change on this file since 24288 was 24288, checked in by ak19, 13 years ago

If the PDFBox extension is not installed, GLI will now display a dialog on start-up informing the user where they can get PDFBox from if they want to process PDF files of v1.5 and greater. This is by way of introducing users to this facility.

  • Property svn:keywords set to Author Date Id Revision
File size: 59.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 // Check for PDFBox
450 if (Gatherer.isGsdlRemote) {
451 DebugStream.println("Not checking for PDFBox.");
452 }
453 else {
454 String gs_dir = GS3 ? gsdl3_src_path : gsdl_path;
455 File pdfboxExtensionFolder = new File(gs_dir+File.separator+"ext"+File.separator+"pdf-box");
456 if (!(pdfboxExtensionFolder.exists() && pdfboxExtensionFolder.isDirectory())) {
457 // The user doesn't have PDFBox, inform them of it
458 String zipExtension = Utility.isWindows() ? "zip" : "tar.gz";
459 missingPDFBox(zipExtension, pdfboxExtensionFolder.getParent());
460 }
461 }
462
463
464 if (Gatherer.isGsdlRemote) {
465 DebugStream.println("Not checking for perl path/exe");
466 }
467 else {
468 // Perl path is a little different as it is perfectly ok to
469 // start the GLI without providing a perl path
470 boolean found_perl = false;
471 if (Configuration.perl_path != null) {
472 // See if the file pointed to actually exists
473 File perl_file = new File(Configuration.perl_path);
474 found_perl = perl_file.exists();
475 perl_file = null;
476 }
477 if (Configuration.perl_path == null || !found_perl) {
478 // Run test to see if we can run perl as is.
479 PerlTest perl_test = new PerlTest();
480 if (perl_test.found()) {
481 // If so replace the perl path with the system
482 // default (or null for unix).
483 Configuration.perl_path = perl_test.toString();
484 found_perl = true;
485 }
486 }
487 if (!found_perl) {
488 // Time for an error message.
489 missingPERL();
490 }
491 }
492
493 // Check that the local can support multiple filename encodings
494 //System.err.println("#### Java identifies current Locale as (file.encoding): "
495 // + System.getProperty("file.encoding"));
496 if(System.getProperty("file.encoding").equals("UTF-8")){
497 // If the locale is UTF-8, Java will interpret all filename bytes as UTF-8,
498 // which is a destructive process as it will convert characters not recognised
499 // by UTF-8 into the invalid character, rather than preserving the bytecodes.
500 // This has the effect that non-UTF8 encoded filenames on a system set to a
501 // UTF-8 locale are not 'seen' by Java (if they contain non-ASCII characters).
502 multipleFilenameEncodingsNotSupported();
503 FilenameEncoding.MULTIPLE_FILENAME_ENCODINGS_SUPPORTED = false;
504 FilenameEncoding.URL_FILE_SEPARATOR = File.separator;
505 } else {
506 FilenameEncoding.MULTIPLE_FILENAME_ENCODINGS_SUPPORTED = true;
507 FilenameEncoding.URL_FILE_SEPARATOR = "/"; // URL file separator is always "/"
508 }
509
510 // Set the default font for all Swing components.
511 FontUIResource default_font = Configuration.getFont("general.font", true);
512 Enumeration keys = UIManager.getDefaults().keys();
513 while (keys.hasMoreElements()) {
514 Object key = keys.nextElement();
515 Object value = UIManager.get(key);
516 if (value instanceof FontUIResource) {
517 UIManager.put(key, default_font);
518 }
519 }
520
521 // At this point (which is where this code originally used to be), we can set up the proxy for the
522 // non-remote case. The remote Greenstone server would already have setup its proxy when required.
523 if(!isGsdlRemote) {
524 setProxy();
525 // Now we set up an Authenticator
526 Authenticator.setDefault(new GAuthenticator());
527 }
528 assoc_man = new FileAssociationManager();
529 // Create File Manager
530 f_man = new FileManager();
531 // Create Collection Manager
532 c_man = new CollectionManager();
533 // Create Recycle Bin
534 recycle_bin = new RecycleBin();
535
536 if (GS3) {
537 if (site_name==null) {
538 site_name = Configuration.site_name;
539 servlet_path = null; // need to reset this
540 }
541 if (servlet_path == null) {
542 servlet_path = Configuration.getServletPath();
543 }
544 }
545
546
547 } catch (Exception exception) {
548 DebugStream.printStackTrace(exception);
549 }
550
551
552 // Create GUI Manager (last) or else suffer the death of a thousand NPE's
553 g_man = new GUIManager(size);
554
555 // Get a list of the core Greenstone classifiers and plugins
556 Classifiers.loadClassifiersList(null);
557 Plugins.loadPluginsList(null);
558
559 // If using a remote Greenstone we need to download the collection configurations now
560 if (Gatherer.isGsdlRemote) {
561 if (remoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
562 // !! Something went wrong downloading the collection configurations
563 System.err.println("Error: Could not download collection configurations.");
564 if(!Gatherer.isApplet) { // don't close the browser if it is an applet!
565 System.exit(0);
566 }
567 }
568 }
569 }
570
571
572 /** Returns the correct version of the (local or remote) Greenstone server if init() has already been called. */
573 public static int serverVersionNumber() {
574 return GS3 ? 3 : 2;
575 }
576
577 /** Returns "Server: version number" if init() has already been called. */
578 public static String getServerVersionAsString() {
579 return "Server: v" + serverVersionNumber();
580 }
581
582 public void openGUI()
583 {
584 // Size and place the frame on the screen
585 Rectangle bounds = Configuration.getBounds("general.bounds", true);
586 if (bounds == null) {
587 // Choose a sensible default value
588 bounds = new Rectangle(0, 0, 640, 480);
589 }
590
591 // Ensure width and height are reasonable
592 size = bounds.getSize();
593 if (size.width < 640) {
594 size.width = 640;
595 }
596 else if (size.width > Configuration.screen_size.width && Configuration.screen_size.width > 0) {
597 size.width = Configuration.screen_size.width;
598 }
599 if (size.height < 480) {
600 size.height = 480;
601 }
602 else if (size.height > Configuration.screen_size.height && Configuration.screen_size.height > 0) {
603 size.height = Configuration.screen_size.height;
604 }
605
606 if (!g_man_built) {
607
608 g_man.display();
609
610 // 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).
611 g_man.setLocation(bounds.x, bounds.y);
612 g_man.setVisible(true);
613
614 // After the window has been made visible, check that it is in the correct place
615 // sometimes java places a window not in the correct place,
616 // but with an offset. If so, we work out what the offset is
617 // and change the desired location to take that into account
618 Point location = g_man.getLocation();
619 int x_offset = bounds.x - location.x;
620 int y_offset = bounds.y - location.y;
621 // If not, offset the window to move it into the correct location
622 if (x_offset > 0 || y_offset > 0) {
623 ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset));
624 g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset);
625 }
626
627 // 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.
628 g_man.afterDisplay();
629 g_man_built = true;
630 }
631 else {
632 g_man.setVisible(true);
633 }
634
635 // Get a list of the core Greenstone classifiers and plugins
636 /*Classifiers.loadClassifiersList(null);
637 Plugins.loadPluginsList(null);
638
639 // If using a remote Greenstone we need to download the collection configurations now
640 if (Gatherer.isGsdlRemote) {
641 if (remoteGreenstoneServer.downloadCollectionConfigurations().equals("")) {
642 // !! Something went wrong downloading the collection configurations
643 System.err.println("Error: Could not download collection configurations.");
644 System.exit(0);
645 }
646 }*/
647
648 // If there was a collection left open last time, reopen it
649 if (open_collection_file_path == null || new File(Gatherer.open_collection_file_path).isDirectory()) {
650
651 //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
652 setMenuBarEnabled(true);
653 } else {
654
655 // If there was a collection left open last time, reopen it
656 c_man.openCollectionFromLastTime();
657 }
658 }
659
660 public static void setMenuBarEnabled(boolean enabled) {
661 g_man.menu_bar.file.setEnabled(enabled);
662 g_man.menu_bar.edit.setEnabled(enabled);
663 }
664
665 /** Exits the Gatherer after ensuring that things needing saving are saved.
666 * @see java.io.FileOutputStream
667 * @see java.io.PrintStream
668 * @see java.lang.Exception
669 * @see javax.swing.JOptionPane
670 * @see org.greenstone.gatherer.Configuration
671 * @see org.greenstone.gatherer.collection.CollectionManager
672 * @see org.greenstone.gatherer.gui.GUIManager
673 */
674 static public void exit(int new_exit_status)
675 {
676 DebugStream.println("In Gatherer.exit()...");
677 exit = true;
678 if (new_exit_status != 0) {
679 // default exit_status is already 0
680 // only remember a new exit status if it is non-trivial
681 exit_status = new_exit_status;
682 }
683
684 // Save the file associations
685 if (assoc_man != null) {
686 assoc_man.save();
687 assoc_man = null;
688 }
689
690 // Get the gui to deallocate
691 if(g_man != null) {
692 g_man.destroy();
693 g_man_built = false;
694 }
695
696 // Flush debug
697 DebugStream.closeDebugStream();
698
699 // If we started a server, we should try to stop it.
700 if (LocalLibraryServer.isRunning() == true) {
701 LocalLibraryServer.stop();
702 }
703
704 // If we're using a remote Greenstone server we need to make sure that all jobs have completed first
705 if (isGsdlRemote) {
706 remoteGreenstoneServer.exit();
707 }
708
709 // Make sure we haven't started up any processes that are still running
710 if (apps.size() == 0) {
711 // If we're running as an applet we don't actually quit (just hide the main GLI window)
712 if (!Gatherer.isApplet) {
713 // This is the end...
714 System.exit(exit_status);
715 }
716 }
717 else {
718 JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
719 g_man.setVisible(false);
720 }
721 }
722
723 static public void exit()
724 {
725 exit(0);
726 }
727
728 /** Returns the path of the current collect directory. */
729 static public String getCollectDirectoryPath()
730 {
731 if (non_standard_collect_directory_path != null) {
732 return non_standard_collect_directory_path;
733 }
734
735 return getDefaultGSCollectDirectoryPath(true); // file separator appended
736
737 }
738
739 // if we need to know whether the local server we are running is server.exe vs apache web server
740 static public boolean isPersistentServer() {
741 return (!isGsdlRemote && LocalLibraryServer.isPersistentServer());
742 }
743
744 /** Returns the path of the Greenstone "collect" directory. */
745 static public String getDefaultGSCollectDirectoryPath(boolean appendSeparator) {
746 String colDir;
747 if (GS3) {
748 colDir = getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect";
749 }
750 else {
751 colDir = Configuration.gsdl_path + "collect";
752 }
753
754 if(appendSeparator) {
755 colDir += File.separator;
756 }
757 return colDir;
758 }
759
760 /** Returns the path of the GLI directory. */
761 static public String getGLIDirectoryPath()
762 {
763 return gli_directory_path;
764 }
765
766
767 /** Returns the path of the GLI "metadata" directory. */
768 static public String getGLIMetadataDirectoryPath()
769 {
770 return getGLIDirectoryPath() + "metadata" + File.separator;
771 }
772
773
774 /** Returns the path of the GLI user directory. */
775 static public String getGLIUserDirectoryPath()
776 {
777 return gli_user_directory_path;
778 }
779
780
781 /** Returns the path of the GLI user "cache" directory. */
782 static public String getGLIUserCacheDirectoryPath()
783 {
784 return getGLIUserDirectoryPath() + "cache" + File.separator;
785 }
786
787
788 /** Returns the path of the GLI user "log" directory. */
789 static public String getGLIUserLogDirectoryPath()
790 {
791 return getGLIUserDirectoryPath() + "log" + File.separator;
792 }
793
794
795 static public String getSitesDirectoryPath()
796 {
797 return Configuration.gsdl3_path + "sites" + File.separator;
798 }
799
800
801 static public void setCollectDirectoryPath(String collect_directory_path)
802 {
803 non_standard_collect_directory_path = collect_directory_path;
804 if (!non_standard_collect_directory_path.endsWith(File.separator)) {
805 non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator;
806 }
807 }
808
809
810 static public void setGLIDirectoryPath(String gli_directory_path_arg)
811 {
812 gli_directory_path = gli_directory_path_arg;
813 }
814
815
816 static public void setGLIUserDirectoryPath(String gli_user_directory_path_arg)
817 {
818 gli_user_directory_path = gli_user_directory_path_arg;
819
820 // Ensure the GLI user directory exists
821 File gli_user_directory = new File(gli_user_directory_path);
822 if (!gli_user_directory.exists() && !gli_user_directory.mkdirs()) {
823 System.err.println("Error: Unable to make directory: " + gli_user_directory);
824 }
825 }
826
827
828 public static void initCollectDirectoryPath(String open_collection) {
829 String defaultColdir = getDefaultGSCollectDirectoryPath(false); // no file separator at end
830 String coldir = defaultColdir;
831 // If local GS and opening a collection outside the standard GS collect folder,
832 // need to open the non-standard collect folder that the collection resides in
833 if (!isGsdlRemote
834 && !open_collection_file_path.startsWith(defaultColdir))
835 {
836 File collectFolder = null;
837
838 if(!open_collection_file_path.equals("")) {
839 if(!open_collection_file_path.endsWith("gli.col")) { // then it's a collect folder
840 collectFolder = new File(open_collection_file_path);
841 } else {
842 // the filepath is a gli.col file. To get the collect folder: the 1st level up
843 // is the collection folder, 2 two levels up is the containing collect folder
844 collectFolder = new File(open_collection_file_path).getParentFile().getParentFile();
845 }
846
847 // Need to deal with colgroups as well: while there's an etc/collect.cfg
848 // in the current collectFolder, move one level up
849 String cfg_file = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
850 if(new File(collectFolder.getAbsolutePath()+File.separator+cfg_file).exists()) { // colgroup
851 collectFolder = collectFolder.getParentFile();
852 }
853 }
854
855 if(collectFolder == null || !collectFolder.exists()) {
856 // if GLI config file specified no collectDir (open_collection_file_path is "")
857 // OR if dealing with a local server but the collectdir no longer exists,
858 // use the default greenstone collect directory, and write that to affected files
859
860 open_collection_file_path = defaultColdir; // default GS collect dir
861 // Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(), true, "");
862 } else { // use the coldir value specified in the flags to GLI or from the last GLI session
863 coldir = collectFolder.getAbsolutePath();
864 }
865 // set it as the current folder
866 setCollectDirectoryPath(coldir); // will ensure the required file separator at end
867 }
868
869 if(!isGsdlRemote) {
870 // LocalLibraryServer would already have set glisite.cfg to the correct collecthome for server.exe
871 // Here we set collecthome in gsdl(3)site.cfg for the GS2 apache web server and GS3 tomcat server
872 String gsdlsitecfg = getGsdlSiteConfigFile();
873 // update the gsdlsite config file and store the old value for use when we exit GLI
874 if(coldir.equals(defaultColdir)) {
875 gsdlsite_collecthome = Utility.updatePropertyConfigFile(
876 gsdlsitecfg, "collecthome", null);
877 } else {
878 gsdlsite_collecthome = Utility.updatePropertyConfigFile(
879 gsdlsitecfg, "collecthome", coldir); // no file separator
880 // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
881 }
882 }
883 }
884
885 /** depending on the version of GS being run, return the path to the current GS' installation's gsdl(3)site.cfg */
886 public static String getGsdlSiteConfigFile() {
887 if(Gatherer.GS3) { // web/WEB-INF/cgi/gsdl3site.cfg
888 return Configuration.gsdl3_path + File.separator + "WEB-INF"
889 + File.separator + "cgi" + File.separator + "gsdl3site.cfg";
890 } else { // cgi-bin/gsdlsite.cfg
891 return Configuration.gsdl_path /* + File.separator */
892 + "cgi-bin" + File.separator + "gsdlsite.cfg";
893 }
894 }
895
896 public static void collectDirectoryHasChanged(
897 String oldCollectPath, String newCollectPath, final Component container)
898 {
899 if(oldCollectPath.equals(newCollectPath)) {
900 return; // nothing to be done
901 }
902
903 // Will use a busy cursor if the process of changing the collect directory takes more
904 // than half a second/500ms. See http://www.catalysoft.com/articles/busyCursor.html
905 Cursor originalCursor = container.getCursor();
906 java.util.TimerTask timerTask = new java.util.TimerTask() {
907 public void run() {
908 // set the cursor on the container:
909 container.setCursor(new Cursor(Cursor.WAIT_CURSOR));
910 }
911 };
912 java.util.Timer timer = new java.util.Timer();
913
914 try {
915 timer.schedule(timerTask, 500);
916
917 // first save any open collection in the old location, then close it
918 if(Gatherer.c_man.getCollection() != null) {
919 Gatherer.g_man.saveThenCloseCurrentCollection(); // close the current collection first
920 }
921
922 // change to new collect path
923 if(newCollectPath.equals(getDefaultGSCollectDirectoryPath(true))) {
924 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
925 true, "");
926 } else {
927 Configuration.setString("general.open_collection"+Configuration.gliPropertyNameSuffix(),
928 true, newCollectPath);
929 }
930 Gatherer.setCollectDirectoryPath(newCollectPath);
931
932
933 // refresh the Documents in Greenstone Collections
934 //WorkspaceTreeModel.refreshGreenstoneCollectionsNode();
935 Gatherer.g_man.refreshWorkspaceTreeGreenstoneCollections();
936
937 // The web server needs to be told where a new (non-standard) collecthome home is.
938 // The web server reads collecthome from cgi-bin/gsdlsite.cfg, where the property
939 // collecthome can be specified if a non-standard collecthome is to be used. If no
940 // such property is specified in the file, then it assumes the standard GS collecthome.
941 // This method does nothing for a remote Greenstone.
942 if(Gatherer.isGsdlRemote) {
943 return;
944 }
945
946 // non-destructive update of gsdl(3)site.cfg (comments preserved)
947 String collectDir = Gatherer.getCollectDirectoryPath();
948 //collectDir = "\"" + collectDir.substring(0, collectDir.length()-1) + "\""; // remove file separator at end
949 collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end
950 Utility.updatePropertyConfigFile(getGsdlSiteConfigFile(), "collecthome", collectDir);
951 // if gsdlsite.cfg does not exist (if using server.exe for instance), the above method will just return
952
953 if(!Gatherer.GS3 && Gatherer.isLocalLibrary) {
954 // for Images in the collection to work, the apache web server
955 // configuration's COLLECTHOME should be updated on collectdir change.
956 // Does nothing for server.exe at the moment
957
958 LocalLibraryServer.reconfigure();
959 }
960 } finally { // Note try-finally section without catch:
961 // "Java's finally clause is guaranteed to be executed even when
962 // an exception is thrown and not caught in the current scope."
963 // See http://www.catalysoft.com/articles/busyCursor.html
964 // the following code fragment is guaranteed to restore the original
965 // cursor now the custom actionPerformed() processing is complete, regardless
966 // of whether the processing method terminates normally or throws an exception
967 // and regardedless of where in the call stack the exception is caught.
968
969 timer.cancel();
970 container.setCursor(originalCursor);
971 }
972 }
973
974
975 static public void refresh(int refresh_reason)
976 {
977 if (g_man != null) {
978
979 g_man.refresh(refresh_reason, c_man.ready());
980 }
981
982 // Now is a good time to force a garbage collect
983 System.gc();
984 }
985
986
987 // used to send reload coll messages to the tomcat server
988 static public void configGS3Server(String site, String command) {
989 if (Configuration.library_url == null){
990 System.out.println("Error: you have not provided the Greenstone Library address.");
991 return;
992
993 }
994
995 try {
996 // need to add the servlet name to the exec address
997 String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command;
998 URL url = new URL(raw_url);
999 DebugStream.println("Action: " + raw_url);
1000 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
1001 int response_code = library_connection.getResponseCode();
1002 if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
1003 DebugStream.println("200 - Complete.");
1004 }
1005 else {
1006 DebugStream.println("404 - Failed.");
1007 }
1008 url = null;
1009 }
1010 catch(java.net.ConnectException connectException) {
1011 JOptionPane.showMessageDialog(g_man, Dictionary.get("Preferences.Connection.Library_Path_Connection_Failure", Configuration.library_url.toString()), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
1012 DebugStream.println(connectException.getMessage());
1013 }
1014 catch (Exception exception) {
1015 DebugStream.printStackTrace(exception);
1016 }
1017 }
1018
1019
1020 /** Used to 'spawn' a new child application when a file is double clicked.
1021 * @param file The file to open
1022 * @see org.greenstone.gatherer.Gatherer.ExternalApplication
1023 */
1024 static public void spawnApplication(File file) {
1025 String [] commands = assoc_man.getCommand(file);
1026 if(commands != null) {
1027 ExternalApplication app = new ExternalApplication(commands);
1028 apps.add(app);
1029 app.start();
1030 }
1031 else {
1032 ///ystem.err.println("No open command available.");
1033 }
1034 }
1035
1036
1037 static public void spawnApplication(String command)
1038 {
1039 ExternalApplication app = new ExternalApplication(command);
1040 apps.add(app);
1041 app.start();
1042 }
1043
1044 static public void spawnApplication(String command, String ID)
1045 {
1046 ExternalApplication app = new ExternalApplication(command, ID);
1047 apps.add(app);
1048 app.start();
1049 }
1050
1051 static public void spawnApplication(String[] commands, String ID)
1052 {
1053 ExternalApplication app = new ExternalApplication(commands, ID);
1054 apps.add(app);
1055 app.start();
1056 }
1057
1058 static public void terminateApplication(String ID) {
1059 for(int i = 0; i < apps.size(); i++) {
1060 ExternalApplication app = (ExternalApplication)apps.get(i);
1061 if(app.getID() != null && app.getID().equals(ID)) {
1062 app.stopExternalApplication();
1063 apps.remove(app);
1064 }
1065 }
1066 }
1067
1068
1069 /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked
1070 * @param url The url to open the browser at
1071 * @see org.greenstone.gatherer.Gatherer.BrowserApplication
1072 */
1073 static public void spawnBrowser(String url) {
1074 String command = assoc_man.getBrowserCommand(url);
1075 if (command != null) {
1076 BrowserApplication app = new BrowserApplication(command, url);
1077 apps.add(app);
1078 app.start();
1079 }
1080 else {
1081 ///ystem.err.println("No browser command available.");
1082 }
1083 }
1084
1085
1086 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
1087 */
1088 static public void missingEXEC() {
1089 WarningDialog dialog;
1090 String configPropertyName = "general.library_url"+Configuration.gliPropertyNameSuffix();
1091
1092 if (GS3) {
1093 // Warning dialog with no cancel button and no "turn off warning" checkbox
1094 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), configPropertyName, false, false);
1095 } else { // local case
1096 dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), configPropertyName, false);
1097 }
1098
1099 JTextField field = new URLField.Text(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false));
1100
1101 // Set the default library URL to the tomcat server and port number
1102 // specified in the build.properties located in the gsdl3_src_path
1103 if (GS3) {
1104 String host = "localhost";
1105 String port = "8080";
1106
1107 File buildPropsFile = new File(Configuration.gsdl3_src_path + File.separator + "build.properties");
1108 if(buildPropsFile.exists()) {
1109 Properties props = new Properties();
1110 try{
1111 props.load(new FileInputStream(buildPropsFile));
1112 host = props.getProperty("tomcat.server", host);
1113 port = props.getProperty("tomcat.port", port);
1114 }catch(Exception e){
1115 DebugStream.println("Could not load build.properties file");
1116 System.err.println("Could not load build.properties file");
1117 }
1118 props = null;
1119 }
1120 String defaultURL = "http://"+host+":"+port+"/"+"greenstone3";
1121 field.setText(defaultURL);
1122 field.selectAll();
1123 }
1124 dialog.setValueField(field);
1125 dialog.display();
1126 dialog.dispose();
1127 dialog = null;
1128
1129 String library_url_string = Configuration.getString(configPropertyName, true);
1130 if (!library_url_string.equals("")) {
1131 try {
1132 // WarningDialog does not allow invalid URLs, so the following is ignored:
1133 // make sure the URL the user provided contains the http:// prefix
1134 // and then save the corrected URL
1135 if(!library_url_string.startsWith("http://")
1136 && !library_url_string.startsWith("https://")) {
1137 library_url_string = "http://"+library_url_string;
1138 Configuration.setString(configPropertyName, true, configPropertyName);
1139 }
1140 Configuration.library_url = new URL(library_url_string);
1141 }
1142 catch (MalformedURLException exception) {
1143 DebugStream.printStackTrace(exception);
1144 }
1145 }
1146 }
1147
1148
1149
1150 /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer.
1151 */
1152 static private void popupFedoraInfo() {
1153
1154 FedoraLogin dialog = new FedoraLogin("Fedora Login", false);
1155
1156 if (Configuration.library_url == null) {
1157
1158 String library_url_string = dialog.getLibraryURL();
1159 if (!library_url_string.equals("")) {
1160 try {
1161 Configuration.library_url = new URL(library_url_string);
1162 }
1163 catch (MalformedURLException exception) {
1164 DebugStream.printStackTrace(exception);
1165 }
1166 }
1167 }
1168
1169 boolean showLogin = true;
1170 do {
1171 if(!dialog.loginRequested()) { // user pressed cancel to exit the FedoraLogin dialog
1172 System.exit(0);
1173 } else {
1174 showLogin = dialog.loginRequested();
1175 String hostname = dialog.getHostname();
1176 String port = dialog.getPort();
1177 String username = dialog.getUsername();
1178 String password = dialog.getPassword();
1179 String protocol = dialog.getProtocol();
1180
1181 Configuration.fedora_info.setHostname(hostname);
1182 Configuration.fedora_info.setPort(port);
1183 Configuration.fedora_info.setUsername(username);
1184 Configuration.fedora_info.setPassword(password);
1185 Configuration.fedora_info.setProtocol(protocol);
1186
1187 String ping_url_str = protocol + "://" + hostname + ":" + port + "/fedora";
1188 String login_str = username + ":" + password;
1189
1190 String login_encoding = Base64.encodeBytes(login_str.getBytes());
1191
1192 try {
1193 URL ping_url = new URL(ping_url_str);
1194 URLConnection uc = ping_url.openConnection();
1195 uc.setRequestProperty ("Authorization", "Basic " + login_encoding);
1196 // Attempt to access some content ...
1197 InputStream content = (InputStream)uc.getInputStream();
1198
1199 // if no exception occurred in the above, we would have come here:
1200 showLogin = false;
1201 dialog.dispose();
1202 }
1203 catch (Exception exception) {
1204 // TODO: move into dictionary
1205 String[] errorMessage = {"Failed to connect to the Fedora server.", "It might not be running, or",
1206 "incorrect username and/or password."};
1207 dialog.setErrorMessage(errorMessage);
1208 //DebugStream.printStackTrace(exception);
1209 // exception occurred, show the dialog again (do this after printing to
1210 // debugStream, else the above does not get done for some reason).
1211 dialog.setVisible(true);
1212 }
1213 }
1214 } while(showLogin);
1215
1216 dialog = null; // no more need of the dialog
1217
1218 // Now we are connected.
1219 }
1220
1221
1222
1223 static private void requestGLIServerURL()
1224 {
1225 WarningDialog dialog;
1226 String[] defaultURLs = {
1227 "http://localhost:8080/greenstone3/cgi-bin/gliserver.pl",
1228 "http://localhost:8080/gsdl/cgi-bin/gliserver.pl"
1229 };
1230
1231 // Warning dialog with no cancel button and no "turn off warning" checkbox
1232 // (since user-input of the gliserver script is mandatory)
1233 dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false, false);
1234
1235 dialog.setValueField(new URLField.DropDown(Configuration.getColor("coloring.editable_foreground", false),
1236 Configuration.getColor("coloring.editable_background", false),
1237 defaultURLs, "general.gliserver_url",
1238 "general.open_collection"+Configuration.gliPropertyNameSuffix(),
1239 "gliserver.pl"));
1240
1241 if (Gatherer.default_gliserver_url!=null){
1242 dialog.setValueField(Gatherer.default_gliserver_url.toString());
1243 }
1244
1245 // A WarningDialog cannot always be made to respond (let alone to exit the program) on close. We
1246 // handle the response of this particular WarningDialog here: a URL for gliserver.pl is a crucial
1247 // piece of user-provided data. Therefore, if no URL was entered for gliserver.pl, it'll exit safely.
1248 dialog.addWindowListener(new WindowAdapter() {
1249 public void windowClosing(WindowEvent e) {
1250 Gatherer.exit();
1251 }
1252 });
1253
1254 dialog.display();
1255 dialog.dispose();
1256 dialog = null;
1257
1258
1259 String gliserver_url_string = Configuration.getString("general.gliserver_url", true);
1260 if (!gliserver_url_string.equals("")) {
1261 try {
1262 Configuration.gliserver_url = new URL(gliserver_url_string);
1263 Configuration.setString("general.gliserver_url", true, gliserver_url_string);
1264 }
1265 catch (MalformedURLException exception) {
1266 DebugStream.printStackTrace(exception);
1267 }
1268 }
1269 }
1270
1271
1272 /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI.
1273 */
1274 static private void missingGSDL() {
1275 WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false);
1276 dialog.display();
1277 dialog.dispose();
1278 dialog = null;
1279 }
1280
1281 /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */
1282 static private void missingImageMagick() {
1283 WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false);
1284 dialog.display();
1285 dialog.dispose();
1286 dialog = null;
1287 }
1288
1289 /** Prints a message informing the user where they can get PDFBox from to process PDF files of v1.5 and greater */
1290 static private void missingPDFBox(String zipExtension, String extFolder) {
1291 WarningDialog dialog = new WarningDialog("warning.MissingPDFBox", Dictionary.get("MissingPDFBox.Title"), Dictionary.get("MissingPDFBox.Message", new String[]{zipExtension, extFolder}), null, false);
1292 dialog.display();
1293 dialog.dispose();
1294 dialog = null;
1295 }
1296
1297 /** 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. */
1298 static private void missingPERL() {
1299 WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false);
1300 dialog.display();
1301 dialog.dispose();
1302 dialog = null;
1303 }
1304
1305 /** Prints a warning message about the OS not supporting multiple filename encodings. */
1306 static private void multipleFilenameEncodingsNotSupported() {
1307 WarningDialog dialog = new WarningDialog("warning.NoEncodingSupport",
1308 Dictionary.get("NoEncodingSupport.Title"),
1309 Dictionary.get("NoEncodingSupport.Message"), null, false);
1310 dialog.display();
1311 dialog.dispose();
1312 dialog = null;
1313 }
1314
1315 /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator.
1316 * @see java.lang.Exception
1317 * @see java.lang.System
1318 * @see java.net.Authenticator
1319 * @see org.greenstone.gatherer.Configuration
1320 * @see org.greenstone.gatherer.GAuthenticator
1321 */
1322 static public void setProxy() {
1323 try {// Can throw several exceptions
1324 if(Configuration.get("general.use_proxy", true)) {
1325 System.setProperty("http.proxyType", "4");
1326 System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true));
1327 System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true));
1328 System.setProperty("http.proxySet", "true");
1329 } else {
1330 System.setProperty("http.proxyHost", "");
1331 System.setProperty("http.proxyPort", "");
1332 System.setProperty("http.proxySet", "false");
1333 }
1334 } catch (Exception error) {
1335 DebugStream.println("Error in Gatherer.initProxy(): " + error);
1336 DebugStream.printStackTrace(error);
1337 }
1338 }
1339
1340
1341 /** 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. */
1342 static private class ExternalApplication
1343 extends Thread {
1344 private Process process = null;
1345 /** The initial command string given to this sub-process. */
1346 private String command = null;
1347 private String[] commands = null;
1348
1349 private String ID = null;
1350
1351 /** Constructor.
1352 * @param command The initial command <strong>String</strong>.
1353 */
1354 public ExternalApplication(String command) {
1355 this.command = command;
1356 }
1357
1358 public ExternalApplication(String[] commands) {
1359 this.commands = commands;
1360 }
1361
1362 public ExternalApplication(String command, String ID) {
1363 this.command = command;
1364 this.ID = ID;
1365 }
1366
1367 public ExternalApplication(String[] commands, String ID) {
1368 this.commands = commands;
1369 this.ID = ID;
1370 }
1371
1372 public String getID() {
1373 return ID;
1374 }
1375
1376 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1377 * @see java.lang.Exception
1378 * @see java.lang.Process
1379 * @see java.lang.Runtime
1380 * @see java.lang.System
1381 * @see java.util.Vector
1382 */
1383 public void run() {
1384 // Call an external process using the args.
1385 try {
1386 if(commands != null) {
1387 StringBuffer whole_command = new StringBuffer();
1388 for(int i = 0; i < commands.length; i++) {
1389 whole_command.append(commands[i]);
1390 whole_command.append(" ");
1391 }
1392 DebugStream.println("Running " + whole_command.toString());
1393 Runtime rt = Runtime.getRuntime();
1394 process = rt.exec(commands);
1395 process.waitFor();
1396 }
1397 else {
1398 DebugStream.println("Running " + command);
1399 Runtime rt = Runtime.getRuntime();
1400 process = rt.exec(command);
1401 process.waitFor();
1402 }
1403 }
1404 catch (Exception exception) {
1405 DebugStream.printStackTrace(exception);
1406 }
1407 // Remove ourself from Gatherer list of threads.
1408 apps.remove(this);
1409 // Call exit if we were the last outstanding child process thread.
1410 if (apps.size() == 0 && exit == true) {
1411 // In my opinion (DB) there is no need to exit here,
1412 // the 'run' method ending naturally brings this
1413 // thread to an end. In fact it is potentially
1414 // dangerous to exit here, as the main thread in the
1415 // Gatherer class may be stopped prematurely. As it so
1416 // happens the point at which the ExternalApplication thread
1417 // is asked to stop (Back in the main Gatherer thread) is after
1418 // various configuration files have been saved.
1419 //
1420 // A similar argument holds for BrowserApplication thread below.
1421 System.exit(exit_status);
1422 }
1423 }
1424 public void stopExternalApplication() {
1425 if(process != null) {
1426 process.destroy();
1427 }
1428 }
1429 }
1430 /** 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. */
1431 static private class BrowserApplication
1432 extends Thread {
1433 private Process process = null;
1434 /** The initial command string given to this sub-process. */
1435 private String command = null;
1436 private String url = null;
1437 private String[] commands = null;
1438
1439 public BrowserApplication(String command, String url) {
1440 StringTokenizer st = new StringTokenizer(command);
1441 int num_tokens = st.countTokens();
1442 this.commands = new String [num_tokens];
1443 int i=0;
1444 while (st.hasMoreTokens()) {
1445 commands[i] = st.nextToken();
1446 i++;
1447 }
1448 //this.commands = commands;
1449 this.url = url;
1450 }
1451 /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
1452 * @see java.lang.Exception
1453 * @see java.lang.Process
1454 * @see java.lang.Runtime
1455 * @see java.lang.System
1456 * @see java.util.Vector
1457 */
1458 public void run() {
1459 // Call an external process using the args.
1460 if(commands == null) {
1461 apps.remove(this);
1462 return;
1463 }
1464 try {
1465 String prog_name = commands[0];
1466 String lower_name = prog_name.toLowerCase();
1467 if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
1468 DebugStream.println("found mozilla or netscape, trying remote it");
1469 // mozilla and netscape, try using a remote command to get things in the same window
1470 String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"};
1471 printArray(new_commands);
1472
1473 Runtime rt = Runtime.getRuntime();
1474 process = rt.exec(new_commands);
1475 int exitCode = process.waitFor();
1476 if (exitCode != 0) { // if Netscape or mozilla was not open
1477 DebugStream.println("couldn't do remote, trying original command");
1478 printArray(commands);
1479 process = rt.exec(commands); // try the original command
1480 }
1481 } else {
1482 // just run what we have been given
1483 StringBuffer whole_command = new StringBuffer();
1484 for(int i = 0; i < commands.length; i++) {
1485 whole_command.append(commands[i]);
1486 whole_command.append(" ");
1487 }
1488 DebugStream.println("Running " + whole_command.toString());
1489 Runtime rt = Runtime.getRuntime();
1490 process = rt.exec(commands);
1491 process.waitFor();
1492 }
1493 }
1494
1495 catch (Exception exception) {
1496 DebugStream.printStackTrace(exception);
1497 }
1498 // Remove ourself from Gatherer list of threads.
1499 apps.remove(this);
1500 // Call exit if we were the last outstanding child process thread.
1501 if (apps.size() == 0 && exit == true) {
1502 System.exit(exit_status);
1503 }
1504 }
1505 public void printArray(String [] array) {
1506 for(int i = 0; i < array.length; i++) {
1507 DebugStream.print(array[i]+" ");
1508 System.err.println(array[i]+" ");
1509 }
1510 }
1511 public void stopBrowserApplication() {
1512 if(process != null) {
1513 process.destroy();
1514 }
1515 }
1516 }
1517
1518
1519 private class ImageMagickTest
1520 {
1521 public boolean found()
1522 {
1523 try {
1524 String[] command = new String[2];
1525 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
1526 command[1] = "-version";
1527 Process image_magick_process = Runtime.getRuntime().exec(command);
1528 image_magick_process.waitFor();
1529
1530 //new way of detection of ImageMagick
1531 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
1532
1533 BufferedReader br = new BufferedReader(isr);
1534 // Capture the standard output stream and seach for two particular occurances: Version and ImageMagick.
1535
1536 String line = br.readLine();
1537 if (line == null) {
1538 return false;
1539 }
1540 String lc_line = line.toLowerCase();
1541 if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
1542 return true;
1543 } else {
1544 return false;
1545 }
1546
1547 //return (image_magick_process.exitValue() == 0);
1548 }
1549 catch (IOException exception) {
1550 return false;
1551 }
1552 catch (InterruptedException exception) {
1553 return false;
1554 }
1555 }
1556 }
1557
1558
1559 private class PerlTest
1560 {
1561 private String[] command = new String[2];
1562
1563 public PerlTest()
1564 {
1565 command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX);
1566 command[1] = "-version";
1567 }
1568
1569 public boolean found()
1570 {
1571 try {
1572 Process perl_process = Runtime.getRuntime().exec(command);
1573 perl_process.waitFor();
1574 return (perl_process.exitValue() == 0);
1575 }
1576 catch (Exception exception) {
1577 return false;
1578 }
1579 }
1580
1581 public String toString() {
1582 return command[0];
1583 }
1584 }
1585
1586 public class GS3ServerThread extends Thread
1587 {
1588 String _gsdl_path = "";
1589
1590 public GS3ServerThread(String gsdl_path)
1591 {
1592 _gsdl_path = gsdl_path;
1593 }
1594
1595 public void run()
1596 {
1597 try
1598 {
1599 String shellCommand = null;
1600 Process p = null;
1601 if (Utility.isWindows())
1602 {
1603 p = Runtime.getRuntime().exec("cmd /C \"cd " + _gsdl_path + File.separator + ".. && ant start\"\"");
1604 }
1605 else
1606 {
1607 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant restart -f \"" + _gsdl_path + ".." + File.separator + "build.xml\""});
1608 }
1609 }
1610 catch(Exception ex)
1611 {
1612 ex.printStackTrace();
1613 }
1614 }
1615 }
1616}
Note: See TracBrowser for help on using the repository browser.