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

Last change on this file since 19758 was 19758, checked in by ak19, 15 years ago

Created new variable in Gatherer.java called isLocalLibrary that defaults to false and gets initialised upon calling LocalLibraryServer.start(). This variable is now also used in PreviewButton.java so that the Local Library Server is still working with a GSI interface launched either via GLI or independently, and still works with an external apache-web server.

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