[8620] | 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;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | import java.io.*;
|
---|
| 31 | import java.lang.*;
|
---|
| 32 | import java.net.*;
|
---|
| 33 | import javax.swing.*;
|
---|
| 34 | import org.greenstone.gatherer.util.GSDLSiteConfig;
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | public class LocalLibraryServer
|
---|
| 38 | {
|
---|
[8622] | 39 | static final private String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
|
---|
| 40 | static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
|
---|
| 41 | static final private String QUIT_COMMAND = "?a=config&cmd=kill";
|
---|
| 42 |
|
---|
| 43 | static private GSDLSiteConfig gsdlsite_cfg_file = null;
|
---|
[10642] | 44 | static private File local_library_server_file = null;
|
---|
| 45 |
|
---|
[8620] | 46 | static private boolean running = false;
|
---|
| 47 |
|
---|
| 48 | static public void addCollection(String collection_name)
|
---|
| 49 | {
|
---|
[8622] | 50 | config(ADD_COMMAND + collection_name);
|
---|
[8620] | 51 | // This is very important -- it ensures that the above command has finished
|
---|
| 52 | config("");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | // Used to send messages to the local library
|
---|
| 57 | // Warning: this has a lot of potential for nasty race conditions
|
---|
| 58 | // The response code is returned immediately -- but this does not mean the local
|
---|
| 59 | // library action has finished!
|
---|
| 60 | static private void config(String command)
|
---|
| 61 | {
|
---|
[8629] | 62 | if (Configuration.library_url == null) {
|
---|
| 63 | System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[8620] | 67 | try {
|
---|
[8627] | 68 | URL url = new URL(Configuration.library_url.toString() + command);
|
---|
[8620] | 69 | HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
|
---|
| 70 | int response_code = library_connection.getResponseCode();
|
---|
[8622] | 71 | if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
|
---|
[8620] | 72 | DebugStream.println("200 - Complete.");
|
---|
| 73 | }
|
---|
| 74 | else {
|
---|
| 75 | DebugStream.println("404 - Failed.");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | catch (Exception ex) {
|
---|
| 79 | DebugStream.printStackTrace(ex);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | static public boolean isRunning()
|
---|
| 85 | {
|
---|
[10643] | 86 | if (!running) return false;
|
---|
| 87 | gsdlsite_cfg_file.load();
|
---|
| 88 | if (gsdlsite_cfg_file.getURL() == null) return false;
|
---|
| 89 | return true;
|
---|
[8620] | 90 | }
|
---|
| 91 |
|
---|
| 92 | static public void releaseCollection(String collection_name)
|
---|
| 93 | {
|
---|
[8622] | 94 | config(RELEASE_COMMAND + collection_name);
|
---|
[8620] | 95 | // This is very important -- it ensures that the above command has finished
|
---|
| 96 | config("");
|
---|
[11574] | 97 |
|
---|
| 98 | // !! HACK: Wait a couple of seconds, in the hope that this will reduce the "could not delete index" errors
|
---|
| 99 | new OneSecondWait();
|
---|
| 100 | new OneSecondWait();
|
---|
[8620] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | static public void start(String gsdl_path, String local_library_server_file_path)
|
---|
| 105 | {
|
---|
[8622] | 106 | // Check the local library server.exe file exists
|
---|
[10642] | 107 | local_library_server_file = new File(local_library_server_file_path);
|
---|
[8620] | 108 | if (!local_library_server_file.exists()) {
|
---|
| 109 | DebugStream.println("No local library at given file path.");
|
---|
| 110 |
|
---|
[8622] | 111 | local_library_server_file = new File(gsdl_path + "server.exe");
|
---|
[8620] | 112 | if (!local_library_server_file.exists()) {
|
---|
| 113 | DebugStream.println("No local library at all.");
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[8622] | 118 | // Check if the server is already running
|
---|
| 119 | gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
|
---|
| 120 | String url = gsdlsite_cfg_file.getURL();
|
---|
| 121 | if (url != null) {
|
---|
| 122 | // If it is already running then set the Greenstone web server address and we're done
|
---|
[8620] | 123 | try {
|
---|
[8627] | 124 | Configuration.library_url = new URL(url);
|
---|
[8624] | 125 | running = true;
|
---|
[8622] | 126 | return;
|
---|
[8620] | 127 | }
|
---|
[8622] | 128 | catch (MalformedURLException exception) {
|
---|
| 129 | DebugStream.printStackTrace(exception);
|
---|
[8620] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[8622] | 133 | // Configure the server for immediate entry
|
---|
| 134 | gsdlsite_cfg_file.set();
|
---|
[8620] | 135 |
|
---|
[8622] | 136 | // Spawn local library server process
|
---|
| 137 | String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
|
---|
[10517] | 138 | Gatherer.spawnApplication(local_library_server_command);
|
---|
[8622] | 139 |
|
---|
| 140 | // Wait until program has started, by reloading and checking the URL field
|
---|
| 141 | gsdlsite_cfg_file.load();
|
---|
| 142 | int attempt_count = 0;
|
---|
| 143 | while (gsdlsite_cfg_file.getURL() == null) {
|
---|
| 144 | new OneSecondWait(); // Wait one second (give or take)
|
---|
| 145 | gsdlsite_cfg_file.load();
|
---|
| 146 | attempt_count++;
|
---|
| 147 |
|
---|
| 148 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
| 149 | if (attempt_count == 60) {
|
---|
| 150 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
| 151 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
| 152 | return;
|
---|
[8620] | 153 | }
|
---|
[8622] | 154 | attempt_count = 0;
|
---|
[8620] | 155 | }
|
---|
[8622] | 156 | }
|
---|
| 157 |
|
---|
| 158 | // Ta-da. Now the url should be available
|
---|
| 159 | try {
|
---|
[8627] | 160 | Configuration.library_url = new URL(gsdlsite_cfg_file.getURL());
|
---|
[8622] | 161 | }
|
---|
| 162 | catch (MalformedURLException exception) {
|
---|
| 163 | DebugStream.printStackTrace(exception);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // A quick test involves opening a connection to get the home page for this collection
|
---|
| 167 | try {
|
---|
[8627] | 168 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
|
---|
| 169 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
[8622] | 170 | connection.getContent();
|
---|
| 171 | }
|
---|
| 172 | catch (IOException bad_url_connection) {
|
---|
| 173 | try {
|
---|
| 174 | // If this fails then we try changing the url to be localhost
|
---|
[8627] | 175 | Configuration.library_url = new URL(gsdlsite_cfg_file.getLocalHostURL());
|
---|
| 176 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
| 177 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
[8622] | 178 | connection.getContent();
|
---|
[8620] | 179 | }
|
---|
[8622] | 180 | catch (IOException worse_url_connection) {
|
---|
| 181 | DebugStream.println("Can't connect to server on either address.");
|
---|
[8627] | 182 | Configuration.library_url = null;
|
---|
[8624] | 183 | return;
|
---|
[8622] | 184 | }
|
---|
[8620] | 185 | }
|
---|
[8624] | 186 |
|
---|
| 187 | running = true;
|
---|
[8620] | 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | static public void stop()
|
---|
| 192 | {
|
---|
[8622] | 193 | if (running == false) {
|
---|
| 194 | return;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // Send the command for it to exit.
|
---|
| 198 | config(QUIT_COMMAND);
|
---|
| 199 |
|
---|
[8624] | 200 | // Wait until program has stopped, by reloading and checking the URL field
|
---|
[8622] | 201 | gsdlsite_cfg_file.load();
|
---|
| 202 | int attempt_count = 0;
|
---|
| 203 | while (gsdlsite_cfg_file.getURL() != null) {
|
---|
| 204 | new OneSecondWait(); // Wait one second (give or take)
|
---|
| 205 | gsdlsite_cfg_file.load();
|
---|
| 206 | attempt_count++;
|
---|
| 207 |
|
---|
| 208 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
| 209 | if (attempt_count == 60) {
|
---|
| 210 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
| 211 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
| 212 | return;
|
---|
[8620] | 213 | }
|
---|
[8622] | 214 | attempt_count = 0;
|
---|
[8620] | 215 | }
|
---|
| 216 | }
|
---|
[8622] | 217 |
|
---|
| 218 | // Restore the gsdlsite_cfg.
|
---|
| 219 | gsdlsite_cfg_file.restore();
|
---|
| 220 |
|
---|
| 221 | // If the local server is still running then our changed values will get overwritten.
|
---|
| 222 | if (gsdlsite_cfg_file.getURL() != null) {
|
---|
[11049] | 223 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
[8622] | 224 | }
|
---|
[8624] | 225 |
|
---|
| 226 | running = false;
|
---|
[8620] | 227 | }
|
---|
[10642] | 228 |
|
---|
| 229 | static public void checkServerRunning() {
|
---|
[10643] | 230 | if (!running) return; // don't worry about it if its not supposed to be running
|
---|
[10642] | 231 | gsdlsite_cfg_file.load();
|
---|
| 232 | if (gsdlsite_cfg_file.getURL() == null) {
|
---|
| 233 | // need to restart the server again
|
---|
| 234 | gsdlsite_cfg_file.set();
|
---|
[8620] | 235 |
|
---|
[10642] | 236 | // Spawn local library server process
|
---|
| 237 | String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
|
---|
| 238 | Gatherer.spawnApplication(local_library_server_command);
|
---|
[8620] | 239 |
|
---|
[10642] | 240 | }
|
---|
| 241 | }
|
---|
[8624] | 242 | static private class OneSecondWait
|
---|
[8620] | 243 | {
|
---|
| 244 | public OneSecondWait()
|
---|
| 245 | {
|
---|
[8624] | 246 | synchronized(this) {
|
---|
| 247 | try {
|
---|
| 248 | wait(1000);
|
---|
| 249 | }
|
---|
| 250 | catch (InterruptedException exception) {
|
---|
| 251 | }
|
---|
[8620] | 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | }
|
---|