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

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

Updated so that the GSI dialog does not silently fail when it fails to launch the browser.

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