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;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.lang.*;
|
---|
32 | import java.net.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import org.greenstone.gatherer.util.GSDLSiteConfig;
|
---|
35 |
|
---|
36 |
|
---|
37 | public class LocalLibraryServer
|
---|
38 | {
|
---|
39 | static final private String ADD_COMMAND = "?a=config&cmd=add-collection&c=";
|
---|
40 | static final private String RELEASE_COMMAND = "?a=config&cmd=release-collection&c=";
|
---|
41 | static final private String QUIT_COMMAND = "?a=config&cmd=kill";
|
---|
42 |
|
---|
43 | static private GSDLSiteConfig gsdlsite_cfg_file = null;
|
---|
44 | static private File local_library_server_file = null;
|
---|
45 |
|
---|
46 | static private boolean running = false;
|
---|
47 |
|
---|
48 | static public void addCollection(String collection_name)
|
---|
49 | {
|
---|
50 | config(ADD_COMMAND + collection_name);
|
---|
51 | // This is very important -- it ensures that the above command has finished
|
---|
52 | config("");
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | // Used to send messages to the local library
|
---|
57 | // Warning: this has a lot of potential for nasty race conditions
|
---|
58 | // The response code is returned immediately -- but this does not mean the local
|
---|
59 | // library action has finished!
|
---|
60 | static private void config(String command)
|
---|
61 | {
|
---|
62 | if (Configuration.library_url == null) {
|
---|
63 | System.err.println("Error: Trying to configure local library with null Configuration.library_url!");
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | try {
|
---|
68 | URL url = new URL(Configuration.library_url.toString() + command);
|
---|
69 | HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
|
---|
70 | int response_code = library_connection.getResponseCode();
|
---|
71 | if (response_code >= HttpURLConnection.HTTP_OK && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
|
---|
72 | DebugStream.println("200 - Complete.");
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | DebugStream.println("404 - Failed.");
|
---|
76 | }
|
---|
77 | }
|
---|
78 | catch (Exception ex) {
|
---|
79 | DebugStream.printStackTrace(ex);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | static public boolean isRunning()
|
---|
85 | {
|
---|
86 | if (!running) return false;
|
---|
87 | gsdlsite_cfg_file.load();
|
---|
88 | if (gsdlsite_cfg_file.getURL() == null) return false;
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static public void releaseCollection(String collection_name)
|
---|
93 | {
|
---|
94 | config(RELEASE_COMMAND + collection_name);
|
---|
95 | // This is very important -- it ensures that the above command has finished
|
---|
96 | config("");
|
---|
97 |
|
---|
98 | // !! HACK: Wait a couple of seconds, in the hope that this will reduce the "could not delete index" errors
|
---|
99 | new OneSecondWait();
|
---|
100 | new OneSecondWait();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | static public void start(String gsdl_path, String local_library_server_file_path)
|
---|
105 | {
|
---|
106 | // Check the local library server.exe file exists
|
---|
107 | local_library_server_file = new File(local_library_server_file_path);
|
---|
108 | if (!local_library_server_file.exists()) {
|
---|
109 | DebugStream.println("No local library at given file path.");
|
---|
110 |
|
---|
111 | local_library_server_file = new File(gsdl_path + "server.exe");
|
---|
112 | if (!local_library_server_file.exists()) {
|
---|
113 | DebugStream.println("No local library at all.");
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Check if the server is already running
|
---|
119 | gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
|
---|
120 | String url = gsdlsite_cfg_file.getURL();
|
---|
121 | if (url != null) {
|
---|
122 | // If it is already running then set the Greenstone web server address and we're done
|
---|
123 | try {
|
---|
124 | Configuration.library_url = new URL(url);
|
---|
125 | running = true;
|
---|
126 | return;
|
---|
127 | }
|
---|
128 | catch (MalformedURLException exception) {
|
---|
129 | DebugStream.printStackTrace(exception);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | // Configure the server for immediate entry
|
---|
134 | gsdlsite_cfg_file.set();
|
---|
135 |
|
---|
136 | // Spawn local library server process
|
---|
137 | String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
|
---|
138 | Gatherer.spawnApplication(local_library_server_command);
|
---|
139 |
|
---|
140 | // Wait until program has started, by reloading and checking the URL field
|
---|
141 | gsdlsite_cfg_file.load();
|
---|
142 | int attempt_count = 0;
|
---|
143 | while (gsdlsite_cfg_file.getURL() == null) {
|
---|
144 | new OneSecondWait(); // Wait one second (give or take)
|
---|
145 | gsdlsite_cfg_file.load();
|
---|
146 | attempt_count++;
|
---|
147 |
|
---|
148 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
149 | if (attempt_count == 60) {
|
---|
150 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
151 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | attempt_count = 0;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // Ta-da. Now the url should be available
|
---|
159 | try {
|
---|
160 | Configuration.library_url = new URL(gsdlsite_cfg_file.getURL());
|
---|
161 | }
|
---|
162 | catch (MalformedURLException exception) {
|
---|
163 | DebugStream.printStackTrace(exception);
|
---|
164 | }
|
---|
165 |
|
---|
166 | // A quick test involves opening a connection to get the home page for this collection
|
---|
167 | try {
|
---|
168 | DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
|
---|
169 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
170 | connection.getContent();
|
---|
171 | }
|
---|
172 | catch (IOException bad_url_connection) {
|
---|
173 | try {
|
---|
174 | // If this fails then we try changing the url to be localhost
|
---|
175 | Configuration.library_url = new URL(gsdlsite_cfg_file.getLocalHostURL());
|
---|
176 | DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
|
---|
177 | URLConnection connection = Configuration.library_url.openConnection();
|
---|
178 | connection.getContent();
|
---|
179 | }
|
---|
180 | catch (IOException worse_url_connection) {
|
---|
181 | DebugStream.println("Can't connect to server on either address.");
|
---|
182 | Configuration.library_url = null;
|
---|
183 | return;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | running = true;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | static public void stop()
|
---|
192 | {
|
---|
193 | if (running == false) {
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | // Send the command for it to exit.
|
---|
198 | config(QUIT_COMMAND);
|
---|
199 |
|
---|
200 | // Wait until program has stopped, by reloading and checking the URL field
|
---|
201 | gsdlsite_cfg_file.load();
|
---|
202 | int attempt_count = 0;
|
---|
203 | while (gsdlsite_cfg_file.getURL() != null) {
|
---|
204 | new OneSecondWait(); // Wait one second (give or take)
|
---|
205 | gsdlsite_cfg_file.load();
|
---|
206 | attempt_count++;
|
---|
207 |
|
---|
208 | // After waiting a minute ask the user whether they want to wait another minute
|
---|
209 | if (attempt_count == 60) {
|
---|
210 | int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
211 | if (try_again == JOptionPane.NO_OPTION) {
|
---|
212 | return;
|
---|
213 | }
|
---|
214 | attempt_count = 0;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | // Restore the gsdlsite_cfg.
|
---|
219 | gsdlsite_cfg_file.restore();
|
---|
220 |
|
---|
221 | // If the local server is still running then our changed values will get overwritten.
|
---|
222 | if (gsdlsite_cfg_file.getURL() != null) {
|
---|
223 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
224 | }
|
---|
225 |
|
---|
226 | running = false;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static public void checkServerRunning() {
|
---|
230 | if (!running) return; // don't worry about it if its not supposed to be running
|
---|
231 | gsdlsite_cfg_file.load();
|
---|
232 | if (gsdlsite_cfg_file.getURL() == null) {
|
---|
233 | // need to restart the server again
|
---|
234 | gsdlsite_cfg_file.set();
|
---|
235 |
|
---|
236 | // Spawn local library server process
|
---|
237 | String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
|
---|
238 | Gatherer.spawnApplication(local_library_server_command);
|
---|
239 |
|
---|
240 | }
|
---|
241 | }
|
---|
242 | static private class OneSecondWait
|
---|
243 | {
|
---|
244 | public OneSecondWait()
|
---|
245 | {
|
---|
246 | synchronized(this) {
|
---|
247 | try {
|
---|
248 | wait(1000);
|
---|
249 | }
|
---|
250 | catch (InterruptedException exception) {
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|