1 |
|
---|
2 | package org.greenstone.server;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Toolkit;
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.InputStreamReader;
|
---|
10 | import java.util.Properties;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.apache.log4j.*;
|
---|
14 |
|
---|
15 | import org.greenstone.util.RunTarget;
|
---|
16 | import org.greenstone.util.BrowserLauncher;
|
---|
17 |
|
---|
18 | import org.greenstone.gsdl3.util.Dictionary;
|
---|
19 |
|
---|
20 | public abstract class BaseServer
|
---|
21 | {
|
---|
22 | static protected String START_CMD;
|
---|
23 | static protected String RESTART_CMD;
|
---|
24 | static protected String CONFIGURE_CMD;
|
---|
25 | static protected String STOP_CMD;
|
---|
26 |
|
---|
27 | static protected final int SERVER_STARTED = 0;
|
---|
28 | static protected final int SERVER_START_FAILED = 1;
|
---|
29 | static protected final int BROWSER_LAUNCHED = 2;
|
---|
30 | static protected final int BROWSER_LAUNCH_FAILED = 3;
|
---|
31 | static protected final int START_SERVER = 4;
|
---|
32 |
|
---|
33 | static protected Properties config_properties;
|
---|
34 | static protected Logger logger_;
|
---|
35 |
|
---|
36 | static public File config_properties_file;
|
---|
37 | static public Dictionary dictionary;
|
---|
38 | static public BaseProperty Property;
|
---|
39 |
|
---|
40 | protected int server_state_ = -1;
|
---|
41 | protected boolean configure_required_ = true;
|
---|
42 | protected String gsdl_home;
|
---|
43 | protected String logs_folder;
|
---|
44 | protected boolean start_browser;
|
---|
45 |
|
---|
46 | protected BaseServerControl server_control_;
|
---|
47 |
|
---|
48 | protected BaseServer(String gsdl_home, String lang, String config_properties_path, String logs)
|
---|
49 | {
|
---|
50 | this.gsdl_home = gsdl_home;
|
---|
51 | // expand the relative location of the logs folder
|
---|
52 | this.logs_folder = this.gsdl_home+File.separator+logs;
|
---|
53 |
|
---|
54 | // make sure we write to the correct logs
|
---|
55 | initLogger();
|
---|
56 | logger_ = Logger.getLogger(BaseServer.class.getName());
|
---|
57 |
|
---|
58 | config_properties_file = new File(config_properties_path);
|
---|
59 |
|
---|
60 | if (!config_properties_file.exists()) {
|
---|
61 | logger_.fatal("Can't find configuration file "+config_properties_path);
|
---|
62 | System.exit(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | config_properties = new Properties();
|
---|
66 | reloadConfigProperties(true); // first time starting the server, work out port_number
|
---|
67 |
|
---|
68 | dictionary = new Dictionary("server", lang, this.getClass().getClassLoader());
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void autoStart()
|
---|
72 | {
|
---|
73 | String auto_start = config_properties.getProperty(BaseServer.Property.AUTOSTART, "true");
|
---|
74 | if (auto_start.equals("true") || auto_start.equals("1")) {
|
---|
75 | String start_browser = config_properties.getProperty(BaseServer.Property.START_BROWSER, "true");
|
---|
76 |
|
---|
77 | if (start_browser.equals("true") || start_browser.equals("1")) {
|
---|
78 | restart();
|
---|
79 | }
|
---|
80 | else{
|
---|
81 | start();
|
---|
82 | }
|
---|
83 |
|
---|
84 | server_control_.setState(java.awt.Frame.ICONIFIED); // minimise the server interface window
|
---|
85 | } else {
|
---|
86 | if (configure_required_){
|
---|
87 | server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
|
---|
88 | int state = run(CONFIGURE_CMD);
|
---|
89 |
|
---|
90 | if (state != RunTarget.SUCCESS){
|
---|
91 | recordError(CONFIGURE_CMD);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | reload(); // browser URL or other important properties may not yet be initialised
|
---|
95 | configure_required_ = false;
|
---|
96 |
|
---|
97 | server_state_ = START_SERVER;
|
---|
98 | server_control_.updateControl();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // package access methods
|
---|
103 | BaseServerControl getServerControl() {
|
---|
104 | return server_control_;
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected int getServerState()
|
---|
108 | {
|
---|
109 | return server_state_;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // override to write to the correct logs
|
---|
113 | protected void initLogger() {}
|
---|
114 |
|
---|
115 | protected abstract int runTarget(String cmd);
|
---|
116 |
|
---|
117 | protected int run(String cmd) {
|
---|
118 | int result = runTarget(cmd);
|
---|
119 | server_control_.repaint();
|
---|
120 | // from Java 7, can just call revalidate() instead of invalidate() and validate():
|
---|
121 | server_control_.invalidate();
|
---|
122 | server_control_.validate();
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public abstract String getBrowserURL();
|
---|
127 | public abstract void reload(); // reload properties, since they may have changed
|
---|
128 | protected void preStop() {}
|
---|
129 | protected void postStart() {}
|
---|
130 |
|
---|
131 | public void reconfigRequired()
|
---|
132 | {
|
---|
133 | configure_required_ = true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void start()
|
---|
137 | {
|
---|
138 | int state = -1;
|
---|
139 | server_state_ = -1;
|
---|
140 | server_control_.updateControl();
|
---|
141 |
|
---|
142 | server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
|
---|
143 | stop(true); // silent, no messages displayed
|
---|
144 |
|
---|
145 | // reconfigure if necessary
|
---|
146 | if (configure_required_){
|
---|
147 | server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
|
---|
148 | state = run(CONFIGURE_CMD);
|
---|
149 |
|
---|
150 | if (state != RunTarget.SUCCESS){
|
---|
151 | recordError(CONFIGURE_CMD);
|
---|
152 | }
|
---|
153 | reload(); // work out the browserURL again
|
---|
154 | configure_required_ = false;
|
---|
155 | }
|
---|
156 | else{
|
---|
157 | recordSuccess(CONFIGURE_CMD);
|
---|
158 | }
|
---|
159 |
|
---|
160 | try{
|
---|
161 | Thread.sleep(5000);
|
---|
162 | } catch(Exception e) {
|
---|
163 | logger_.error("Exception trying to sleep: " + e);
|
---|
164 | }
|
---|
165 | state = run(START_CMD);
|
---|
166 |
|
---|
167 | if (state != RunTarget.SUCCESS){
|
---|
168 | recordError(START_CMD);
|
---|
169 | server_state_ = SERVER_START_FAILED;
|
---|
170 | }
|
---|
171 | else{
|
---|
172 | recordSuccess(START_CMD);
|
---|
173 | server_state_ = SERVER_STARTED;
|
---|
174 | postStart();
|
---|
175 | }
|
---|
176 |
|
---|
177 | server_control_.updateControl();
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected void recordError(String message){
|
---|
181 | message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
|
---|
182 | server_control_.displayMessage(message);
|
---|
183 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | protected void recordError(String message, Exception e){
|
---|
188 | message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
|
---|
189 | server_control_.displayMessage(message);
|
---|
190 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected void recordSuccess(String message){
|
---|
194 | message = dictionary.get("ServerControl.Success",new String[]{message});
|
---|
195 | server_control_.displayMessage(message);
|
---|
196 | logger_.info(message);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void launchBrowser() {
|
---|
200 | server_state_ = -1;
|
---|
201 | server_control_.updateControl();
|
---|
202 | String url = getBrowserURL();
|
---|
203 | String message = dictionary.get("ServerControl.LaunchingBrowser");
|
---|
204 | server_control_.displayMessage(message);
|
---|
205 | //recordError("**** browserURL: " + url);
|
---|
206 | BrowserLauncher launcher = new BrowserLauncher(config_properties.getProperty(BaseServer.Property.BROWSER_PATH, ""), url);
|
---|
207 | logger_.info(message);
|
---|
208 |
|
---|
209 | launcher.start();
|
---|
210 |
|
---|
211 | //wait for a while
|
---|
212 | while(launcher.getBrowserState() == -1){
|
---|
213 | try{
|
---|
214 | Thread.sleep(3000);
|
---|
215 | }
|
---|
216 | catch(Exception e){
|
---|
217 | logger_.error(e);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
|
---|
222 | recordError(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
223 | server_state_ = BROWSER_LAUNCH_FAILED;
|
---|
224 | }
|
---|
225 | else{
|
---|
226 | recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
227 | server_state_ = BROWSER_LAUNCHED;
|
---|
228 | }
|
---|
229 |
|
---|
230 | server_control_.updateControl();
|
---|
231 | }
|
---|
232 |
|
---|
233 | public void restart(){
|
---|
234 | start();
|
---|
235 | if (server_state_ == SERVER_STARTED){
|
---|
236 | launchBrowser();
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | // Preserving the current behaviour of stop() which is to
|
---|
241 | // display the message on stopping
|
---|
242 | public void stop() {
|
---|
243 | stop(false);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void stop(boolean silent) {
|
---|
247 | preStop();
|
---|
248 | if(!silent) {
|
---|
249 | server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
|
---|
250 | }
|
---|
251 | int state = run(STOP_CMD);
|
---|
252 |
|
---|
253 | if (state != RunTarget.SUCCESS){
|
---|
254 | recordError(STOP_CMD);
|
---|
255 | }
|
---|
256 | else{
|
---|
257 | recordSuccess(STOP_CMD);
|
---|
258 | }
|
---|
259 |
|
---|
260 | }
|
---|
261 |
|
---|
262 | // returns true on success
|
---|
263 | public boolean reloadConfigProperties(boolean port_has_changed) {
|
---|
264 | try {
|
---|
265 | FileInputStream in = new FileInputStream(config_properties_file);
|
---|
266 |
|
---|
267 | if (in != null) {
|
---|
268 | logger_.info("loading configuration properties: " + config_properties_file);
|
---|
269 | config_properties.load(in);
|
---|
270 | in.close();
|
---|
271 | } else {
|
---|
272 | logger_.error("Couldn't load configuration properties from " + config_properties_file + "!");
|
---|
273 | }
|
---|
274 | } catch (Exception e) {
|
---|
275 | logger_.error("Exception trying to reload configuration properties " +config_properties_file + ": " +e);
|
---|
276 | }
|
---|
277 |
|
---|
278 | return true;
|
---|
279 | }
|
---|
280 |
|
---|
281 | }
|
---|