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