- Timestamp:
- 2009-04-15T18:27:33+12:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/trunk/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java
r18650 r18987 37 37 import org.greenstone.gatherer.Dictionary; 38 38 import org.greenstone.gatherer.Gatherer; 39 import org.greenstone.gatherer.util.PortFinder; 40 import org.greenstone.gatherer.util.Utility; 39 41 40 42 … … 49 51 50 52 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(); 51 65 52 66 static public void addCollection(String collection_name) 53 67 { 54 config(ADD_COMMAND + collection_name); 68 if (isPersistentServer) { 69 config(ADD_COMMAND + collection_name); 70 } 55 71 } 56 72 … … 59 75 static private void config(String command) 60 76 { 77 if (!isPersistentServer) { 78 return; 79 } 61 80 if (Configuration.library_url == null) { 62 81 System.err.println("Error: Trying to configure local library with null Configuration.library_url!"); … … 93 112 } 94 113 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 } 95 137 96 138 static public boolean isRunning() 97 139 { 98 140 if (!running) return false; 99 llssite_cfg_file.load( );141 llssite_cfg_file.load(true); 100 142 if (llssite_cfg_file.getURL() == null) return false; 101 143 return true; … … 105 147 static public void releaseCollection(String collection_name) 106 148 { 107 config(RELEASE_COMMAND + collection_name); 108 } 109 149 if (isPersistentServer) { 150 config(RELEASE_COMMAND + collection_name); 151 } 152 } 110 153 111 154 static public void start(String gsdl_path, String local_library_server_file_path) 112 155 { 113 // Check the local library server .exe fileexists156 // Check the local library server file (server.exe or gs2-server.sh) exists 114 157 local_library_server_file = new File(local_library_server_file_path); 158 115 159 if (!local_library_server_file.exists()) { 116 160 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); 119 164 if (!local_library_server_file.exists()) { 120 165 DebugStream.println("No local library at all."); … … 122 167 } 123 168 } 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); 124 173 125 174 // Check if the server is already running 126 llssite_cfg_file = new LLSSiteConfig(local_library_server_file);127 175 String url = llssite_cfg_file.getURL(); 128 176 if (url != null) { 129 177 // 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. 130 180 try { 131 181 Configuration.library_url = new URL(url); 132 182 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 } 133 188 return; 134 189 } … … 136 191 DebugStream.printStackTrace(exception); 137 192 } 138 } 193 } 139 194 140 195 // Configure the server for immediate entry … … 142 197 143 198 // 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); 149 226 int attempt_count = 0; 150 while (llssite_cfg_file.getURL() == null) {227 while (llssite_cfg_file.getURL() != null) { 151 228 new OneSecondWait(); // Wait one second (give or take) 152 llssite_cfg_file.load( );229 llssite_cfg_file.load(false); 153 230 attempt_count++; 154 231 … … 163 240 } 164 241 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 165 294 // Ta-da. Now the url should be available 166 295 try { … … 173 302 // A quick test involves opening a connection to get the home page for this collection 174 303 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 + "'"); 176 305 URLConnection connection = Configuration.library_url.openConnection(); 177 306 connection.getContent(); … … 184 313 URLConnection connection = Configuration.library_url.openConnection(); 185 314 connection.getContent(); 315 186 316 } 187 317 catch (IOException worse_url_connection) { … … 195 325 } 196 326 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 field208 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 minute216 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 236 327 static public void checkServerRunning() { 237 328 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 { 240 340 // need to restart the server again 241 341 llssite_cfg_file.set(); 242 342 243 343 // 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 249 351 static private class OneSecondWait 250 352 { … … 268 370 private String autoenter_initial; 269 371 private String start_browser_initial; 372 373 private long lastModified = 0; 270 374 271 375 static final private String AUTOENTER = "autoenter"; … … 274 378 static final private String FALSE = "0"; 275 379 static final private String GLISITE_CFG = "glisite.cfg"; 276 static final private String GSDL = "g sdl";380 static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone 277 381 static final private String LLSSITE_CFG = "llssite.cfg"; 278 382 static final private String LOCAL_HOST = "http://localhost"; … … 326 430 } 327 431 } 432 433 if(glisite_cfg.exists()) { 434 lastModified = glisite_cfg.lastModified(); 435 } 328 436 } 329 437 … … 360 468 // URL is made from url and portnumber 361 469 String url = (String) get(URL); 470 471 if(!Utility.isWindows()) { 472 return url; 473 } 474 362 475 if(url != null) { 363 476 StringBuffer temp = new StringBuffer(url); … … 383 496 } 384 497 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 386 510 if(glisite_cfg.exists()) { 387 511 debug("Load: " + glisite_cfg.getAbsolutePath());
Note:
See TracChangeset
for help on using the changeset viewer.