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 |
|
---|
28 | static protected Properties build_properties;
|
---|
29 | static protected Logger logger_;
|
---|
30 |
|
---|
31 | static public File build_properties_file;
|
---|
32 | static public Dictionary dictionary;
|
---|
33 | static public BaseProperty Property;
|
---|
34 |
|
---|
35 | protected int server_state_ = -1;
|
---|
36 | protected boolean configure_required_ = true;
|
---|
37 | protected String gsdl_home;
|
---|
38 | protected BaseServerControl server_control_;
|
---|
39 |
|
---|
40 | protected BaseServer(String gsdl_home, String lang, String build_properties_path)
|
---|
41 | {
|
---|
42 | this.gsdl_home = gsdl_home;
|
---|
43 |
|
---|
44 | // make sure we write to the correct logs
|
---|
45 | initLogger();
|
---|
46 | logger_ = Logger.getLogger(BaseServer.class.getName());
|
---|
47 |
|
---|
48 | build_properties_file = new File(build_properties_path);
|
---|
49 |
|
---|
50 | if (!build_properties_file.exists()) {
|
---|
51 | logger_.fatal("Can't find build.properties file "+build_properties_path);
|
---|
52 | System.exit(1);
|
---|
53 | }
|
---|
54 |
|
---|
55 | build_properties = new Properties();
|
---|
56 | reloadBuildProperties();
|
---|
57 |
|
---|
58 | dictionary = new Dictionary("server", lang, this.getClass().getClassLoader());
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | // override to write to the correct logs
|
---|
63 | protected void initLogger() {}
|
---|
64 |
|
---|
65 | public void autoStart()
|
---|
66 | {
|
---|
67 | String auto_start = build_properties.getProperty(BaseServer.Property.AUTOSTART);
|
---|
68 | if (auto_start != null && auto_start.equals("true")) {
|
---|
69 | restart();
|
---|
70 | }
|
---|
71 | else{
|
---|
72 | start();
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected int getServerState()
|
---|
78 | {
|
---|
79 | return server_state_;
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected abstract int runTarget(String cmd);
|
---|
83 |
|
---|
84 |
|
---|
85 | public void reconfigRequired()
|
---|
86 | {
|
---|
87 | configure_required_ = true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void start()
|
---|
91 | {
|
---|
92 | int state = -1;
|
---|
93 | server_state_ = -1;
|
---|
94 | server_control_.updateControl();
|
---|
95 |
|
---|
96 | server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
|
---|
97 | stop(true); // silent, no messages displayed
|
---|
98 |
|
---|
99 | // reconfigure if necessary
|
---|
100 | if (configure_required_){
|
---|
101 | server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
|
---|
102 | state = runTarget(CONFIGURE_CMD);
|
---|
103 |
|
---|
104 | if (state != RunTarget.SUCCESS){
|
---|
105 | recordError(CONFIGURE_CMD);
|
---|
106 | }
|
---|
107 | reload();
|
---|
108 | configure_required_ = false;
|
---|
109 | }
|
---|
110 | else{
|
---|
111 | recordSuccess(CONFIGURE_CMD);
|
---|
112 | }
|
---|
113 |
|
---|
114 | state = runTarget(START_CMD);
|
---|
115 |
|
---|
116 | if (state != RunTarget.SUCCESS){
|
---|
117 | recordError(START_CMD);
|
---|
118 | server_state_ = SERVER_START_FAILED;
|
---|
119 |
|
---|
120 | }
|
---|
121 | else{
|
---|
122 | recordSuccess(START_CMD);
|
---|
123 | server_state_ = SERVER_STARTED;
|
---|
124 | }
|
---|
125 |
|
---|
126 | server_control_.updateControl();
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected void recordError(String message){
|
---|
130 | message = dictionary.get("ServerControl.Error",new String[]{message,gsdl_home});
|
---|
131 | server_control_.displayMessage(message);
|
---|
132 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | protected void recordError(String message, Exception e){
|
---|
137 | message = dictionary.get("ServerControl.Error",new String[]{message,gsdl_home});
|
---|
138 | server_control_.displayMessage(message);
|
---|
139 | logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
|
---|
140 | }
|
---|
141 |
|
---|
142 | protected void recordSuccess(String message){
|
---|
143 | message = dictionary.get("ServerControl.Success",new String[]{message});
|
---|
144 | server_control_.displayMessage(message);
|
---|
145 | logger_.info(message);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | public abstract String getBrowserURL();
|
---|
150 | public abstract void reload(); // reload properties, since they may have changed
|
---|
151 |
|
---|
152 | public void launchBrowser() {
|
---|
153 | server_state_ = -1;
|
---|
154 | server_control_.updateControl();
|
---|
155 | String message = dictionary.get("ServerControl.LaunchingBrowser");
|
---|
156 | server_control_.displayMessage(message);
|
---|
157 | String url = getBrowserURL();
|
---|
158 | //recordError("**** browserURL: " + url);
|
---|
159 | BrowserLauncher launcher = new BrowserLauncher(build_properties.getProperty(BaseServer.Property.BROWSER_PATH),url);
|
---|
160 | logger_.info(message);
|
---|
161 |
|
---|
162 | launcher.start();
|
---|
163 |
|
---|
164 | //wait for a while
|
---|
165 | while(launcher.getBrowserState() == -1){
|
---|
166 | try{
|
---|
167 | Thread.sleep(3000);
|
---|
168 | }
|
---|
169 | catch(Exception e){
|
---|
170 | logger_.error(e);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
|
---|
175 | recordError(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
176 | server_state_ = BROWSER_LAUNCH_FAILED;
|
---|
177 | }
|
---|
178 | else{
|
---|
179 | recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
180 | server_state_ = BROWSER_LAUNCHED;
|
---|
181 | }
|
---|
182 |
|
---|
183 | server_control_.updateControl();
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void restart(){
|
---|
187 | start();
|
---|
188 | if (server_state_ == SERVER_STARTED){
|
---|
189 | launchBrowser();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | // Preserving the current behaviour of stop() which is to
|
---|
194 | // display the message on stopping
|
---|
195 | public void stop() {
|
---|
196 | stop(false);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void stop(boolean silent) {
|
---|
200 | if(!silent) {
|
---|
201 | server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
|
---|
202 | }
|
---|
203 | int state = runTarget(STOP_CMD);
|
---|
204 |
|
---|
205 | if (state != RunTarget.SUCCESS){
|
---|
206 | recordError(STOP_CMD);
|
---|
207 | }
|
---|
208 | else{
|
---|
209 | recordSuccess(STOP_CMD);
|
---|
210 | }
|
---|
211 |
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void reloadBuildProperties() {
|
---|
215 | try {
|
---|
216 | //InputStream in = this.getClass().getClassLoader().getResourceAsStream("build.properties");
|
---|
217 | FileInputStream in = new FileInputStream(build_properties_file);
|
---|
218 |
|
---|
219 | if (in != null) {
|
---|
220 | logger_.info("loading build properties");
|
---|
221 | build_properties.load(in);
|
---|
222 | in.close();
|
---|
223 | } else {
|
---|
224 | logger_.error("Couldn't load build properties!");
|
---|
225 | }
|
---|
226 | } catch (Exception e) {
|
---|
227 | logger_.error("Exception trying to reload build.properties: "+e);
|
---|
228 | }
|
---|
229 |
|
---|
230 | }
|
---|
231 |
|
---|
232 | }
|
---|