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

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

Fixed a bug that Kathy found: when gsdlsite.cfg is missing and autostart is false, it didn't generate the gsdlsite.cfg from the template .in file before.

File size: 7.3 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, ""),url);
193 logger_.info(message);
194
195 launcher.start();
196
197 //wait for a while
198 while(launcher.getBrowserState() == -1){
199 try{
200 Thread.sleep(3000);
201 }
202 catch(Exception e){
203 logger_.error(e);
204 }
205 }
206
207 if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
208 recordError(dictionary.get("ServerControl.LaunchBrowser"));
209 server_state_ = BROWSER_LAUNCH_FAILED;
210 }
211 else{
212 recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
213 server_state_ = BROWSER_LAUNCHED;
214 }
215
216 server_control_.updateControl();
217 }
218
219 public void restart(){
220 start();
221 if (server_state_ == SERVER_STARTED){
222 launchBrowser();
223 }
224 }
225
226 // Preserving the current behaviour of stop() which is to
227 // display the message on stopping
228 public void stop() {
229 stop(false);
230 }
231
232 public void stop(boolean silent) {
233 preStop();
234 if(!silent) {
235 server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
236 }
237 int state = runTarget(STOP_CMD);
238
239 if (state != RunTarget.SUCCESS){
240 recordError(STOP_CMD);
241 }
242 else{
243 recordSuccess(STOP_CMD);
244 }
245
246 }
247
248 public void reloadConfigProperties() {
249 try {
250 FileInputStream in = new FileInputStream(config_properties_file);
251
252 if (in != null) {
253 logger_.info("loading configuration properties: " + config_properties_file);
254 config_properties.load(in);
255 in.close();
256 } else {
257 logger_.error("Couldn't load configuration properties from " + config_properties_file + "!");
258 }
259 } catch (Exception e) {
260 logger_.error("Exception trying to reload configuration properties " +config_properties_file + ": " +e);
261 }
262
263 }
264
265}
Note: See TracBrowser for help on using the repository browser.