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

Last change on this file since 22661 was 22661, checked in by ak19, 14 years ago

Next set of changes for ticket 152: moveable collectdir (so that collect dir can be on a pen drive). These are changes for when GLI's LocalLibraryServer tells server.jar to reconfigure itself (to update the COLLECTHOME alias in the apache web server's httpd.conf file when the collectdir has changed) and restart the server.

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