Changeset 8620


Ignore:
Timestamp:
2004-11-23T09:29:02+13:00 (19 years ago)
Author:
mdewsnip
Message:

Moved all the local library server code in a new LocalLibraryServer class. This will make things a lot cleaner and more easily allow a few bugs and features to be done.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
1 added
9 edited

Legend:

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

    r8396 r8620  
    9797    static public Configuration self = null;
    9898
    99     static public File exec_file;
    10099    /** The path (or url) to the webserver which is serving the Greenstone collection. */
    101100    static public String exec_path = null;
     
    163162        }
    164163    }
    165     // If the above failed, then its up to us to try and figure out what to do.
    166     if(exec_address == null) {
    167         // Try building a file from the given exec_path
    168         try {
    169         File local_file = new File(exec_path);
    170         if(local_file.exists()) {
    171             // All good. I hope.
    172             exec_file = local_file;
    173         }
    174         else {
    175             DebugStream.println("No local library at given file path.");
    176         }
    177         }
    178         // All sorts of errors might be thrown by a bogus file path.
    179         catch (Exception error) {
    180         DebugStream.println("Libary url does not indicate the server.exe file.");
    181         ///atherer.printStackTrace(error);
    182         }
    183         // We can generate the path to where the local library should be and use that if it is there.
    184         if(exec_file == null) {
    185         File server_exe = new File(gsdl_path + Utility.SERVER_EXE);
    186         if(server_exe.exists()) {
    187             exec_file = server_exe;
    188         }
    189         else {
    190             DebugStream.println("No local library.");
    191         }
    192         }
    193         // If we get to here with no exec_address nor an exec_file its just plain not going to work.
    194     }
    195     else {
    196         DebugStream.println("exec_address != null -> " + exec_address);
    197     }
    198164
    199165    this.perl_path = perl_path;
     
    290256        }
    291257    }
    292     DebugStream.println("EXEC_FILE = " + exec_file);
     258
    293259    DebugStream.println("EXEC_ADDRESS = " + exec_address);
    294260    }
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r8613 r8620  
    4949import org.greenstone.gatherer.gui.URLField;
    5050import org.greenstone.gatherer.gui.WarningDialog;
    51 import org.greenstone.gatherer.util.GSDLSiteConfig;
    5251import org.greenstone.gatherer.util.StaticStrings;
    5352import org.greenstone.gatherer.util.Utility;
     
    116115    /** the action recorder dialog */
    117116    static public ActionRecorderDialog feedback_dialog = null;
    118     /** 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. */
    119     static public String extra_env[] = null;
    120     private GSDLSiteConfig gsdlsite_cfg = null;
    121     private ExternalApplication server = null;
     117
    122118
    123119    /** Magic to allow Enter to fire the default button. */
     
    136132
    137133
    138     public GUIManager init(Dimension size, String gsdl_path, String gsdl3_path,
     134    public GUIManager init(Dimension size, String gsdl_path, String gsdl3_path, String local_library_path,
    139135               String exec_path, boolean debug_enabled, String perl_path,
    140136               boolean no_load, String open_collection,
     
    143139               String wget_path)
    144140    {
    145 
    146141    // This will hopefully catch ctrl-c and terminate, and exit gracefully. However it is platform specific, and may not be supported by some JVMs.
    147142    /** It does, but I get bloody sick of it working when the Gatherer hangs.
     
    202197        }
    203198
    204         // If we were given a server run it if neccessary.
    205         if (!GS3 && Configuration.exec_file != null) {
    206         startServerEXE();
     199        // Start up the local library server, if that's what we want
     200        if (Utility.isWindows() && local_library_path != null && !GS3) {
     201        LocalLibraryServer.start(gsdl_path, local_library_path);
    207202        }
    208203
    209204        // 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.
    210         if (Configuration.exec_file == null && Configuration.exec_address == null) {
    211         DebugStream.println("config.exec_file is null");
     205        if (local_library_path == null && Configuration.exec_address == null) {
     206        DebugStream.println("local_library_path is null");
    212207        DebugStream.println("config.exec_address is null");
    213208        missingEXEC();
     
    433428
    434429    // If we started a server, we should try to stop it.
    435     if(!GS3 && gsdlsite_cfg != null) {
    436         stopServerEXE();
     430    if (LocalLibraryServer.isRunning() == true) {
     431        LocalLibraryServer.stop();
    437432    }
    438433
     
    445440        JOptionPane.showMessageDialog(g_man, Dictionary.get("General.Outstanding_Processes"), Dictionary.get("General.Outstanding_Processes_Title"), JOptionPane.ERROR_MESSAGE);
    446441        g_man.setVisible(false);
    447     }
    448     }
    449 
    450     // Used to send messages to the local library
    451     // Warning: this has a lot of potential for nasty race conditions
    452     // The response code is returned immediately -- but this does not mean the local
    453     //   library action has finished!
    454     static public void configServer(String command)
    455     {
    456     try {
    457         String raw_url = Configuration.exec_address.toString() + command;
    458         URL url = new URL(raw_url);
    459         DebugStream.println("Action: " + raw_url);
    460         HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
    461         int response_code = library_connection.getResponseCode();
    462         if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
    463         DebugStream.println("200 - Complete.");
    464         }
    465         else {
    466         DebugStream.println("404 - Failed.");
    467         }
    468         url = null;
    469     }
    470     catch (Exception ex) {
    471         DebugStream.printStackTrace(ex);
    472442    }
    473443    }
     
    522492    }
    523493    }
    524    
     494
     495
     496    public void spawnApplication(String command)
     497    {
     498    ExternalApplication app = new ExternalApplication(command);
     499    apps.add(app);
     500    app.start();
     501    }
     502
     503
    525504    /** Used to 'spawn' a new  browser application or reset an existing one when the preview button is clicked
    526505     * @param url The url to open the browser at
     
    681660    }
    682661
    683     private void startServerEXE() {
    684     if(Configuration.exec_file != null && Configuration.exec_address == null && Utility.isWindows() && !GS3) {
    685         // First of all we create a GSDLSiteCFG object and check if a URL is already present, in which case the server is already running.
    686         gsdlsite_cfg = new GSDLSiteConfig(Configuration.exec_file);
    687         String url = gsdlsite_cfg.getURL();
    688         // If its already running then set exec address.
    689         if(url != null) {
    690         try {
    691             Configuration.exec_address = new URL(url);
    692         }
    693         catch(Exception error) {
    694         }
    695         }
    696         // Otherwise its time to run the server in a spawned process.
    697         if(Configuration.exec_address == null && Configuration.exec_file.exists()) {
    698         // Configure for immediate entry. Note that this only works if the gsdlsite.cfg file exists.
    699         gsdlsite_cfg.set();
    700         // Spawn server
    701         String command = Configuration.exec_file.getAbsolutePath() + " " + gsdlsite_cfg.getSiteConfigFilename();
    702         server = new ExternalApplication(command);
    703         server.start();
    704         command = null;
    705         // Now we have to wait until program has started. We do this by reloading and checking
    706         ///ystem.err.print("Waiting until the local library has loaded");
    707         try {
    708             gsdlsite_cfg.load();
    709            
    710             int try_again = JOptionPane.YES_OPTION;
    711             int attempt_count = 0;
    712             while(gsdlsite_cfg.getURL() == null && try_again == JOptionPane.YES_OPTION) {
    713             ///ystem.err.print(".");
    714             if(attempt_count == 60) {
    715                 attempt_count = 0;
    716                 try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
    717             }
    718             else {
    719                 synchronized(this) {
    720                 wait(1000); // Wait one second (give or take)
    721                 }
    722                 gsdlsite_cfg.load();
    723                 attempt_count++;
    724             }
    725             }
    726            
    727             if((url = gsdlsite_cfg.getURL()) != null) {
    728             // Ta-da. Now the url should be available.
    729             Configuration.exec_address = new URL(url);
    730            
    731             // A quick test involves opening a connection to get the home page  for this collection. If this fails then we try changing the url to be localhost.
    732             try {
    733                 DebugStream.println("Try connecting to server on config url: '" + Configuration.exec_address + "'");
    734                 URLConnection connection = Configuration.exec_address.openConnection();
    735                 connection.getContent();
    736             }
    737             catch(IOException bad_url_connection) {
    738                 try {
    739                 DebugStream.println("Try connecting to server on local host: '" + gsdlsite_cfg.getLocalHostURL() + "'");
    740                 Configuration.exec_address = new URL(gsdlsite_cfg.getLocalHostURL   ());
    741                 URLConnection connection = Configuration.exec_address.openConnection();
    742                 connection.getContent();
    743                 }
    744                 catch(IOException worse_url_connection) {
    745                 DebugStream.println("Can't connect to server on either address.");
    746                 Configuration.exec_address = null;
    747                 Configuration.exec_file = null;
    748                 }
    749             }
    750             }
    751             // Unable to start local library. Show appropriate message.
    752             else {
    753             missingEXEC();
    754             }
    755         }
    756         catch (Exception exception) {
    757             DebugStream.printStackTrace(exception);
    758         }
    759         }
    760         // Can't do a damb thing.
    761     }
    762     DebugStream.println("Having started server.exe, exec_address is: " + Configuration.exec_address);
    763     }
    764 
    765     private void stopServerEXE() {
    766     if(server != null && Configuration.exec_address != null) {
    767         // See if its already exited for some reason.
    768         gsdlsite_cfg.load();
    769         if(gsdlsite_cfg.getURL() != null) {
    770         // Send the command for it to exit.
    771         configServer(GSDLSiteConfig.QUIT_COMMAND);
    772         // Wait until it exits.
    773         try {
    774             gsdlsite_cfg.load();
    775             int try_again = JOptionPane.YES_OPTION;
    776             int attempt_count = 0;
    777             while(gsdlsite_cfg.getURL() != null && try_again == JOptionPane.YES_OPTION) {
    778             if(attempt_count == 60) {
    779                 attempt_count = 0;
    780                 try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
    781             }
    782             else {
    783                 synchronized(this) {
    784                 wait(1000); // Wait one second (give or take)
    785                 }
    786                 gsdlsite_cfg.load();
    787                 attempt_count++;
    788             }
    789             }
    790             //if(gsdlsite_cfg.getURL() != null) {
    791             //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    792             //}
    793         }
    794         catch (Exception exception) {
    795             DebugStream.printStackTrace(exception);
    796         }
    797         }
    798         // Restore the gsdlsite_cfg.
    799         if(gsdlsite_cfg != null) {
    800         gsdlsite_cfg.restore();
    801         }
    802         // If the local server is still running then our changed values will get overwritten.
    803         if(gsdlsite_cfg.getURL() != null) {
    804         JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitFailed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    805         }
    806         gsdlsite_cfg = null;
    807         server = null;
    808     }
    809     }
    810662
    811663    // TODO fill this in
     
    865717        apps.remove(this);
    866718        // Call exit if we were the last outstanding child process thread.
    867         if(apps.size() == 0 && exit == true) {
    868         stopServerEXE();
     719        if (apps.size() == 0 && exit == true) {
     720        LocalLibraryServer.stop();
    869721        System.exit(0);
    870722        }
     
    953805        apps.remove(this);
    954806        // Call exit if we were the last outstanding child process thread.
    955         if(apps.size() == 0 && exit == true) {
    956         stopServerEXE();
     807        if (apps.size() == 0 && exit == true) {
     808        LocalLibraryServer.stop();
    957809        System.exit(0);
    958810        }
  • trunk/gli/src/org/greenstone/gatherer/GathererApplet.java

    r8250 r8620  
    171171   
    172172
    173     g_man = gatherer.init(size, go.gsdl_path, go.gsdl3_path,
     173    g_man = gatherer.init(size, go.gsdl_path, go.gsdl3_path, go.local_library_path,
    174174                    go.exec_path, go.debug, go.perl_path,
    175175                    go.no_load, go.filename, go.site_name,
  • trunk/gli/src/org/greenstone/gatherer/GathererProg.java

    r7991 r8620  
    108108    Splash splash = new Splash();
    109109    GUIManager g_man
    110         = gatherer.init(size, go.gsdl_path, go.gsdl3_path,
     110        = gatherer.init(size, go.gsdl_path, go.gsdl3_path, go.local_library_path,
    111111                 go.exec_path, go.debug, go.perl_path,
    112112                 go.no_load, go.filename, go.site_name,
  • trunk/gli/src/org/greenstone/gatherer/GetOpt.java

    r8618 r8620  
    186186            // Parse the file path of the local library server
    187187            else if (argument_name.equals(StaticStrings.LOCAL_LIBRARY_ARGUMENT)) {
    188             exec_path = argument_value;
    189             // If there is no colon in first five characters of
    190             // the exec_path (which would either be an existing
    191             // protocol, or a windows file path to say a local
    192             // library), we can append the protocol http://.
    193             if(argument_value.lastIndexOf(StaticStrings.COLON_CHARACTER, 5) == -1) {
    194                 exec_path = StaticStrings.HTTP_PROTOCOL_STR + argument_value;
    195             }
    196             else {
    197                 exec_path = argument_value;
    198             }
    199             // If the user has given us an address, but it ends
    200             // with a '/' we assume we're using the greenstone
    201             // library.cgi
    202             if(exec_path.startsWith(StaticStrings.HTTP_PROTOCOL_STR) && exec_path.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
    203                 exec_path = exec_path + StaticStrings.LIBRARY_STR;
    204             }
     188            local_library_path = argument_value;
     189
     190//          exec_path = argument_value;
     191//          // If there is no colon in first five characters of
     192//          // the exec_path (which would either be an existing
     193//          // protocol, or a windows file path to say a local
     194//          // library), we can append the protocol http://.
     195//          if(argument_value.lastIndexOf(StaticStrings.COLON_CHARACTER, 5) == -1) {
     196//              exec_path = StaticStrings.HTTP_PROTOCOL_STR + argument_value;
     197//          }
     198//          else {
     199//              exec_path = argument_value;
     200//          }
     201//          // If the user has given us an address, but it ends
     202//          // with a '/' we assume we're using the greenstone
     203//          // library.cgi
     204//          if(exec_path.startsWith(StaticStrings.HTTP_PROTOCOL_STR) && exec_path.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
     205//              exec_path = exec_path + StaticStrings.LIBRARY_STR;
     206//          }
    205207            }
    206208            // Parse the path to PERL. If not provided its assumes
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionDesignManager.java

    r8601 r8620  
    3838import org.greenstone.gatherer.Gatherer;
    3939import org.greenstone.gatherer.GathererApplet;
    40 import org.greenstone.gatherer.util.GSDLSiteConfig;
     40import org.greenstone.gatherer.LocalLibraryServer;
    4141import org.greenstone.gatherer.util.Utility;
    4242import org.w3c.dom.*;
     
    200200    public void save() {
    201201    // Release collection as necessary
     202    String collection_name = Gatherer.c_man.getCollection().getName();
    202203    boolean collection_released = false;
    203     String col_name = Gatherer.c_man.getCollection().getName();
    204204    boolean formats_changed = format_manager.formatsChanged();
    205205
    206     if (formats_changed && Gatherer.c_man.built() && Configuration.exec_file != null) {
     206    if (formats_changed && Gatherer.c_man.built() && LocalLibraryServer.isRunning() == true) {
    207207        // Release the collection
    208         Gatherer.configServer(GSDLSiteConfig.RELEASE_COMMAND + col_name);
    209         // This is very important -- it ensures that the above command has finished
    210         Gatherer.configServer("");
     208        LocalLibraryServer.releaseCollection(collection_name);
    211209        collection_released = true;
    212210    }
     
    218216        if (formats_changed && Gatherer.c_man.built()) {
    219217        // upload etc/collect.cfg to server to reflect changes
    220         Utility.zipup(col_name,Utility.CONFIG_FILE);
    221         GathererApplet.upload_url_zip(col_name,"etc");
     218        Utility.zipup(collection_name, Utility.CONFIG_FILE);
     219        GathererApplet.upload_url_zip(collection_name, "etc");
    222220        }
    223221    }
     
    226224    if (collection_released) {
    227225        // Now re-add collection to server to force format commands to be processed
    228         Gatherer.configServer(GSDLSiteConfig.ADD_COMMAND + col_name);
    229         // This is very important -- it ensures that the above command has finished
    230         Gatherer.configServer("");
     226        LocalLibraryServer.addCollection(collection_name);
    231227    }
    232228
  • trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java

    r8606 r8620  
    4848import org.greenstone.gatherer.Dictionary;
    4949import org.greenstone.gatherer.Gatherer;
     50import org.greenstone.gatherer.LocalLibraryServer;
    5051import org.greenstone.gatherer.ServletConfiguration;
    5152import org.greenstone.gatherer.cdm.CollectionDesignManager;
     
    7273import org.greenstone.gatherer.util.ArrayTools;
    7374import org.greenstone.gatherer.util.Codec;
    74 import org.greenstone.gatherer.util.GSDLSiteConfig;
    7575import org.greenstone.gatherer.util.StaticStrings;
    7676import org.greenstone.gatherer.util.Utility;
     
    11521152    else if(event.getType() == GShell.BUILD && event.getStatus() == GShell.OK) {
    11531153        if(installCollection()) {
    1154         // If we have a local library running (that we know about) then we ask it to add our newly create collection
    1155         if (Configuration.exec_file != null) {
    1156             Gatherer.configServer(GSDLSiteConfig.ADD_COMMAND + collection.getName());
    1157             // This is very important -- it ensures that the above command has finished
    1158             Gatherer.configServer("");
     1154        // If we have a local library running then ask it to add our newly create collection
     1155        if (LocalLibraryServer.isRunning() == true) {
     1156            LocalLibraryServer.addCollection(collection.getName());
    11591157        }
    11601158        else if (Gatherer.GS3) {
     
    13561354    try {
    13571355        // Ensure that the local library has released this collection so we can delete the index directory
    1358         if (!Gatherer.GS3 && Configuration.exec_file != null) {
    1359         Gatherer.configServer(GSDLSiteConfig.RELEASE_COMMAND + collection.getName());
    1360 
    1361         // This is very important -- it ensures that the above command has finished
    1362         // This prevents the nasty "could not remove index directory" race condition!
    1363         Gatherer.configServer("");
     1356        if (LocalLibraryServer.isRunning() == true) {
     1357        LocalLibraryServer.releaseCollection(collection.getName());
    13641358        }
    13651359
  • trunk/gli/src/org/greenstone/gatherer/collection/DeleteCollectionPrompt.java

    r8597 r8620  
    4747import org.greenstone.gatherer.Dictionary;
    4848import org.greenstone.gatherer.Gatherer;
     49import org.greenstone.gatherer.LocalLibraryServer;
    4950import org.greenstone.gatherer.gui.GLIButton;
    5051import org.greenstone.gatherer.gui.ModalDialog;
     
    5253import org.greenstone.gatherer.gui.tree.WorkspaceTree;
    5354import org.greenstone.gatherer.util.ArrayTools;
    54 import org.greenstone.gatherer.util.GSDLSiteConfig;
    5555import org.greenstone.gatherer.util.StaticStrings;
    5656import org.greenstone.gatherer.util.Utility;
     
    308308        // Delete the selected collection.
    309309
    310         // first of all we must release it from the local library
    311         if (Configuration.exec_file != null) {
    312         Gatherer.configServer(GSDLSiteConfig.RELEASE_COMMAND + collection.getShortName());
    313         // This is very important -- it ensures that the above command has finished
    314         Gatherer.configServer("");
     310        // First we must release it from the local library
     311        if (LocalLibraryServer.isRunning() == true) {
     312        LocalLibraryServer.releaseCollection(collection.getShortName());
    315313        }
    316314
  • trunk/gli/src/org/greenstone/gatherer/collection/ExportCollectionPrompt.java

    r8590 r8620  
    5656import org.greenstone.gatherer.shell.GShellListener;
    5757import org.greenstone.gatherer.util.ArrayTools;
    58 import org.greenstone.gatherer.util.GSDLSiteConfig;
    5958import org.greenstone.gatherer.util.StaticStrings;
    6059import org.greenstone.gatherer.util.Utility;
Note: See TracChangeset for help on using the changeset viewer.