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

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

Part of changes to how GLI deals with an independently launched GSI which uses llssite.cfg (as opposed to one that GLI launches and which uses glisite.cfg). Already commited the corresponding changes made to Gatherer.java and PreviewButton.java

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