Changeset 4335


Ignore:
Timestamp:
2003-05-26T16:25:32+12:00 (21 years ago)
Author:
jmt12
Message:

GLI now recognizes local library - John

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r4303 r4335  
    5858import org.greenstone.gatherer.msm.MetadataSetManager;
    5959import org.greenstone.gatherer.util.ArrayTools;
     60import org.greenstone.gatherer.util.GSDLSiteConfig;
    6061import org.greenstone.gatherer.util.Utility;
    6162import sun.misc.*;
     
    99100     /** Extra environment information which must be set before shell processes will run properly. Should always be null if the startup script/program has done its job properly. */
    100101     static public String extra_env[] = null;
     102     private GSDLSiteConfig gsdlsite_cfg = null;
     103     private ExternalApplication server = null;
    101104     /** The name of the Gatherers configuration file. */
    102105     static private String CONFIG_FILE_NAME = "gatherer.cfg";
     
    168171                dictionary = new Dictionary(config.getLocale("general.locale", true), config.getFont("general.font", true));
    169172
     173                // If we were given a server run it if neccessary.
     174                startServerEXE();
     175
    170176                // Having loaded the configuration (necessary to determine if certain warnings have been disabled) and dictionary, we now check if the necessary path variables have been provided.
    171                
     177
    172178                if(config.exec_file == null && config.exec_address == null) {
    173179                     missingEXEC(dictionary);
     
    176182                     missingGSDL(dictionary);
    177183                }
    178                 // Perl path is a little different as it is perfectly ok to start the Gatherer without providing a perl path 
     184                // Perl path is a little different as it is perfectly ok to start the Gatherer without providing a perl path
    179185                boolean found_perl = false;
    180186                if(config.perl_path != null) {
     
    301307                config.setDimension("general.size", true, size);
    302308          }
     309
    303310          // Save configuration.
    304311          saveConfig();
     
    314321          }
    315322          if(apps.size() == 0) {
    316                 try {
    317                      wait(200);
    318                 }
    319                 catch(Exception error) {
    320                 }
     323                stopServerEXE();
     324                //try {
     325                //   wait(200);
     326                //}
     327                //catch(Exception error) {
     328                //}
    321329                System.exit(0);
    322330          }
     
    444452              else if(args[i].equals("-library") && (i + 1) < args.length) {
    445453                     exec_path = args[i+1];
     454                     // If the user has given us an address, but it ends with a '/' we assume we're using the greenstone library.cgi
     455                     if(exec_path.endsWith("/")) {
     456                          exec_path = exec_path + "library";
     457                     }
    446458              }
    447459              else if(args[i].equals("-perl")) {
     
    625637          try {
    626638                config = new Configuration(gsdl_path, exec_path, perl_path);
    627           } 
     639          }
    628640          catch (Exception error) {
    629641                Gatherer.println("config.xml is not a well formed XML document.");
     
    652664     }
    653665
     666     private void startServerEXE() {
     667          if(config.exec_file != null && config.exec_address == null && Utility.isWindows()) {
     668                // First of all we create a GSDLSiteCFG object and check if a URL is already present, in which case the server is already running.
     669                gsdlsite_cfg = new GSDLSiteConfig(config.exec_file);
     670                if(gsdlsite_cfg.exists()) {
     671                     String url = gsdlsite_cfg.getURL();
     672                     // If its already running then set exec address.
     673                     if(url != null) {
     674                          try {
     675                                config.exec_address = new URL(url);
     676                                gsdlsite_cfg = null; // Don't need this anymore.
     677                          }
     678                          catch(Exception error) {
     679                          }
     680                     }
     681                     // Otherwise its time to run the server in a spawned process.
     682                     if(config.exec_address == null) {
     683                          // Configure for immediate entry.
     684                          gsdlsite_cfg.setAutoEnter();
     685                          // Spawn server
     686                          server = new ExternalApplication(config.exec_file.getAbsolutePath());
     687                          apps.add(server);
     688                          server.start();
     689                          // Now we have to wait until program has started. We do this by reloading and checking
     690                          try {
     691                                gsdlsite_cfg.load();
     692                                while((url = gsdlsite_cfg.getURL()) == null) {
     693                                    synchronized(this) {
     694                                        wait(1000);
     695                                    }
     696                                     gsdlsite_cfg.load();
     697                                }
     698                                // Ta-da. Now the url should be available.
     699                                config.exec_address = new URL(url);
     700                          }
     701                          catch (Exception error) {
     702                              error.printStackTrace();
     703                          }
     704                     }
     705                }
     706                // Can't do a damb thing.
     707          }
     708     }
     709
     710    private void stopServerEXE() {
     711        // If we started the local library server, then kill it.
     712        if(server != null) {
     713            // Wait until it exits.
     714            try {
     715                gsdlsite_cfg.load();
     716                while(gsdlsite_cfg.getURL() != null) {
     717                    synchronized(this) {
     718                        wait(1000);
     719                    }
     720                    gsdlsite_cfg.load();
     721                }
     722            }
     723            catch (Exception error) {
     724              error.printStackTrace();
     725            }
     726            // Restore the gsdlsite_cfg
     727            if(gsdlsite_cfg != null) {
     728                gsdlsite_cfg.restoreAutoEnter();
     729            }
     730        }
     731    }
     732
    654733     /** 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. */
    655734     private class ExternalApplication
    656735          extends Thread {
     736          private Process process = null;
    657737          /** The initial command string given to this sub-process. */
    658738          private String command = null;
     
    675755                     debug("Running " + command);
    676756                     Runtime rt = Runtime.getRuntime();
    677                      Process prcs = rt.exec(command);
    678                      prcs.waitFor();
     757                     process = rt.exec(command);
     758                     process.waitFor();
    679759                }
    680760                catch (Exception error) {
     
    685765                // Call exit if we were the last outstanding child process thread.
    686766                if(apps.size() == 0 && exit == true) {
     767                     stopServerEXE();
    687768                     System.exit(0);
    688769                }
     770          }
     771          public void stopExternalApplication() {
     772             if(process != null) {
     773                process.destroy();
     774             }
    689775          }
    690776     }
Note: See TracChangeset for help on using the changeset viewer.