Changeset 18969

Show
Ignore:
Timestamp:
15.04.2009 14:53:44 (11 years ago)
Author:
ak19
Message:

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

Location:
greenstone3/trunk/src/java/org/greenstone/server
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/server/BaseServer.java

    r18868 r18969  
    6262    reloadConfigProperties(); 
    6363 
    64     dictionary = new Dictionary("server", lang, this.getClass().getClassLoader()); 
    65  
     64    dictionary = new Dictionary("server", lang, this.getClass().getClassLoader());   
    6665    } 
    6766 
     
    6968    { 
    7069    String auto_start = config_properties.getProperty(BaseServer.Property.AUTOSTART, "true"); 
    71  
    7270    if (auto_start.equals("true") || auto_start.equals("1")) { 
    7371        String start_browser = config_properties.getProperty(BaseServer.Property.START_BROWSER, "true"); 
     
    7876        else{ 
    7977        start(); 
    80         }   
     78        } 
     79         
     80        server_control_.setState(java.awt.Frame.ICONIFIED); // minimise the server interface window 
    8181    } else { 
    8282        reload(); // browser URL or other important properties may not yet be initialised 
     
    8686    } 
    8787 
    88     // package access method 
     88    // package access methods 
    8989    BaseServerControl getServerControl() {  
    9090    return server_control_;  
     
    103103    public abstract void reload(); // reload properties, since they may have changed 
    104104    protected void preStop() {} 
    105  
     105    protected void postStart() {} 
     106     
    106107    public void reconfigRequired() 
    107108    { 
     
    126127           recordError(CONFIGURE_CMD); 
    127128       } 
    128        reload(); 
     129       reload(); // work out the browserURL again 
    129130       configure_required_ = false; 
    130131    } 
     
    137138    if (state != RunTarget.SUCCESS){ 
    138139        recordError(START_CMD); 
    139             server_state_ = SERVER_START_FAILED;   
    140              
     140            server_state_ = SERVER_START_FAILED;             
    141141    }                
    142142    else{ 
    143143        recordSuccess(START_CMD);  
    144144            server_state_ = SERVER_STARTED;  
     145        postStart(); 
    145146        }  
    146147 
  • greenstone3/trunk/src/java/org/greenstone/server/BaseServerControl.java

    r18868 r18969  
    165165        } 
    166166        default:{ 
     167        enter_button.setText(BaseServer.dictionary.get("ServerControl.EnterLibrary")); 
    167168        enter_button.setEnabled(false); 
    168169        fMenu.setEnabled(false); 
     
    175176    info_label.setText(stringToHTML("<br>"+message));            
    176177    } 
    177      
     178 
    178179    private class MyAdapter extends WindowAdapter{    
    179180    public void windowClosing(WindowEvent env){ 
  • greenstone3/trunk/src/java/org/greenstone/server/BaseServerSettings.java

    r18868 r18969  
    6060    } 
    6161 
    62     String auto_start_str = server.config_properties.getProperty(BaseServer.Property.AUTOSTART); 
    63     if ((auto_start_str.trim()).equals("true")) { 
     62    String auto_start_str = server.config_properties.getProperty(BaseServer.Property.AUTOSTART).trim(); 
     63    if (auto_start_str.equals("true") || auto_start_str.equals("1")) { 
    6464        this.autoStart = true; 
    6565    } else { 
     
    272272        newFileLines = scriptReadWrite.queryReplace(oldFileLines, BaseServer.Property.WEB_PORT, portNum+""); 
    273273         
    274         String newAutoEnter = (new Boolean(autoEnter.isSelected())).toString(); 
    275         newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.AUTOSTART, newAutoEnter); 
    276          
    277274        // call the subclass' save() method to save custom elements 
    278275        save(scriptReadWrite, newFileLines); 
     
    287284                         newFileLines); 
    288285        server.reloadConfigProperties(); 
     286        server.reload(); // work out the URL again in case it has changed 
    289287        if (require_restart){ 
    290288            JOptionPane.showMessageDialog(null,server.dictionary.get("ServerSetting.SettingChanged"),"Info", JOptionPane.INFORMATION_MESSAGE); 
     289            if(autoStart) { 
     290            server.autoStart(); 
     291            server.getServerControl().updateControl(); 
     292            } 
    291293        } 
    292294        }  
  • greenstone3/trunk/src/java/org/greenstone/server/BrowserLauncher.java

    r18558 r18969  
    9494        String lower_name = prog_name.toLowerCase(); 
    9595        if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("firefox") != -1) {  
    96         logger.info("found mozilla or firefox, trying remote it"); 
     96        logger.info("found mozilla or firefox, trying to remotely launch it"); 
    9797                // mozilla and netscape, try using a remote command to get things in the same window 
    9898        String new_command = prog_name +" -raise -remote openURL("+url+",new-tab)"; 
  • greenstone3/trunk/src/java/org/greenstone/server/Server2.java

    r18868 r18969  
    22package org.greenstone.server; 
    33 
     4import java.io.BufferedReader; 
    45import java.io.File; 
    56import java.io.FileInputStream; 
    67import java.io.FileOutputStream; 
     8import java.io.InputStreamReader; 
     9import java.io.IOException; 
     10import java.net.ServerSocket; 
     11import java.net.Socket; 
     12import java.net.URL; 
     13//import java.net.URLConnection; 
    714import java.util.Properties; 
    815import java.util.ArrayList; 
     
    1724    protected String libraryURL; 
    1825 
    19     public Server2(String gsdl2_home, String lang, String configfile) 
     26    private class QuitListener extends Thread  
     27    { 
     28    int quitPort = -1; 
     29    ServerSocket serverSocket = null; 
     30 
     31    public QuitListener(int quitport) throws Exception { 
     32        ///Server2.this.recordSuccess("In QuitListener constructor"); 
     33        this.quitPort = quitport; 
     34        serverSocket = new ServerSocket(quitPort); 
     35    } 
     36 
     37    public void run() { 
     38        // Server2.this.recordSuccess("In QuitListener.run()"); 
     39 
     40        Socket connection = null; 
     41 
     42        try { 
     43        // wait for a connection 
     44        connection = serverSocket.accept(); 
     45 
     46        ///Server2.this.recordSuccess("In QuitListener - accepted connection"); 
     47 
     48        // read input 
     49        try { 
     50            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     51            String line = null; 
     52            if((line = reader.readLine()) != null) { 
     53            // Server2.this.recordSuccess("In QuitListener - Got a line"); 
     54            if(line.equals("QUIT")) { 
     55                // Server2.this.recordSuccess("In QuitListener - line is QUIT"); 
     56                reader.close(); 
     57                reader = null; 
     58                serverSocket.close(); 
     59                serverSocket = null; 
     60            } 
     61            } 
     62        } catch(Exception e) { 
     63            Server2.this.recordError("Exception in QuitListener thread."); 
     64        } finally { 
     65            Server2.this.stop(); 
     66            System.exit(0); 
     67        } 
     68        } catch(IOException ioe) { 
     69        Server2.this.recordError("Server2.QuitListener: Unable to make the connection with the client socket." + ioe); 
     70        } 
     71    } 
     72    } 
     73 
     74 
     75    public Server2(String gsdl2_home, String lang, String configfile, int quitPort) 
    2076    { 
    2177    super(gsdl2_home, lang, configfile, "etc"+File.separator+"logs-gsi");    
    2278                   // configfile can be either glisite.cfg or llssite.cfg 
    2379 
    24  
    2580    Property = new Server2Property(); 
    26  
     81     
    2782    String frame_title = dictionary.get("ServerControl.Frame_Title"); 
    2883    server_control_ = new Server2Control(this,frame_title); 
    2984 
    30     /** Make command tagets for managing Web server */ 
     85    /* Make command tagets for managing Web server */ 
    3186    START_CMD     = "web-start"; 
    3287    RESTART_CMD   = "web-restart"; 
     
    3489    STOP_CMD      = "web-stop"; 
    3590 
     91    // now we can monitor for the quit command if applicable 
     92    // First check if given port is within the range of allowed ports 
     93    if(!PortFinder.isAssignablePortNumber(quitPort)) { 
     94        recordError("QuitPort provided is not within acceptable range: (" 
     95                   + PortFinder.PORTS_RESERVED + " - " + PortFinder.MAX_PORT + "]" ); 
     96        quitPort = -1; 
     97    } 
     98     
     99    if(quitPort != -1) { 
     100        try { 
     101        new QuitListener(quitPort).start(); 
     102        } catch(Exception e) { 
     103        Server2.this.recordError("Exception constructing the QuitListener thread."); 
     104        } 
     105    } 
     106     
    36107    autoStart(); 
    37108    } 
     
    98169    // default values, to be replaced with what's in gsdlsite.cfg 
    99170    String host = "localhost"; 
    100     String port = "8080"; 
     171    String port = "80"; 
    101172    String gwcgi; 
    102173    String httpprefix = "greenstone"; 
     
    123194        recordError("Could not open gsdlsite_cfg for reading, using default library prefix."); 
    124195        } 
    125         reloadConfigProperties(); 
     196        //reloadConfigProperties(); 
    126197        port = config_properties.getProperty("portnumber", port); 
    127  
    128198    } catch(Exception e) { 
    129199        recordError("Exception trying to load properties from gsdlsite_cfg. Using default library prefix.", e); 
    130200        suffix = httpprefix + suffix; 
    131201    } 
    132  
     202     
    133203    libraryURL = "http://" + host + ":" + port + suffix; 
    134      
    135     // the URL has been changed 
    136     // write the URL to the file since this should work like GS2's Local Lib Server for Windows 
    137     ScriptReadWrite scriptReadWrite = new ScriptReadWrite(); 
    138     ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file); 
    139     scriptReadWrite.replaceOrAddLine(fileLines, "url", libraryURL, true); 
    140     scriptReadWrite.writeOutFile(config_properties_file, fileLines); 
    141     } 
     204    } 
     205 
     206    public void reloadConfigProperties() { 
     207    super.reloadConfigProperties(); 
     208 
     209    // make sure the port is okay, otherwise find another port 
     210    // first choice is port 80, second choice starts at 8282 
     211    String port = config_properties.getProperty("portnumber", "80"); 
     212    int portDefault = 8282; 
     213    try { 
     214        int portNum = Integer.parseInt(port); 
     215         
     216        if(!PortFinder.isPortAvailable(portNum)) { 
     217         
     218        PortFinder portFinder = new PortFinder(portDefault, 101); 
     219        portNum = portFinder.findPortInRange(); 
     220        port = (portNum == -1) ? Integer.toString(portDefault) : Integer.toString(portNum); 
     221        config_properties.setProperty("portnumber", port); // store the correct port 
     222 
     223        // write this updated port to the config file, since the configure target uses the file to run 
     224        ScriptReadWrite scriptReadWrite = new ScriptReadWrite(); 
     225        ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file); 
     226        scriptReadWrite.replaceOrAddLine(fileLines, "portnumber", port, false); // write the correct port 
     227        scriptReadWrite.writeOutFile(config_properties_file, fileLines); 
     228 
     229        configure_required_ = true; 
     230        }     
     231    } catch (Exception e) { 
     232        recordError("Exception in Server2.reload(): " + e.getMessage()); 
     233        port = Integer.toString(portDefault); 
     234    } 
     235     
     236    } 
     237 
    142238     
    143239    // About to stop the webserver 
     
    153249    } 
    154250 
     251    // Called when the URL has been changed (called after a reload() and starting the server). 
     252    // By the time we get here, reload() would already have been called and have set 
     253    // both the port and worked out libraryURL 
     254    // This method needs to write the URL to the configfile since things should work 
     255    // like GS2's Local Lib Server for Windows 
     256    protected void postStart() { 
     257 
     258    URL libURL = null; 
     259    try { 
     260        libURL = new URL(libraryURL); 
     261    } catch (Exception e) { 
     262        recordError("Unable to convert library URL string into a valid URL, Server2.java." + e); 
     263    } 
     264     
     265    // 1. Test that the server is running at the libraryURL before writing it out to glisite.cfg/configfile  
     266    // A quick test involves opening a connection to get the home page for this collection 
     267    if(libURL != null) { 
     268 
     269        boolean ready = false;  
     270        for(int i = 0; i < 60 && !ready; i++) { 
     271        try { 
     272            libURL.openConnection(); 
     273            //URLConnection connection = new URL(libraryURL).openConnection(); 
     274            //connection.getContent(); 
     275            ready = true; 
     276            recordSuccess("Try connecting to server on url: '" + libraryURL + "'"); 
     277        } catch (IOException bad_url_connection) { 
     278            // keep looping 
     279            recordSuccess("NOT YET CONNECTED. Waiting to try again..."); 
     280            try { 
     281            Thread.sleep(1000); 
     282            } catch (InterruptedException ie) { 
     283            ready = true; 
     284            recordError("Unexpected: got an InterruptedException in sleeping thread, Server2.java." + ie); 
     285            } 
     286        } catch (Exception e) { 
     287            ready = true; 
     288            recordError("Got an Exception while waiting for the connection to become live, Server2.java." + e); 
     289        } 
     290        } 
     291    } 
     292 
     293    // 2. Now write the URL to the config file 
     294    String port = config_properties.getProperty("portnumber"); 
     295 
     296    ScriptReadWrite scriptReadWrite = new ScriptReadWrite(); 
     297    ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file); 
     298    scriptReadWrite.replaceOrAddLine(fileLines, "url", libraryURL, true); 
     299    scriptReadWrite.replaceOrAddLine(fileLines, "portnumber", port, false); // write the correct port 
     300    scriptReadWrite.writeOutFile(config_properties_file, fileLines); 
     301    } 
     302 
     303 
    155304    public static void main (String[] args) 
    156305    { 
    157         if ((args.length < 1) || (args.length > 3)) { 
    158         System.err.println("Usage: java org.greenstone.server.Server2 <gsdl2-home-dir> [lang] [--config=configfile]"); 
     306        if ((args.length < 1) || (args.length > 4)) { 
     307        System.err.println( 
     308           "Usage: java org.greenstone.server.Server2 <gsdl2-home-dir> [lang] [--config=configfile] [--quitport=portNum]"); 
    159309        System.exit(1); 
    160310    } 
     
    170320     
    171321    // if no config file is given, then the following defaults to llssite.cfg 
    172     String configfile = (args.length==3 && args[2].startsWith("--config=")) ? args[2] : gsdl2_home+File.separator+"llssite.cfg"; 
     322    String configfile = (args.length>=3 && args[2].startsWith("--config=")) ? args[2] : gsdl2_home+File.separator+"llssite.cfg"; 
    173323    int equalSign = configfile.indexOf('='); 
    174324    if(equalSign != -1) { 
     
    176326    } 
    177327 
    178     new Server2(gsdl2_home,lang,configfile); 
     328    String quitport = (args.length == 4 && args[3].startsWith("--quitport=")) ? args[3] : ""; 
     329    equalSign = quitport.indexOf('='); 
     330    int port = -1; 
     331    if(equalSign != -1) { 
     332        try { 
     333        quitport = quitport.substring(equalSign+1); 
     334        port = Integer.parseInt(quitport); 
     335        } catch(Exception e) { // parse fails 
     336        System.err.println("Port must be numeric. Continuing without it."); 
     337        } 
     338    } 
     339 
     340    //System.err.println("Running server with config file: " + configfile); 
     341     
     342    new Server2(gsdl2_home, lang, configfile, port); 
    179343    } 
    180344} 
  • greenstone3/trunk/src/java/org/greenstone/server/Server2Settings.java

    r18868 r18969  
    3838    public String onSave()  
    3939    { 
    40     return ""; 
     40    String result = ""; 
     41    if (!server.getBrowserURL().equals(prefix_combobox.getSelectedItem())) { 
     42        result += "changed"; 
     43        result += "restart"; 
     44    } 
     45    return result; 
    4146    } 
    4247 
    4348    public void save(ScriptReadWrite scriptReadWrite, ArrayList newFileLines)  
    4449    { 
    45     String newAutoEnter = autoEnter.isSelected() ? "1" : "0"; 
    46     newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.AUTOSTART, newAutoEnter); 
     50    boolean auto_enter = autoEnter.isSelected(); 
     51    if(autoStart != auto_enter) { 
     52        String newAutoEnter = auto_enter ? "1" : "0"; 
     53        newFileLines = scriptReadWrite.queryReplace(newFileLines, BaseServer.Property.AUTOSTART, newAutoEnter); 
     54    } 
    4755    } 
    4856