Changeset 22661

Show
Ignore:
Timestamp:
17.08.2010 18:43:08 (9 years ago)
Author:
ak19
Message:

Next set of changes for ticket 152: moveable collectdir (so that collect dir can be on a pen drive). These are changes for when GLI's LocalLibraryServer? tells server.jar to reconfigure itself (to update the COLLECTHOME alias in the apache web server's httpd.conf file when the collectdir has changed) and restart the server.

Location:
main/trunk/gli
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/classes/dictionary.properties

    r22605 r22661  
    632632General.Outstanding_Processes_Title:Waiting To Exit 
    633633General.Pure_Cancel_Tooltip:Abort this action (no changes will be made) 
     634General.Reconfigure:Reconfigure your web server manually 
    634635General.Review_Output:Please review output below: 
    635636General.Redo:Redo 
     
    10111012Server.QuitTimeOut:The greenstone local library has not responded to the\nautomatic close command for {0} seconds. Do you\nwish to wait for another {0} seconds? 
    10121013Server.StartUpTimeOut:The greenstone local library has not responded to the\nstart command for {0} seconds. Do you\nwish to wait for another {0} seconds? 
     1014Server.Reconfigure:You just changed collecthome.\nAs GLI didn't start your webserver, it can't reconfigure this automatically.\nPlease reconfigure your server manually to work with the change to collecthome. 
    10131015# 
    10141016#***** Sources ***** 
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r22605 r22661  
    829829 
    830830    // non-destructive update of gsdl(3)site.cfg (comments preserved) 
    831     Utility.updatePropertyConfigFile(gsdlsitecfg, "collecthome", Gatherer.getCollectDirectoryPath()); 
     831    String collectDir = Gatherer.getCollectDirectoryPath(); 
     832    collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end     
     833    Utility.updatePropertyConfigFile(gsdlsitecfg, "collecthome", collectDir); 
     834 
     835    if(!Gatherer.GS3 && Gatherer.isLocalLibrary) { 
     836        // for Images in the collection to work, the apache web server  
     837        // configuration's COLLECTHOME should be updated on collectdir change.  
     838        // Does nothing for server.exe at the moment 
     839 
     840        LocalLibraryServer.reconfigure(); 
     841    } 
    832842    } 
    833843 
  • main/trunk/gli/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java

    r22608 r22661  
    5858    // The socket port number that we will use to communicate the termination 
    5959    static private int port; 
     60    static private Socket clientSocket = null; 
     61    static private Writer clientSocketWriter = null; 
    6062 
    6163    // The server is persistent if it does not have to reload all the values 
     
    125127         
    126128    try { 
    127         Socket clientSocket = new Socket("localhost", port); 
    128         Writer writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
    129         writer.write(message); 
    130         writer.close(); 
    131         writer = null; 
     129        if(clientSocket == null) { 
     130        clientSocket = new Socket("localhost", port); 
     131        } 
     132        if(clientSocketWriter == null) { 
     133        clientSocketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); 
     134        } 
     135        clientSocketWriter.write(message); 
     136        clientSocketWriter.flush(); 
     137         
    132138    } catch (Exception e) { 
    133139        System.err.println("An exception occurred when trying to send the message: " + message  
     
    286292    } 
    287293 
     294    /** Call this when the collect directory has changed. Only works when using  
     295     * the web server launched by GLI. */ 
     296    static public void reconfigure() { 
     297    if(isPersistentServer) { 
     298        System.err.println("***** Reconfigure is not yet implemented for server.exe"); 
     299        return; 
     300    } 
     301 
     302    // can't control the GSI/server if it was launched independent of GLI 
     303    if(llssite_cfg_file.isIndependentGSI()) { 
     304        JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.Reconfigure"), Dictionary.get("General.Reconfigure"), JOptionPane.INFORMATION_MESSAGE); 
     305        return; 
     306    } 
     307 
     308    // someone may have closed the web server 
     309    if(checkServerRunning()) { // need a server to be running in order for us to send a reconfigure message to it 
     310        return; // it has reconfigured and restarted the server 
     311    } 
     312 
     313    // If it was already running, tell the server a reconfigure is required on next restart 
     314    // and then restart 
     315    if(running && sendMessageToServer("RECONFIGURE\n")) { 
     316        // restart 
     317        if(running) { 
     318        running = false; 
     319 
     320        if(sendMessageToServer("RESTART\n")) { //if(sendMessageToServer("RECONFIGURE\n")) { 
     321            // wait for the server to update the URL in the gli config file 
     322            llssite_cfg_file.setLastModified(); 
     323             
     324            int attempt_count = 0; 
     325            while (!llssite_cfg_file.isModified()) { 
     326            new OneSecondWait();  // Wait one second (give or take) 
     327            attempt_count++; 
     328             
     329            // After waiting for the specified time, ask the user whether they want to wait for that long again 
     330            if (attempt_count == WAITING_TIME) { 
     331                break; // can't be waiting forever, we'll be waiting again below 
     332            } 
     333            }            
     334 
     335            try {        
     336            testServerRunning(); // will set running = true when the server is up and running successfully 
     337            } catch (IOException bad_url_connection) { 
     338            try { 
     339                // If this fails then we try changing the url to be localhost 
     340                Configuration.library_url = new URL(llssite_cfg_file.getLocalHostURL()); 
     341                DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'"); 
     342                URLConnection connection = Configuration.library_url.openConnection(); 
     343                connection.getContent(); 
     344                running = true; 
     345                 
     346            } catch (IOException worse_url_connection) { 
     347                DebugStream.println("Can't connect to server on either address."); 
     348                Configuration.library_url = null; 
     349                running = false; 
     350            } 
     351            } 
     352        }  
     353        } 
     354    } else { 
     355        System.err.println("GLI was unable to send a reconfigure request to the local library server." 
     356               + "\nPlease reconfigure and restart the local Greenstone server manually."); 
     357    } 
     358    } 
     359 
    288360 
    289361    static public void stop() 
     
    303375        config(QUIT_COMMAND); 
    304376    } else { 
    305         if(sendMessageToServer("QUIT")) { 
     377        boolean success = sendMessageToServer("QUIT\n"); 
     378        try { 
     379        if(clientSocketWriter != null) { 
     380            clientSocketWriter.close(); 
     381            clientSocketWriter = null; 
     382        } 
     383        clientSocket = null; 
     384        } catch(Exception e) { 
     385        System.err.println("An exception occurred when trying to close the socket" 
     386                   + "\nto the LocalLibraryServer.\n" + e); 
     387        } 
     388        if(success) {        
    306389        Gatherer.terminateApplication(ID); 
    307390        } else { 
     
    446529    } 
    447530 
    448     static public void checkServerRunning() { 
    449     if (!running) return; // don't worry about it if it's not supposed to be running  
     531    /** @returns true if it had to restart the server. Returns false if it didn't restart it. */ 
     532    static public boolean checkServerRunning() { 
     533    boolean serverRestarted = false; 
     534 
     535    if (!running) return false; // don't worry about it if it's not supposed to be running  
    450536    llssite_cfg_file.load(true); // don't force reload, load only if modified 
    451537     
     
    454540        if(url.equals(LLSSiteConfig.URL_PENDING)) { 
    455541        running = false; 
    456         return; 
     542        return false; 
    457543        } 
    458544 
     
    480566            running = true; 
    481567        } 
    482         return; // don't need to launch the server, one has been independently launched 
     568        return false; // don't need to launch the server, one has been independently launched 
    483569        } else { 
    484570        // else we try using the glisite configfile 
    485571        llssite_cfg_file.useGLISiteCfg(local_library_server_file); 
    486572            //llssite_cfg_file.set(); 
     573         
     574        // since we're going to restart the server, make sure to reset all 
     575        // the client socket and its writer (for communicating with the web server)  
     576        // will only be reinstantiated if they are first nulled 
     577        if(clientSocket != null) { 
     578            clientSocket = null; 
     579        } 
     580        if(clientSocketWriter != null) { 
     581            try{  
     582            clientSocketWriter.close(); 
     583            clientSocketWriter = null; 
     584            } catch(Exception e) { 
     585            System.err.println("Unable to close the client socket outputstream."); 
     586            } finally { 
     587            clientSocketWriter = null; 
     588            } 
     589        } 
    487590         
    488591        // Spawn local library server process 
     
    496599        try { 
    497600            testServerRunning(); // don't return until the webserver is up and running 
     601            serverRestarted = true; 
    498602        } catch (IOException bad_url_connection) { 
    499603            DebugStream.println("Can't connect to server on address " + Configuration.library_url); 
     
    502606        } 
    503607    } 
     608    return serverRestarted; 
    504609    } 
    505610 
     
    645750        url.append((String)get(PORTNUMBER)); 
    646751        String enterlib = (String)get(ENTERLIB); 
    647         if(enterlib == null || enterlib.length() == 0) { 
     752        if(!isPersistentServer || enterlib == null || enterlib.length() == 0) { 
    648753        // Use the default /gsdl and hope for the best. 
    649754        url.append(SEPARATOR); 
     
    702807        debug("Found Local Library Address: " + url); 
    703808        return url; 
     809    } 
     810 
     811    public void setLastModified() { 
     812        if(isModified()) { 
     813        lastModified = configFile.lastModified(); 
     814        }  
    704815    } 
    705816