[13592] | 1 | /**
|
---|
[22678] | 2 | /**
|
---|
[13592] | 3 | *############################################################################
|
---|
| 4 | * A component of the Greenstone Librarian Interface, part of the Greenstone
|
---|
| 5 | * digital library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
|
---|
| 9 | *
|
---|
| 10 | * Copyright (C) 2004 New Zealand Digital Library Project
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | * (at your option) any later version.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful,
|
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | * GNU General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, write to the Free Software
|
---|
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | *############################################################################
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | package org.greenstone.gatherer.greenstone;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | import java.io.*;
|
---|
| 32 | import java.lang.*;
|
---|
| 33 | import java.net.*;
|
---|
[13792] | 34 | import java.util.*;
|
---|
[13592] | 35 | import javax.swing.*;
|
---|
| 36 | import org.greenstone.gatherer.Configuration;
|
---|
| 37 | import org.greenstone.gatherer.DebugStream;
|
---|
| 38 | import org.greenstone.gatherer.Dictionary;
|
---|
| 39 | import org.greenstone.gatherer.Gatherer;
|
---|
[18987] | 40 | import org.greenstone.gatherer.util.PortFinder;
|
---|
| 41 | import org.greenstone.gatherer.util.Utility;
|
---|
[13592] | 42 |
|
---|
| 43 |
|
---|
| 44 | public class LocalLibraryServer
|
---|
| 45 | {
|
---|
[19484] | 46 | static final private int WAITING_TIME = 20; // number of seconds to wait for the server to start and stop
|
---|
[19243] | 47 |
|
---|
[13592] | 48 | static final private String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
|
---|
| 49 | static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
|
---|
| 50 | static final private String QUIT_COMMAND = "?a=config&cmd=kill";
|
---|
[22678] | 51 | static final private String RESTART_COMMAND = "?a=config&cmd=restart";
|
---|
[13592] | 52 |
|
---|
[18650] | 53 | static private LLSSiteConfig llssite_cfg_file = null;
|
---|
[13592] | 54 | static private File local_library_server_file = null;
|
---|
| 55 |
|
---|
| 56 | static private boolean running = false;
|
---|
[18987] | 57 | static private String ID = "greenstone-server"; // a sort of process ID
|
---|
[13592] | 58 |
|
---|
[18987] | 59 | // Need to use sockets to tell the server program to terminate when on Linux
|
---|
| 60 | // The socket port number that we will use to communicate the termination
|
---|
| 61 | static private int port;
|
---|
[22661] | 62 | static private Socket clientSocket = null;
|
---|
| 63 | static private Writer clientSocketWriter = null;
|
---|
[18987] | 64 |
|
---|
| 65 | // The server is persistent if it does not have to reload all the values
|
---|
| 66 | // over and over again each time. Tomcat is persistent and fastcgi is too,
|
---|
| 67 | // but the Apache webserver is not persistent by default.
|
---|
| 68 | // Change the initialisation of this value depending on whether fastcgi is
|
---|
[20480] | 69 | // on. At the moment, this does not apply to the Apache local library server.
|
---|
| 70 | static private boolean isPersistentServer = false;
|
---|
[18987] | 71 |
|
---|
[13592] | 72 | static public void addCollection(String collection_name)
|
---|
| 73 | {
|
---|
[18987] | 74 | if (isPersistentServer) {
|
---|
| 75 | config(ADD_COMMAND + collection_name);
|
---|
| 76 | }
|
---|
[13592] | 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | // Used to send messages to the local library
|
---|
| 81 | static private void config(String command)
|
---|
| 82 | {
|
---|
[18987] | 83 | if (!isPersistentServer) {
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
[13592] | 86 | if (Configuration.library_url == null) {
|
---|
| 87 | System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | try {
|
---|
| 92 | URL url = new URL(Configuration.library_url.toString() + command);
|
---|
| 93 | HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
|
---|
[13779] | 94 |
|
---|
| 95 | // It's very important that we read the output of the command
|
---|
| 96 | // This ensures that the command has actually finished
|
---|
| 97 | // (The response code is returned immediately)
|
---|
| 98 | InputStream library_is = library_connection.getInputStream();
|
---|
| 99 | BufferedReader library_in = new BufferedReader(new InputStreamReader(library_is, "UTF-8"));
|
---|
| 100 | String library_output_line = library_in.readLine();
|
---|
| 101 | while (library_output_line != null) {
|
---|
| 102 | DebugStream.println("Local library server output: " + library_output_line);
|
---|
| 103 | library_output_line = library_in.readLine();
|
---|
| 104 | }
|
---|
| 105 | library_in.close();
|
---|
| 106 |
|
---|
[13592] | 107 | int response_code = library_connection.getResponseCode();
|
---|
| 108 | if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
|
---|
| 109 | DebugStream.println("200 - Complete.");
|
---|
| 110 | }
|
---|
| 111 | else {
|
---|
| 112 | DebugStream.println("404 - Failed.");
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
[13779] | 115 | catch (Exception exception) {
|
---|
| 116 | DebugStream.printStackTrace(exception);
|
---|
[13592] | 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[20480] | 120 | // Used to send messages to the local library server wrapper to the Apache web server (server.jar)
|
---|
[18987] | 121 | static private boolean sendMessageToServer(String message) {
|
---|
[20480] | 122 | if(isPersistentServer) {
|
---|
[18987] | 123 | return false;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | if(port == -1) {
|
---|
| 127 | return false;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | try {
|
---|
[22661] | 131 | if(clientSocket == null) {
|
---|
| 132 | clientSocket = new Socket("localhost", port);
|
---|
| 133 | }
|
---|
| 134 | if(clientSocketWriter == null) {
|
---|
| 135 | clientSocketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
---|
| 136 | }
|
---|
| 137 | clientSocketWriter.write(message);
|
---|
| 138 | clientSocketWriter.flush();
|
---|
| 139 |
|
---|
[18987] | 140 | } catch (Exception e) {
|
---|
| 141 | System.err.println("An exception occurred when trying to send the message: " + message
|
---|
| 142 | + "\nto the LocalLibraryServer.\n" + e);
|
---|
| 143 | return false;
|
---|
| 144 | }
|
---|
| 145 | return true;
|
---|
| 146 | }
|
---|
[13592] | 147 |
|
---|
| 148 | static public boolean isRunning()
|
---|
| 149 | {
|
---|
[19544] | 150 | if (!running) return false; // if the url is pending, then running would also be false (server not started up yet)
|
---|
| 151 |
|
---|
[18987] | 152 | llssite_cfg_file.load(true);
|
---|
[19544] | 153 | String url = llssite_cfg_file.getURL();
|
---|
| 154 | if (url == null) return false;
|
---|
| 155 |
|
---|
| 156 | // Called by Gatherer to check whether we need to stop the server.
|
---|
| 157 | // if the url is pending, then the GSI hasn't started the server up yet
|
---|
| 158 | // Already covered in !running
|
---|
| 159 | //if (url.equals(LLSSiteConfig.URL_PENDING)) return false;
|
---|
| 160 |
|
---|
[13592] | 161 | return true;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[22771] | 164 | /** collection name can be a group-qualified collect name */
|
---|
[13592] | 165 | static public void releaseCollection(String collection_name)
|
---|
| 166 | {
|
---|
[18987] | 167 | if (isPersistentServer) {
|
---|
| 168 | config(RELEASE_COMMAND + collection_name);
|
---|
| 169 | }
|
---|
[13592] | 170 | }
|
---|
| 171 |
|
---|
[19758] | 172 | static public boolean start(String gsdl_path, String local_library_server_file_path)
|
---|
[13592] | 173 | {
|
---|
[18987] | 174 | // Check the local library server file (server.exe or gs2-server.sh) exists
|
---|
[19758] | 175 | if(local_library_server_file_path == null) {
|
---|
| 176 | return false;
|
---|
| 177 | }
|
---|
[20480] | 178 | local_library_server_file = new File(local_library_server_file_path);
|
---|
| 179 | if (local_library_server_file.exists()) {
|
---|
| 180 | if(local_library_server_file.getName().equals("server.exe")) {
|
---|
| 181 | isPersistentServer = true;
|
---|
[22608] | 182 | } // else it may be gs2-web-server.bat
|
---|
[20480] | 183 | }
|
---|
| 184 | else { // local_library_server_file does not exist
|
---|
[13592] | 185 | DebugStream.println("No local library at given file path.");
|
---|
[20480] | 186 |
|
---|
[22608] | 187 | if(Utility.isWindows()) { // test for server.exe and gs2-web-server.bat
|
---|
[20480] | 188 | local_library_server_file = new File(gsdl_path + "server.exe");
|
---|
| 189 | if (local_library_server_file.exists()) {
|
---|
| 190 | isPersistentServer = true;
|
---|
| 191 | } else {
|
---|
[22608] | 192 | local_library_server_file = new File(gsdl_path + "gs2-web-server.bat");
|
---|
[20480] | 193 | if (!local_library_server_file.exists()) {
|
---|
| 194 | DebugStream.println("No local library at all.");
|
---|
| 195 | return false;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | } else { // linux
|
---|
| 199 | local_library_server_file = new File(gsdl_path + "gs2-server.sh");
|
---|
| 200 | if (!local_library_server_file.exists()) {
|
---|
| 201 | DebugStream.println("No local library at all.");
|
---|
| 202 | return false;
|
---|
| 203 | }
|
---|
[13592] | 204 | }
|
---|
[19194] | 205 | }
|
---|
| 206 |
|
---|
[20480] | 207 | if(!isPersistentServer) {
|
---|
| 208 | // In the case of the Local Library Server on Linux or on Win where there's no server.exe, do an extra test:
|
---|
| 209 | // If GS2 was not configured with --enable-apache-httpd, then there is no apache webserver folder,
|
---|
| 210 | // even though the gs2-server.sh file would still be there. That means if the folder is absent
|
---|
| 211 | // we still have no local library server.
|
---|
| 212 |
|
---|
| 213 | File localServerFolder = new File(gsdl_path, "apache-httpd");
|
---|
| 214 | if (!localServerFolder.exists() && !localServerFolder.isDirectory()) {
|
---|
| 215 | DebugStream.println("The web server does not exist at "
|
---|
| 216 | + localServerFolder.getAbsolutePath() + "\nNo local library at all. Trying web library");
|
---|
| 217 | return false;
|
---|
| 218 | } // else apache-httpd folder exists
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[19758] | 221 | llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
|
---|
| 222 | if(!llssite_cfg_file.isConfigFileSet()) {
|
---|
| 223 | return false;
|
---|
[13592] | 224 | }
|
---|
[19194] | 225 |
|
---|
[19758] | 226 | // from now on return true: we're in local_library_mode (even if the server is not running)
|
---|
| 227 |
|
---|
| 228 |
|
---|
[19544] | 229 | // If the user launched the GSI independent of GLI, but user has not pressed
|
---|
| 230 | // Enter Library yet, then we will obtain the previewURL later.
|
---|
| 231 | if(LocalLibraryServer.isURLPending()) {
|
---|
| 232 | // running is still false when the URL is pending because only GSI is running, not the server
|
---|
[19758] | 233 | return true;
|
---|
[19544] | 234 | } else if(llssite_cfg_file.isIndependentGSI()) {
|
---|
| 235 | // there is a url, it's not pending, and it is llssite.cfg: meaning GSI has started up
|
---|
[19585] | 236 | running = true;
|
---|
[19758] | 237 | return true;
|
---|
[19544] | 238 | }
|
---|
| 239 |
|
---|
[18987] | 240 | // Spawn local library server process
|
---|
[21998] | 241 | final String QUOTES = Utility.isWindows() ? "\"" : ""; // need to embed path in quotes on Windows for spaces (interferes on Linux)
|
---|
| 242 | String local_library_server_command = QUOTES+local_library_server_file.getAbsolutePath()+QUOTES + getExtraLaunchArguments(llssite_cfg_file);
|
---|
[22608] | 243 | if(Utility.isWindows() && !isPersistentServer) { // launching gs2-web-server.bat (Windows) needs cmd start
|
---|
[20480] | 244 | local_library_server_command = "cmd.exe /c start \"GSI\" " + local_library_server_command;
|
---|
| 245 | }
|
---|
[13592] | 246 |
|
---|
| 247 | // Check if the server is already running
|
---|
[18650] | 248 | String url = llssite_cfg_file.getURL();
|
---|
[13592] | 249 | if (url != null) {
|
---|
| 250 | // If it is already running then set the Greenstone web server address and we're done
|
---|
[18987] | 251 | // E.g. if previously GLI was not properly shut down, the URL property (signifying
|
---|
| 252 | // the server is still running) would still be in the glisite_cfg file.
|
---|
[13592] | 253 | try {
|
---|
| 254 | Configuration.library_url = new URL(url);
|
---|
| 255 | running = true;
|
---|
[18987] | 256 |
|
---|
[19004] | 257 | // Run the server interface
|
---|
| 258 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
[19758] | 259 | return true;
|
---|
[13592] | 260 | }
|
---|
| 261 | catch (MalformedURLException exception) {
|
---|
| 262 | DebugStream.printStackTrace(exception);
|
---|
| 263 | }
|
---|
[18987] | 264 | }
|
---|
[13592] | 265 |
|
---|
| 266 | // Configure the server for immediate entry
|
---|
[18650] | 267 | //llssite_cfg_file.set();
|
---|
[13592] | 268 |
|
---|
| 269 | // Spawn local library server process
|
---|
[18987] | 270 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
| 271 |
|
---|
| 272 | // Wait until program has started
|
---|
[19003] | 273 | try {
|
---|
[20480] | 274 | //System.err.println("**** testing server...");
|
---|
| 275 | testServerRunning(); // will set running = true when the server is up and running successfully
|
---|
| 276 | //System.err.println("**** Is server running: " + running);
|
---|
[19003] | 277 | } catch (IOException bad_url_connection) {
|
---|
| 278 | try {
|
---|
| 279 | // If this fails then we try changing the url to be localhost
|
---|
| 280 | Configuration.library_url = new URL(llssite_cfg_file.getLocalHostURL());
|
---|
| 281 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
| 282 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
| 283 | connection.getContent();
|
---|
| 284 | running = true;
|
---|
| 285 |
|
---|
| 286 | } catch (IOException worse_url_connection) {
|
---|
| 287 | DebugStream.println("Can't connect to server on either address.");
|
---|
| 288 | Configuration.library_url = null;
|
---|
| 289 | running = false;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
[19758] | 292 |
|
---|
| 293 | return true;
|
---|
[18987] | 294 | }
|
---|
[13592] | 295 |
|
---|
[22661] | 296 | /** Call this when the collect directory has changed. Only works when using
|
---|
| 297 | * the web server launched by GLI. */
|
---|
| 298 | static public void reconfigure() {
|
---|
[22678] | 299 | if(isPersistentServer) {
|
---|
| 300 | // 1st: server may have nulled URL if server was stopped inbetween,
|
---|
| 301 | // so load that new URL, if GLI conf file modified
|
---|
| 302 | // Then put the new collectDir into the GLI configfile being used
|
---|
| 303 | llssite_cfg_file.load(true);
|
---|
| 304 |
|
---|
| 305 | String collectDir = Gatherer.getCollectDirectoryPath();
|
---|
| 306 | collectDir = collectDir.substring(0, collectDir.length()-1); // remove file separator at end
|
---|
| 307 | llssite_cfg_file.put(LLSSiteConfig.COLLECTHOME, collectDir);
|
---|
| 308 | llssite_cfg_file.save();
|
---|
| 309 |
|
---|
| 310 | // tell the server to restart, so it will read the new configuration
|
---|
| 311 |
|
---|
| 312 | // (1) need a server to be running in order for us to send a restart message to it
|
---|
| 313 | if(checkServerRunning()) { // if true, it wasn't running before, but has now restarted the server. We're done
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 | // (2) otherwise the server may already have been running, need to tell it to restart after setting the collect dir
|
---|
| 317 | if(running) {
|
---|
| 318 | config(RESTART_COMMAND);
|
---|
| 319 | }
|
---|
[22661] | 320 | return;
|
---|
| 321 | }
|
---|
[18987] | 322 |
|
---|
[22661] | 323 | // can't control the GSI/server if it was launched independent of GLI
|
---|
| 324 | if(llssite_cfg_file.isIndependentGSI()) {
|
---|
| 325 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.Reconfigure"), Dictionary.get("General.Reconfigure"), JOptionPane.INFORMATION_MESSAGE);
|
---|
| 326 | return;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | // someone may have closed the web server
|
---|
| 330 | if(checkServerRunning()) { // need a server to be running in order for us to send a reconfigure message to it
|
---|
| 331 | return; // it has reconfigured and restarted the server
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | // If it was already running, tell the server a reconfigure is required on next restart
|
---|
| 335 | // and then restart
|
---|
| 336 | if(running && sendMessageToServer("RECONFIGURE\n")) {
|
---|
| 337 | // restart
|
---|
| 338 | if(running) {
|
---|
| 339 | running = false;
|
---|
| 340 |
|
---|
| 341 | if(sendMessageToServer("RESTART\n")) { //if(sendMessageToServer("RECONFIGURE\n")) {
|
---|
| 342 | // wait for the server to update the URL in the gli config file
|
---|
| 343 | llssite_cfg_file.setLastModified();
|
---|
| 344 |
|
---|
| 345 | int attempt_count = 0;
|
---|
| 346 | while (!llssite_cfg_file.isModified()) {
|
---|
| 347 | new OneSecondWait(); // Wait one second (give or take)
|
---|
| 348 | attempt_count++;
|
---|
| 349 |
|
---|
| 350 | // After waiting for the specified time, ask the user whether they want to wait for that long again
|
---|
| 351 | if (attempt_count == WAITING_TIME) {
|
---|
| 352 | break; // can't be waiting forever, we'll be waiting again below
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | try {
|
---|
| 357 | testServerRunning(); // will set running = true when the server is up and running successfully
|
---|
| 358 | } catch (IOException bad_url_connection) {
|
---|
| 359 | try {
|
---|
| 360 | // If this fails then we try changing the url to be localhost
|
---|
| 361 | Configuration.library_url = new URL(llssite_cfg_file.getLocalHostURL());
|
---|
| 362 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
| 363 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
| 364 | connection.getContent();
|
---|
| 365 | running = true;
|
---|
| 366 |
|
---|
| 367 | } catch (IOException worse_url_connection) {
|
---|
| 368 | DebugStream.println("Can't connect to server on either address.");
|
---|
| 369 | Configuration.library_url = null;
|
---|
| 370 | running = false;
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 | } else {
|
---|
| 376 | System.err.println("GLI was unable to send a reconfigure request to the local library server."
|
---|
| 377 | + "\nPlease reconfigure and restart the local Greenstone server manually.");
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 |
|
---|
[18987] | 382 | static public void stop()
|
---|
| 383 | {
|
---|
[19544] | 384 | if (!running) {
|
---|
| 385 | // also the case if the URL is pending in an independently launched GSI
|
---|
[18987] | 386 | return;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[19544] | 389 | // don't (can't) shutdown the GSI/server if it was launched independent of GLI
|
---|
| 390 | if(llssite_cfg_file.isIndependentGSI()) {
|
---|
| 391 | return;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[18987] | 394 | // Send the command for it to exit.
|
---|
| 395 | if (isPersistentServer) {
|
---|
| 396 | config(QUIT_COMMAND);
|
---|
| 397 | } else {
|
---|
[22661] | 398 | boolean success = sendMessageToServer("QUIT\n");
|
---|
| 399 | try {
|
---|
| 400 | if(clientSocketWriter != null) {
|
---|
| 401 | clientSocketWriter.close();
|
---|
| 402 | clientSocketWriter = null;
|
---|
| 403 | }
|
---|
| 404 | clientSocket = null;
|
---|
| 405 | } catch(Exception e) {
|
---|
| 406 | System.err.println("An exception occurred when trying to close the socket"
|
---|
| 407 | + "\nto the LocalLibraryServer.\n" + e);
|
---|
| 408 | }
|
---|
| 409 | if(success) {
|
---|
[18987] | 410 | Gatherer.terminateApplication(ID);
|
---|
| 411 | } else {
|
---|
| 412 | System.err.println("Unable to stop the server, since there's no communication port to send the quit msg over."
|
---|
| 413 | + "\nPlease stop the local Greenstone server manually.");
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | // Wait until program has stopped, by reloading and checking the URL field
|
---|
| 418 | llssite_cfg_file.load(false);
|
---|
| 419 | int attempt_count = 0;
|
---|
[19544] | 420 | String url = llssite_cfg_file.getURL();
|
---|
| 421 | while (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) { // if pending, the server is already stopped (not running)
|
---|
[18987] | 422 | new OneSecondWait(); // Wait one second (give or take)
|
---|
| 423 | llssite_cfg_file.load(false);
|
---|
| 424 | attempt_count++;
|
---|
| 425 |
|
---|
[19243] | 426 | // After waiting for the specified time, ask the user whether they want to wait for that long again
|
---|
| 427 | if (attempt_count == WAITING_TIME) {
|
---|
| 428 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut", Integer.toString(WAITING_TIME)),
|
---|
| 429 | Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
[18987] | 430 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
| 431 | return;
|
---|
| 432 | }
|
---|
| 433 | attempt_count = 0;
|
---|
| 434 | }
|
---|
[19544] | 435 | // read the url again to see if it's updated
|
---|
| 436 | url = llssite_cfg_file.getURL();
|
---|
[18987] | 437 | }
|
---|
| 438 |
|
---|
| 439 | // Restore the llssite_cfg.
|
---|
| 440 | llssite_cfg_file.restore();
|
---|
| 441 |
|
---|
| 442 | // If the local server is still running then our changed values will get overwritten.
|
---|
[19544] | 443 | url = llssite_cfg_file.getURL();
|
---|
| 444 | if (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
[18987] | 445 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | running = false;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | static private String getExtraLaunchArguments(LLSSiteConfig site_cfg_file) {
|
---|
| 452 | String args = " " + site_cfg_file.getSiteConfigFilename();
|
---|
| 453 |
|
---|
[20480] | 454 | if(isPersistentServer) {
|
---|
[18987] | 455 | return args;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[20480] | 458 | // Else, when running the Local Apache Library Server on Linux/Win, need to provide a port argument
|
---|
[18987] | 459 | try {
|
---|
| 460 | PortFinder portFinder = new PortFinder(50100, 100);
|
---|
[19442] | 461 | port = portFinder.findPortInRange(false); // silent mode
|
---|
[18987] | 462 | } catch(Exception e) {
|
---|
| 463 | System.err.println("Exception when trying to find an available port: " + e);
|
---|
| 464 | port = -1;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | return args + " --quitport=" + port;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 |
|
---|
| 471 | // This method first tests whether there is a URL in the llssite_cfg_file
|
---|
| 472 | // and after that appears, it tests whether the URL is functional.
|
---|
[19003] | 473 | static private void testServerRunning() throws IOException {
|
---|
[13592] | 474 | // Wait until program has started, by reloading and checking the URL field
|
---|
[18987] | 475 | llssite_cfg_file.load(false);
|
---|
[13592] | 476 | int attempt_count = 0;
|
---|
[18650] | 477 | while (llssite_cfg_file.getURL() == null) {
|
---|
[13592] | 478 | new OneSecondWait(); // Wait one second (give or take)
|
---|
[20480] | 479 | //System.err.println("**** One Second Wait");
|
---|
[18987] | 480 | llssite_cfg_file.load(false);
|
---|
[13592] | 481 | attempt_count++;
|
---|
| 482 |
|
---|
[19243] | 483 | // After waiting for the specified time, ask the user whether they want to wait for that long again
|
---|
| 484 | if (attempt_count == WAITING_TIME) {
|
---|
| 485 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.StartUpTimeOut", Integer.toString(WAITING_TIME)),
|
---|
| 486 | Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
[13592] | 487 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
| 488 | return;
|
---|
| 489 | }
|
---|
| 490 | attempt_count = 0;
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
[18987] | 493 |
|
---|
[13592] | 494 | // Ta-da. Now the url should be available
|
---|
| 495 | try {
|
---|
[18650] | 496 | Configuration.library_url = new URL(llssite_cfg_file.getURL());
|
---|
[13592] | 497 | }
|
---|
| 498 | catch (MalformedURLException exception) {
|
---|
| 499 | DebugStream.printStackTrace(exception);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | // A quick test involves opening a connection to get the home page for this collection
|
---|
| 503 | try {
|
---|
[18987] | 504 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url + "'");
|
---|
[13592] | 505 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
| 506 | connection.getContent();
|
---|
[19003] | 507 | running = true;
|
---|
[13592] | 508 | }
|
---|
| 509 | catch (IOException bad_url_connection) {
|
---|
[19003] | 510 | throw bad_url_connection;
|
---|
[13592] | 511 | }
|
---|
| 512 |
|
---|
[19003] | 513 |
|
---|
[13592] | 514 | }
|
---|
| 515 |
|
---|
[19544] | 516 | /** Returns true if we're waiting on the user to click on Enter Library button
|
---|
| 517 | * in an independently launched GSI (one not launched by GLI). */
|
---|
| 518 | static public boolean isURLPending() {
|
---|
[20480] | 519 | //if(Configuration.library_url != null) {
|
---|
| 520 | //System.err.println("**** Configuration.library_url: " + Configuration.library_url );
|
---|
| 521 | //return false; // don't need to do anything as we already have the url
|
---|
| 522 | //}
|
---|
[19544] | 523 |
|
---|
[18987] | 524 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
[13592] | 525 |
|
---|
[18987] | 526 | String url = llssite_cfg_file.getURL();
|
---|
[19544] | 527 |
|
---|
[18987] | 528 | if(url != null) {
|
---|
[19544] | 529 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
| 530 | running = false; // imagine if they restarted an external GSI
|
---|
| 531 | return true;
|
---|
| 532 | } else {
|
---|
| 533 | // a valid URL at last
|
---|
| 534 | try {
|
---|
| 535 | Configuration.library_url = new URL(url);
|
---|
| 536 | running = true;
|
---|
| 537 | }
|
---|
| 538 | catch (MalformedURLException exception) {
|
---|
[19585] | 539 | running = false;
|
---|
[19544] | 540 | exception.printStackTrace();
|
---|
[19585] | 541 | DebugStream.printStackTrace(exception);
|
---|
[19544] | 542 | }
|
---|
| 543 | return false;
|
---|
| 544 | }
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | // If either the URL is null--which means no independent GSI (Greenstone server interface
|
---|
| 548 | // app) was launched--or if the 'URL' doesn't say it's pending, then we're not waiting
|
---|
| 549 | return false;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[22661] | 552 | /** @returns true if it had to restart the server. Returns false if it didn't restart it. */
|
---|
| 553 | static public boolean checkServerRunning() {
|
---|
| 554 | boolean serverRestarted = false;
|
---|
| 555 |
|
---|
| 556 | if (!running) return false; // don't worry about it if it's not supposed to be running
|
---|
[19544] | 557 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
| 558 |
|
---|
| 559 | String url = llssite_cfg_file.getURL();
|
---|
| 560 | if(url != null) {
|
---|
[19585] | 561 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
[19547] | 562 | running = false;
|
---|
[22661] | 563 | return false;
|
---|
[19547] | 564 | }
|
---|
| 565 |
|
---|
| 566 | // else, valid URL:
|
---|
[18987] | 567 | try {
|
---|
| 568 | Configuration.library_url = new URL(url);
|
---|
[19585] | 569 | running = true;
|
---|
[13592] | 570 | }
|
---|
[18987] | 571 | catch (MalformedURLException exception) {
|
---|
[19585] | 572 | running = false;
|
---|
[18987] | 573 | DebugStream.printStackTrace(exception);
|
---|
[19544] | 574 | exception.printStackTrace();
|
---|
[18987] | 575 | }
|
---|
[19585] | 576 | }
|
---|
[19547] | 577 | else { // NO URL in current ConfigFile, check the other configfile for a URL
|
---|
| 578 | // to see if the server was restarted using that file.
|
---|
| 579 | // Otherwise need to restart the server again with GLIsite.cfg
|
---|
| 580 | llssite_cfg_file.save(); // save the configfile, because we may be reloading another
|
---|
| 581 |
|
---|
| 582 | if(llssite_cfg_file.usingLLS_configFile()) { // if a GSI is already using llssite_cfg, this would have loaded it in
|
---|
[19585] | 583 | url = llssite_cfg_file.getURL(); //if(isURLPending()) {
|
---|
| 584 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
[19547] | 585 | running = false;
|
---|
| 586 | } else {
|
---|
| 587 | running = true;
|
---|
| 588 | }
|
---|
[22661] | 589 | return false; // don't need to launch the server, one has been independently launched
|
---|
[19544] | 590 | } else {
|
---|
[19547] | 591 | // else we try using the glisite configfile
|
---|
| 592 | llssite_cfg_file.useGLISiteCfg(local_library_server_file);
|
---|
| 593 | //llssite_cfg_file.set();
|
---|
[22661] | 594 |
|
---|
| 595 | // since we're going to restart the server, make sure to reset all
|
---|
| 596 | // the client socket and its writer (for communicating with the web server)
|
---|
| 597 | // will only be reinstantiated if they are first nulled
|
---|
| 598 | if(clientSocket != null) {
|
---|
| 599 | clientSocket = null;
|
---|
| 600 | }
|
---|
| 601 | if(clientSocketWriter != null) {
|
---|
| 602 | try{
|
---|
| 603 | clientSocketWriter.close();
|
---|
| 604 | clientSocketWriter = null;
|
---|
| 605 | } catch(Exception e) {
|
---|
| 606 | System.err.println("Unable to close the client socket outputstream.");
|
---|
| 607 | } finally {
|
---|
| 608 | clientSocketWriter = null;
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
[19547] | 611 |
|
---|
| 612 | // Spawn local library server process
|
---|
[20480] | 613 | running = false;
|
---|
[21998] | 614 | final String QUOTES = Utility.isWindows() ? "\"" : ""; // need to embed path in quotes on Windows for spaces (interferes on Linux)
|
---|
| 615 | String local_library_server_command = QUOTES+local_library_server_file.getAbsolutePath()+QUOTES + getExtraLaunchArguments(llssite_cfg_file);
|
---|
[22608] | 616 | if(Utility.isWindows() && !isPersistentServer) { // launching gs2-web-server.bat (Windows) needs cmd start
|
---|
[20480] | 617 | local_library_server_command = "cmd.exe /c start \"GSI\" " + local_library_server_command;
|
---|
| 618 | }
|
---|
[19547] | 619 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
| 620 | try {
|
---|
| 621 | testServerRunning(); // don't return until the webserver is up and running
|
---|
[22661] | 622 | serverRestarted = true;
|
---|
[19547] | 623 | } catch (IOException bad_url_connection) {
|
---|
| 624 | DebugStream.println("Can't connect to server on address " + Configuration.library_url);
|
---|
| 625 | running = false;
|
---|
| 626 | }
|
---|
[19003] | 627 | }
|
---|
[13592] | 628 | }
|
---|
[22661] | 629 | return serverRestarted;
|
---|
[13592] | 630 | }
|
---|
[18987] | 631 |
|
---|
[13592] | 632 | static private class OneSecondWait
|
---|
| 633 | {
|
---|
| 634 | public OneSecondWait()
|
---|
| 635 | {
|
---|
| 636 | synchronized(this) {
|
---|
| 637 | try {
|
---|
| 638 | wait(1000);
|
---|
| 639 | }
|
---|
| 640 | catch (InterruptedException exception) {
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 | }
|
---|
[13792] | 645 |
|
---|
| 646 |
|
---|
[18650] | 647 | static public class LLSSiteConfig
|
---|
[19544] | 648 | extends LinkedHashMap
|
---|
| 649 | {
|
---|
| 650 | private File configFile;
|
---|
| 651 |
|
---|
[18650] | 652 | private File llssite_cfg;
|
---|
[13792] | 653 | private File glisite_cfg;
|
---|
| 654 | private String autoenter_initial;
|
---|
| 655 | private String start_browser_initial;
|
---|
[18987] | 656 |
|
---|
| 657 | private long lastModified = 0;
|
---|
[13792] | 658 |
|
---|
| 659 | static final private String AUTOENTER = "autoenter";
|
---|
| 660 | static final private String COLON = ":";
|
---|
| 661 | static final private String ENTERLIB = "enterlib";
|
---|
| 662 | static final private String FALSE = "0";
|
---|
| 663 | static final private String GLISITE_CFG = "glisite.cfg";
|
---|
[18987] | 664 | static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone
|
---|
[18650] | 665 | static final private String LLSSITE_CFG = "llssite.cfg";
|
---|
[13792] | 666 | static final private String LOCAL_HOST = "http://localhost";
|
---|
| 667 | static final private String PORTNUMBER = "portnumber";
|
---|
| 668 | static final private String SEPARATOR = "/";
|
---|
| 669 | static final private String SPECIFIC_CONFIG = "--config=";
|
---|
| 670 | static final private String STARTBROWSER = "start_browser";
|
---|
| 671 | static final private String TRUE = "1";
|
---|
| 672 | static final private String URL = "url";
|
---|
[22678] | 673 | static final private String COLLECTHOME = "collecthome";
|
---|
| 674 |
|
---|
[19544] | 675 | static final public String URL_PENDING = "URL_pending";
|
---|
| 676 |
|
---|
[19547] | 677 |
|
---|
[18650] | 678 | public LLSSiteConfig(File server_exe) {
|
---|
| 679 | debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath());
|
---|
[18136] | 680 |
|
---|
[18650] | 681 | llssite_cfg = new File(server_exe.getParentFile(), LLSSITE_CFG);
|
---|
[13792] | 682 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
[18136] | 683 |
|
---|
[19544] | 684 | configFile = null;
|
---|
| 685 |
|
---|
| 686 | autoenter_initial = null;
|
---|
| 687 | start_browser_initial = null;
|
---|
| 688 |
|
---|
| 689 | // first test if server was started independently of GLI
|
---|
| 690 | // if so, the config file we'd be using would be llssite.cfg
|
---|
| 691 | if(!usingLLS_configFile()) { // if we were using llssite_cfg, this would have loaded it in
|
---|
| 692 | // else we try using the glisite configfile
|
---|
| 693 | useGLISiteCfg(server_exe);
|
---|
| 694 | }
|
---|
[22771] | 695 |
|
---|
[22806] | 696 | // GS2. We're in the constructor, so we'll be running the LLS the first time
|
---|
| 697 | // Set the correct collectdir (from last time) before the server is started up
|
---|
[23015] | 698 |
|
---|
| 699 | //String collectDir = Configuration.getString("general.open_collection_gs2", true);
|
---|
| 700 | String orig_collection_path = Configuration.getString("general.open_collection_gs2", true);
|
---|
| 701 | String collectDir = Gatherer.getCollectDirectoryPath();
|
---|
| 702 | String defaultColDir = Gatherer.getDefaultGSCollectDirectoryPath(true); // with file separator at end
|
---|
| 703 |
|
---|
[22806] | 704 | // If opening a collection outside the standard GS collect folder, need
|
---|
[23015] | 705 | // to open the non-standard collect folder that the collection resides in.
|
---|
| 706 | // Also, would have been forced to change the loaded collection--and hence collectDir--
|
---|
| 707 | // to the default coldir if orig_col_path was "" or led to a collectdir that didn't exist
|
---|
| 708 | // (in the last case, gsdlsite.cfg already corrected in Gatherer if coldir was changed).
|
---|
| 709 |
|
---|
| 710 | if(orig_collection_path.equals("")) { // default GS collect dir path
|
---|
| 711 | return;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | if (Gatherer.open_collection_file_path != null // may be null if no_load was specified on GLI startup
|
---|
| 715 | && (!orig_collection_path.startsWith(collectDir) // if coldir would've been changed on startup OR if coldir is non-standard collect folder
|
---|
| 716 | || !collectDir.equals(defaultColDir)))
|
---|
| 717 | {
|
---|
| 718 | // if we're running *server.exe* and if the current collect dir at Local Lib's startup is
|
---|
| 719 | // anything other than the default GS collect dir or if we changed back to the default collect
|
---|
| 720 | // dir because the non-standard collectDir didn't exist/wasn't specified in the GLI config
|
---|
| 721 | // file, then write out the new collectDir (minus file separator at end) to the lls site conf in use
|
---|
[22806] | 722 | if (isPersistentServer) { // server.exe, so we're dealing with a local GS2
|
---|
[22771] | 723 | put(COLLECTHOME, collectDir);
|
---|
| 724 | save();
|
---|
| 725 | lastModified = configFile.lastModified();
|
---|
[23015] | 726 | }
|
---|
| 727 | } // now the correct current collect dir will get loaded when the server is started up hereafter
|
---|
[19544] | 728 | }
|
---|
| 729 |
|
---|
| 730 | /** Tries to get a glisite.cfg file and then loads it, setting it as the configFile */
|
---|
[19585] | 731 | public void useGLISiteCfg(File server_exe)
|
---|
| 732 | {
|
---|
[19513] | 733 | if(!glisite_cfg.exists()) { // create it from the templates or the llssite.cfg file
|
---|
[19544] | 734 |
|
---|
[19030] | 735 | File llssite_cfg_in = new File(server_exe.getParentFile(), LLSSITE_CFG+".in");
|
---|
| 736 | File glisite_cfg_in = new File(server_exe.getParentFile(), GLISITE_CFG+".in");
|
---|
[19544] | 737 |
|
---|
[19030] | 738 | // need to generate glisite_cfg from glisite_cfg_in, llssite_cfg or llssite.cfg.in
|
---|
| 739 | if(glisite_cfg_in.exists()) {
|
---|
| 740 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
| 741 | }
|
---|
| 742 | else if(llssite_cfg_in.exists()) {
|
---|
| 743 | copyConfigFile(llssite_cfg_in, glisite_cfg_in, true); // adjust for glisite.cfg
|
---|
| 744 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
| 745 | }
|
---|
| 746 | else if(llssite_cfg.exists()) {
|
---|
| 747 | copyConfigFile(llssite_cfg, glisite_cfg, true); // adjust for glisite.cfg
|
---|
| 748 | }
|
---|
| 749 | else {
|
---|
| 750 | debug("Neither the file glisite.cfg nor llssite.cfg can be found!");
|
---|
| 751 | }
|
---|
| 752 | }
|
---|
[19544] | 753 | // use the config file now
|
---|
[18136] | 754 | if(glisite_cfg.exists()) {
|
---|
| 755 | configFile = glisite_cfg;
|
---|
[19544] | 756 | load(false); // force reload
|
---|
[19547] | 757 | lastModified = configFile.lastModified();
|
---|
[19030] | 758 | }
|
---|
[19544] | 759 | }
|
---|
[19030] | 760 |
|
---|
[19544] | 761 | /** Tests whether the server interface is up, running independently of GLI
|
---|
| 762 | * If so, we don't need to launch the server interface.
|
---|
| 763 | * The server interface may not have started up the server itself though
|
---|
| 764 | * (in which case the server URL would be URL_PENDING).
|
---|
| 765 | * This method returns true if the server interface has already started
|
---|
| 766 | * and, if so, it would have loaded in the llssite_cfg configFile.
|
---|
| 767 | */
|
---|
[19547] | 768 | public boolean usingLLS_configFile() {
|
---|
[19544] | 769 | if(!llssite_cfg.exists()) {
|
---|
| 770 | return false;
|
---|
[13792] | 771 | }
|
---|
[18987] | 772 |
|
---|
[19547] | 773 | // Now to check if the configfile contains the URL line
|
---|
[19544] | 774 | configFile = llssite_cfg;
|
---|
| 775 | load(false); // force load
|
---|
| 776 |
|
---|
| 777 | if(getURL() == null) {
|
---|
| 778 | configFile = null;
|
---|
| 779 | clear(); // we're not using llssite_cfg, so clear the values we just read
|
---|
| 780 | return false;
|
---|
[18987] | 781 | }
|
---|
[19544] | 782 |
|
---|
[19547] | 783 | lastModified = configFile.lastModified();
|
---|
[19544] | 784 | return true;
|
---|
[13792] | 785 | }
|
---|
[19758] | 786 |
|
---|
| 787 | /** To test we've actually instantiated this object meaningfully. If so, then configFile is set */
|
---|
| 788 | public boolean isConfigFileSet() {
|
---|
| 789 | return (configFile != null && configFile.exists());
|
---|
| 790 | }
|
---|
[18136] | 791 |
|
---|
[19544] | 792 | /** @return true if GSI was started up independently and outside of GLI.
|
---|
| 793 | * In such a case, GLI would be using llssite_cfg. */
|
---|
| 794 | public boolean isIndependentGSI() {
|
---|
| 795 | return (configFile == llssite_cfg);
|
---|
| 796 | }
|
---|
| 797 |
|
---|
[13792] | 798 | public boolean exists() {
|
---|
[19544] | 799 | return configFile.exists();
|
---|
[13792] | 800 | }
|
---|
| 801 |
|
---|
| 802 | public String getLocalHostURL() {
|
---|
| 803 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
| 804 | url.append(COLON);
|
---|
| 805 | url.append((String)get(PORTNUMBER));
|
---|
| 806 | String enterlib = (String)get(ENTERLIB);
|
---|
[22661] | 807 | if(!isPersistentServer || enterlib == null || enterlib.length() == 0) {
|
---|
[13792] | 808 | // Use the default /gsdl and hope for the best.
|
---|
| 809 | url.append(SEPARATOR);
|
---|
| 810 | url.append(GSDL);
|
---|
| 811 | }
|
---|
| 812 | else {
|
---|
| 813 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
| 814 | url.append(SEPARATOR);
|
---|
| 815 | }
|
---|
| 816 | url.append(enterlib);
|
---|
| 817 | }
|
---|
| 818 | enterlib = null;
|
---|
| 819 | debug("Found Local Library Address: " + url.toString());
|
---|
| 820 | return url.toString();
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[19544] | 823 | /** @return the cmd-line parameter for the configfile used to launch
|
---|
| 824 | * the server through GLI: --config <glisite.cfg/llssite.cfg file path>. */
|
---|
[13792] | 825 | public String getSiteConfigFilename() {
|
---|
[19544] | 826 | return SPECIFIC_CONFIG + configFile.getAbsolutePath();
|
---|
[13792] | 827 | }
|
---|
| 828 |
|
---|
| 829 | public String getURL() {
|
---|
| 830 | // URL is made from url and portnumber
|
---|
| 831 | String url = (String) get(URL);
|
---|
[18987] | 832 |
|
---|
[19544] | 833 | // server interface is already up, independent of GLI
|
---|
| 834 | // but it has not started the server (hence URL is pending)
|
---|
| 835 | if(url != null && url.equals(URL_PENDING)) {
|
---|
| 836 | return url;
|
---|
| 837 | }
|
---|
| 838 |
|
---|
[20480] | 839 | if(!isPersistentServer) {
|
---|
[18987] | 840 | return url;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
[13792] | 843 | if(url != null) {
|
---|
| 844 | StringBuffer temp = new StringBuffer(url);
|
---|
| 845 | temp.append(COLON);
|
---|
| 846 | temp.append((String)get(PORTNUMBER));
|
---|
| 847 | String enterlib = (String)get(ENTERLIB);
|
---|
| 848 | if(enterlib == null || enterlib.length() == 0) {
|
---|
[19544] | 849 | // Use the default /greenstone prefix and hope for the best.
|
---|
[13792] | 850 | temp.append(SEPARATOR);
|
---|
| 851 | temp.append(GSDL);
|
---|
| 852 | }
|
---|
| 853 | else {
|
---|
| 854 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
| 855 | temp.append(SEPARATOR);
|
---|
| 856 | }
|
---|
| 857 | temp.append(enterlib);
|
---|
| 858 | }
|
---|
| 859 | enterlib = null;
|
---|
| 860 | url = temp.toString();
|
---|
| 861 | }
|
---|
| 862 | debug("Found Local Library Address: " + url);
|
---|
| 863 | return url;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
[22661] | 866 | public void setLastModified() {
|
---|
| 867 | if(isModified()) {
|
---|
| 868 | lastModified = configFile.lastModified();
|
---|
| 869 | }
|
---|
| 870 | }
|
---|
| 871 |
|
---|
[18987] | 872 | public boolean isModified() {
|
---|
[19544] | 873 | return (lastModified != configFile.lastModified());
|
---|
[18987] | 874 | }
|
---|
| 875 |
|
---|
| 876 | public void load(boolean reloadOnlyIfModified) {
|
---|
| 877 |
|
---|
[19544] | 878 | if(configFile == null) {
|
---|
| 879 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
| 880 | }
|
---|
| 881 |
|
---|
[18987] | 882 | if(isModified()) {
|
---|
[19544] | 883 | lastModified = configFile.lastModified();
|
---|
[18987] | 884 | } else if(reloadOnlyIfModified) {
|
---|
| 885 | return; // asked to reload only if modified. Don't reload since not modified
|
---|
| 886 | }
|
---|
| 887 |
|
---|
[19544] | 888 | if(configFile.exists()) {
|
---|
| 889 | debug("Load: " + configFile.getAbsolutePath());
|
---|
[13792] | 890 | clear();
|
---|
| 891 | try {
|
---|
[19544] | 892 | BufferedReader in = new BufferedReader(new FileReader(configFile));
|
---|
[13792] | 893 | String line = null;
|
---|
| 894 | while((line = in.readLine()) != null) {
|
---|
| 895 | String key = null;
|
---|
| 896 | String value = null;
|
---|
| 897 | int index = -1;
|
---|
| 898 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
| 899 | key = line.substring(0, index);
|
---|
| 900 | value = line.substring(index + 1);
|
---|
| 901 | }
|
---|
| 902 | else {
|
---|
| 903 | key = line;
|
---|
| 904 | }
|
---|
| 905 | put(key, value);
|
---|
| 906 | }
|
---|
[22678] | 907 |
|
---|
| 908 | in.close();
|
---|
[13792] | 909 | }
|
---|
| 910 | catch (Exception error) {
|
---|
| 911 | error.printStackTrace();
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 | else {
|
---|
[19544] | 915 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
[13792] | 916 | }
|
---|
| 917 | }
|
---|
| 918 |
|
---|
| 919 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
| 920 | public void restore() {
|
---|
[19544] | 921 | if(configFile != null) {
|
---|
[13792] | 922 | // Delete the file
|
---|
[19544] | 923 | configFile.delete();
|
---|
[13792] | 924 | }
|
---|
| 925 | else {
|
---|
| 926 | debug("Restore Initial Settings");
|
---|
| 927 | put(AUTOENTER, autoenter_initial);
|
---|
| 928 | put(STARTBROWSER, start_browser_initial);
|
---|
| 929 | remove(URL);
|
---|
| 930 | save();
|
---|
| 931 | }
|
---|
| 932 | }
|
---|
| 933 |
|
---|
| 934 | public void set() {
|
---|
| 935 | debug("Set Session Settings");
|
---|
| 936 | if(autoenter_initial == null) {
|
---|
| 937 | autoenter_initial = (String) get(AUTOENTER);
|
---|
| 938 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
| 939 | }
|
---|
| 940 | put(AUTOENTER, TRUE);
|
---|
| 941 | if(start_browser_initial == null) {
|
---|
| 942 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
| 943 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
| 944 | }
|
---|
| 945 | put(STARTBROWSER, FALSE);
|
---|
| 946 | save();
|
---|
| 947 | }
|
---|
| 948 |
|
---|
| 949 | private void debug(String message) {
|
---|
| 950 | ///ystem.err.println(message);
|
---|
| 951 | }
|
---|
| 952 |
|
---|
| 953 | private void save() {
|
---|
[18650] | 954 | //debug("Save: " + llssite_cfg.getAbsolutePath());
|
---|
[19544] | 955 | debug("Save: " + configFile.getAbsolutePath());
|
---|
[13792] | 956 | try {
|
---|
[18650] | 957 | //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false));
|
---|
[19544] | 958 | BufferedWriter out = new BufferedWriter(new FileWriter(configFile, false));
|
---|
[13792] | 959 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
| 960 | String key = (String) keys.next();
|
---|
| 961 | String value = (String) get(key);
|
---|
| 962 | out.write(key, 0, key.length());
|
---|
| 963 | if(value != null) {
|
---|
| 964 | out.write('=');
|
---|
[19544] | 965 |
|
---|
| 966 | // if the server is using llssite.cfg, don't overwrite its default
|
---|
| 967 | // autoenter and startbrowser values
|
---|
| 968 | if(configFile == llssite_cfg && (key == AUTOENTER || key == STARTBROWSER)) {
|
---|
| 969 | if(key == AUTOENTER) {
|
---|
| 970 | out.write(autoenter_initial, 0, autoenter_initial.length());
|
---|
| 971 | } else { // STARTBROWSER
|
---|
| 972 | out.write(start_browser_initial, 0, start_browser_initial.length());
|
---|
| 973 | }
|
---|
| 974 | } else {
|
---|
| 975 | out.write(value, 0, value.length());
|
---|
| 976 | }
|
---|
[13792] | 977 | }
|
---|
| 978 | out.newLine();
|
---|
| 979 | }
|
---|
| 980 | out.flush();
|
---|
| 981 | out.close();
|
---|
| 982 | }
|
---|
| 983 | catch (Exception error) {
|
---|
| 984 | error.printStackTrace();
|
---|
| 985 | }
|
---|
| 986 | }
|
---|
[19030] | 987 |
|
---|
| 988 | private static void copyConfigFile(File source_cfg, File dest_cfg, boolean setToGliSiteDefaults) {
|
---|
| 989 | // source_cfg file should exist
|
---|
| 990 | // dest_cfg file should not yet exist
|
---|
| 991 | // If setToGliSiteDefaults is true, then GLIsite.cfg's default configuration
|
---|
| 992 | // is applied to concerned lines: autoenter=1, and startbrowser=0
|
---|
| 993 |
|
---|
| 994 | try {
|
---|
| 995 | BufferedReader in = new BufferedReader(new FileReader(source_cfg));
|
---|
| 996 | BufferedWriter out = new BufferedWriter(new FileWriter(dest_cfg, false));
|
---|
| 997 |
|
---|
| 998 | String line = null;
|
---|
| 999 | while((line = in.readLine()) != null) {
|
---|
| 1000 |
|
---|
| 1001 | if(setToGliSiteDefaults) {
|
---|
| 1002 | if(line.startsWith(AUTOENTER)) {
|
---|
| 1003 | line = AUTOENTER+"=1";
|
---|
| 1004 | }
|
---|
| 1005 | else if(line.startsWith(STARTBROWSER)) {
|
---|
| 1006 | line = STARTBROWSER+"=0";
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 |
|
---|
| 1010 | // write out the line
|
---|
[22771] | 1011 | out.write(line);
|
---|
| 1012 | out.newLine();
|
---|
[19030] | 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | out.flush();
|
---|
| 1016 | in.close();
|
---|
| 1017 | out.close();
|
---|
| 1018 | } catch(Exception e) {
|
---|
| 1019 | System.err.println("Exception occurred when trying to copy the config file "
|
---|
| 1020 | + source_cfg.getName() + " to " + dest_cfg.getName() + ": " + e);
|
---|
| 1021 | e.printStackTrace();
|
---|
| 1022 | }
|
---|
| 1023 | }
|
---|
[13792] | 1024 | }
|
---|
[13592] | 1025 | }
|
---|