source: greenstone3/trunk/src/java/org/greenstone/server/BaseServer.java@ 20908

Last change on this file since 20908 was 20908, checked in by ak19, 14 years ago

Updated to display the URL when the browser fails to launch (and also when it launches successfully). Most of the previous commit to BrowserLauncher.java have been reverted now, and some additional changes have been made to it.

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