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 String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
|
---|
46 | static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
|
---|
47 | static final private String QUIT_COMMAND = "?a=config&cmd=kill";
|
---|
48 |
|
---|
49 | static private LLSSiteConfig llssite_cfg_file = null;
|
---|
50 | static private File local_library_server_file = null;
|
---|
51 |
|
---|
52 | static private boolean running = false;
|
---|
53 | static private String ID = "greenstone-server"; // a sort of process ID
|
---|
54 |
|
---|
55 | // Need to use sockets to tell the server program to terminate when on Linux
|
---|
56 | // The socket port number that we will use to communicate the termination
|
---|
57 | static private int port;
|
---|
58 |
|
---|
59 | // The server is persistent if it does not have to reload all the values
|
---|
60 | // over and over again each time. Tomcat is persistent and fastcgi is too,
|
---|
61 | // but the Apache webserver is not persistent by default.
|
---|
62 | // Change the initialisation of this value depending on whether fastcgi is
|
---|
63 | // on. At the moment, this does not apply to the Linux' local library server.
|
---|
64 | static private boolean isPersistentServer = Utility.isWindows();
|
---|
65 |
|
---|
66 | static public void addCollection(String collection_name)
|
---|
67 | {
|
---|
68 | if (isPersistentServer) {
|
---|
69 | config(ADD_COMMAND + collection_name);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | // Used to send messages to the local library
|
---|
75 | static private void config(String command)
|
---|
76 | {
|
---|
77 | if (!isPersistentServer) {
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | if (Configuration.library_url == null) {
|
---|
81 | System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | try {
|
---|
86 | URL url = new URL(Configuration.library_url.toString() + command);
|
---|
87 | HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
|
---|
88 |
|
---|
89 | // It's very important that we read the output of the command
|
---|
90 | // This ensures that the command has actually finished
|
---|
91 | // (The response code is returned immediately)
|
---|
92 | InputStream library_is = library_connection.getInputStream();
|
---|
93 | BufferedReader library_in = new BufferedReader(new InputStreamReader(library_is, "UTF-8"));
|
---|
94 | String library_output_line = library_in.readLine();
|
---|
95 | while (library_output_line != null) {
|
---|
96 | DebugStream.println("Local library server output: " + library_output_line);
|
---|
97 | library_output_line = library_in.readLine();
|
---|
98 | }
|
---|
99 | library_in.close();
|
---|
100 |
|
---|
101 | int response_code = library_connection.getResponseCode();
|
---|
102 | if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
|
---|
103 | DebugStream.println("200 - Complete.");
|
---|
104 | }
|
---|
105 | else {
|
---|
106 | DebugStream.println("404 - Failed.");
|
---|
107 | }
|
---|
108 | }
|
---|
109 | catch (Exception exception) {
|
---|
110 | DebugStream.printStackTrace(exception);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Used to send messages to the local server on Linux
|
---|
115 | static private boolean sendMessageToServer(String message) {
|
---|
116 | if(Utility.isWindows()) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if(port == -1) {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | try {
|
---|
125 | Socket clientSocket = new Socket("localhost", port);
|
---|
126 | Writer writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
---|
127 | writer.write(message);
|
---|
128 | writer.close();
|
---|
129 | writer = null;
|
---|
130 | } catch (Exception e) {
|
---|
131 | System.err.println("An exception occurred when trying to send the message: " + message
|
---|
132 | + "\nto the LocalLibraryServer.\n" + e);
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static public boolean isRunning()
|
---|
139 | {
|
---|
140 | if (!running) return false;
|
---|
141 | llssite_cfg_file.load(true);
|
---|
142 | if (llssite_cfg_file.getURL() == null) return false;
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | static public void releaseCollection(String collection_name)
|
---|
148 | {
|
---|
149 | if (isPersistentServer) {
|
---|
150 | config(RELEASE_COMMAND + collection_name);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | static public void start(String gsdl_path, String local_library_server_file_path)
|
---|
155 | {
|
---|
156 | // Check the local library server file (server.exe or gs2-server.sh) exists
|
---|
157 | local_library_server_file = new File(local_library_server_file_path);
|
---|
158 |
|
---|
159 | if (!local_library_server_file.exists()) {
|
---|
160 | DebugStream.println("No local library at given file path.");
|
---|
161 |
|
---|
162 | String defaultServerFilename = Utility.isWindows() ? "server.exe" : "gs2-server.sh";
|
---|
163 | local_library_server_file = new File(gsdl_path + defaultServerFilename);
|
---|
164 | if (!local_library_server_file.exists()) {
|
---|
165 | DebugStream.println("No local library at all.");
|
---|
166 | return;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | llssite_cfg_file = new LLSSiteConfig(local_library_server_file);
|
---|
170 |
|
---|
171 | // Spawn local library server process
|
---|
172 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
173 |
|
---|
174 | // Check if the server is already running
|
---|
175 | String url = llssite_cfg_file.getURL();
|
---|
176 | if (url != null) {
|
---|
177 | // If it is already running then set the Greenstone web server address and we're done
|
---|
178 | // E.g. if previously GLI was not properly shut down, the URL property (signifying
|
---|
179 | // the server is still running) would still be in the glisite_cfg file.
|
---|
180 | try {
|
---|
181 | Configuration.library_url = new URL(url);
|
---|
182 | running = true;
|
---|
183 |
|
---|
184 | // on Linux, run the server interface (will work with any server already running)
|
---|
185 | if(!Utility.isWindows()) {
|
---|
186 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
187 | }
|
---|
188 | return;
|
---|
189 | }
|
---|
190 | catch (MalformedURLException exception) {
|
---|
191 | DebugStream.printStackTrace(exception);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | // Configure the server for immediate entry
|
---|
196 | //llssite_cfg_file.set();
|
---|
197 |
|
---|
198 | // Spawn local library server process
|
---|
199 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
200 |
|
---|
201 | // Wait until program has started
|
---|
202 | testServerRunning(); // will set running = true when the server is up and running successfully
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | static public void stop()
|
---|
207 | {
|
---|
208 | if (running == false) {
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | // Send the command for it to exit.
|
---|
213 | if (isPersistentServer) {
|
---|
214 | config(QUIT_COMMAND);
|
---|
215 | } else {
|
---|
216 | if(sendMessageToServer("QUIT")) {
|
---|
217 | Gatherer.terminateApplication(ID);
|
---|
218 | } else {
|
---|
219 | System.err.println("Unable to stop the server, since there's no communication port to send the quit msg over."
|
---|
220 | + "\nPlease stop the local Greenstone server manually.");
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | // Wait until program has stopped, by reloading and checking the URL field
|
---|
225 | llssite_cfg_file.load(false);
|
---|
226 | int attempt_count = 0;
|
---|
227 | while (llssite_cfg_file.getURL() != null) {
|
---|
228 | new OneSecondWait(); // Wait one second (give or take)
|
---|
229 | llssite_cfg_file.load(false);
|
---|
230 | attempt_count++;
|
---|
231 |
|
---|
232 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
233 | if (attempt_count == 60) {
|
---|
234 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
235 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
236 | return;
|
---|
237 | }
|
---|
238 | attempt_count = 0;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | // Restore the llssite_cfg.
|
---|
243 | llssite_cfg_file.restore();
|
---|
244 |
|
---|
245 | // If the local server is still running then our changed values will get overwritten.
|
---|
246 | if (llssite_cfg_file.getURL() != null) {
|
---|
247 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
248 | }
|
---|
249 |
|
---|
250 | running = false;
|
---|
251 | }
|
---|
252 |
|
---|
253 | static private String getExtraLaunchArguments(LLSSiteConfig site_cfg_file) {
|
---|
254 | String args = " " + site_cfg_file.getSiteConfigFilename();
|
---|
255 |
|
---|
256 | if(Utility.isWindows()) {
|
---|
257 | return args;
|
---|
258 | }
|
---|
259 |
|
---|
260 | // Else, when running the Local Library Server on Linux, need to provide a port argument
|
---|
261 | try {
|
---|
262 | PortFinder portFinder = new PortFinder(50100, 100);
|
---|
263 | port = portFinder.findPortInRange();
|
---|
264 | } catch(Exception e) {
|
---|
265 | System.err.println("Exception when trying to find an available port: " + e);
|
---|
266 | port = -1;
|
---|
267 | }
|
---|
268 |
|
---|
269 | return args + " --quitport=" + port;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | // This method first tests whether there is a URL in the llssite_cfg_file
|
---|
274 | // and after that appears, it tests whether the URL is functional.
|
---|
275 | static private void testServerRunning() {
|
---|
276 | // Wait until program has started, by reloading and checking the URL field
|
---|
277 | llssite_cfg_file.load(false);
|
---|
278 | int attempt_count = 0;
|
---|
279 | while (llssite_cfg_file.getURL() == null) {
|
---|
280 | new OneSecondWait(); // Wait one second (give or take)
|
---|
281 | llssite_cfg_file.load(false);
|
---|
282 | attempt_count++;
|
---|
283 |
|
---|
284 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
285 | if (attempt_count == 60) {
|
---|
286 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
287 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
288 | return;
|
---|
289 | }
|
---|
290 | attempt_count = 0;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | // Ta-da. Now the url should be available
|
---|
295 | try {
|
---|
296 | Configuration.library_url = new URL(llssite_cfg_file.getURL());
|
---|
297 | }
|
---|
298 | catch (MalformedURLException exception) {
|
---|
299 | DebugStream.printStackTrace(exception);
|
---|
300 | }
|
---|
301 |
|
---|
302 | // A quick test involves opening a connection to get the home page for this collection
|
---|
303 | try {
|
---|
304 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url + "'");
|
---|
305 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
306 | connection.getContent();
|
---|
307 | }
|
---|
308 | catch (IOException bad_url_connection) {
|
---|
309 | try {
|
---|
310 | // If this fails then we try changing the url to be localhost
|
---|
311 | Configuration.library_url = new URL(llssite_cfg_file.getLocalHostURL());
|
---|
312 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
313 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
314 | connection.getContent();
|
---|
315 |
|
---|
316 | }
|
---|
317 | catch (IOException worse_url_connection) {
|
---|
318 | DebugStream.println("Can't connect to server on either address.");
|
---|
319 | Configuration.library_url = null;
|
---|
320 | return;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | running = true;
|
---|
325 | }
|
---|
326 |
|
---|
327 | static public void checkServerRunning() {
|
---|
328 | if (!running) return; // don't worry about it if its not supposed to be running
|
---|
329 | llssite_cfg_file.load(true); // don't force reload, load only if modified
|
---|
330 |
|
---|
331 | String url = llssite_cfg_file.getURL();
|
---|
332 | if(url != null) {
|
---|
333 | try {
|
---|
334 | Configuration.library_url = new URL(url);
|
---|
335 | }
|
---|
336 | catch (MalformedURLException exception) {
|
---|
337 | DebugStream.printStackTrace(exception);
|
---|
338 | }
|
---|
339 | } else {
|
---|
340 | // need to restart the server again
|
---|
341 | llssite_cfg_file.set();
|
---|
342 |
|
---|
343 | // Spawn local library server process
|
---|
344 | String local_library_server_command = local_library_server_file.getAbsolutePath() + getExtraLaunchArguments(llssite_cfg_file);
|
---|
345 | running = false;
|
---|
346 | Gatherer.spawnApplication(local_library_server_command, ID);
|
---|
347 | testServerRunning(); // don't return until the webserver is up and running
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | static private class OneSecondWait
|
---|
352 | {
|
---|
353 | public OneSecondWait()
|
---|
354 | {
|
---|
355 | synchronized(this) {
|
---|
356 | try {
|
---|
357 | wait(1000);
|
---|
358 | }
|
---|
359 | catch (InterruptedException exception) {
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | static public class LLSSiteConfig
|
---|
367 | extends LinkedHashMap {
|
---|
368 | private File llssite_cfg;
|
---|
369 | private File glisite_cfg;
|
---|
370 | private String autoenter_initial;
|
---|
371 | private String start_browser_initial;
|
---|
372 |
|
---|
373 | private long lastModified = 0;
|
---|
374 |
|
---|
375 | static final private String AUTOENTER = "autoenter";
|
---|
376 | static final private String COLON = ":";
|
---|
377 | static final private String ENTERLIB = "enterlib";
|
---|
378 | static final private String FALSE = "0";
|
---|
379 | static final private String GLISITE_CFG = "glisite.cfg";
|
---|
380 | static final private String GSDL = "greenstone"; // httpprefix is no longer /gsdl but /greenstone
|
---|
381 | static final private String LLSSITE_CFG = "llssite.cfg";
|
---|
382 | static final private String LOCAL_HOST = "http://localhost";
|
---|
383 | static final private String PORTNUMBER = "portnumber";
|
---|
384 | static final private String SEPARATOR = "/";
|
---|
385 | static final private String SPECIFIC_CONFIG = "--config=";
|
---|
386 | static final private String STARTBROWSER = "start_browser";
|
---|
387 | static final private String TRUE = "1";
|
---|
388 | static final private String URL = "url";
|
---|
389 |
|
---|
390 | public LLSSiteConfig(File server_exe) {
|
---|
391 | debug("New LLSSiteConfig for: " + server_exe.getAbsolutePath());
|
---|
392 |
|
---|
393 | llssite_cfg = new File(server_exe.getParentFile(), LLSSITE_CFG);
|
---|
394 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
395 |
|
---|
396 | File configFile = null;
|
---|
397 | if(glisite_cfg.exists()) {
|
---|
398 | configFile = glisite_cfg;
|
---|
399 | } else if(llssite_cfg.exists()) {
|
---|
400 | configFile = llssite_cfg;
|
---|
401 | } else {
|
---|
402 | debug("Neither the file glisite.cfg nor llssite.cfg can be found!");
|
---|
403 | }
|
---|
404 |
|
---|
405 | autoenter_initial = null;
|
---|
406 | start_browser_initial = null;
|
---|
407 | if(configFile != null) {
|
---|
408 | debug("Load: " + configFile.getAbsolutePath());
|
---|
409 | clear();
|
---|
410 | try {
|
---|
411 | BufferedReader in = new BufferedReader(new FileReader(configFile));
|
---|
412 | String line = null;
|
---|
413 | while((line = in.readLine()) != null) {
|
---|
414 | String key = null;
|
---|
415 | String value = null;
|
---|
416 | int index = -1;
|
---|
417 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
418 | key = line.substring(0, index);
|
---|
419 | value = line.substring(index + 1);
|
---|
420 | }
|
---|
421 | else {
|
---|
422 | key = line;
|
---|
423 | }
|
---|
424 | put(key, value);
|
---|
425 | }
|
---|
426 | in.close();
|
---|
427 | }
|
---|
428 | catch (Exception error) {
|
---|
429 | error.printStackTrace();
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | if(glisite_cfg.exists()) {
|
---|
434 | lastModified = glisite_cfg.lastModified();
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | public boolean exists() {
|
---|
439 | return llssite_cfg.exists();
|
---|
440 | }
|
---|
441 |
|
---|
442 | public String getLocalHostURL() {
|
---|
443 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
444 | url.append(COLON);
|
---|
445 | url.append((String)get(PORTNUMBER));
|
---|
446 | String enterlib = (String)get(ENTERLIB);
|
---|
447 | if(enterlib == null || enterlib.length() == 0) {
|
---|
448 | // Use the default /gsdl and hope for the best.
|
---|
449 | url.append(SEPARATOR);
|
---|
450 | url.append(GSDL);
|
---|
451 | }
|
---|
452 | else {
|
---|
453 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
454 | url.append(SEPARATOR);
|
---|
455 | }
|
---|
456 | url.append(enterlib);
|
---|
457 | }
|
---|
458 | enterlib = null;
|
---|
459 | debug("Found Local Library Address: " + url.toString());
|
---|
460 | return url.toString();
|
---|
461 | }
|
---|
462 |
|
---|
463 | public String getSiteConfigFilename() {
|
---|
464 | return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
|
---|
465 | }
|
---|
466 |
|
---|
467 | public String getURL() {
|
---|
468 | // URL is made from url and portnumber
|
---|
469 | String url = (String) get(URL);
|
---|
470 |
|
---|
471 | if(!Utility.isWindows()) {
|
---|
472 | return url;
|
---|
473 | }
|
---|
474 |
|
---|
475 | if(url != null) {
|
---|
476 | StringBuffer temp = new StringBuffer(url);
|
---|
477 | temp.append(COLON);
|
---|
478 | temp.append((String)get(PORTNUMBER));
|
---|
479 | String enterlib = (String)get(ENTERLIB);
|
---|
480 | if(enterlib == null || enterlib.length() == 0) {
|
---|
481 | // Use the default /gsdl and hope for the best.
|
---|
482 | temp.append(SEPARATOR);
|
---|
483 | temp.append(GSDL);
|
---|
484 | }
|
---|
485 | else {
|
---|
486 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
487 | temp.append(SEPARATOR);
|
---|
488 | }
|
---|
489 | temp.append(enterlib);
|
---|
490 | }
|
---|
491 | enterlib = null;
|
---|
492 | url = temp.toString();
|
---|
493 | }
|
---|
494 | debug("Found Local Library Address: " + url);
|
---|
495 | return url;
|
---|
496 | }
|
---|
497 |
|
---|
498 | public boolean isModified() {
|
---|
499 | return (lastModified != glisite_cfg.lastModified());
|
---|
500 | }
|
---|
501 |
|
---|
502 | public void load(boolean reloadOnlyIfModified) {
|
---|
503 |
|
---|
504 | if(isModified()) {
|
---|
505 | lastModified = glisite_cfg.lastModified();
|
---|
506 | } else if(reloadOnlyIfModified) {
|
---|
507 | return; // asked to reload only if modified. Don't reload since not modified
|
---|
508 | }
|
---|
509 |
|
---|
510 | if(glisite_cfg.exists()) {
|
---|
511 | debug("Load: " + glisite_cfg.getAbsolutePath());
|
---|
512 | clear();
|
---|
513 | try {
|
---|
514 | BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
|
---|
515 | String line = null;
|
---|
516 | while((line = in.readLine()) != null) {
|
---|
517 | String key = null;
|
---|
518 | String value = null;
|
---|
519 | int index = -1;
|
---|
520 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
521 | key = line.substring(0, index);
|
---|
522 | value = line.substring(index + 1);
|
---|
523 | }
|
---|
524 | else {
|
---|
525 | key = line;
|
---|
526 | }
|
---|
527 | put(key, value);
|
---|
528 | }
|
---|
529 | in.close();
|
---|
530 | }
|
---|
531 | catch (Exception error) {
|
---|
532 | error.printStackTrace();
|
---|
533 | }
|
---|
534 | }
|
---|
535 | else {
|
---|
536 | debug("No glisite.cfg file can be found!");
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
541 | public void restore() {
|
---|
542 | if(glisite_cfg != null) {
|
---|
543 | // Delete the file
|
---|
544 | glisite_cfg.delete();
|
---|
545 | }
|
---|
546 | else {
|
---|
547 | debug("Restore Initial Settings");
|
---|
548 | put(AUTOENTER, autoenter_initial);
|
---|
549 | put(STARTBROWSER, start_browser_initial);
|
---|
550 | remove(URL);
|
---|
551 | save();
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | public void set() {
|
---|
556 | debug("Set Session Settings");
|
---|
557 | if(autoenter_initial == null) {
|
---|
558 | autoenter_initial = (String) get(AUTOENTER);
|
---|
559 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
560 | }
|
---|
561 | put(AUTOENTER, TRUE);
|
---|
562 | if(start_browser_initial == null) {
|
---|
563 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
564 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
565 | }
|
---|
566 | put(STARTBROWSER, FALSE);
|
---|
567 | save();
|
---|
568 | }
|
---|
569 |
|
---|
570 | private void debug(String message) {
|
---|
571 | ///ystem.err.println(message);
|
---|
572 | }
|
---|
573 |
|
---|
574 | private void save() {
|
---|
575 | //debug("Save: " + llssite_cfg.getAbsolutePath());
|
---|
576 | debug("Save: " + glisite_cfg.getAbsolutePath());
|
---|
577 | try {
|
---|
578 | //BufferedWriter out = new BufferedWriter(new FileWriter(llssite_cfg, false));
|
---|
579 | BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
|
---|
580 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
581 | String key = (String) keys.next();
|
---|
582 | String value = (String) get(key);
|
---|
583 | out.write(key, 0, key.length());
|
---|
584 | if(value != null) {
|
---|
585 | out.write('=');
|
---|
586 | out.write(value, 0, value.length());
|
---|
587 | }
|
---|
588 | out.newLine();
|
---|
589 | }
|
---|
590 | out.flush();
|
---|
591 | out.close();
|
---|
592 | }
|
---|
593 | catch (Exception error) {
|
---|
594 | error.printStackTrace();
|
---|
595 | }
|
---|
596 | }
|
---|
597 | }
|
---|
598 | }
|
---|