/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer; import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.text.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.GAuthenticator; import org.greenstone.gatherer.collection.CollectionManager; import org.greenstone.gatherer.feedback.ActionRecorderDialog; import org.greenstone.gatherer.file.FileManager; import org.greenstone.gatherer.file.FileAssociationManager; import org.greenstone.gatherer.file.RecycleBin; import org.greenstone.gatherer.greenstone.Classifiers; import org.greenstone.gatherer.greenstone.LocalGreenstone; import org.greenstone.gatherer.greenstone.LocalLibraryServer; import org.greenstone.gatherer.greenstone.Plugins; import org.greenstone.gatherer.greenstone3.ServletConfiguration; import org.greenstone.gatherer.gui.GUIManager; import org.greenstone.gatherer.gui.URLField; import org.greenstone.gatherer.gui.WarningDialog; import org.greenstone.gatherer.remote.RemoteGreenstoneServer; import org.greenstone.gatherer.util.JarTools; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; /** Containing the top-level "core" for the Gatherer, this class is the * common core for the GLI application and applet. It first parses the * command line arguments, preparing to update the configuration as * required. Next it loads several important support classes such as the * Configuration and Dictionary. Finally it creates the other important * managers and sends them on their way. * @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 */ public class Gatherer { /** The name of the GLI. */ static final public String PROGRAM_NAME = "Greenstone Librarian Interface"; /** The current version of the GLI. */ static final public String PROGRAM_VERSION = "v2.72"; static private Dimension size = new Dimension(800, 540); /** Has the exit flag been set? */ static final public int EXIT_THEN_RESTART= 2; static public boolean exit = false; static public int exit_status = 0; static private String gli_directory_path = null; static private String gli_user_directory_path = null; static public String client_operating_system = null; /** All of the external applications that must exit before we close the Gatherer. */ static private Vector apps = new Vector(); static private String non_standard_collect_directory_path = null; static public String open_collection_file_path = null; /** A public reference to the FileAssociationManager. */ static public FileAssociationManager assoc_man; /** A public reference to the CollectionManager. */ static public CollectionManager c_man; /** A public reference to the RecycleBin. */ static public RecycleBin recycle_bin; /** a reference to the Servlet Configuration is GS3 */ static public ServletConfiguration servlet_config; /** A public reference to the FileManager. */ static public FileManager f_man; /** A public reference to the GUIManager. */ static public GUIManager g_man = null; static private boolean g_man_built = false; /** We are using the GLI for GS3 */ static public boolean GS3 = false; static public boolean isApplet = false; static public boolean isGsdlRemote = false; // feedback stuff /** is the feedback feature enabled? */ static public boolean feedback_enabled = true; /** the action recorder dialog */ static public ActionRecorderDialog feedback_dialog = null; // Refresh reasons static public final int COLLECTION_OPENED = 0; static public final int COLLECTION_CLOSED = 1; static public final int COLLECTION_REBUILT = 2; static public final int PREFERENCES_CHANGED = 3; //////kk added//////// static public String cgiBase=""; ///////////////// /** Magic to allow Enter to fire the default button. */ static { KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP); map.removeKeyStrokeBinding(enter); } public Gatherer(String[] args) { // Display the version to make error reports a lot more useful System.err.println("Version: " + PROGRAM_VERSION + "\n"); JarTools.initialise(this); GetOpt go = new GetOpt(args); // Remember the GSDLOS value client_operating_system = go.client_operating_system; // If feedback is enabled, set up the recorder dialog if (go.feedback_enabled) { // Use the default locale for now - this will be changed by the Gatherer run method feedback_enabled = true; feedback_dialog = new ActionRecorderDialog(Locale.getDefault()); } // Are we using a remote Greenstone? if (go.use_remote_greenstone) { isGsdlRemote = true; // We don't have a local Greenstone! go.gsdl_path = null; // We have to use our own collect directory since we can't use the Greenstone one setCollectDirectoryPath(getGLIUserDirectoryPath() + "collect" + File.separator); } // No, we have a local Greenstone else { LocalGreenstone.setDirectoryPath(go.gsdl_path); } // Users may specify a non-standard collect directory (eg. when running one GLI in a network environment) if (go.collect_directory_path != null) { setCollectDirectoryPath(go.collect_directory_path); } // More special code for running with a remote Greenstone if (isGsdlRemote) { Configuration.TEMPLATE_CONFIG_XML = "xml/configRemote.xml"; Configuration.CONFIG_XML = "configRemote.xml"; Configuration.GS3_CONFIG_XML = "config3Remote.xml"; File collect_directory = new File(Gatherer.getCollectDirectoryPath()); if (!collect_directory.exists() && !collect_directory.mkdir()) { System.err.println("Warning: Unable to make directory: " + collect_directory); } } init(go.gsdl_path, go.gsdl3_path, go.gsdl3_src_path, go.local_library_path, go.library_url_string, go.gliserver_url_string, go.debug, go.perl_path, go.no_load, go.filename, go.site_name, go.servlet_path); } public void init(String gsdl_path, String gsdl3_path, String gsdl3_src_path, String local_library_path, String library_url_string, String gliserver_url_string, boolean debug_enabled, String perl_path, boolean no_load, String open_collection, String site_name, String servlet_path) { if (gsdl3_path != null && !gsdl3_path.equals("")) { this.GS3 = true; } else { gsdl3_path = null; gsdl3_src_path = null; } // Create the debug stream if required if (debug_enabled) { DebugStream.enableDebugging(); Calendar now = Calendar.getInstance(); String debug_file_path = "debug" + now.get(Calendar.DATE) + "-" + now.get(Calendar.MONTH) + "-" + now.get(Calendar.YEAR) + ".txt"; // Debug file is created in the user's GLI directory debug_file_path = getGLIUserDirectoryPath() + debug_file_path; DebugStream.println("Debug file path: " + debug_file_path); DebugStream.setDebugFile(debug_file_path); DebugStream.print(System.getProperties()); } // Delete plugins.dat and classifiers.dat files from previous versions of the GLI (no longer used) File plugins_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "plugins.dat"); if (plugins_dat_file.exists()) { System.err.println("Deleting plugins.dat file..."); Utility.delete(plugins_dat_file); } File classifiers_dat_file = new File(Gatherer.getGLIUserDirectoryPath() + "classifiers.dat"); if (classifiers_dat_file.exists()) { System.err.println("Deleting classifiers.dat file..."); Utility.delete(classifiers_dat_file); } try { // Load GLI config file new Configuration(getGLIUserDirectoryPath(), gsdl_path, gsdl3_path, gsdl3_src_path, site_name); if (GS3) { // Load Greenstone 3 servlet configuration servlet_config = new ServletConfiguration(gsdl3_path); } // Check we know where Perl is Configuration.perl_path = perl_path; if (isGsdlRemote && Utility.isWindows() && Configuration.perl_path != null) { if (Configuration.perl_path.toLowerCase().endsWith("perl.exe")) { Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - "perl.exe".length()); } if (Configuration.perl_path.endsWith(File.separator)) { Configuration.perl_path = Configuration.perl_path.substring(0, Configuration.perl_path.length() - File.separator.length()); } } if (GS3 && Configuration.servlet_path == null) { Configuration.servlet_path = servlet_config.getServletPath(Configuration.site_name); } // the feedback dialog has been loaded with a default locale, // now set the user specified one if (feedback_enabled && feedback_dialog != null) { feedback_dialog.setLocale(Configuration.getLocale("general.locale", true)); } // Read Dictionary new Dictionary(Configuration.getLocale("general.locale", true), Configuration.getFont("general.font", true)); // check that we are using Sun Java String java_vendor = System.getProperty("java.vendor"); if (!java_vendor.equals("Sun Microsystems Inc.")) { System.err.println(Dictionary.get("General.NotSunJava", java_vendor)); } // Unless we're using remote building, we need to know where the local Greenstone is if (!isGsdlRemote && gsdl_path == null) { missingGSDL(); } // Start up the local library server, if that's what we want if (Utility.isWindows() && local_library_path != null && !isGsdlRemote && !GS3) { LocalLibraryServer.start(gsdl_path, local_library_path); } // The "-library_url" option overwrites anything in the config files if (library_url_string != null && library_url_string.length() > 0) { try { System.err.println("Setting library_url to " + library_url_string + "..."); Configuration.library_url = new URL(library_url_string); } catch (MalformedURLException error) { DebugStream.printStackTrace(error); } } // Check that we now know the Greenstone library URL, since we need this for previewing collections DebugStream.println("Configuration.library_url = " + Configuration.library_url); if (Configuration.library_url == null) { missingEXEC(); } // The "-gliserver_url" option overwrites anything in the config files if (gliserver_url_string != null && gliserver_url_string.length() > 0) { try { System.err.println("Setting gliserver_url to " + gliserver_url_string + "..."); Configuration.gliserver_url = new URL(gliserver_url_string); } catch (MalformedURLException error) { DebugStream.printStackTrace(error); } } // If we're using a remote Greenstone we need to know where the gliserver script is DebugStream.println("Configuration.gliserver_url = " + Configuration.gliserver_url); if (isGsdlRemote) { if (Configuration.gliserver_url == null) { missingGLIServer(); } if (Configuration.gliserver_url != null) { gliserver_url_string = Configuration.gliserver_url.toString(); } } // Check for ImageMagick if (Gatherer.isGsdlRemote) { DebugStream.println("Not checking for ImageMagick."); } else if (!(new ImageMagickTest()).found()) { // Time for a warning message missingImageMagick(); } if (Gatherer.isGsdlRemote) { DebugStream.println("Not checking for perl path/exe"); } else { // Perl path is a little different as it is perfectly ok to // start the GLI without providing a perl path boolean found_perl = false; if (Configuration.perl_path != null) { // See if the file pointed to actually exists File perl_file = new File(Configuration.perl_path); found_perl = perl_file.exists(); perl_file = null; } if (Configuration.perl_path == null || !found_perl) { // Run test to see if we can run perl as is. PerlTest perl_test = new PerlTest(); if (perl_test.found()) { // If so replace the perl path with the system // default (or null for unix). Configuration.perl_path = perl_test.toString(); found_perl = true; } } if (!found_perl) { // Time for an error message. missingPERL(); } } // Set the default font for all Swing components. FontUIResource default_font = Configuration.getFont("general.font", true); Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get(key); if (value instanceof FontUIResource) { UIManager.put(key, default_font); } } // Set up proxy setProxy(); // Now we set up an Authenticator Authenticator.setDefault(new GAuthenticator()); assoc_man = new FileAssociationManager(); // Create File Manager f_man = new FileManager(); // Create Collection Manager c_man = new CollectionManager(); // Create Recycle Bin recycle_bin = new RecycleBin(); if (GS3) { if (site_name==null) { site_name = Configuration.site_name; servlet_path = null; // need to reset this } if (servlet_path == null) { servlet_path = Configuration.getServletPath(); } } open_collection_file_path = open_collection; if (open_collection_file_path == null) { open_collection_file_path = Configuration.getString("general.open_collection", true); } if (no_load || open_collection_file_path.equals("")) { open_collection_file_path = null; } } catch (Exception exception) { DebugStream.printStackTrace(exception); } // Create GUI Manager (last) or else suffer the death of a thousand NPE's g_man = new GUIManager(size); } public void openGUI() { // Size and place the frame on the screen Rectangle bounds = Configuration.getBounds("general.bounds", true); if (bounds == null) { // Choose a sensible default value bounds = new Rectangle(0, 0, 640, 480); } // Ensure width and height are reasonable size = bounds.getSize(); if (size.width < 640) { size.width = 640; } else if (size.width > Configuration.screen_size.width && Configuration.screen_size.width > 0) { size.width = Configuration.screen_size.width; } if (size.height < 480) { size.height = 480; } else if (size.height > Configuration.screen_size.height && Configuration.screen_size.height > 0) { size.height = Configuration.screen_size.height; } if (!g_man_built) { g_man.display(); // 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). g_man.setLocation(bounds.x, bounds.y); g_man.setVisible(true); // After the window has been made visible, check that it is in the correct place // sometimes java places a window not in the correct place, // but with an offset. If so, we work out what the offset is // and change the desired location to take that into account Point location = g_man.getLocation(); int x_offset = bounds.x - location.x; int y_offset = bounds.y - location.y; // If not, offset the window to move it into the correct location if (x_offset > 0 || y_offset > 0) { ///ystem.err.println("changing the location to "+(bounds.x + x_offset)+" "+ (bounds.y + y_offset)); g_man.setLocation(bounds.x + x_offset, bounds.y + y_offset); } // 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. g_man.afterDisplay(); g_man_built = true; } else { g_man.setVisible(true); } // Get a list of the core Greenstone classifiers and plugins Classifiers.loadClassifiersList(null); Plugins.loadPluginsList(null); // If using a remote Greenstone we need to download the collection configurations now if (Gatherer.isGsdlRemote) { if (RemoteGreenstoneServer.downloadCollectionConfigurations().equals("")) { // !! Something went wrong downloading the collection configurations System.err.println("Error: Could not download collection configurations."); System.exit(0); } } // If there was a collection left open last time, reopen it c_man.openCollectionFromLastTime(); } /** Exits the Gatherer after ensuring that things needing saving are saved. * @see java.io.FileOutputStream * @see java.io.PrintStream * @see java.lang.Exception * @see javax.swing.JOptionPane * @see org.greenstone.gatherer.Configuration * @see org.greenstone.gatherer.collection.CollectionManager * @see org.greenstone.gatherer.gui.GUIManager */ static public void exit(int new_exit_status) { DebugStream.println("In Gatherer.exit()..."); exit = true; if (new_exit_status != 0) { // default exit_status is already 0 // only remember a new exit status if it is non-trivial exit_status = new_exit_status; } // Save the file associations if (assoc_man != null) { assoc_man.save(); assoc_man = null; } // Get the gui to deallocate g_man.destroy(); g_man_built = false; // Flush debug DebugStream.closeDebugStream(); // If we started a server, we should try to stop it. if (LocalLibraryServer.isRunning() == true) { LocalLibraryServer.stop(); } // If we're using a remote Greenstone server we need to make sure that all jobs have completed first if (isGsdlRemote) { RemoteGreenstoneServer.exit(); } // Make sure we haven't started up any processes that are still running if (apps.size() == 0) { // If we're running as an applet we don't actually quit (just hide the main GLI window) if (!Gatherer.isApplet) { // This is the end... System.exit(exit_status); } } else { JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE); g_man.setVisible(false); } } static public void exit() { exit(0); } /** Returns the path of the Greenstone "collect" directory. */ static public String getCollectDirectoryPath() { if (non_standard_collect_directory_path != null) { return non_standard_collect_directory_path; } if (!GS3) { return Configuration.gsdl_path + "collect" + File.separator; } else { return getSitesDirectoryPath() + Configuration.site_name + File.separator + "collect" + File.separator; } } /** Returns the path of the GLI directory. */ static public String getGLIDirectoryPath() { return gli_directory_path; } /** Returns the path of the GLI "metadata" directory. */ static public String getGLIMetadataDirectoryPath() { return getGLIDirectoryPath() + "metadata" + File.separator; } /** Returns the path of the GLI user directory. */ static public String getGLIUserDirectoryPath() { return gli_user_directory_path; } /** Returns the path of the GLI user "cache" directory. */ static public String getGLIUserCacheDirectoryPath() { return getGLIUserDirectoryPath() + "cache" + File.separator; } /** Returns the path of the GLI user "log" directory. */ static public String getGLIUserLogDirectoryPath() { return getGLIUserDirectoryPath() + "log" + File.separator; } static public String getSitesDirectoryPath() { return Configuration.gsdl3_path + "sites" + File.separator; } static public void setCollectDirectoryPath(String collect_directory_path) { non_standard_collect_directory_path = collect_directory_path; if (!non_standard_collect_directory_path.endsWith(File.separator)) { non_standard_collect_directory_path = non_standard_collect_directory_path + File.separator; } } static public void setGLIDirectoryPath(String gli_directory_path_arg) { gli_directory_path = gli_directory_path_arg; } static public void setGLIUserDirectoryPath(String gli_user_directory_path_arg) { gli_user_directory_path = gli_user_directory_path_arg; // Ensure the GLI user directory exists File gli_user_directory = new File(gli_user_directory_path); if (!gli_user_directory.exists() && !gli_user_directory.mkdirs()) { System.err.println("Error: Unable to make directory: " + gli_user_directory); } } static public void refresh(int refresh_reason) { if (g_man != null) { g_man.refresh(refresh_reason, c_man.ready()); } // Now is a good time to force a garbage collect System.gc(); } // used to send reload coll messages to the tomcat server static public void configGS3Server(String site, String command) { if (Configuration.library_url == null) return; try { // need to add the servlet name to the exec address String raw_url = Configuration.library_url.toString() + Configuration.getServletPath() + command; URL url = new URL(raw_url); DebugStream.println("Action: " + raw_url); HttpURLConnection library_connection = (HttpURLConnection) url.openConnection(); int response_code = library_connection.getResponseCode(); if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) { DebugStream.println("200 - Complete."); } else { DebugStream.println("404 - Failed."); } url = null; } catch (Exception exception) { DebugStream.printStackTrace(exception); } } /** Used to 'spawn' a new child application when a file is double clicked. * @param file The file to open * @see org.greenstone.gatherer.Gatherer.ExternalApplication */ static public void spawnApplication(File file) { String [] commands = assoc_man.getCommand(file); if(commands != null) { ExternalApplication app = new ExternalApplication(commands); apps.add(app); app.start(); } else { ///ystem.err.println("No open command available."); } } static public void spawnApplication(String command) { ExternalApplication app = new ExternalApplication(command); apps.add(app); app.start(); } /** Used to 'spawn' a new browser application or reset an existing one when the preview button is clicked * @param url The url to open the browser at * @see org.greenstone.gatherer.Gatherer.BrowserApplication */ static public void spawnBrowser(String url) { String command = assoc_man.getBrowserCommand(url); if (command != null) { BrowserApplication app = new BrowserApplication(command, url); apps.add(app); app.start(); } else { ///ystem.err.println("No browser command available."); } } /** Prints a warning message about a missing library path, which means the final collection cannot be previewed in the Gatherer. */ static private void missingEXEC() { WarningDialog dialog; if (GS3) { dialog = new WarningDialog("warning.MissingEXEC_GS3", Dictionary.get("MissingEXEC_GS3.Title"), Dictionary.get("MissingEXEC_GS3.Message"), "general.library_url", false); } else { dialog = new WarningDialog("warning.MissingEXEC", Dictionary.get("MissingEXEC.Title"), Dictionary.get("MissingEXEC.Message"), "general.library_url", false); } dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false))); dialog.display(); dialog.dispose(); dialog = null; String library_url_string = Configuration.getString("general.library_url", true); if (!library_url_string.equals("")) { try { Configuration.library_url = new URL(library_url_string); } catch (MalformedURLException exception) { DebugStream.printStackTrace(exception); } } } static private void missingGLIServer() { WarningDialog dialog = new WarningDialog("warning.MissingGLIServer", Dictionary.get("MissingGLIServer.Title"), Dictionary.get("MissingGLIServer.Message"), "general.gliserver_url", false); dialog.setValueField(new URLField(Configuration.getColor("coloring.editable_foreground", false), Configuration.getColor("coloring.editable_background", false))); dialog.display(); dialog.dispose(); dialog = null; String gliserver_url_string = Configuration.getString("general.gliserver_url", true); if (!gliserver_url_string.equals("")) { try { Configuration.gliserver_url = new URL(gliserver_url_string); } catch (MalformedURLException exception) { DebugStream.printStackTrace(exception); } } } /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI. */ static private void missingGSDL() { WarningDialog dialog = new WarningDialog("warning.MissingGSDL", Dictionary.get("MissingGSDL.Title"), Dictionary.get("MissingGSDL.Message"), null, false); dialog.display(); dialog.dispose(); dialog = null; } /** Prints a warning message about missing a valid ImageMagick path, which although not fatal means building image collections won't work */ static private void missingImageMagick() { WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", Dictionary.get("MissingImageMagick.Title"), Dictionary.get("MissingImageMagick.Message"), null, false); dialog.display(); dialog.dispose(); dialog = null; } /** 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. */ static private void missingPERL() { WarningDialog dialog = new WarningDialog("warning.MissingPERL", Dictionary.get("MissingPERL.Title"), Dictionary.get("MissingPERL.Message"), null, false); dialog.display(); dialog.dispose(); dialog = null; } /** Sets up the proxy connection by setting JVM Environment flags and creating a new Authenticator. * @see java.lang.Exception * @see java.lang.System * @see java.net.Authenticator * @see org.greenstone.gatherer.Configuration * @see org.greenstone.gatherer.GAuthenticator */ static public void setProxy() { try {// Can throw several exceptions if(Configuration.get("general.use_proxy", true)) { System.setProperty("http.proxyType", "4"); System.setProperty("http.proxyHost", Configuration.getString("general.proxy_host", true)); System.setProperty("http.proxyPort", Configuration.getString("general.proxy_port", true)); System.setProperty("http.proxySet", "true"); } else { System.setProperty("http.proxyHost", ""); System.setProperty("http.proxyPort", ""); System.setProperty("http.proxySet", "false"); } } catch (Exception error) { DebugStream.println("Error in Gatherer.initProxy(): " + error); DebugStream.printStackTrace(error); } } /** 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. */ static private class ExternalApplication extends Thread { private Process process = null; /** The initial command string given to this sub-process. */ private String command = null; private String[] commands = null; /** Constructor. * @param command The initial command String. */ public ExternalApplication(String command) { this.command = command; } public ExternalApplication(String[] commands) { this.commands = commands; } /** We start the child process inside a new thread so it doesn't block the rest of Gatherer. * @see java.lang.Exception * @see java.lang.Process * @see java.lang.Runtime * @see java.lang.System * @see java.util.Vector */ public void run() { // Call an external process using the args. try { if(commands != null) { StringBuffer whole_command = new StringBuffer(); for(int i = 0; i < commands.length; i++) { whole_command.append(commands[i]); whole_command.append(" "); } DebugStream.println("Running " + whole_command.toString()); Runtime rt = Runtime.getRuntime(); process = rt.exec(commands); process.waitFor(); } else { DebugStream.println("Running " + command); Runtime rt = Runtime.getRuntime(); process = rt.exec(command); process.waitFor(); } } catch (Exception exception) { DebugStream.printStackTrace(exception); } // Remove ourself from Gatherer list of threads. apps.remove(this); // Call exit if we were the last outstanding child process thread. if (apps.size() == 0 && exit == true) { // In my opinion (DB) there is no need to exit here, // the 'run' method ending naturally brings this // thread to an end. In fact it is potentially // dangerous to exit here, as the main thread in the // Gatherer class may be stopped prematurely. As it so // happens the point at which the ExternalApplication thread // is asked to stop (Back in the main Gatherer thread) is after // various configuration files have been saved. // // A similar argument holds for BrowserApplication thread below. System.exit(exit_status); } } public void stopExternalApplication() { if(process != null) { process.destroy(); } } } /** 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. */ static private class BrowserApplication extends Thread { private Process process = null; /** The initial command string given to this sub-process. */ private String command = null; private String url = null; private String[] commands = null; public BrowserApplication(String command, String url) { StringTokenizer st = new StringTokenizer(command); int num_tokens = st.countTokens(); this.commands = new String [num_tokens]; int i=0; while (st.hasMoreTokens()) { commands[i] = st.nextToken(); i++; } //this.commands = commands; this.url = url; } /** We start the child process inside a new thread so it doesn't block the rest of Gatherer. * @see java.lang.Exception * @see java.lang.Process * @see java.lang.Runtime * @see java.lang.System * @see java.util.Vector */ public void run() { // Call an external process using the args. if(commands == null) { apps.remove(this); return; } try { String prog_name = commands[0]; String lower_name = prog_name.toLowerCase(); if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) { DebugStream.println("found mozilla or netscape, trying remote it"); // mozilla and netscape, try using a remote command to get things in the same window String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+",new-tab)"}; printArray(new_commands); Runtime rt = Runtime.getRuntime(); process = rt.exec(new_commands); int exitCode = process.waitFor(); if (exitCode != 0) { // if Netscape or mozilla was not open DebugStream.println("couldn't do remote, trying original command"); printArray(commands); process = rt.exec(commands); // try the original command } } else { // just run what we have been given StringBuffer whole_command = new StringBuffer(); for(int i = 0; i < commands.length; i++) { whole_command.append(commands[i]); whole_command.append(" "); } DebugStream.println("Running " + whole_command.toString()); Runtime rt = Runtime.getRuntime(); process = rt.exec(commands); process.waitFor(); } } catch (Exception exception) { DebugStream.printStackTrace(exception); } // Remove ourself from Gatherer list of threads. apps.remove(this); // Call exit if we were the last outstanding child process thread. if (apps.size() == 0 && exit == true) { System.exit(exit_status); } } public void printArray(String [] array) { for(int i = 0; i < array.length; i++) { DebugStream.print(array[i]+" "); System.err.println(array[i]+" "); } } public void stopBrowserApplication() { if(process != null) { process.destroy(); } } } private class ImageMagickTest { public boolean found() { try { String[] command = new String[2]; command[0] = (Utility.isWindows() ? "identify.exe" : "identify"); command[1] = "-version"; Process image_magick_process = Runtime.getRuntime().exec(command); image_magick_process.waitFor(); return (image_magick_process.exitValue() == 0); } catch (IOException exception) { return false; } catch (InterruptedException exception) { return false; } } } private class PerlTest { private String[] command = new String[2]; public PerlTest() { command[0] = (Utility.isWindows() ? Utility.PERL_EXECUTABLE_WINDOWS : Utility.PERL_EXECUTABLE_UNIX); command[1] = "-version"; } public boolean found() { try { Process perl_process = Runtime.getRuntime().exec(command); perl_process.waitFor(); return (perl_process.exitValue() == 0); } catch (Exception exception) { return false; } } public String toString() { return command[0]; } } }