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

Last change on this file since 10642 was 10642, checked in by kjdon, 19 years ago

added a new method to LocalLIbraryServer to check that the server.exe is actually running, and restart it if not, and call this when click preview button, so that if local library is down, then it gets restarted.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 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;
28
29
30import java.io.*;
31import java.lang.*;
32import java.net.*;
33import javax.swing.*;
34import org.greenstone.gatherer.util.GSDLSiteConfig;
35
36
37public 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 return running;
87 }
88
89 static public void releaseCollection(String collection_name)
90 {
91 config(RELEASE_COMMAND + collection_name);
92 // This is very important -- it ensures that the above command has finished
93 config("");
94 }
95
96
97 static public void start(String gsdl_path, String local_library_server_file_path)
98 {
99 // Check the local library server.exe file exists
100 local_library_server_file = new File(local_library_server_file_path);
101 if (!local_library_server_file.exists()) {
102 DebugStream.println("No local library at given file path.");
103
104 local_library_server_file = new File(gsdl_path + "server.exe");
105 if (!local_library_server_file.exists()) {
106 DebugStream.println("No local library at all.");
107 return;
108 }
109 }
110
111 // Check if the server is already running
112 gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
113 String url = gsdlsite_cfg_file.getURL();
114 if (url != null) {
115 // If it is already running then set the Greenstone web server address and we're done
116 try {
117 Configuration.library_url = new URL(url);
118 running = true;
119 return;
120 }
121 catch (MalformedURLException exception) {
122 DebugStream.printStackTrace(exception);
123 }
124 }
125
126 // Configure the server for immediate entry
127 gsdlsite_cfg_file.set();
128
129 // Spawn local library server process
130 String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
131 Gatherer.spawnApplication(local_library_server_command);
132
133 // Wait until program has started, by reloading and checking the URL field
134 gsdlsite_cfg_file.load();
135 int attempt_count = 0;
136 while (gsdlsite_cfg_file.getURL() == null) {
137 new OneSecondWait(); // Wait one second (give or take)
138 gsdlsite_cfg_file.load();
139 attempt_count++;
140
141 // After waiting a minute ask the user whether they want to wait another minute
142 if (attempt_count == 60) {
143 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
144 if (try_again == JOptionPane.NO_OPTION) {
145 return;
146 }
147 attempt_count = 0;
148 }
149 }
150
151 // Ta-da. Now the url should be available
152 try {
153 Configuration.library_url = new URL(gsdlsite_cfg_file.getURL());
154 }
155 catch (MalformedURLException exception) {
156 DebugStream.printStackTrace(exception);
157 }
158
159 // A quick test involves opening a connection to get the home page for this collection
160 try {
161 DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
162 URLConnection connection = Configuration.library_url.openConnection();
163 connection.getContent();
164 }
165 catch (IOException bad_url_connection) {
166 try {
167 // If this fails then we try changing the url to be localhost
168 Configuration.library_url = new URL(gsdlsite_cfg_file.getLocalHostURL());
169 DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
170 URLConnection connection = Configuration.library_url.openConnection();
171 connection.getContent();
172 }
173 catch (IOException worse_url_connection) {
174 DebugStream.println("Can't connect to server on either address.");
175 Configuration.library_url = null;
176 return;
177 }
178 }
179
180 running = true;
181 }
182
183
184 static public void stop()
185 {
186 if (running == false) {
187 return;
188 }
189
190 // Send the command for it to exit.
191 config(QUIT_COMMAND);
192
193 // Wait until program has stopped, by reloading and checking the URL field
194 gsdlsite_cfg_file.load();
195 int attempt_count = 0;
196 while (gsdlsite_cfg_file.getURL() != null) {
197 new OneSecondWait(); // Wait one second (give or take)
198 gsdlsite_cfg_file.load();
199 attempt_count++;
200
201 // After waiting a minute ask the user whether they want to wait another minute
202 if (attempt_count == 60) {
203 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
204 if (try_again == JOptionPane.NO_OPTION) {
205 return;
206 }
207 attempt_count = 0;
208 }
209 }
210
211 //if(gsdlsite_cfg_file.getURL() != null) {
212 //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
213 //}
214
215 // Restore the gsdlsite_cfg.
216 gsdlsite_cfg_file.restore();
217
218 // If the local server is still running then our changed values will get overwritten.
219 if (gsdlsite_cfg_file.getURL() != null) {
220 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitFailed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
221 }
222
223 running = false;
224 }
225
226 static public void checkServerRunning() {
227 gsdlsite_cfg_file.load();
228 if (gsdlsite_cfg_file.getURL() == null) {
229 // need to restart the server again
230 gsdlsite_cfg_file.set();
231
232 // Spawn local library server process
233 String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
234 Gatherer.spawnApplication(local_library_server_command);
235
236 }
237 }
238 static private class OneSecondWait
239 {
240 public OneSecondWait()
241 {
242 synchronized(this) {
243 try {
244 wait(1000);
245 }
246 catch (InterruptedException exception) {
247 }
248 }
249 }
250 }
251}
Note: See TracBrowser for help on using the repository browser.