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

Last change on this file since 20632 was 20632, checked in by ak19, 15 years ago

When restarting, server needs to sleep between stop and start for GS2 on Windows to finish terminating the apache web server

File size: 7.1 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 reload(); // browser URL or other important properties may not yet be initialised
83 server_state_ = START_SERVER;
84 server_control_.updateControl();
85 }
86 }
87
88 // package access methods
89 BaseServerControl getServerControl() {
90 return server_control_;
91 }
92
93 protected int getServerState()
94 {
95 return server_state_;
96 }
97
98 // override to write to the correct logs
99 protected void initLogger() {}
100
101 protected abstract int runTarget(String cmd);
102 public abstract String getBrowserURL();
103 public abstract void reload(); // reload properties, since they may have changed
104 protected void preStop() {}
105 protected void postStart() {}
106
107 public void reconfigRequired()
108 {
109 configure_required_ = true;
110 }
111
112 public void start()
113 {
114 int state = -1;
115 server_state_ = -1;
116 server_control_.updateControl();
117
118 server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
119 stop(true); // silent, no messages displayed
120
121 // reconfigure if necessary
122 if (configure_required_){
123 server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
124 state = runTarget(CONFIGURE_CMD);
125
126 if (state != RunTarget.SUCCESS){
127 recordError(CONFIGURE_CMD);
128 }
129 reload(); // work out the browserURL again
130 configure_required_ = false;
131 }
132 else{
133 recordSuccess(CONFIGURE_CMD);
134 }
135
136 try{
137 Thread.sleep(5000);
138 } catch(Exception e) {
139 logger_.error("Exception trying to sleep: " + e);
140 }
141 state = runTarget(START_CMD);
142
143 if (state != RunTarget.SUCCESS){
144 recordError(START_CMD);
145 server_state_ = SERVER_START_FAILED;
146 }
147 else{
148 recordSuccess(START_CMD);
149 server_state_ = SERVER_STARTED;
150 postStart();
151 }
152
153 server_control_.updateControl();
154 }
155
156 protected void recordError(String message){
157 message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
158 server_control_.displayMessage(message);
159 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
160 }
161
162
163 protected void recordError(String message, Exception e){
164 message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
165 server_control_.displayMessage(message);
166 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
167 }
168
169 protected void recordSuccess(String message){
170 message = dictionary.get("ServerControl.Success",new String[]{message});
171 server_control_.displayMessage(message);
172 logger_.info(message);
173 }
174
175 public void launchBrowser() {
176 server_state_ = -1;
177 server_control_.updateControl();
178 String message = dictionary.get("ServerControl.LaunchingBrowser");
179 server_control_.displayMessage(message);
180 String url = getBrowserURL();
181 //recordError("**** browserURL: " + url);
182 BrowserLauncher launcher = new BrowserLauncher(config_properties.getProperty(BaseServer.Property.BROWSER_PATH, ""),url);
183 logger_.info(message);
184
185 launcher.start();
186
187 //wait for a while
188 while(launcher.getBrowserState() == -1){
189 try{
190 Thread.sleep(3000);
191 }
192 catch(Exception e){
193 logger_.error(e);
194 }
195 }
196
197 if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
198 recordError(dictionary.get("ServerControl.LaunchBrowser"));
199 server_state_ = BROWSER_LAUNCH_FAILED;
200 }
201 else{
202 recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
203 server_state_ = BROWSER_LAUNCHED;
204 }
205
206 server_control_.updateControl();
207 }
208
209 public void restart(){
210 start();
211 if (server_state_ == SERVER_STARTED){
212 launchBrowser();
213 }
214 }
215
216 // Preserving the current behaviour of stop() which is to
217 // display the message on stopping
218 public void stop() {
219 stop(false);
220 }
221
222 public void stop(boolean silent) {
223 preStop();
224 if(!silent) {
225 server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
226 }
227 int state = runTarget(STOP_CMD);
228
229 if (state != RunTarget.SUCCESS){
230 recordError(STOP_CMD);
231 }
232 else{
233 recordSuccess(STOP_CMD);
234 }
235
236 }
237
238 public void reloadConfigProperties() {
239 try {
240 FileInputStream in = new FileInputStream(config_properties_file);
241
242 if (in != null) {
243 logger_.info("loading configuration properties: " + config_properties_file);
244 config_properties.load(in);
245 in.close();
246 } else {
247 logger_.error("Couldn't load configuration properties from " + config_properties_file + "!");
248 }
249 } catch (Exception e) {
250 logger_.error("Exception trying to reload configuration properties " +config_properties_file + ": " +e);
251 }
252
253 }
254
255}
Note: See TracBrowser for help on using the repository browser.