1 |
|
---|
2 | package org.greenstone.server;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Toolkit;
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.InputStreamReader;
|
---|
10 | import java.util.Properties;
|
---|
11 |
|
---|
12 | import org.apache.log4j.*;
|
---|
13 |
|
---|
14 | import org.greenstone.gsdl3.util.Dictionary;
|
---|
15 |
|
---|
16 | public 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 |
|
---|
68 | public void autoStart()
|
---|
69 | {
|
---|
70 | String auto_start = config_properties.getProperty(BaseServer.Property.AUTOSTART, "true");
|
---|
71 |
|
---|
72 | if (auto_start.equals("true") || auto_start.equals("1")) {
|
---|
73 | String start_browser = config_properties.getProperty(BaseServer.Property.START_BROWSER, "true");
|
---|
74 |
|
---|
75 | if (start_browser.equals("true") || start_browser.equals("1")) {
|
---|
76 | restart();
|
---|
77 | }
|
---|
78 | else{
|
---|
79 | start();
|
---|
80 | }
|
---|
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 method
|
---|
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 |
|
---|
106 | public void reconfigRequired()
|
---|
107 | {
|
---|
108 | configure_required_ = true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void start()
|
---|
112 | {
|
---|
113 | int state = -1;
|
---|
114 | server_state_ = -1;
|
---|
115 | server_control_.updateControl();
|
---|
116 |
|
---|
117 | server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
|
---|
118 | stop(true); // silent, no messages displayed
|
---|
119 |
|
---|
120 | // reconfigure if necessary
|
---|
121 | if (configure_required_){
|
---|
122 | server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
|
---|
123 | state = runTarget(CONFIGURE_CMD);
|
---|
124 |
|
---|
125 | if (state != RunTarget.SUCCESS){
|
---|
126 | recordError(CONFIGURE_CMD);
|
---|
127 | }
|
---|
128 | reload();
|
---|
129 | configure_required_ = false;
|
---|
130 | }
|
---|
131 | else{
|
---|
132 | recordSuccess(CONFIGURE_CMD);
|
---|
133 | }
|
---|
134 |
|
---|
135 | state = runTarget(START_CMD);
|
---|
136 |
|
---|
137 | if (state != RunTarget.SUCCESS){
|
---|
138 | recordError(START_CMD);
|
---|
139 | server_state_ = SERVER_START_FAILED;
|
---|
140 |
|
---|
141 | }
|
---|
142 | else{
|
---|
143 | recordSuccess(START_CMD);
|
---|
144 | server_state_ = SERVER_STARTED;
|
---|
145 | }
|
---|
146 |
|
---|
147 | server_control_.updateControl();
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected void recordError(String message){
|
---|
151 | message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
|
---|
152 | server_control_.displayMessage(message);
|
---|
153 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | protected void recordError(String message, Exception e){
|
---|
158 | message = dictionary.get("ServerControl.Error",new String[]{message,logs_folder});
|
---|
159 | server_control_.displayMessage(message);
|
---|
160 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected void recordSuccess(String message){
|
---|
164 | message = dictionary.get("ServerControl.Success",new String[]{message});
|
---|
165 | server_control_.displayMessage(message);
|
---|
166 | logger_.info(message);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void launchBrowser() {
|
---|
170 | server_state_ = -1;
|
---|
171 | server_control_.updateControl();
|
---|
172 | String message = dictionary.get("ServerControl.LaunchingBrowser");
|
---|
173 | server_control_.displayMessage(message);
|
---|
174 | String url = getBrowserURL();
|
---|
175 | //recordError("**** browserURL: " + url);
|
---|
176 | BrowserLauncher launcher = new BrowserLauncher(config_properties.getProperty(BaseServer.Property.BROWSER_PATH, ""),url);
|
---|
177 | logger_.info(message);
|
---|
178 |
|
---|
179 | launcher.start();
|
---|
180 |
|
---|
181 | //wait for a while
|
---|
182 | while(launcher.getBrowserState() == -1){
|
---|
183 | try{
|
---|
184 | Thread.sleep(3000);
|
---|
185 | }
|
---|
186 | catch(Exception e){
|
---|
187 | logger_.error(e);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
|
---|
192 | recordError(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
193 | server_state_ = BROWSER_LAUNCH_FAILED;
|
---|
194 | }
|
---|
195 | else{
|
---|
196 | recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
197 | server_state_ = BROWSER_LAUNCHED;
|
---|
198 | }
|
---|
199 |
|
---|
200 | server_control_.updateControl();
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void restart(){
|
---|
204 | start();
|
---|
205 | if (server_state_ == SERVER_STARTED){
|
---|
206 | launchBrowser();
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | // Preserving the current behaviour of stop() which is to
|
---|
211 | // display the message on stopping
|
---|
212 | public void stop() {
|
---|
213 | stop(false);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void stop(boolean silent) {
|
---|
217 | preStop();
|
---|
218 | if(!silent) {
|
---|
219 | server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
|
---|
220 | }
|
---|
221 | int state = runTarget(STOP_CMD);
|
---|
222 |
|
---|
223 | if (state != RunTarget.SUCCESS){
|
---|
224 | recordError(STOP_CMD);
|
---|
225 | }
|
---|
226 | else{
|
---|
227 | recordSuccess(STOP_CMD);
|
---|
228 | }
|
---|
229 |
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void reloadConfigProperties() {
|
---|
233 | try {
|
---|
234 | FileInputStream in = new FileInputStream(config_properties_file);
|
---|
235 |
|
---|
236 | if (in != null) {
|
---|
237 | logger_.info("loading configuration properties: " + config_properties_file);
|
---|
238 | config_properties.load(in);
|
---|
239 | in.close();
|
---|
240 | } else {
|
---|
241 | logger_.error("Couldn't load configuration properties from " + config_properties_file + "!");
|
---|
242 | }
|
---|
243 | } catch (Exception e) {
|
---|
244 | logger_.error("Exception trying to reload configuration properties " +config_properties_file + ": " +e);
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|
248 |
|
---|
249 | }
|
---|