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

Last change on this file since 8622 was 8622, checked in by mdewsnip, 19 years ago

Tidied up some more local library stuff.

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