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

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

changed this so isRunning() does check to see if it is actually running - delete collection, save etc don't get errors if the server has been shut down

  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 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 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
99
100 static public void start(String gsdl_path, String local_library_server_file_path)
101 {
102 // Check the local library server.exe file exists
103 local_library_server_file = new File(local_library_server_file_path);
104 if (!local_library_server_file.exists()) {
105 DebugStream.println("No local library at given file path.");
106
107 local_library_server_file = new File(gsdl_path + "server.exe");
108 if (!local_library_server_file.exists()) {
109 DebugStream.println("No local library at all.");
110 return;
111 }
112 }
113
114 // Check if the server is already running
115 gsdlsite_cfg_file = new GSDLSiteConfig(local_library_server_file);
116 String url = gsdlsite_cfg_file.getURL();
117 if (url != null) {
118 // If it is already running then set the Greenstone web server address and we're done
119 try {
120 Configuration.library_url = new URL(url);
121 running = true;
122 return;
123 }
124 catch (MalformedURLException exception) {
125 DebugStream.printStackTrace(exception);
126 }
127 }
128
129 // Configure the server for immediate entry
130 gsdlsite_cfg_file.set();
131
132 // Spawn local library server process
133 String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg_file.getSiteConfigFilename();
134 Gatherer.spawnApplication(local_library_server_command);
135
136 // Wait until program has started, by reloading and checking the URL field
137 gsdlsite_cfg_file.load();
138 int attempt_count = 0;
139 while (gsdlsite_cfg_file.getURL() == null) {
140 new OneSecondWait(); // Wait one second (give or take)
141 gsdlsite_cfg_file.load();
142 attempt_count++;
143
144 // After waiting a minute ask the user whether they want to wait another minute
145 if (attempt_count == 60) {
146 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
147 if (try_again == JOptionPane.NO_OPTION) {
148 return;
149 }
150 attempt_count = 0;
151 }
152 }
153
154 // Ta-da. Now the url should be available
155 try {
156 Configuration.library_url = new URL(gsdlsite_cfg_file.getURL());
157 }
158 catch (MalformedURLException exception) {
159 DebugStream.printStackTrace(exception);
160 }
161
162 // A quick test involves opening a connection to get the home page for this collection
163 try {
164 DebugStream.println("Try connecting to server on config url: '" + Configuration.library_url+ "'");
165 URLConnection connection = Configuration.library_url.openConnection();
166 connection.getContent();
167 }
168 catch (IOException bad_url_connection) {
169 try {
170 // If this fails then we try changing the url to be localhost
171 Configuration.library_url = new URL(gsdlsite_cfg_file.getLocalHostURL());
172 DebugStream.println("Try connecting to server on local host: '" + Configuration.library_url + "'");
173 URLConnection connection = Configuration.library_url.openConnection();
174 connection.getContent();
175 }
176 catch (IOException worse_url_connection) {
177 DebugStream.println("Can't connect to server on either address.");
178 Configuration.library_url = null;
179 return;
180 }
181 }
182
183 running = true;
184 }
185
186
187 static public void stop()
188 {
189 if (running == false) {
190 return;
191 }
192
193 // Send the command for it to exit.
194 config(QUIT_COMMAND);
195
196 // Wait until program has stopped, by reloading and checking the URL field
197 gsdlsite_cfg_file.load();
198 int attempt_count = 0;
199 while (gsdlsite_cfg_file.getURL() != null) {
200 new OneSecondWait(); // Wait one second (give or take)
201 gsdlsite_cfg_file.load();
202 attempt_count++;
203
204 // After waiting a minute ask the user whether they want to wait another minute
205 if (attempt_count == 60) {
206 int try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
207 if (try_again == JOptionPane.NO_OPTION) {
208 return;
209 }
210 attempt_count = 0;
211 }
212 }
213
214 //if(gsdlsite_cfg_file.getURL() != null) {
215 //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
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.QuitFailed"), 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}
Note: See TracBrowser for help on using the repository browser.