Ignore:
Timestamp:
2011-06-28T17:07:42+12:00 (13 years ago)
Author:
ak19
Message:

Part 1 of 2 commits for ticket 766 (the other commits will be in GS2): the GSI settings dialog now provides the ability to freeze the port number.

File:
1 edited

Legend:

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

    r23251 r24207  
    2626import org.greenstone.server.BaseServer;
    2727import org.greenstone.server.BaseProperty;
     28
    2829
    2930public class Server2 extends BaseServer
     
    287288        suffix = httpprefix + suffix;
    288289    }
    289    
     290
    290291    libraryURL = "http://" + host + ":" + port + suffix;
    291292    }
     
    297298    // first choice is port 80, second choice starts at 8282
    298299    String port = config_properties.getProperty("portnumber", "80");
     300    String keepport = config_properties.getProperty("keepport", "0"); // default is to not try to keep the same port
     301
    299302    int portDefault = 8282;
    300303    try {
     
    302305        boolean verbose = true;
    303306        if(!PortFinder.isPortAvailable(portNum, verbose)) { // first time, print any Port Unavailable messages
    304        
    305         PortFinder portFinder = new PortFinder(portDefault, 101);
    306         // Search for a free port silently from now on--don't want more
    307         // messages saying that a port could not be found...
    308         portNum = portFinder.findPortInRange(!verbose);
    309        
    310         if (portNum == -1) {
    311             // If we've still not found a free port, do we try the default port again?
    312             System.err.println("No free port found. Going to try on " + portDefault + " anyway.");
    313             port = Integer.toString(portDefault);
     307        if(keepport.equals("1")) {
     308            String errorMsg = "Unable to run the Greenstone server on port " + port + ". It may already be in use.";
     309            System.err.println("\n******************");
     310            logger_.error(errorMsg);
     311            System.err.println("If you wish to try another port, go to File > Settings of the Greenstone Server interface and either change the port number or untick the \"Do Not Modify Port\" option there. Then press the \"Enter Library\" button.");
     312            System.err.println("******************\n");
    314313        } else {
    315             port = Integer.toString(portNum);
    316         }
    317         config_properties.setProperty("portnumber", port); // store the correct port
    318 
    319         // write this updated port to the config file, since the configure target uses the file to run
    320         ScriptReadWrite scriptReadWrite = new ScriptReadWrite();
    321         ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file);
    322         scriptReadWrite.replaceOrAddLine(fileLines, "portnumber", port, false); // write the correct port
    323         scriptReadWrite.writeOutFile(config_properties_file, fileLines);
    324 
    325         configure_required_ = true;
    326         System.err.println("Running server on port " + port + ".");
     314
     315            PortFinder portFinder = new PortFinder(portDefault, 101);
     316            // Search for a free port silently from now on--don't want more
     317            // messages saying that a port could not be found...
     318            portNum = portFinder.findPortInRange(!verbose);
     319           
     320            if (portNum == -1) {
     321            // If we've still not found a free port, do we try the default port again?
     322            System.err.println("No free port found. Going to try on " + portDefault + " anyway.");
     323            port = Integer.toString(portDefault);
     324            } else {
     325            port = Integer.toString(portNum);
     326            }
     327            config_properties.setProperty("portnumber", port); // store the correct port
     328           
     329            // write this updated port to the config file, since the configure target uses the file to run
     330            ScriptReadWrite scriptReadWrite = new ScriptReadWrite();
     331            ArrayList fileLines = scriptReadWrite.readInFile(BaseServer.config_properties_file);
     332            scriptReadWrite.replaceOrAddLine(fileLines, "portnumber", port, false); // write the correct port
     333            scriptReadWrite.writeOutFile(config_properties_file, fileLines);
     334           
     335            configure_required_ = true;
     336            System.err.println("Running server on port " + port + ".");
     337        }
    327338        }   
    328339    } catch (Exception e) {
Note: See TracChangeset for help on using the changeset viewer.