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

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

Moved the LocalLibraryServer class into the "greenstone" package.

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