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

Last change on this file since 24479 was 24479, checked in by ak19, 13 years ago

When do not modify port is selected, it displays an error message and does nothing until you hit the restart button.

File size: 7.6 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.util.RunTarget;
16import org.greenstone.util.BrowserLauncher;
17
18import org.greenstone.gsdl3.util.Dictionary;
19
20public 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 = runTarget(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 public abstract String getBrowserURL();
117 public abstract void reload(); // reload properties, since they may have changed
118 protected void preStop() {}
119 protected void postStart() {}
120
121 public void reconfigRequired()
122 {
123 configure_required_ = true;
124 }
125
126 public void start()
127 {
128 int state = -1;
129 server_state_ = -1;
130 server_control_.updateControl();
131
132 server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
133 stop(true); // silent, no messages displayed
134
135 // reconfigure if necessary
136 if (configure_required_){
137 server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
138 state = runTarget(CONFIGURE_CMD);
139
140 if (state != RunTarget.SUCCESS){
141 recordError(CONFIGURE_CMD);
142 }
143 reload(); // work out the browserURL again
144 configure_required_ = false;
145 }
146 else{
147 recordSuccess(CONFIGURE_CMD);
148 }
149
150 try{
151 Thread.sleep(5000);
152 } catch(Exception e) {
153 logger_.error("Exception trying to sleep: " + e);
154 }
155 state = runTarget(START_CMD);
156
157 if (state != RunTarget.SUCCESS){
158 recordError(START_CMD);
159 server_state_ = SERVER_START_FAILED;
160 }
161 else{
162 recordSuccess(START_CMD);
163 server_state_ = SERVER_STARTED;
164 postStart();
165 }
166
167 server_control_.updateControl();
168 }
169
170 protected void recordError(String message){
171 message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
172 server_control_.displayMessage(message);
173 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
174 }
175
176
177 protected void recordError(String message, Exception e){
178 message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
179 server_control_.displayMessage(message);
180 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
181 }
182
183 protected void recordSuccess(String message){
184 message = dictionary.get("ServerControl.Success",new String[]{message});
185 server_control_.displayMessage(message);
186 logger_.info(message);
187 }
188
189 public void launchBrowser() {
190 server_state_ = -1;
191 server_control_.updateControl();
192 String url = getBrowserURL();
193 String message = dictionary.get("ServerControl.LaunchingBrowser");
194 server_control_.displayMessage(message);
195 //recordError("**** browserURL: " + url);
196 BrowserLauncher launcher = new BrowserLauncher(config_properties.getProperty(BaseServer.Property.BROWSER_PATH, ""), url);
197 logger_.info(message);
198
199 launcher.start();
200
201 //wait for a while
202 while(launcher.getBrowserState() == -1){
203 try{
204 Thread.sleep(3000);
205 }
206 catch(Exception e){
207 logger_.error(e);
208 }
209 }
210
211 if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
212 recordError(dictionary.get("ServerControl.LaunchBrowser"));
213 server_state_ = BROWSER_LAUNCH_FAILED;
214 }
215 else{
216 recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
217 server_state_ = BROWSER_LAUNCHED;
218 }
219
220 server_control_.updateControl();
221 }
222
223 public void restart(){
224 start();
225 if (server_state_ == SERVER_STARTED){
226 launchBrowser();
227 }
228 }
229
230 // Preserving the current behaviour of stop() which is to
231 // display the message on stopping
232 public void stop() {
233 stop(false);
234 }
235
236 public void stop(boolean silent) {
237 preStop();
238 if(!silent) {
239 server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
240 }
241 int state = runTarget(STOP_CMD);
242
243 if (state != RunTarget.SUCCESS){
244 recordError(STOP_CMD);
245 }
246 else{
247 recordSuccess(STOP_CMD);
248 }
249
250 }
251
252 // returns true on success
253 public boolean reloadConfigProperties(boolean port_has_changed) {
254 try {
255 FileInputStream in = new FileInputStream(config_properties_file);
256
257 if (in != null) {
258 logger_.info("loading configuration properties: " + config_properties_file);
259 config_properties.load(in);
260 in.close();
261 } else {
262 logger_.error("Couldn't load configuration properties from " + config_properties_file + "!");
263 }
264 } catch (Exception e) {
265 logger_.error("Exception trying to reload configuration properties " +config_properties_file + ": " +e);
266 }
267
268 return true;
269 }
270
271}
Note: See TracBrowser for help on using the repository browser.