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 | running = true;
|
---|
203 | return;
|
---|
204 | }
|
---|
205 |
|
---|
206 | // Spawn local library server process
|
---|
207 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
208 |
|
---|
209 | // Check if the server is already running
|
---|
210 | String url = llssite_cfg_file.getURL();
|
---|
211 | if (url != null) {
|
---|
212 | // If it is already running then set the Greenstone web server address and we're done
|
---|
213 | // E.g. if previously GLI was not properly shut down, the URL property (signifying
|
---|
214 | // the server is still running) would still be in the glisite_cfg file.
|
---|
215 | try {
|
---|
216 | Configuration.library_url = new URL(url);
|
---|
217 | running = true;
|
---|
218 |
|
---|
219 | // Run the server interface
|
---|
220 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
221 | return;
|
---|
222 | }
|
---|
223 | catch (MalformedURLException exception) {
|
---|
224 | DebugStream.printStackTrace(exception);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | // Configure the server for immediate entry
|
---|
229 | //llssite_cfg_file.set();
|
---|
230 |
|
---|
231 | // Spawn local library server process
|
---|
232 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
233 |
|
---|
234 | // Wait until program has started
|
---|
235 | try {
|
---|
236 | testServerRunning(); // will set running = true when the server is up and running successfully
|
---|
237 | } catch (IOException bad_url_connection) {
|
---|
238 | try {
|
---|
239 | // If this fails then we try changing the url to be localhost
|
---|
240 | Configuration.library_url = new URL(llssite_cfg_file.getLocalHostURL());
|
---|
241 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
242 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
243 | connection.getContent();
|
---|
244 | running = true;
|
---|
245 |
|
---|
246 | } catch (IOException worse_url_connection) {
|
---|
247 | DebugStream.println("Can't connect to server on either address.");
|
---|
248 | Configuration.library_url = null;
|
---|
249 | running = false;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | static public void stop()
|
---|
256 | {
|
---|
257 | if (!running) {
|
---|
258 | // also the case if the URL is pending in an independently launched GSI
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | // don't (can't) shutdown the GSI/server if it was launched independent of GLI
|
---|
263 | if(llssite_cfg_file.isIndependentGSI()) {
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | // Send the command for it to exit.
|
---|
268 | if (isPersistentServer) {
|
---|
269 | config(QUIT_COMMAND);
|
---|
270 | } else {
|
---|
271 | if(sendMessageToServer("QUIT")) {
|
---|
272 | Gatherer.terminateApplication(ID);
|
---|
273 | } else {
|
---|
274 | System.err.println("Unable to stop the server, since there's no communication port to send the quit msg over."
|
---|
275 | + "\nPlease stop the local Greenstone server manually.");
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Wait until program has stopped, by reloading and checking the URL field
|
---|
280 | llssite_cfg_file.load(false);
|
---|
281 | int attempt_count = 0;
|
---|
282 | String url = llssite_cfg_file.getURL();
|
---|
283 | while (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) { // if pending, the server is already stopped (not running)
|
---|
284 | new OneSecondWait(); // Wait one second (give or take)
|
---|
285 | llssite_cfg_file.load(false);
|
---|
286 | attempt_count++;
|
---|
287 |
|
---|
288 | // After waiting for the specified time, ask the user whether they want to wait for that long again
|
---|
289 | if (attempt_count == WAITING_TIME) {
|
---|
290 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut", Integer.toString(WAITING_TIME)),
|
---|
291 | Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
292 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
293 | return;
|
---|
294 | }
|
---|
295 | attempt_count = 0;
|
---|
296 | }
|
---|
297 | // read the url again to see if it's updated
|
---|
298 | url = llssite_cfg_file.getURL();
|
---|
299 | }
|
---|
300 |
|
---|
301 | // Restore the llssite_cfg.
|
---|
302 | llssite_cfg_file.restore();
|
---|
303 |
|
---|
304 | // If the local server is still running then our changed values will get overwritten.
|
---|
305 | url = llssite_cfg_file.getURL();
|
---|
306 | if (url != null && !url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
307 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
308 | }
|
---|
309 |
|
---|
310 | running = false;
|
---|
311 | }
|
---|
312 |
|
---|
313 | static private String getExtraLaunchArguments(LLSSiteConfig site_cfg_file) {
|
---|
314 | String args = " " + site_cfg_file.getSiteConfigFilename();
|
---|
315 |
|
---|
316 | if(Utility.isWindows()) {
|
---|
317 | return args;
|
---|
318 | }
|
---|
319 |
|
---|
320 | // Else, when running the Local Library Server on Linux, need to provide a port argument
|
---|
321 | try {
|
---|
322 | PortFinder portFinder = new PortFinder(50100, 100);
|
---|
323 | port = portFinder.findPortInRange(false); // silent mode
|
---|
324 | } catch(Exception e) {
|
---|
325 | System.err.println("Exception when trying to find an available port: " + e);
|
---|
326 | port = -1;
|
---|
327 | }
|
---|
328 |
|
---|
329 | return args + " --quitport=" + port;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | // This method first tests whether there is a URL in the llssite_cfg_file
|
---|
334 | // and after that appears, it tests whether the URL is functional.
|
---|
335 | static private void testServerRunning() throws IOException {
|
---|
336 | // Wait until program has started, by reloading and checking the URL field
|
---|
337 | llssite_cfg_file.load(false);
|
---|
338 | int attempt_count = 0;
|
---|
339 | while (llssite_cfg_file.getURL() == null) {
|
---|
340 | new OneSecondWait(); // Wait one second (give or take)
|
---|
341 | llssite_cfg_file.load(false);
|
---|
342 | attempt_count++;
|
---|
343 |
|
---|
344 | // After waiting for the specified time, ask the user whether they want to wait for that long again
|
---|
345 | if (attempt_count == WAITING_TIME) {
|
---|
346 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.StartUpTimeOut", Integer.toString(WAITING_TIME)),
|
---|
347 | Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
348 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
349 | return;
|
---|
350 | }
|
---|
351 | attempt_count = 0;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | // Ta-da. Now the url should be available
|
---|
356 | try {
|
---|
357 | Configuration.library_url = new URL(llssite_cfg_file.getURL());
|
---|
358 | }
|
---|
359 | catch (MalformedURLException exception) {
|
---|
360 | DebugStream.printStackTrace(exception);
|
---|
361 | }
|
---|
362 |
|
---|
363 | // A quick test involves opening a connection to get the home page for this collection
|
---|
364 | try {
|
---|
365 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url + "'");
|
---|
366 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
367 | connection.getContent();
|
---|
368 | running = true;
|
---|
369 | }
|
---|
370 | catch (IOException bad_url_connection) {
|
---|
371 | throw bad_url_connection;
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | }
|
---|
376 |
|
---|
377 | /** Returns true if we're waiting on the user to click on Enter Library button
|
---|
378 | * in an independently launched GSI (one not launched by GLI). */
|
---|
379 | static public boolean isURLPending() {
|
---|
380 |
|
---|
381 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
382 |
|
---|
383 | String url = llssite_cfg_file.getURL();
|
---|
384 |
|
---|
385 | if(url != null) {
|
---|
386 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
387 | running = false; // imagine if they restarted an external GSI
|
---|
388 | return true;
|
---|
389 | } else {
|
---|
390 | // a valid URL at last
|
---|
391 | try {
|
---|
392 | Configuration.library_url = new URL(url);
|
---|
393 | running = true;
|
---|
394 | }
|
---|
395 | catch (MalformedURLException exception) {
|
---|
396 | running = false;
|
---|
397 | exception.printStackTrace();
|
---|
398 | DebugStream.printStackTrace(exception);
|
---|
399 | }
|
---|
400 | return false;
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | // If either the URL is null--which means no independent GSI (Greenstone server interface
|
---|
405 | // app) was launched--or if the 'URL' doesn't say it's pending, then we're not waiting
|
---|
406 | return false;
|
---|
407 | }
|
---|
408 |
|
---|
409 | static public void checkServerRunning() {
|
---|
410 | if (!running) return; // don't worry about it if it's not supposed to be running
|
---|
411 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
412 |
|
---|
413 | String url = llssite_cfg_file.getURL();
|
---|
414 | if(url != null) {
|
---|
415 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
416 | running = false;
|
---|
417 | return;
|
---|
418 | }
|
---|
419 |
|
---|
420 | // else, valid URL:
|
---|
421 | try {
|
---|
422 | Configuration.library_url = new URL(url);
|
---|
423 | running = true;
|
---|
424 | // Run the server interface
|
---|
425 | //Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
426 | }
|
---|
427 | catch (MalformedURLException exception) {
|
---|
428 | running = false;
|
---|
429 | DebugStream.printStackTrace(exception);
|
---|
430 | exception.printStackTrace();
|
---|
431 | }
|
---|
432 | }
|
---|
433 | else { // NO URL in current ConfigFile, check the other configfile for a URL
|
---|
434 | // to see if the server was restarted using that file.
|
---|
435 | // Otherwise need to restart the server again with GLIsite.cfg
|
---|
436 | llssite_cfg_file.save(); // save the configfile, because we may be reloading another
|
---|
437 |
|
---|
438 | if(llssite_cfg_file.usingLLS_configFile()) { // if a GSI is already using llssite_cfg, this would have loaded it in
|
---|
439 | url = llssite_cfg_file.getURL(); //if(isURLPending()) {
|
---|
440 | if(url.equals(LLSSiteConfig.URL_PENDING)) {
|
---|
441 | running = false;
|
---|
442 | } else {
|
---|
443 | running = true;
|
---|
444 | }
|
---|
445 | return; // don't need to launch the server, one has been independently launched
|
---|
446 | } else {
|
---|
447 | // else we try using the glisite configfile
|
---|
448 | llssite_cfg_file.useGLISiteCfg(local_library_server_file);
|
---|
449 | //llssite_cfg_file.set();
|
---|
450 |
|
---|
451 | // Spawn local library server process
|
---|
452 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
453 | running = false;
|
---|
454 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
455 | try {
|
---|
456 | testServerRunning(); // don't return until the webserver is up and running
|
---|
457 | } catch (IOException bad_url_connection) {
|
---|
458 | DebugStream.println("Can't connect to server on address " + Configuration.library_url);
|
---|
459 | running = false;
|
---|
460 | }
|
---|
461 | }
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | static private class OneSecondWait
|
---|
466 | {
|
---|
467 | public OneSecondWait()
|
---|
468 | {
|
---|
469 | synchronized(this) {
|
---|
470 | try {
|
---|
471 | wait(1000);
|
---|
472 | }
|
---|
473 | catch (InterruptedException exception) {
|
---|
474 | }
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | static public class LLSSiteConfig
|
---|
481 | extends LinkedHashMap
|
---|
482 | {
|
---|
483 | private File configFile;
|
---|
484 |
|
---|
485 | private File llssite_cfg;
|
---|
486 | private File glisite_cfg;
|
---|
487 | private String autoenter_initial;
|
---|
488 | private String start_browser_initial;
|
---|
489 |
|
---|
490 | private long lastModified = 0;
|
---|
491 |
|
---|
492 | static final private String AUTOENTER = "autoenter";
|
---|
493 | static final private String COLON = ":";
|
---|
494 | static final private String ENTERLIB = "enterlib";
|
---|
495 | static final private String FALSE = "0";
|
---|
496 | static final private String GLISITE_CFG = "glisite.cfg";
|
---|
497 | static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone
|
---|
498 | static final private String LLSSITE_CFG = "llssite.cfg";
|
---|
499 | static final private String LOCAL_HOST = "http://localhost";
|
---|
500 | static final private String PORTNUMBER = "portnumber";
|
---|
501 | static final private String SEPARATOR = "/";
|
---|
502 | static final private String SPECIFIC_CONFIG = "--config=";
|
---|
503 | static final private String STARTBROWSER = "start_browser";
|
---|
504 | static final private String TRUE = "1";
|
---|
505 | static final private String URL = "url";
|
---|
506 |
|
---|
507 | static final public String URL_PENDING = "URL_pending";
|
---|
508 |
|
---|
509 |
|
---|
510 | public LLSSiteConfig(File server_exe) {
|
---|
511 | debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath());
|
---|
512 |
|
---|
513 | llssite_cfg = new File(server_exe.getParentFile(), LLSSITE_CFG);
|
---|
514 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
515 |
|
---|
516 | configFile = null;
|
---|
517 |
|
---|
518 | autoenter_initial = null;
|
---|
519 | start_browser_initial = null;
|
---|
520 |
|
---|
521 | // first test if server was started independently of GLI
|
---|
522 | // if so, the config file we'd be using would be llssite.cfg
|
---|
523 | if(!usingLLS_configFile()) { // if we were using llssite_cfg, this would have loaded it in
|
---|
524 | // else we try using the glisite configfile
|
---|
525 | useGLISiteCfg(server_exe);
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Tries to get a glisite.cfg file and then loads it, setting it as the configFile */
|
---|
530 | public void useGLISiteCfg(File server_exe)
|
---|
531 | {
|
---|
532 | if(!glisite_cfg.exists()) { // create it from the templates or the llssite.cfg file
|
---|
533 |
|
---|
534 | File llssite_cfg_in = new File(server_exe.getParentFile(), LLSSITE_CFG+".in");
|
---|
535 | File glisite_cfg_in = new File(server_exe.getParentFile(), GLISITE_CFG+".in");
|
---|
536 |
|
---|
537 | // need to generate glisite_cfg from glisite_cfg_in, llssite_cfg or llssite.cfg.in
|
---|
538 | if(glisite_cfg_in.exists()) {
|
---|
539 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
540 | }
|
---|
541 | else if(llssite_cfg_in.exists()) {
|
---|
542 | copyConfigFile(llssite_cfg_in, glisite_cfg_in, true); // adjust for glisite.cfg
|
---|
543 | copyConfigFile(glisite_cfg_in, glisite_cfg, false);
|
---|
544 | }
|
---|
545 | else if(llssite_cfg.exists()) {
|
---|
546 | copyConfigFile(llssite_cfg, glisite_cfg, true); // adjust for glisite.cfg
|
---|
547 | }
|
---|
548 | else {
|
---|
549 | debug("Neither the file glisite.cfg nor llssite.cfg can be found!");
|
---|
550 | }
|
---|
551 | }
|
---|
552 | // use the config file now
|
---|
553 | if(glisite_cfg.exists()) {
|
---|
554 | configFile = glisite_cfg;
|
---|
555 | load(false); // force reload
|
---|
556 | lastModified = configFile.lastModified();
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | /** Tests whether the server interface is up, running independently of GLI
|
---|
561 | * If so, we don't need to launch the server interface.
|
---|
562 | * The server interface may not have started up the server itself though
|
---|
563 | * (in which case the server URL would be URL_PENDING).
|
---|
564 | * This method returns true if the server interface has already started
|
---|
565 | * and, if so, it would have loaded in the llssite_cfg configFile.
|
---|
566 | */
|
---|
567 | public boolean usingLLS_configFile() {
|
---|
568 | if(!llssite_cfg.exists()) {
|
---|
569 | return false;
|
---|
570 | }
|
---|
571 |
|
---|
572 | // Now to check if the configfile contains the URL line
|
---|
573 | configFile = llssite_cfg;
|
---|
574 | load(false); // force load
|
---|
575 |
|
---|
576 | if(getURL() == null) {
|
---|
577 | configFile = null;
|
---|
578 | clear(); // we're not using llssite_cfg, so clear the values we just read
|
---|
579 | return false;
|
---|
580 | }
|
---|
581 |
|
---|
582 | lastModified = configFile.lastModified();
|
---|
583 | return true;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /** @return true if GSI was started up independently and outside of GLI.
|
---|
587 | * In such a case, GLI would be using llssite_cfg. */
|
---|
588 | public boolean isIndependentGSI() {
|
---|
589 | return (configFile == llssite_cfg);
|
---|
590 | }
|
---|
591 |
|
---|
592 | public boolean exists() {
|
---|
593 | return configFile.exists();
|
---|
594 | }
|
---|
595 |
|
---|
596 | public String getLocalHostURL() {
|
---|
597 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
598 | url.append(COLON);
|
---|
599 | url.append((String)get(PORTNUMBER));
|
---|
600 | String enterlib = (String)get(ENTERLIB);
|
---|
601 | if(enterlib == null || enterlib.length() == 0) {
|
---|
602 | // Use the default /gsdl and hope for the best.
|
---|
603 | url.append(SEPARATOR);
|
---|
604 | url.append(GSDL);
|
---|
605 | }
|
---|
606 | else {
|
---|
607 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
608 | url.append(SEPARATOR);
|
---|
609 | }
|
---|
610 | url.append(enterlib);
|
---|
611 | }
|
---|
612 | enterlib = null;
|
---|
613 | debug("Found Local Library Address: " + url.toString());
|
---|
614 | return url.toString();
|
---|
615 | }
|
---|
616 |
|
---|
617 | /** @return the cmd-line parameter for the configfile used to launch
|
---|
618 | * the server through GLI: --config <glisite.cfg/llssite.cfg file path>. */
|
---|
619 | public String getSiteConfigFilename() {
|
---|
620 | return SPECIFIC_CONFIG + configFile.getAbsolutePath();
|
---|
621 | }
|
---|
622 |
|
---|
623 | public String getURL() {
|
---|
624 | // URL is made from url and portnumber
|
---|
625 | String url = (String) get(URL);
|
---|
626 |
|
---|
627 | // server interface is already up, independent of GLI
|
---|
628 | // but it has not started the server (hence URL is pending)
|
---|
629 | if(url != null && url.equals(URL_PENDING)) {
|
---|
630 | return url;
|
---|
631 | }
|
---|
632 |
|
---|
633 | if(!Utility.isWindows()) {
|
---|
634 | return url;
|
---|
635 | }
|
---|
636 |
|
---|
637 | if(url != null) {
|
---|
638 | StringBuffer temp = new StringBuffer(url);
|
---|
639 | temp.append(COLON);
|
---|
640 | temp.append((String)get(PORTNUMBER));
|
---|
641 | String enterlib = (String)get(ENTERLIB);
|
---|
642 | if(enterlib == null || enterlib.length() == 0) {
|
---|
643 | // Use the default /greenstone prefix and hope for the best.
|
---|
644 | temp.append(SEPARATOR);
|
---|
645 | temp.append(GSDL);
|
---|
646 | }
|
---|
647 | else {
|
---|
648 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
649 | temp.append(SEPARATOR);
|
---|
650 | }
|
---|
651 | temp.append(enterlib);
|
---|
652 | }
|
---|
653 | enterlib = null;
|
---|
654 | url = temp.toString();
|
---|
655 | }
|
---|
656 | debug("Found Local Library Address: " + url);
|
---|
657 | return url;
|
---|
658 | }
|
---|
659 |
|
---|
660 | public boolean isModified() {
|
---|
661 | return (lastModified != configFile.lastModified());
|
---|
662 | }
|
---|
663 |
|
---|
664 | public void load(boolean reloadOnlyIfModified) {
|
---|
665 |
|
---|
666 | if(configFile == null) {
|
---|
667 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
668 | }
|
---|
669 |
|
---|
670 | if(isModified()) {
|
---|
671 | lastModified = configFile.lastModified();
|
---|
672 | } else if(reloadOnlyIfModified) {
|
---|
673 | return; // asked to reload only if modified. Don't reload since not modified
|
---|
674 | }
|
---|
675 |
|
---|
676 | if(configFile.exists()) {
|
---|
677 | debug("Load: " + configFile.getAbsolutePath());
|
---|
678 | clear();
|
---|
679 | try {
|
---|
680 | BufferedReader in = new BufferedReader(new FileReader(configFile));
|
---|
681 | String line = null;
|
---|
682 | while((line = in.readLine()) != null) {
|
---|
683 | String key = null;
|
---|
684 | String value = null;
|
---|
685 | int index = -1;
|
---|
686 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
687 | key = line.substring(0, index);
|
---|
688 | value = line.substring(index + 1);
|
---|
689 | }
|
---|
690 | else {
|
---|
691 | key = line;
|
---|
692 | }
|
---|
693 | put(key, value);
|
---|
694 | }
|
---|
695 | in.close();
|
---|
696 | }
|
---|
697 | catch (Exception error) {
|
---|
698 | error.printStackTrace();
|
---|
699 | }
|
---|
700 | }
|
---|
701 | else {
|
---|
702 | debug(configFile.getAbsolutePath() + " cannot be found!");
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
707 | public void restore() {
|
---|
708 | if(configFile != null) {
|
---|
709 | // Delete the file
|
---|
710 | configFile.delete();
|
---|
711 | }
|
---|
712 | else {
|
---|
713 | debug("Restore Initial Settings");
|
---|
714 | put(AUTOENTER, autoenter_initial);
|
---|
715 | put(STARTBROWSER, start_browser_initial);
|
---|
716 | remove(URL);
|
---|
717 | save();
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | public void set() {
|
---|
722 | debug("Set Session Settings");
|
---|
723 | if(autoenter_initial == null) {
|
---|
724 | autoenter_initial = (String) get(AUTOENTER);
|
---|
725 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
726 | }
|
---|
727 | put(AUTOENTER, TRUE);
|
---|
728 | if(start_browser_initial == null) {
|
---|
729 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
730 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
731 | }
|
---|
732 | put(STARTBROWSER, FALSE);
|
---|
733 | save();
|
---|
734 | }
|
---|
735 |
|
---|
736 | private void debug(String message) {
|
---|
737 | ///ystem.err.println(message);
|
---|
738 | }
|
---|
739 |
|
---|
740 | private void save() {
|
---|
741 | //debug("Save: " + llssite_cfg.getAbsolutePath());
|
---|
742 | debug("Save: " + configFile.getAbsolutePath());
|
---|
743 | try {
|
---|
744 | //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false));
|
---|
745 | BufferedWriter out = new BufferedWriter(new FileWriter(configFile, false));
|
---|
746 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
747 | String key = (String) keys.next();
|
---|
748 | String value = (String) get(key);
|
---|
749 | out.write(key, 0, key.length());
|
---|
750 | if(value != null) {
|
---|
751 | out.write('=');
|
---|
752 |
|
---|
753 | // if the server is using llssite.cfg, don't overwrite its default
|
---|
754 | // autoenter and startbrowser values
|
---|
755 | if(configFile == llssite_cfg && (key == AUTOENTER || key == STARTBROWSER)) {
|
---|
756 | if(key == AUTOENTER) {
|
---|
757 | out.write(autoenter_initial, 0, autoenter_initial.length());
|
---|
758 | } else { // STARTBROWSER
|
---|
759 | out.write(start_browser_initial, 0, start_browser_initial.length());
|
---|
760 | }
|
---|
761 | } else {
|
---|
762 | out.write(value, 0, value.length());
|
---|
763 | }
|
---|
764 | }
|
---|
765 | out.newLine();
|
---|
766 | }
|
---|
767 | out.flush();
|
---|
768 | out.close();
|
---|
769 | }
|
---|
770 | catch (Exception error) {
|
---|
771 | error.printStackTrace();
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | private static void copyConfigFile(File source_cfg, File dest_cfg, boolean setToGliSiteDefaults) {
|
---|
776 | // source_cfg file should exist
|
---|
777 | // dest_cfg file should not yet exist
|
---|
778 | // If setToGliSiteDefaults is true, then GLIsite.cfg's default configuration
|
---|
779 | // is applied to concerned lines: autoenter=1, and startbrowser=0
|
---|
780 |
|
---|
781 | try {
|
---|
782 | BufferedReader in = new BufferedReader(new FileReader(source_cfg));
|
---|
783 | BufferedWriter out = new BufferedWriter(new FileWriter(dest_cfg, false));
|
---|
784 |
|
---|
785 | String line = null;
|
---|
786 | while((line = in.readLine()) != null) {
|
---|
787 |
|
---|
788 | if(setToGliSiteDefaults) {
|
---|
789 | if(line.startsWith(AUTOENTER)) {
|
---|
790 | line = AUTOENTER+"=1";
|
---|
791 | }
|
---|
792 | else if(line.startsWith(STARTBROWSER)) {
|
---|
793 | line = STARTBROWSER+"=0";
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | // write out the line
|
---|
798 | out.write(line + "\n");
|
---|
799 | }
|
---|
800 |
|
---|
801 | out.flush();
|
---|
802 | in.close();
|
---|
803 | out.close();
|
---|
804 | } catch(Exception e) {
|
---|
805 | System.err.println("Exception occurred when trying to copy the config file "
|
---|
806 | + source_cfg.getName() + " to " + dest_cfg.getName() + ": " + e);
|
---|
807 | e.printStackTrace();
|
---|
808 | }
|
---|
809 | }
|
---|
810 | }
|
---|
811 | }
|
---|