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 |
|
---|
27 | package org.greenstone.gatherer;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.lang.*;
|
---|
32 | import java.net.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import org.greenstone.gatherer.util.GSDLSiteConfig;
|
---|
35 | import org.greenstone.gatherer.util.Utility;
|
---|
36 |
|
---|
37 |
|
---|
38 | public class LocalLibraryServer
|
---|
39 | {
|
---|
40 | static private GSDLSiteConfig gsdlsite_cfg = null;
|
---|
41 | static private boolean running = false;
|
---|
42 |
|
---|
43 |
|
---|
44 | static public void addCollection(String collection_name)
|
---|
45 | {
|
---|
46 | config(GSDLSiteConfig.ADD_COMMAND + collection_name);
|
---|
47 | // This is very important -- it ensures that the above command has finished
|
---|
48 | config("");
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | // Used to send messages to the local library
|
---|
53 | // Warning: this has a lot of potential for nasty race conditions
|
---|
54 | // The response code is returned immediately -- but this does not mean the local
|
---|
55 | // library action has finished!
|
---|
56 | static private void config(String command)
|
---|
57 | {
|
---|
58 | try {
|
---|
59 | String raw_url = Configuration.exec_address.toString() + command;
|
---|
60 | URL url = new URL(raw_url);
|
---|
61 | DebugStream.println("Action: " + raw_url);
|
---|
62 | HttpURLConnection library_connection = (HttpURLConnection) url.openConnection();
|
---|
63 | int response_code = library_connection.getResponseCode();
|
---|
64 | if(HttpURLConnection.HTTP_OK <= response_code && response_code < HttpURLConnection.HTTP_MULT_CHOICE) {
|
---|
65 | DebugStream.println("200 - Complete.");
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | DebugStream.println("404 - Failed.");
|
---|
69 | }
|
---|
70 | url = null;
|
---|
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(GSDLSiteConfig.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 | File local_library_server_file = new File(local_library_server_file_path);
|
---|
95 | if (!local_library_server_file.exists()) {
|
---|
96 | DebugStream.println("No local library at given file path.");
|
---|
97 |
|
---|
98 | local_library_server_file = new File(gsdl_path + Utility.SERVER_EXE);
|
---|
99 | if (!local_library_server_file.exists()) {
|
---|
100 | DebugStream.println("No local library at all.");
|
---|
101 | return;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | // First of all we create a GSDLSiteCFG object and check if a URL is already present, in which case the server is already running.
|
---|
106 | gsdlsite_cfg = new GSDLSiteConfig(local_library_server_file);
|
---|
107 | String url = gsdlsite_cfg.getURL();
|
---|
108 | // If its already running then set exec address.
|
---|
109 | if(url != null) {
|
---|
110 | try {
|
---|
111 | Configuration.exec_address = new URL(url);
|
---|
112 | }
|
---|
113 | catch(Exception error) {
|
---|
114 | }
|
---|
115 | }
|
---|
116 | // Otherwise its time to run the server in a spawned process.
|
---|
117 | if(Configuration.exec_address == null && local_library_server_file.exists()) {
|
---|
118 | // Configure for immediate entry. Note that this only works if the gsdlsite.cfg file exists.
|
---|
119 | gsdlsite_cfg.set();
|
---|
120 |
|
---|
121 | // Spawn local library server process
|
---|
122 | String local_library_server_command = local_library_server_file.getAbsolutePath() + " " + gsdlsite_cfg.getSiteConfigFilename();
|
---|
123 | Gatherer.self.spawnApplication(local_library_server_command);
|
---|
124 |
|
---|
125 | // Now we have to wait until program has started. We do this by reloading and checking
|
---|
126 | ///ystem.err.print("Waiting until the local library has loaded");
|
---|
127 | try {
|
---|
128 | gsdlsite_cfg.load();
|
---|
129 |
|
---|
130 | int try_again = JOptionPane.YES_OPTION;
|
---|
131 | int attempt_count = 0;
|
---|
132 | while(gsdlsite_cfg.getURL() == null && try_again == JOptionPane.YES_OPTION) {
|
---|
133 | ///ystem.err.print(".");
|
---|
134 | if(attempt_count == 60) {
|
---|
135 | attempt_count = 0;
|
---|
136 | try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
137 | }
|
---|
138 | else {
|
---|
139 | new OneSecondWait(); // Wait one second (give or take)
|
---|
140 | gsdlsite_cfg.load();
|
---|
141 | attempt_count++;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if((url = gsdlsite_cfg.getURL()) != null) {
|
---|
146 | // Ta-da. Now the url should be available.
|
---|
147 | Configuration.exec_address = new URL(url);
|
---|
148 |
|
---|
149 | // A quick test involves opening a connection to get the home page for this collection. If this fails then we try changing the url to be localhost.
|
---|
150 | try {
|
---|
151 | DebugStream.println("Try connecting to server on config url: '" + Configuration.exec_address + "'");
|
---|
152 | URLConnection connection = Configuration.exec_address.openConnection();
|
---|
153 | connection.getContent();
|
---|
154 | }
|
---|
155 | catch(IOException bad_url_connection) {
|
---|
156 | try {
|
---|
157 | DebugStream.println("Try connecting to server on local host: '" + gsdlsite_cfg.getLocalHostURL() + "'");
|
---|
158 | Configuration.exec_address = new URL(gsdlsite_cfg.getLocalHostURL ());
|
---|
159 | URLConnection connection = Configuration.exec_address.openConnection();
|
---|
160 | connection.getContent();
|
---|
161 | }
|
---|
162 | catch(IOException worse_url_connection) {
|
---|
163 | DebugStream.println("Can't connect to server on either address.");
|
---|
164 | Configuration.exec_address = null;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | catch (Exception exception) {
|
---|
170 | DebugStream.printStackTrace(exception);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | DebugStream.println("Having started server.exe, exec_address is: " + Configuration.exec_address);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | static public void stop()
|
---|
178 | {
|
---|
179 | if (running == true && Configuration.exec_address != null) {
|
---|
180 | // See if its already exited for some reason.
|
---|
181 | gsdlsite_cfg.load();
|
---|
182 | if(gsdlsite_cfg.getURL() != null) {
|
---|
183 | // Send the command for it to exit.
|
---|
184 | config(GSDLSiteConfig.QUIT_COMMAND);
|
---|
185 | // Wait until it exits.
|
---|
186 | try {
|
---|
187 | gsdlsite_cfg.load();
|
---|
188 | int try_again = JOptionPane.YES_OPTION;
|
---|
189 | int attempt_count = 0;
|
---|
190 | while(gsdlsite_cfg.getURL() != null && try_again == JOptionPane.YES_OPTION) {
|
---|
191 | if(attempt_count == 60) {
|
---|
192 | attempt_count = 0;
|
---|
193 | try_again = JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("Server.QuitTimeOut"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION);
|
---|
194 | }
|
---|
195 | else {
|
---|
196 | new OneSecondWait(); // Wait one second (give or take)
|
---|
197 | gsdlsite_cfg.load();
|
---|
198 | attempt_count++;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | //if(gsdlsite_cfg.getURL() != null) {
|
---|
202 | //JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitManual"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
203 | //}
|
---|
204 | }
|
---|
205 | catch (Exception exception) {
|
---|
206 | DebugStream.printStackTrace(exception);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | // Restore the gsdlsite_cfg.
|
---|
210 | if(gsdlsite_cfg != null) {
|
---|
211 | gsdlsite_cfg.restore();
|
---|
212 | }
|
---|
213 | // If the local server is still running then our changed values will get overwritten.
|
---|
214 | if(gsdlsite_cfg.getURL() != null) {
|
---|
215 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("Server.QuitFailed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
216 | }
|
---|
217 | gsdlsite_cfg = null;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | static public class OneSecondWait
|
---|
223 | {
|
---|
224 | public OneSecondWait()
|
---|
225 | {
|
---|
226 | try {
|
---|
227 | wait(1000);
|
---|
228 | }
|
---|
229 | catch (InterruptedException exception) {
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|