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

Last change on this file since 13593 was 13593, checked in by mdewsnip, 17 years ago

Moved the GSDLSiteConfig class out of the util package and into the greenstone package, since it is only used by LocalLibraryServer.

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