/** *######################################################################### * * 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.cdm.ClassifierManager; import org.greenstone.gatherer.cdm.PluginManager; 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.gui.GUIManager; import org.greenstone.gatherer.gui.URLField; import org.greenstone.gatherer.gui.WarningDialog; 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 { /** Has the exit flag been set? */ public boolean exit = false; /** All of the external applications that must exit before we close the Gatherer. */ public 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 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; static private boolean g_man_built = false; /** A static reference to ourselves. */ static public Gatherer self; /** We are using the GLI for GS3 */ static public boolean GS3 = false; static public boolean isGsdlRemote = false; static public String cgiBase = ""; // 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; /** 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() { this.self = this; Utility.initImages(this); } public GUIManager init(Dimension size, String gsdl_path, String gsdl3_path, String local_library_path, String library_url_string, boolean debug_enabled, String perl_path, boolean no_load, String open_collection, String site_name, String servlet_path, String wget_path) { if (gsdl3_path != null && !gsdl3_path.equals("")) { this.GS3 = true; } else { gsdl3_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 = Utility.getGLIUserFolder().toString() + File.separator + debug_file_path; DebugStream.println("Debug file path: " + debug_file_path); DebugStream.setDebugFile(debug_file_path); DebugStream.print(System.getProperties()); } try { // Load GLI config file new Configuration(gsdl_path, gsdl3_path, site_name); if (Configuration.just_created_new_config_file) { // Delete the plugins.dat and classifiers.dat files PluginManager.clearPluginCache(); ClassifierManager.clearClassifierCache(); } if (GS3) { // Load Greenstone 3 servelt 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)); } if (gsdl_path == null) { missingGSDL(); } // Start up the local library server, if that's what we want if (Utility.isWindows() && local_library_path != null && !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(); } // 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(); } } boolean download_workflow = Configuration.get("workflow.download", false); if (download_workflow) { // If the WGet path hasn't been specified by the user, try the Greenstone one if (wget_path == null) { File wget_file = new File(Utility.getWGetPath(gsdl_path)); if (wget_file.exists()) { wget_path = Utility.getWGetPath(gsdl_path); } } // If we still don't have a WGet path, give up (there isn't anything else we can do) String wget_version_str = null; if (wget_path == null) { wget_version_str = StaticStrings.NO_WGET_STR; missingWGET(); } // Otherwise check the version of WGet else { wget_version_str = testWGetVersion(wget_path); if (wget_version_str.equals(StaticStrings.WGET_OLD_STR)) { oldWGET(); } } // Store the new values in the configuration DebugStream.println("WGet path: " + wget_path); Configuration.setWGetPath(wget_path); Configuration.setWGetVersion(wget_version_str); } // 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(); 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); return g_man; } public void run(Dimension size, GUIManager g_man) { // 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) { size.width = Configuration.screen_size.width; } if (size.height < 480) { size.height = 480; } else if (size.height > Configuration.screen_size.height) { 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); } } /** 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 */ public void exit() { exit = true; // If we have an open collection make note of it. Configuration.setString("general.open_collection", true, null); if(c_man.ready()) { ///ystem.err.println("Collection open."); if(c_man.saved()) { ///ystem.err.println("Collection has been recently saved, so I'll remember it for next time."); Configuration.setString("general.open_collection", true, c_man.getCollectionFilePath()); } c_man.closeCollection(); } if(assoc_man != null) { assoc_man.save(); assoc_man = null; } // Store the current position and size (if reasonable) of the Gatherer for next time Rectangle bounds = g_man.getBounds(); Configuration.setBounds("general.bounds", true, bounds); // Save configuration. Configuration.save(); // Get the gui to deallocate g_man.setVisible(false); 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(apps.size() == 0) { if (!Gatherer.isGsdlRemote) { System.exit(0); } } 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 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; } } static public String getSitesDirectoryPath() { return Configuration.gsdl3_path + File.separator + "web" + File.separator + "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 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) { 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 */ 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."); } } 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 */ 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 public void missingEXEC() { WarningDialog dialog; if (GS3) { dialog = new WarningDialog("warning.MissingEXEC_GS3", "MissingEXEC_GS3.Title", Dictionary.get("MissingEXEC_GS3.Message"), "general.library_url", false); } else { dialog = new WarningDialog("warning.MissingEXEC", "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), Configuration.getColor("coloring.error_foreground", false), Configuration.getColor("coloring.error_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); } } } /** Prints a warning message about a missing GSDL path, which although not fatal pretty much ensures nothing will work properly in the GLI. */ static public void missingGSDL() { WarningDialog dialog = new WarningDialog("warning.MissingGSDL", "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 public void missingImageMagick() { WarningDialog dialog = new WarningDialog("warning.MissingImageMagick", "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 public void missingPERL() { WarningDialog dialog = new WarningDialog("warning.MissingPERL", "MissingPERL.Title", Dictionary.get("MissingPERL.Message"), null, false); dialog.display(); dialog.dispose(); dialog = null; } /** Prints a warning message about missing a valid WGet path, which although not fatal means mirroring won't work */ static public void missingWGET() { WarningDialog dialog = new WarningDialog("warning.MissingWGET", "MissingWGET.Title", Dictionary.get("MissingWGET.Message"), null, false); dialog.display(); dialog.dispose(); dialog = null; } /** Prints a warning message about having an old version of WGet. not fatal, but mirroring may not work properly */ static public void oldWGET() { WarningDialog dialog = new WarningDialog("warning.OldWGET", "OldWGET.Title", Dictionary.get("OldWGET.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.proxySet", "false"); } } catch (Exception error) { DebugStream.println("Error in Gatherer.initProxy(): " + error); DebugStream.printStackTrace(error); } } // TODO fill this in private String testWGetVersion(String wget_path) { return StaticStrings.WGET_STR; } /** 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. */ 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) { System.exit(0); } } 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. */ 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(0); } } 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 (Exception 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]; } } }