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