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.Utility;
|
---|
41 |
|
---|
42 |
|
---|
43 | public class LocalLibraryServer
|
---|
44 | {
|
---|
45 | static final private int WAITING_TIME = 20; // number of seconds to wait for the server to start and stop
|
---|
46 |
|
---|
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 |
|
---|
51 | static private LLSSiteConfig llssite_cfg_file = null;
|
---|
52 | static private File local_library_server_file = null;
|
---|
53 |
|
---|
54 | static private boolean running = false;
|
---|
55 | static private String ID = "greenstone-server"; // a sort of process ID
|
---|
56 |
|
---|
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 |
|
---|
68 | static public void addCollection(String collection_name)
|
---|
69 | {
|
---|
70 | if (isPersistentServer) {
|
---|
71 | config(ADD_COMMAND + collection_name);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | // Used to send messages to the local library
|
---|
77 | static private void config(String command)
|
---|
78 | {
|
---|
79 | if (!isPersistentServer) {
|
---|
80 | return;
|
---|
81 | }
|
---|
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();
|
---|
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 |
|
---|
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 | }
|
---|
111 | catch (Exception exception) {
|
---|
112 | DebugStream.printStackTrace(exception);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
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 | }
|
---|
139 |
|
---|
140 | static public boolean isRunning()
|
---|
141 | {
|
---|
142 | if (!running) return false; // if the url is pending, then running would also be false (server not started up yet)
|
---|
143 |
|
---|
144 | llssite_cfg_file.load(true);
|
---|
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 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | static public void releaseCollection(String collection_name)
|
---|
158 | {
|
---|
159 | if (isPersistentServer) {
|
---|
160 | config(RELEASE_COMMAND + collection_name);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | static public void start(String gsdl_path, String local_library_server_file_path)
|
---|
165 | {
|
---|
166 | // Check the local library server file (server.exe or gs2-server.sh) exists
|
---|
167 | local_library_server_file = new File(local_library_server_file_path);
|
---|
168 |
|
---|
169 | if (!local_library_server_file.exists()) {
|
---|
170 | DebugStream.println("No local library at given file path.");
|
---|
171 |
|
---|
172 | String defaultServerFilename = Utility.isWindows() ? "server.exe" : "gs2-server.sh";
|
---|
173 | local_library_server_file = new File(gsdl_path + defaultServerFilename);
|
---|
174 | if (!local_library_server_file.exists()) {
|
---|
175 | DebugStream.println("No local library at all.");
|
---|
176 | return;
|
---|
177 | }
|
---|
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 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
|
---|
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 |
|
---|
205 | // Spawn local library server process
|
---|
206 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
207 |
|
---|
208 | // Check if the server is already running
|
---|
209 | String url = llssite_cfg_file.getURL();
|
---|
210 | if (url != null) {
|
---|
211 | // If it is already running then set the Greenstone web server address and we're done
|
---|
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.
|
---|
214 | try {
|
---|
215 | Configuration.library_url = new URL(url);
|
---|
216 | running = true;
|
---|
217 |
|
---|
218 | // Run the server interface
|
---|
219 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
220 | return;
|
---|
221 | }
|
---|
222 | catch (MalformedURLException exception) {
|
---|
223 | DebugStream.printStackTrace(exception);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | // Configure the server for immediate entry
|
---|
228 | //llssite_cfg_file.set();
|
---|
229 |
|
---|
230 | // Spawn local library server process
|
---|
231 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
232 |
|
---|
233 | // Wait until program has started
|
---|
234 | try {
|
---|
235 | testServerRunning(); // will set running = true when the server is up and running successfully
|
---|
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 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | static public void stop()
|
---|
255 | {
|
---|
256 | if (!running) {
|
---|
257 | // also the case if the URL is pending in an independently launched GSI
|
---|
258 | return;
|
---|
259 | }
|
---|
260 |
|
---|
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 |
|
---|
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;
|
---|
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)
|
---|
283 | new OneSecondWait(); // Wait one second (give or take)
|
---|
284 | llssite_cfg_file.load(false);
|
---|
285 | attempt_count++;
|
---|
286 |
|
---|
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);
|
---|
291 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
292 | return;
|
---|
293 | }
|
---|
294 | attempt_count = 0;
|
---|
295 | }
|
---|
296 | // read the url again to see if it's updated
|
---|
297 | url = llssite_cfg_file.getURL();
|
---|
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.
|
---|
304 | url = llssite_cfg_file.getURL();
|
---|
305 | if (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
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);
|
---|
322 | port = portFinder.findPortInRange(false); // silent mode
|
---|
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.
|
---|
334 | static private void testServerRunning() throws IOException {
|
---|
335 | // Wait until program has started, by reloading and checking the URL field
|
---|
336 | llssite_cfg_file.load(false);
|
---|
337 | int attempt_count = 0;
|
---|
338 | while (llssite_cfg_file.getURL() == null) {
|
---|
339 | new OneSecondWait(); // Wait one second (give or take)
|
---|
340 | llssite_cfg_file.load(false);
|
---|
341 | attempt_count++;
|
---|
342 |
|
---|
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);
|
---|
347 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
348 | return;
|
---|
349 | }
|
---|
350 | attempt_count = 0;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | // Ta-da. Now the url should be available
|
---|
355 | try {
|
---|
356 | Configuration.library_url = new URL(llssite_cfg_file.getURL());
|
---|
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 {
|
---|
364 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url + "'");
|
---|
365 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
366 | connection.getContent();
|
---|
367 | running = true;
|
---|
368 | }
|
---|
369 | catch (IOException bad_url_connection) {
|
---|
370 | throw bad_url_connection;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | }
|
---|
375 |
|
---|
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 |
|
---|
384 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
385 |
|
---|
386 | String url = llssite_cfg_file.getURL();
|
---|
387 |
|
---|
388 | if(url != null) {
|
---|
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) {
|
---|
417 | try {
|
---|
418 | Configuration.library_url = new URL(url);
|
---|
419 | }
|
---|
420 | catch (MalformedURLException exception) {
|
---|
421 | DebugStream.printStackTrace(exception);
|
---|
422 | exception.printStackTrace();
|
---|
423 | }
|
---|
424 | } else {
|
---|
425 | // need to restart the server again
|
---|
426 |
|
---|
427 | // if we were running an independently launched GSI before, but the GSI has been
|
---|
428 | // exited, it would need to be relaunched, but from within GLI (dependent) this time.
|
---|
429 | if(llssite_cfg_file.isIndependentGSI()) {
|
---|
430 | // first save the current configFile (llssite.cfg), then set to use glisite.cfg
|
---|
431 | llssite_cfg_file.save();
|
---|
432 | llssite_cfg_file.relaunchAsDependentGSI(local_library_server_file);
|
---|
433 | } else {
|
---|
434 | // We were using glisite.cfg before, don't need to swap config files
|
---|
435 | llssite_cfg_file.set();
|
---|
436 | }
|
---|
437 | // Spawn local library server process
|
---|
438 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
439 | running = false;
|
---|
440 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
441 | try {
|
---|
442 | testServerRunning(); // don't return until the webserver is up and running
|
---|
443 | } catch (IOException bad_url_connection) {
|
---|
444 | DebugStream.println("Can't connect to server on address " + Configuration.library_url);
|
---|
445 | running = false;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | static private class OneSecondWait
|
---|
451 | {
|
---|
452 | public OneSecondWait()
|
---|
453 | {
|
---|
454 | synchronized(this) {
|
---|
455 | try {
|
---|
456 | wait(1000);
|
---|
457 | }
|
---|
458 | catch (InterruptedException exception) {
|
---|
459 | }
|
---|
460 | }
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | static public class LLSSiteConfig
|
---|
466 | extends LinkedHashMap
|
---|
467 | {
|
---|
468 | private File configFile;
|
---|
469 |
|
---|
470 | private File llssite_cfg;
|
---|
471 | private File glisite_cfg;
|
---|
472 | private String autoenter_initial;
|
---|
473 | private String start_browser_initial;
|
---|
474 |
|
---|
475 | private long lastModified = 0;
|
---|
476 |
|
---|
477 | static final private String AUTOENTER = "autoenter";
|
---|
478 | static final private String COLON = ":";
|
---|
479 | static final private String ENTERLIB = "enterlib";
|
---|
480 | static final private String FALSE = "0";
|
---|
481 | static final private String GLISITE_CFG = "glisite.cfg";
|
---|
482 | static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone
|
---|
483 | static final private String LLSSITE_CFG = "llssite.cfg";
|
---|
484 | static final private String LOCAL_HOST = "http://localhost";
|
---|
485 | static final private String PORTNUMBER = "portnumber";
|
---|
486 | static final private String SEPARATOR = "/";
|
---|
487 | static final private String SPECIFIC_CONFIG = "--config=";
|
---|
488 | static final private String STARTBROWSER = "start_browser";
|
---|
489 | static final private String TRUE = "1";
|
---|
490 | static final private String URL = "url";
|
---|
491 |
|
---|
492 | static final public String URL_PENDING = "URL_pending";
|
---|
493 |
|
---|
494 | public LLSSiteConfig(File server_exe) {
|
---|
495 | debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath());
|
---|
496 |
|
---|
497 | llssite_cfg = new File(server_exe.getParentFile(), LLSSITE_CFG);
|
---|
498 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
499 |
|
---|
500 | configFile = null;
|
---|
501 |
|
---|
502 | autoenter_initial = null;
|
---|
503 | start_browser_initial = null;
|
---|
504 |
|
---|
505 | // first test if server was started independently of GLI
|
---|
506 | // if so, the config file we'd be using would be llssite.cfg
|
---|
507 | if(!usingLLS_configFile()) { // if we were using llssite_cfg, this would have loaded it in
|
---|
508 | // else we try using the glisite configfile
|
---|
509 | useGLISiteCfg(server_exe);
|
---|
510 | }
|
---|
511 |
|
---|
512 | if(configFile != null && configFile.exists()) {
|
---|
513 | System.err.println("Using configfile: " + configFile);
|
---|
514 | lastModified = configFile.lastModified();
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | /** Tries to get a glisite.cfg file and then loads it, setting it as the configFile */
|
---|
519 | public void useGLISiteCfg(File server_exe) {
|
---|
520 | if(!glisite_cfg.exists()) { // create it from the templates or the llssite.cfg file
|
---|
521 |
|
---|
522 | File llssite_cfg_in = new File(server_exe.getParentFile(), LLSSITE_CFG+".in");
|
---|
523 | File glisite_cfg_in = new File(server_exe.getParentFile(), GLISITE_CFG+".in");
|
---|
524 |
|
---|
525 | // need to generate glisite_cfg from glisite_cfg_in, llssite_cfg or llssite.cfg.in
|
---|
526 | if(glisite_cfg_in.exists()) {
|
---|
527 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
528 | }
|
---|
529 | else if(llssite_cfg_in.exists()) {
|
---|
530 | copyConfigFile(llssite_cfg_in, glisite_cfg_in, true); // adjust for glisite.cfg
|
---|
531 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
532 | }
|
---|
533 | else if(llssite_cfg.exists()) {
|
---|
534 | copyConfigFile(llssite_cfg, glisite_cfg, true); // adjust for glisite.cfg
|
---|
535 | }
|
---|
536 | else {
|
---|
537 | debug("Neither the file glisite.cfg nor llssite.cfg can be found!");
|
---|
538 | }
|
---|
539 | }
|
---|
540 | // use the config file now
|
---|
541 | if(glisite_cfg.exists()) {
|
---|
542 | configFile = glisite_cfg;
|
---|
543 | load(false); // force reload
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | /** Tests whether the server interface is up, running independently of GLI
|
---|
548 | * If so, we don't need to launch the server interface.
|
---|
549 | * The server interface may not have started up the server itself though
|
---|
550 | * (in which case the server URL would be URL_PENDING).
|
---|
551 | * This method returns true if the server interface has already started
|
---|
552 | * and, if so, it would have loaded in the llssite_cfg configFile.
|
---|
553 | */
|
---|
554 | private boolean usingLLS_configFile() {
|
---|
555 | if(!llssite_cfg.exists()) {
|
---|
556 | return false;
|
---|
557 | }
|
---|
558 |
|
---|
559 | // check if the configfile contains the URL line
|
---|
560 | configFile = llssite_cfg;
|
---|
561 | load(false); // force load
|
---|
562 |
|
---|
563 | if(getURL() == null) {
|
---|
564 | configFile = null;
|
---|
565 | clear(); // we're not using llssite_cfg, so clear the values we just read
|
---|
566 | return false;
|
---|
567 | }
|
---|
568 |
|
---|
569 | //System.err.println("***** we're using llssite_configfile, url:" + getURL());
|
---|
570 | return true;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /** @return true if GSI was started up independently and outside of GLI.
|
---|
574 | * In such a case, GLI would be using llssite_cfg. */
|
---|
575 | public boolean isIndependentGSI() {
|
---|
576 | return (configFile == llssite_cfg);
|
---|
577 | }
|
---|
578 |
|
---|
579 | /** Call this when an independently launched GSI server has been stopped
|
---|
580 | * (one using llssite_cfg) and GLI needs to next launch another server.
|
---|
581 | * In such a case, since GLI itself is relaunching the GSI, we use glisite_cfg.
|
---|
582 | */
|
---|
583 | public void relaunchAsDependentGSI(File server_exe) {
|
---|
584 | useGLISiteCfg(server_exe);
|
---|
585 | }
|
---|
586 |
|
---|
587 | public boolean exists() {
|
---|
588 | return configFile.exists();
|
---|
589 | }
|
---|
590 |
|
---|
591 | public String getLocalHostURL() {
|
---|
592 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
593 | url.append(COLON);
|
---|
594 | url.append((String)get(PORTNUMBER));
|
---|
595 | String enterlib = (String)get(ENTERLIB);
|
---|
596 | if(enterlib == null || enterlib.length() == 0) {
|
---|
597 | // Use the default /gsdl and hope for the best.
|
---|
598 | url.append(SEPARATOR);
|
---|
599 | url.append(GSDL);
|
---|
600 | }
|
---|
601 | else {
|
---|
602 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
603 | url.append(SEPARATOR);
|
---|
604 | }
|
---|
605 | url.append(enterlib);
|
---|
606 | }
|
---|
607 | enterlib = null;
|
---|
608 | debug("Found Local Library Address: " + url.toString());
|
---|
609 | return url.toString();
|
---|
610 | }
|
---|
611 |
|
---|
612 | /** @return the cmd-line parameter for the configfile used to launch
|
---|
613 | * the server through GLI: --config <glisite.cfg/llssite.cfg file path>. */
|
---|
614 | public String getSiteConfigFilename() {
|
---|
615 | return SPECIFIC_CONFIG + configFile.getAbsolutePath();
|
---|
616 | }
|
---|
617 |
|
---|
618 | public String getURL() {
|
---|
619 | // URL is made from url and portnumber
|
---|
620 | String url = (String) get(URL);
|
---|
621 |
|
---|
622 | // server interface is already up, independent of GLI
|
---|
623 | // but it has not started the server (hence URL is pending)
|
---|
624 | if(url != null && url.equals(URL_PENDING)) {
|
---|
625 | return url;
|
---|
626 | }
|
---|
627 |
|
---|
628 | if(!Utility.isWindows()) {
|
---|
629 | return url;
|
---|
630 | }
|
---|
631 |
|
---|
632 | if(url != null) {
|
---|
633 | StringBuffer temp = new StringBuffer(url);
|
---|
634 | temp.append(COLON);
|
---|
635 | temp.append((String)get(PORTNUMBER));
|
---|
636 | String enterlib = (String)get(ENTERLIB);
|
---|
637 | if(enterlib == null || enterlib.length() == 0) {
|
---|
638 | // Use the default /greenstone prefix and hope for the best.
|
---|
639 | temp.append(SEPARATOR);
|
---|
640 | temp.append(GSDL);
|
---|
641 | }
|
---|
642 | else {
|
---|
643 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
644 | temp.append(SEPARATOR);
|
---|
645 | }
|
---|
646 | temp.append(enterlib);
|
---|
647 | }
|
---|
648 | enterlib = null;
|
---|
649 | url = temp.toString();
|
---|
650 | }
|
---|
651 | debug("Found Local Library Address: " + url);
|
---|
652 | return url;
|
---|
653 | }
|
---|
654 |
|
---|
655 | public boolean isModified() {
|
---|
656 | return (lastModified != configFile.lastModified());
|
---|
657 | }
|
---|
658 |
|
---|
659 | public void load(boolean reloadOnlyIfModified) {
|
---|
660 |
|
---|
661 | if(configFile == null) {
|
---|
662 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
663 | }
|
---|
664 |
|
---|
665 | if(isModified()) {
|
---|
666 | lastModified = configFile.lastModified();
|
---|
667 | } else if(reloadOnlyIfModified) {
|
---|
668 | return; // asked to reload only if modified. Don't reload since not modified
|
---|
669 | }
|
---|
670 |
|
---|
671 | if(configFile.exists()) {
|
---|
672 | debug("Load: " + configFile.getAbsolutePath());
|
---|
673 | clear();
|
---|
674 | try {
|
---|
675 | BufferedReader in = new BufferedReader(new FileReader(configFile));
|
---|
676 | String line = null;
|
---|
677 | while((line = in.readLine()) != null) {
|
---|
678 | String key = null;
|
---|
679 | String value = null;
|
---|
680 | int index = -1;
|
---|
681 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
682 | key = line.substring(0, index);
|
---|
683 | value = line.substring(index + 1);
|
---|
684 | }
|
---|
685 | else {
|
---|
686 | key = line;
|
---|
687 | }
|
---|
688 | put(key, value);
|
---|
689 | }
|
---|
690 | in.close();
|
---|
691 | }
|
---|
692 | catch (Exception error) {
|
---|
693 | error.printStackTrace();
|
---|
694 | }
|
---|
695 | }
|
---|
696 | else {
|
---|
697 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
702 | public void restore() {
|
---|
703 | if(configFile != null) {
|
---|
704 | // Delete the file
|
---|
705 | configFile.delete();
|
---|
706 | }
|
---|
707 | else {
|
---|
708 | debug("Restore Initial Settings");
|
---|
709 | put(AUTOENTER, autoenter_initial);
|
---|
710 | put(STARTBROWSER, start_browser_initial);
|
---|
711 | remove(URL);
|
---|
712 | save();
|
---|
713 | }
|
---|
714 | }
|
---|
715 |
|
---|
716 | public void set() {
|
---|
717 | debug("Set Session Settings");
|
---|
718 | if(autoenter_initial == null) {
|
---|
719 | autoenter_initial = (String) get(AUTOENTER);
|
---|
720 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
721 | }
|
---|
722 | put(AUTOENTER, TRUE);
|
---|
723 | if(start_browser_initial == null) {
|
---|
724 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
725 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
726 | }
|
---|
727 | put(STARTBROWSER, FALSE);
|
---|
728 | save();
|
---|
729 | }
|
---|
730 |
|
---|
731 | private void debug(String message) {
|
---|
732 | ///ystem.err.println(message);
|
---|
733 | }
|
---|
734 |
|
---|
735 | private void save() {
|
---|
736 | //debug("Save: " + llssite_cfg.getAbsolutePath());
|
---|
737 | debug("Save: " + configFile.getAbsolutePath());
|
---|
738 | try {
|
---|
739 | //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false));
|
---|
740 | BufferedWriter out = new BufferedWriter(new FileWriter(configFile, false));
|
---|
741 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
742 | String key = (String) keys.next();
|
---|
743 | String value = (String) get(key);
|
---|
744 | out.write(key, 0, key.length());
|
---|
745 | if(value != null) {
|
---|
746 | out.write('=');
|
---|
747 |
|
---|
748 | // if the server is using llssite.cfg, don't overwrite its default
|
---|
749 | // autoenter and startbrowser values
|
---|
750 | if(configFile == llssite_cfg && (key == AUTOENTER || key == STARTBROWSER)) {
|
---|
751 | if(key == AUTOENTER) {
|
---|
752 | out.write(autoenter_initial, 0, autoenter_initial.length());
|
---|
753 | } else { // STARTBROWSER
|
---|
754 | out.write(start_browser_initial, 0, start_browser_initial.length());
|
---|
755 | }
|
---|
756 | } else {
|
---|
757 | out.write(value, 0, value.length());
|
---|
758 | }
|
---|
759 | }
|
---|
760 | out.newLine();
|
---|
761 | }
|
---|
762 | out.flush();
|
---|
763 | out.close();
|
---|
764 | }
|
---|
765 | catch (Exception error) {
|
---|
766 | error.printStackTrace();
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | private static void copyConfigFile(File source_cfg, File dest_cfg, boolean setToGliSiteDefaults) {
|
---|
771 | // source_cfg file should exist
|
---|
772 | // dest_cfg file should not yet exist
|
---|
773 | // If setToGliSiteDefaults is true, then GLIsite.cfg's default configuration
|
---|
774 | // is applied to concerned lines: autoenter=1, and startbrowser=0
|
---|
775 |
|
---|
776 | try {
|
---|
777 | BufferedReader in = new BufferedReader(new FileReader(source_cfg));
|
---|
778 | BufferedWriter out = new BufferedWriter(new FileWriter(dest_cfg, false));
|
---|
779 |
|
---|
780 | String line = null;
|
---|
781 | while((line = in.readLine()) != null) {
|
---|
782 |
|
---|
783 | if(setToGliSiteDefaults) {
|
---|
784 | if(line.startsWith(AUTOENTER)) {
|
---|
785 | line = AUTOENTER+"=1";
|
---|
786 | }
|
---|
787 | else if(line.startsWith(STARTBROWSER)) {
|
---|
788 | line = STARTBROWSER+"=0";
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | // write out the line
|
---|
793 | out.write(line + "\n");
|
---|
794 | }
|
---|
795 |
|
---|
796 | out.flush();
|
---|
797 | in.close();
|
---|
798 | out.close();
|
---|
799 | } catch(Exception e) {
|
---|
800 | System.err.println("Exception occurred when trying to copy the config file "
|
---|
801 | + source_cfg.getName() + " to " + dest_cfg.getName() + ": " + e);
|
---|
802 | e.printStackTrace();
|
---|
803 | }
|
---|
804 | }
|
---|
805 | }
|
---|
806 | }
|
---|