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 |
|
---|
40 |
|
---|
41 | public 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 | gsdlsite_cfg = new File(server_exe.getParentFile(), GSDLSITE_CFG);
|
---|
289 | glisite_cfg = new File(server_exe.getParentFile(), GLISITE_CFG);
|
---|
290 | autoenter_initial = null;
|
---|
291 | start_browser_initial = null;
|
---|
292 | if(gsdlsite_cfg.exists()) {
|
---|
293 | debug("Load: " + gsdlsite_cfg.getAbsolutePath());
|
---|
294 | clear();
|
---|
295 | try {
|
---|
296 | BufferedReader in = new BufferedReader(new FileReader(gsdlsite_cfg));
|
---|
297 | String line = null;
|
---|
298 | while((line = in.readLine()) != null) {
|
---|
299 | String key = null;
|
---|
300 | String value = null;
|
---|
301 | int index = -1;
|
---|
302 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
303 | key = line.substring(0, index);
|
---|
304 | value = line.substring(index + 1);
|
---|
305 | }
|
---|
306 | else {
|
---|
307 | key = line;
|
---|
308 | }
|
---|
309 | put(key, value);
|
---|
310 | }
|
---|
311 | in.close();
|
---|
312 | }
|
---|
313 | catch (Exception error) {
|
---|
314 | error.printStackTrace();
|
---|
315 | }
|
---|
316 | }
|
---|
317 | else {
|
---|
318 | debug("No GSDLsite.cfg file can be found!");
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | public boolean exists() {
|
---|
323 | return gsdlsite_cfg.exists();
|
---|
324 | }
|
---|
325 |
|
---|
326 | public String getLocalHostURL() {
|
---|
327 | StringBuffer url = new StringBuffer(LOCAL_HOST);
|
---|
328 | url.append(COLON);
|
---|
329 | url.append((String)get(PORTNUMBER));
|
---|
330 | String enterlib = (String)get(ENTERLIB);
|
---|
331 | if(enterlib == null || enterlib.length() == 0) {
|
---|
332 | // Use the default /gsdl and hope for the best.
|
---|
333 | url.append(SEPARATOR);
|
---|
334 | url.append(GSDL);
|
---|
335 | }
|
---|
336 | else {
|
---|
337 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
338 | url.append(SEPARATOR);
|
---|
339 | }
|
---|
340 | url.append(enterlib);
|
---|
341 | }
|
---|
342 | enterlib = null;
|
---|
343 | debug("Found Local Library Address: " + url.toString());
|
---|
344 | return url.toString();
|
---|
345 | }
|
---|
346 |
|
---|
347 | public String getSiteConfigFilename() {
|
---|
348 | return SPECIFIC_CONFIG + glisite_cfg.getAbsolutePath();
|
---|
349 | }
|
---|
350 |
|
---|
351 | public String getURL() {
|
---|
352 | // URL is made from url and portnumber
|
---|
353 | String url = (String) get(URL);
|
---|
354 | if(url != null) {
|
---|
355 | StringBuffer temp = new StringBuffer(url);
|
---|
356 | temp.append(COLON);
|
---|
357 | temp.append((String)get(PORTNUMBER));
|
---|
358 | String enterlib = (String)get(ENTERLIB);
|
---|
359 | if(enterlib == null || enterlib.length() == 0) {
|
---|
360 | // Use the default /gsdl and hope for the best.
|
---|
361 | temp.append(SEPARATOR);
|
---|
362 | temp.append(GSDL);
|
---|
363 | }
|
---|
364 | else {
|
---|
365 | if(!enterlib.startsWith(SEPARATOR)) {
|
---|
366 | temp.append(SEPARATOR);
|
---|
367 | }
|
---|
368 | temp.append(enterlib);
|
---|
369 | }
|
---|
370 | enterlib = null;
|
---|
371 | url = temp.toString();
|
---|
372 | }
|
---|
373 | debug("Found Local Library Address: " + url);
|
---|
374 | return url;
|
---|
375 | }
|
---|
376 |
|
---|
377 | public void load() {
|
---|
378 | if(glisite_cfg.exists()) {
|
---|
379 | debug("Load: " + glisite_cfg.getAbsolutePath());
|
---|
380 | clear();
|
---|
381 | try {
|
---|
382 | BufferedReader in = new BufferedReader(new FileReader(glisite_cfg));
|
---|
383 | String line = null;
|
---|
384 | while((line = in.readLine()) != null) {
|
---|
385 | String key = null;
|
---|
386 | String value = null;
|
---|
387 | int index = -1;
|
---|
388 | if((index = line.indexOf("=")) != -1 && line.length() >= index + 1) {
|
---|
389 | key = line.substring(0, index);
|
---|
390 | value = line.substring(index + 1);
|
---|
391 | }
|
---|
392 | else {
|
---|
393 | key = line;
|
---|
394 | }
|
---|
395 | put(key, value);
|
---|
396 | }
|
---|
397 | in.close();
|
---|
398 | }
|
---|
399 | catch (Exception error) {
|
---|
400 | error.printStackTrace();
|
---|
401 | }
|
---|
402 | }
|
---|
403 | else {
|
---|
404 | debug("No GSDLsite.cfg file can be found!");
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | /** Restore the autoenter value to its initial value, and remove url if present. */
|
---|
409 | public void restore() {
|
---|
410 | if(glisite_cfg != null) {
|
---|
411 | // Delete the file
|
---|
412 | glisite_cfg.delete();
|
---|
413 | }
|
---|
414 | else {
|
---|
415 | debug("Restore Initial Settings");
|
---|
416 | put(AUTOENTER, autoenter_initial);
|
---|
417 | put(STARTBROWSER, start_browser_initial);
|
---|
418 | remove(URL);
|
---|
419 | save();
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | public void set() {
|
---|
424 | debug("Set Session Settings");
|
---|
425 | if(autoenter_initial == null) {
|
---|
426 | autoenter_initial = (String) get(AUTOENTER);
|
---|
427 | debug("Remember autoenter was: " + autoenter_initial);
|
---|
428 | }
|
---|
429 | put(AUTOENTER, TRUE);
|
---|
430 | if(start_browser_initial == null) {
|
---|
431 | start_browser_initial = (String) get(STARTBROWSER);
|
---|
432 | debug("Remember start_browser was: " + start_browser_initial);
|
---|
433 | }
|
---|
434 | put(STARTBROWSER, FALSE);
|
---|
435 | save();
|
---|
436 | }
|
---|
437 |
|
---|
438 | private void debug(String message) {
|
---|
439 | ///ystem.err.println(message);
|
---|
440 | }
|
---|
441 |
|
---|
442 | private void save() {
|
---|
443 | //debug("Save: " + gsdlsite_cfg.getAbsolutePath());
|
---|
444 | debug("Save: " + glisite_cfg.getAbsolutePath());
|
---|
445 | try {
|
---|
446 | //BufferedWriter out = new BufferedWriter(new FileWriter(gsdlsite_cfg, false));
|
---|
447 | BufferedWriter out = new BufferedWriter(new FileWriter(glisite_cfg, false));
|
---|
448 | for(Iterator keys = keySet().iterator(); keys.hasNext(); ) {
|
---|
449 | String key = (String) keys.next();
|
---|
450 | String value = (String) get(key);
|
---|
451 | out.write(key, 0, key.length());
|
---|
452 | if(value != null) {
|
---|
453 | out.write('=');
|
---|
454 | out.write(value, 0, value.length());
|
---|
455 | }
|
---|
456 | out.newLine();
|
---|
457 | }
|
---|
458 | out.flush();
|
---|
459 | out.close();
|
---|
460 | }
|
---|
461 | catch (Exception error) {
|
---|
462 | error.printStackTrace();
|
---|
463 | }
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|