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

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

Now first checks whether glisite.cfg exists and loads that, if not it looks for gsdlsite.cfg and loads that. Previously it would only look for and try to load gsdlsite.cfg, which meant that it ignored any server configuration values I changed in glisite.cfg (like autoenter and startbrowser).

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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;
39
40
41public class LocalLibraryServer
42{
43 static final private String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
44 static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
45 static final private String QUIT_COMMAND = "?a=config&cmd=kill";
46
47 static private GSDLSiteConfig gsdlsite_cfg_file = null;
48 static private File local_library_server_file = null;
49
50 static private boolean running = false;
51
52 static public void addCollection(String collection_name)
53 {
54 config(ADD_COMMAND + collection_name);
55 }
56
57
58 // Used to send messages to the local library
59 static private void config(String command)
60 {
61 if (Configuration.library_url == null) {
62 System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
63 return;
64 }
65
66 try {
67 URL url = new URL(Configuration.library_url.toString() + command);
68 HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
69
70 // It's very important that we read the output of the command
71 // This ensures that the command has actually finished
72 // (The response code is returned immediately)
73 InputStream library_is = library_connection.getInputStream();
74 BufferedReader library_in = new BufferedReader(new InputStreamReader(library_is, "UTF-8"));
75 String library_output_line = library_in.readLine();
76 while (library_output_line != null) {
77 DebugStream.println("Local library server output: " + library_output_line);
78 library_output_line = library_in.readLine();
79 }
80 library_in.close();
81
82 int response_code = library_connection.getResponseCode();
83 if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
84 DebugStream.println("200 - Complete.");
85 }
86 else {
87 DebugStream.println("404 - Failed.");
88 }
89 }
90 catch (Exception exception) {
91 DebugStream.printStackTrace(exception);
92 }
93 }
94
95
96 static public boolean isRunning()
97 {
98 if (!running) return false;
99 gsdlsite_cfg_file.load();
100 if (gsdlsite_cfg_file.getURL() == null) return false;
101 return true;
102 }
103
104
105 static public void releaseCollection(String collection_name)
106 {
107 config(RELEASE_COMMAND + collection_name);
108 }
109
110
111 static public void start(String gsdl_path, String local_library_server_file_path)
112 {
113 // Check the local library server.exe file exists
114 local_library_server_file = new File(local_library_server_file_path);
115 if (!local_library_server_file.exists()) {
116 DebugStream.println("No local library at given file path.");
117
118 local_library_server_file = new File(gsdl_path + "server.exe");
119 if (!local_library_server_file.exists()) {
120 DebugStream.println("No local library at all.");
121 return;
122 }
123 }
124
125 // Check if the server is already running
126 gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
127 String url = gsdlsite_cfg_file.getURL();
128 if (url != null) {
129 // If it is already running then set the Greenstone web server address and we're done
130 try {
131 Configuration.library_url = new URL(url);
132 running = true;
133 return;
134 }
135 catch (MalformedURLException exception) {
136 DebugStream.printStackTrace(exception);
137 }
138 }
139
140 // Configure the server for immediate entry
141 //gsdlsite_cfg_file.set();
142
143 // Spawn local library server process
144 String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
145 Gatherer.spawnApplication(local_library_server_command);
146
147 // Wait until program has started, by reloading and checking the URL field
148 gsdlsite_cfg_file.load();
149 int attempt_count = 0;
150 while (gsdlsite_cfg_file.getURL() == null) {
151 new OneSecondWait(); // Wait one second (give or take)
152 gsdlsite_cfg_file.load();
153 attempt_count++;
154
155 // After waiting a minute ask the user whether they want to wait another minute
156 if (attempt_count == 60) {
157 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
158 if (try_again == JOptionPane.NO_OPTION) {
159 return;
160 }
161 attempt_count = 0;
162 }
163 }
164
165 // Ta-da. Now the url should be available
166 try {
167 Configuration.library_url = new URL(gsdlsite_cfg_file.getURL());
168 }
169 catch (MalformedURLException exception) {
170 DebugStream.printStackTrace(exception);
171 }
172
173 // A quick test involves opening a connection to get the home page for this collection
174 try {
175 DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
176 URLConnection connection = Configuration.library_url.openConnection();
177 connection.getContent();
178 }
179 catch (IOException bad_url_connection) {
180 try {
181 // If this fails then we try changing the url to be localhost
182 Configuration.library_url = new URL(gsdlsite_cfg_file.getLocalHostURL());
183 DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
184 URLConnection connection = Configuration.library_url.openConnection();
185 connection.getContent();
186 }
187 catch (IOException worse_url_connection) {
188 DebugStream.println("Can't connect to server on either address.");
189 Configuration.library_url = null;
190 return;
191 }
192 }
193
194 running = true;
195 }
196
197
198 static public void stop()
199 {
200 if (running == false) {
201 return;
202 }
203
204 // Send the command for it to exit.
205 config(QUIT_COMMAND);
206
207 // Wait until program has stopped, by reloading and checking the URL field
208 gsdlsite_cfg_file.load();
209 int attempt_count = 0;
210 while (gsdlsite_cfg_file.getURL() != null) {
211 new OneSecondWait(); // Wait one second (give or take)
212 gsdlsite_cfg_file.load();
213 attempt_count++;
214
215 // After waiting a minute ask the user whether they want to wait another minute
216 if (attempt_count == 60) {
217 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
218 if (try_again == JOptionPane.NO_OPTION) {
219 return;
220 }
221 attempt_count = 0;
222 }
223 }
224
225 // Restore the gsdlsite_cfg.
226 gsdlsite_cfg_file.restore();
227
228 // If the local server is still running then our changed values will get overwritten.
229 if (gsdlsite_cfg_file.getURL() != null) {
230 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
231 }
232
233 running = false;
234 }
235
236 static public void checkServerRunning() {
237 if (!running) return; // don't worry about it if its not supposed to be running
238 gsdlsite_cfg_file.load();
239 if (gsdlsite_cfg_file.getURL() == null) {
240 // need to restart the server again
241 gsdlsite_cfg_file.set();
242
243 // Spawn local library server process
244 String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
245 Gatherer.spawnApplication(local_library_server_command);
246
247 }
248 }
249 static private class OneSecondWait
250 {
251 public OneSecondWait()
252 {
253 synchronized(this) {
254 try {
255 wait(1000);
256 }
257 catch (InterruptedException exception) {
258 }
259 }
260 }
261 }
262
263
264 static public class GSDLSiteConfig
265 extends LinkedHashMap {
266 private File gsdlsite_cfg;
267 private File glisite_cfg;
268 private String autoenter_initial;
269 private String start_browser_initial;
270
271 static final private String AUTOENTER = "autoenter";
272 static final private String COLON = ":";
273 static final private String ENTERLIB = "enterlib";
274 static final private String FALSE = "0";
275 static final private String GLISITE_CFG = "glisite.cfg";
276 static final private String GSDL = "gsdl";
277 static final private String GSDLSITE_CFG = "gsdlsite.cfg";
278 static final private String LOCAL_HOST = "http://localhost";
279 static final private String PORTNUMBER = "portnumber";
280 static final private String SEPARATOR = "/";
281 static final private String SPECIFIC_CONFIG = "--config=";
282 static final private String STARTBROWSER = "start_browser";
283 static final private String TRUE = "1";
284 static final private String URL = "url";
285
286 public GSDLSiteConfig(File server_exe) {
287 debug("New GSDLSiteConfig for: " + server_exe.getAbsolutePath());
288
289 gsdlsite_cfg = new File(server_exe.getParentFile(), GSDLSITE_CFG);
290 glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
291
292 File configFile = null;
293 if(glisite_cfg.exists()) {
294 configFile = glisite_cfg;
295 } else if(gsdlsite_cfg.exists()) {
296 configFile = gsdlsite_cfg;
297 } else {
298 debug("Neither the file glisite.cfg nor GSDLsite.cfg can be found!");
299 }
300
301 autoenter_initial = null;
302 start_browser_initial = null;
303 if(configFile != null) {
304 debug("Load: " + configFile.getAbsolutePath());
305 clear();
306 try {
307 BufferedReader in = new BufferedReader(new FileReader(configFile));
308 String line = null;
309 while((line = in.readLine()) != null) {
310 String key = null;
311 String value = null;
312 int index = -1;
313 if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
314 key = line.substring(0, index);
315 value = line.substring(index + 1);
316 }
317 else {
318 key = line;
319 }
320 put(key, value);
321 }
322 in.close();
323 }
324 catch (Exception error) {
325 error.printStackTrace();
326 }
327 }
328 }
329
330 public boolean exists() {
331 return gsdlsite_cfg.exists();
332 }
333
334 public String getLocalHostURL() {
335 StringBuffer url = new StringBuffer(LOCAL_HOST);
336 url.append(COLON);
337 url.append((String)get(PORTNUMBER));
338 String enterlib = (String)get(ENTERLIB);
339 if(enterlib == null || enterlib.length() == 0) {
340 // Use the default /gsdl and hope for the best.
341 url.append(SEPARATOR);
342 url.append(GSDL);
343 }
344 else {
345 if(!enterlib.startsWith(SEPARATOR)) {
346 url.append(SEPARATOR);
347 }
348 url.append(enterlib);
349 }
350 enterlib = null;
351 debug("Found Local Library Address: " + url.toString());
352 return url.toString();
353 }
354
355 public String getSiteConfigFilename() {
356 return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
357 }
358
359 public String getURL() {
360 // URL is made from url and portnumber
361 String url = (String) get(URL);
362 if(url != null) {
363 StringBuffer temp = new StringBuffer(url);
364 temp.append(COLON);
365 temp.append((String)get(PORTNUMBER));
366 String enterlib = (String)get(ENTERLIB);
367 if(enterlib == null || enterlib.length() == 0) {
368 // Use the default /gsdl and hope for the best.
369 temp.append(SEPARATOR);
370 temp.append(GSDL);
371 }
372 else {
373 if(!enterlib.startsWith(SEPARATOR)) {
374 temp.append(SEPARATOR);
375 }
376 temp.append(enterlib);
377 }
378 enterlib = null;
379 url = temp.toString();
380 }
381 debug("Found Local Library Address: " + url);
382 return url;
383 }
384
385 public void load() {
386 if(glisite_cfg.exists()) {
387 debug("Load: " + glisite_cfg.getAbsolutePath());
388 clear();
389 try {
390 BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
391 String line = null;
392 while((line = in.readLine()) != null) {
393 String key = null;
394 String value = null;
395 int index = -1;
396 if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
397 key = line.substring(0, index);
398 value = line.substring(index + 1);
399 }
400 else {
401 key = line;
402 }
403 put(key, value);
404 }
405 in.close();
406 }
407 catch (Exception error) {
408 error.printStackTrace();
409 }
410 }
411 else {
412 debug("No glisite.cfg file can be found!");
413 }
414 }
415
416 /** Restore the autoenter value to its initial value, and remove url if present. */
417 public void restore() {
418 if(glisite_cfg != null) {
419 // Delete the file
420 glisite_cfg.delete();
421 }
422 else {
423 debug("Restore Initial Settings");
424 put(AUTOENTER, autoenter_initial);
425 put(STARTBROWSER, start_browser_initial);
426 remove(URL);
427 save();
428 }
429 }
430
431 public void set() {
432 debug("Set Session Settings");
433 if(autoenter_initial == null) {
434 autoenter_initial = (String) get(AUTOENTER);
435 debug("Remember autoenter was: " + autoenter_initial);
436 }
437 put(AUTOENTER, TRUE);
438 if(start_browser_initial == null) {
439 start_browser_initial = (String) get(STARTBROWSER);
440 debug("Remember start_browser was: " + start_browser_initial);
441 }
442 put(STARTBROWSER, FALSE);
443 save();
444 }
445
446 private void debug(String message) {
447 ///ystem.err.println(message);
448 }
449
450 private void save() {
451 //debug("Save: " + gsdlsite_cfg.getAbsolutePath());
452 debug("Save: " + glisite_cfg.getAbsolutePath());
453 try {
454 //BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
455 BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
456 for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
457 String key = (String) keys.next();
458 String value = (String) get(key);
459 out.write(key, 0, key.length());
460 if(value != null) {
461 out.write('=');
462 out.write(value, 0, value.length());
463 }
464 out.newLine();
465 }
466 out.flush();
467 out.close();
468 }
469 catch (Exception error) {
470 error.printStackTrace();
471 }
472 }
473 }
474}
Note: See TracBrowser for help on using the repository browser.