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