source: main/trunk/gli/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java@ 21998

Last change on this file since 21998 was 21998, checked in by ak19, 14 years ago

Fix to recent commit: embedding path to server script in quotes broke on Linux. So now we do this only for Windows.

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