Changeset 18987


Ignore:
Timestamp:
04/15/09 18:27:33 (12 years ago)
Author:
ak19
Message:

Updated several files such that the Server2 code in server.jar can interact with GLI code so that the local GS2 server for Linux works the same way as the GS2 Local Library Server on windows.

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

Legend:

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

    r18644 r18987  
    325325        }
    326326        else { // local greenstone: Start up the local library server, if that's what we want
    327         if (Utility.isWindows() && local_library_path != null && !GS3) {
     327        if (local_library_path != null && !GS3) {
    328328            LocalLibraryServer.start(gsdl_path, local_library_path);
    329329        }
     
    795795    apps.add(app);
    796796    app.start();
     797    }
     798
     799    static public void spawnApplication(String command, String ID)
     800    {
     801    ExternalApplication app = new ExternalApplication(command, ID);
     802    apps.add(app);
     803    app.start();
     804    }
     805
     806    static public void terminateApplication(String ID) {
     807    for(int i = 0; i < apps.size(); i++) {
     808        ExternalApplication app = (ExternalApplication)apps.get(i);
     809        if(app.getID() != null && app.getID().equals(ID)) {
     810        app.stopExternalApplication();
     811        apps.remove(app);
     812        }
     813    }
    797814    }
    798815
     
    10601077    private String command = null;
    10611078    private String[] commands = null;
     1079
     1080    private String ID = null;
     1081
    10621082    /** Constructor.
    10631083     * @param command The initial command <strong>String</strong>.
     
    10701090        this.commands = commands;
    10711091    }
     1092
     1093    public ExternalApplication(String command, String ID) {
     1094        this.command = command;
     1095        this.ID = ID;
     1096    }
     1097   
     1098    public String getID() {
     1099        return ID;
     1100    }
     1101
    10721102    /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
    10731103     * @see java.lang.Exception
  • gli/trunk/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java

    r18650 r18987  
    3737import org.greenstone.gatherer.Dictionary;
    3838import org.greenstone.gatherer.Gatherer;
     39import org.greenstone.gatherer.util.PortFinder;
     40import org.greenstone.gatherer.util.Utility;
    3941
    4042
     
    4951
    5052    static private boolean running = false;
     53    static private String ID = "greenstone-server"; // a sort of process ID
     54
     55    // Need to use sockets to tell the server program to terminate when on Linux
     56    // The socket port number that we will use to communicate the termination
     57    static private int port;
     58
     59    // The server is persistent if it does not have to reload all the values
     60    // over and over again each time. Tomcat is persistent and fastcgi is too,
     61    // but the Apache webserver is not persistent by default.
     62    // Change the initialisation of this value depending on whether fastcgi is
     63    // on. At the moment, this does not apply to the Linux' local library server.
     64    static private boolean isPersistentServer = Utility.isWindows();
    5165
    5266    static public void addCollection(String collection_name)
    5367    {
    54     config(ADD_COMMAND + collection_name);
     68    if (isPersistentServer) {
     69        config(ADD_COMMAND + collection_name);
     70    }
    5571    }
    5672
     
    5975    static private void config(String command)
    6076    {
     77    if (!isPersistentServer) {
     78        return;
     79    }
    6180    if (Configuration.library_url == null) {
    6281        System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
     
    93112    }
    94113
     114    // Used to send messages to the local server on Linux
     115    static private boolean sendMessageToServer(String message) {
     116    if(Utility.isWindows()) {
     117        return false;
     118    }
     119   
     120    if(port == -1) {
     121        return false;
     122    }
     123       
     124    try {
     125        Socket clientSocket = new Socket("localhost", port);
     126        Writer writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
     127        writer.write(message);
     128        writer.close();
     129        writer = null;
     130    } catch (Exception e) {
     131        System.err.println("An exception occurred when trying to send the message: " + message
     132                   + "\nto the LocalLibraryServer.\n" + e);
     133        return false;
     134    }
     135    return true;
     136    }
    95137
    96138    static public boolean isRunning()
    97139    {
    98140    if (!running) return false;
    99     llssite_cfg_file.load();
     141    llssite_cfg_file.load(true);
    100142    if (llssite_cfg_file.getURL() == null)  return false;
    101143    return true;
     
    105147    static public void releaseCollection(String collection_name)
    106148    {
    107     config(RELEASE_COMMAND + collection_name);
    108     }
    109 
     149    if (isPersistentServer) {
     150        config(RELEASE_COMMAND + collection_name);
     151    }
     152    }
    110153
    111154    static public void start(String gsdl_path, String local_library_server_file_path)
    112155    {
    113     // Check the local library server.exe file exists
     156    // Check the local library server file (server.exe or gs2-server.sh) exists
    114157    local_library_server_file = new File(local_library_server_file_path);
     158   
    115159    if (!local_library_server_file.exists()) {
    116160        DebugStream.println("No local library at given file path.");
    117 
    118         local_library_server_file = new File(gsdl_path + "server.exe");
     161   
     162        String defaultServerFilename = Utility.isWindows() ? "server.exe" : "gs2-server.sh";
     163        local_library_server_file = new File(gsdl_path + defaultServerFilename);
    119164        if (!local_library_server_file.exists()) {
    120165        DebugStream.println("No local library at all.");
     
    122167        }
    123168    }
     169    llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
     170   
     171    // Spawn local library server process
     172    String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
    124173
    125174    // Check if the server is already running
    126     llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
    127175    String url = llssite_cfg_file.getURL();
    128176    if (url != null) {
    129177        // If it is already running then set the Greenstone web server address and we're done
     178        // E.g. if previously GLI was not properly shut down, the URL property (signifying
     179        // the server is still running) would still be in the glisite_cfg file.
    130180        try {
    131181        Configuration.library_url = new URL(url);
    132182        running = true;
     183
     184        // on Linux, run the server interface (will work with any server already running)
     185        if(!Utility.isWindows()) {
     186            Gatherer.spawnApplication(local_library_server_command, ID);
     187        }
    133188        return;
    134189        }
     
    136191        DebugStream.printStackTrace(exception);
    137192        }
    138     }
     193    }   
    139194
    140195    // Configure the server for immediate entry
     
    142197
    143198    // Spawn local library server process
    144     String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + llssite_cfg_file.getSiteConfigFilename();
    145     Gatherer.spawnApplication(local_library_server_command);
    146 
    147     // Wait until program has started, by reloading and checking the URL field
    148     llssite_cfg_file.load();
     199    Gatherer.spawnApplication(local_library_server_command, ID);
     200   
     201    // Wait until program has started
     202    testServerRunning(); // will set running = true when the server is up and running successfully
     203    }
     204
     205
     206    static public void stop()
     207    {
     208    if (running == false) {
     209        return;
     210    }
     211
     212    // Send the command for it to exit.
     213    if (isPersistentServer) {
     214        config(QUIT_COMMAND);
     215    } else {
     216        if(sendMessageToServer("QUIT")) {
     217        Gatherer.terminateApplication(ID);
     218        } else {
     219        System.err.println("Unable to stop the server, since there's no communication port to send the quit msg over."
     220                   + "\nPlease stop the local Greenstone server manually.");       
     221        }
     222    }
     223
     224    // Wait until program has stopped, by reloading and checking the URL field
     225    llssite_cfg_file.load(false);
    149226    int attempt_count = 0;
    150     while (llssite_cfg_file.getURL() == null) {
     227    while (llssite_cfg_file.getURL() != null) {
    151228        new OneSecondWait();  // Wait one second (give or take)
    152         llssite_cfg_file.load();
     229        llssite_cfg_file.load(false);
    153230        attempt_count++;
    154231
     
    163240    }
    164241
     242    // Restore the llssite_cfg.
     243    llssite_cfg_file.restore();
     244
     245    // If the local server is still running then our changed values will get overwritten.
     246    if (llssite_cfg_file.getURL() != null) {
     247        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
     248    }
     249
     250    running = false;
     251    }
     252   
     253    static private String getExtraLaunchArguments(LLSSiteConfig site_cfg_file) {
     254    String args = " " + site_cfg_file.getSiteConfigFilename();
     255
     256    if(Utility.isWindows()) {
     257        return args;
     258    }
     259   
     260    // Else, when running the Local Library Server on Linux, need to provide a port argument
     261    try {
     262        PortFinder portFinder = new PortFinder(50100, 100);
     263        port = portFinder.findPortInRange();
     264    } catch(Exception e) {
     265        System.err.println("Exception when trying to find an available port: " + e);
     266        port = -1;
     267    }
     268   
     269    return args + " --quitport=" + port;
     270    }
     271
     272
     273    // This method first tests whether there is a URL in the llssite_cfg_file
     274    // and after that appears, it tests whether the URL is functional.
     275    static private void testServerRunning() {
     276    // Wait until program has started, by reloading and checking the URL field
     277    llssite_cfg_file.load(false);
     278    int attempt_count = 0;
     279    while (llssite_cfg_file.getURL() == null) {
     280        new OneSecondWait();  // Wait one second (give or take)
     281        llssite_cfg_file.load(false);
     282        attempt_count++;
     283
     284        // After waiting a minute ask the user whether they want to wait another minute
     285        if (attempt_count == 60) {
     286        int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
     287        if (try_again == JOptionPane.NO_OPTION) {
     288            return;
     289        }
     290        attempt_count = 0;
     291        }
     292    }
     293   
    165294    // Ta-da. Now the url should be available
    166295    try {
     
    173302    // A quick test involves opening a connection to get the home page for this collection
    174303    try {
    175         DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
     304        DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url + "'");
    176305        URLConnection connection = Configuration.library_url.openConnection();
    177306        connection.getContent();
     
    184313        URLConnection connection = Configuration.library_url.openConnection();
    185314        connection.getContent();
     315       
    186316        }
    187317        catch (IOException worse_url_connection) {
     
    195325    }
    196326
    197 
    198     static public void stop()
    199     {
    200     if (running == false) {
    201         return;
    202     }
    203 
    204     // Send the command for it to exit.
    205     config(QUIT_COMMAND);
    206 
    207     // Wait until program has stopped, by reloading and checking the URL field
    208     llssite_cfg_file.load();
    209     int attempt_count = 0;
    210     while (llssite_cfg_file.getURL() != null) {
    211         new OneSecondWait();  // Wait one second (give or take)
    212         llssite_cfg_file.load();
    213         attempt_count++;
    214 
    215         // After waiting a minute ask the user whether they want to wait another minute
    216         if (attempt_count == 60) {
    217         int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
    218         if (try_again == JOptionPane.NO_OPTION) {
    219             return;
    220         }
    221         attempt_count = 0;
    222         }
    223     }
    224 
    225     // Restore the llssite_cfg.
    226     llssite_cfg_file.restore();
    227 
    228     // If the local server is still running then our changed values will get overwritten.
    229     if (llssite_cfg_file.getURL() != null) {
    230         JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
    231     }
    232 
    233     running = false;
    234     }
    235    
    236327    static public void checkServerRunning() {
    237328    if (!running) return; // don't worry about it if its not supposed to be running
    238     llssite_cfg_file.load();
    239     if (llssite_cfg_file.getURL() == null) {
     329    llssite_cfg_file.load(true); // don't force reload, load only if modified
     330
     331    String url = llssite_cfg_file.getURL();
     332    if(url != null) {
     333        try {
     334        Configuration.library_url = new URL(url);
     335        }
     336        catch (MalformedURLException exception) {
     337        DebugStream.printStackTrace(exception);
     338        }
     339    } else {
    240340        // need to restart the server again
    241341        llssite_cfg_file.set();
    242342
    243343        // Spawn local library server process
    244         String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + llssite_cfg_file.getSiteConfigFilename();
    245         Gatherer.spawnApplication(local_library_server_command);
    246 
    247     }
    248     }
     344        String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
     345        running = false;
     346        Gatherer.spawnApplication(local_library_server_command, ID);
     347        testServerRunning(); // don't return until the webserver is up and running
     348    }
     349    }
     350
    249351    static private class OneSecondWait
    250352    {
     
    268370    private String autoenter_initial;
    269371    private String start_browser_initial;
     372
     373    private long lastModified = 0;
    270374   
    271375    static final private String AUTOENTER = "autoenter";
     
    274378    static final private String FALSE = "0";
    275379    static final private String GLISITE_CFG = "glisite.cfg";
    276     static final private String GSDL = "gsdl";
     380    static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone
    277381    static final private String LLSSITE_CFG = "llssite.cfg";
    278382    static final private String LOCAL_HOST = "http://localhost";
     
    326430        }
    327431        }
     432
     433        if(glisite_cfg.exists()) {
     434        lastModified = glisite_cfg.lastModified();
     435        }
    328436    }
    329437   
     
    360468        // URL is made from url and portnumber
    361469        String url = (String) get(URL);
     470
     471        if(!Utility.isWindows()) {
     472        return url;
     473        }
     474
    362475        if(url != null) {
    363476        StringBuffer temp = new StringBuffer(url);
     
    383496    }
    384497
    385     public void load() {
     498    public boolean isModified() {
     499        return (lastModified != glisite_cfg.lastModified());
     500    }
     501
     502    public void load(boolean reloadOnlyIfModified) {       
     503
     504        if(isModified()) {
     505        lastModified = glisite_cfg.lastModified();
     506        } else if(reloadOnlyIfModified) {
     507        return; // asked to reload only if modified. Don't reload since not modified
     508        }
     509
    386510        if(glisite_cfg.exists()) {
    387511        debug("Load: " + glisite_cfg.getAbsolutePath());
  • gli/trunk/src/org/greenstone/gatherer/gui/PreviewButton.java

    r15361 r18987  
    8080        return;
    8181    }
     82
     83    // check that the local library server is still running  - doesn't do anything if it's not supposed to be running
     84    // !! Don't like this here
     85    LocalLibraryServer.checkServerRunning();
     86   
    8287    // FLI or GLI for GS2 case (no library_url)
    8388    preview_address = Configuration.library_url.toString() + "?c=" + CollectionManager.getLoadedCollectionName() + "&l=" + Configuration.getLanguage();
     
    124129       
    125130        configureHomeURL();
    126         // check that the local library server is still running  - doesn't do anything if its not supposed to be running
    127         // !! Don't like this here
    128         LocalLibraryServer.checkServerRunning();
     131       
    129132        if (Gatherer.GS3) {
    130133        // we need to rerun the convert in case the format stuff has changed
Note: See TracChangeset for help on using the changeset viewer.