- Timestamp:
- 2009-04-15T14:53:44+12:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
greenstone3/trunk/src/java/org/greenstone/server/Server2.java
r18868 r18969 2 2 package org.greenstone.server; 3 3 4 import java.io.BufferedReader; 4 5 import java.io.File; 5 6 import java.io.FileInputStream; 6 7 import java.io.FileOutputStream; 8 import java.io.InputStreamReader; 9 import java.io.IOException; 10 import java.net.ServerSocket; 11 import java.net.Socket; 12 import java.net.URL; 13 //import java.net.URLConnection; 7 14 import java.util.Properties; 8 15 import java.util.ArrayList; … … 17 24 protected String libraryURL; 18 25 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) 20 76 { 21 77 super(gsdl2_home, lang, configfile, "etc"+File.separator+"logs-gsi"); 22 78 // configfile can be either glisite.cfg or llssite.cfg 23 79 24 25 80 Property = new Server2Property(); 26 81 27 82 String frame_title = dictionary.get("ServerControl.Frame_Title"); 28 83 server_control_ = new Server2Control(this,frame_title); 29 84 30 /* *Make command tagets for managing Web server */85 /* Make command tagets for managing Web server */ 31 86 START_CMD = "web-start"; 32 87 RESTART_CMD = "web-restart"; … … 34 89 STOP_CMD = "web-stop"; 35 90 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 36 107 autoStart(); 37 108 } … … 98 169 // default values, to be replaced with what's in gsdlsite.cfg 99 170 String host = "localhost"; 100 String port = "80 80";171 String port = "80"; 101 172 String gwcgi; 102 173 String httpprefix = "greenstone"; … … 123 194 recordError("Could not open gsdlsite_cfg for reading, using default library prefix."); 124 195 } 125 reloadConfigProperties();196 //reloadConfigProperties(); 126 197 port = config_properties.getProperty("portnumber", port); 127 128 198 } catch(Exception e) { 129 199 recordError("Exception trying to load properties from gsdlsite_cfg. Using default library prefix.", e); 130 200 suffix = httpprefix + suffix; 131 201 } 132 202 133 203 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 142 238 143 239 // About to stop the webserver … … 153 249 } 154 250 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 155 304 public static void main (String[] args) 156 305 { 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]"); 159 309 System.exit(1); 160 310 } … … 170 320 171 321 // 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"; 173 323 int equalSign = configfile.indexOf('='); 174 324 if(equalSign != -1) { … … 176 326 } 177 327 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); 179 343 } 180 344 }
Note:
See TracChangeset
for help on using the changeset viewer.