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