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

Last change on this file since 18711 was 18711, checked in by ak19, 15 years ago

Added package-access method getServerControl so that the Settings Dialog can be initialised upon creation with the server object's ServerControl JFrame as parent frame, instead of the Settings Dialog floating away annoyingly under all the regular linux windows.

File size: 6.0 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;
11
12import org.apache.log4j.*;
13
14import org.greenstone.gsdl3.util.Dictionary;
15
16public 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 public void autoStart()
63 {
64 String auto_start = build_properties.getProperty(BaseServer.Property.AUTOSTART);
65 if (auto_start != null && auto_start.equals("true")) {
66 restart();
67 }
68 else{
69 start();
70 }
71
72 }
73
74 // package access method
75 BaseServerControl getServerControl() {
76 return server_control_;
77 }
78
79 protected int getServerState()
80 {
81 return server_state_;
82 }
83
84 // override to write to the correct logs
85 protected void initLogger() {}
86
87 protected abstract int runTarget(String cmd);
88 public abstract String getBrowserURL();
89 public abstract void reload(); // reload properties, since they may have changed
90
91 public void reconfigRequired()
92 {
93 configure_required_ = true;
94 }
95
96 public void start()
97 {
98 int state = -1;
99 server_state_ = -1;
100 server_control_.updateControl();
101
102 server_control_.displayMessage(dictionary.get("ServerControl.Starting"));
103 stop(true); // silent, no messages displayed
104
105 // reconfigure if necessary
106 if (configure_required_){
107 server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
108 state = runTarget(CONFIGURE_CMD);
109
110 if (state != RunTarget.SUCCESS){
111 recordError(CONFIGURE_CMD);
112 }
113 reload();
114 configure_required_ = false;
115 }
116 else{
117 recordSuccess(CONFIGURE_CMD);
118 }
119
120 state = runTarget(START_CMD);
121
122 if (state != RunTarget.SUCCESS){
123 recordError(START_CMD);
124 server_state_ = SERVER_START_FAILED;
125
126 }
127 else{
128 recordSuccess(START_CMD);
129 server_state_ = SERVER_STARTED;
130 }
131
132 server_control_.updateControl();
133 }
134
135 protected void recordError(String message){
136 message = dictionary.get("ServerControl.Error",new String[]{message,gsdl_home});
137 server_control_.displayMessage(message);
138 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}));
139 }
140
141
142 protected void recordError(String message, Exception e){
143 message = dictionary.get("ServerControl.Error",new String[]{message,gsdl_home});
144 server_control_.displayMessage(message);
145 logger_.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
146 }
147
148 protected void recordSuccess(String message){
149 message = dictionary.get("ServerControl.Success",new String[]{message});
150 server_control_.displayMessage(message);
151 logger_.info(message);
152 }
153
154 public void launchBrowser() {
155 server_state_ = -1;
156 server_control_.updateControl();
157 String message = dictionary.get("ServerControl.LaunchingBrowser");
158 server_control_.displayMessage(message);
159 String url = getBrowserURL();
160 //recordError("**** browserURL: " + url);
161 BrowserLauncher launcher = new BrowserLauncher(build_properties.getProperty(BaseServer.Property.BROWSER_PATH),url);
162 logger_.info(message);
163
164 launcher.start();
165
166 //wait for a while
167 while(launcher.getBrowserState() == -1){
168 try{
169 Thread.sleep(3000);
170 }
171 catch(Exception e){
172 logger_.error(e);
173 }
174 }
175
176 if (launcher.getBrowserState() != BrowserLauncher.LAUNCHSUCCESS ){
177 recordError(dictionary.get("ServerControl.LaunchBrowser"));
178 server_state_ = BROWSER_LAUNCH_FAILED;
179 }
180 else{
181 recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
182 server_state_ = BROWSER_LAUNCHED;
183 }
184
185 server_control_.updateControl();
186 }
187
188 public void restart(){
189 start();
190 if (server_state_ == SERVER_STARTED){
191 launchBrowser();
192 }
193 }
194
195 // Preserving the current behaviour of stop() which is to
196 // display the message on stopping
197 public void stop() {
198 stop(false);
199 }
200
201 public void stop(boolean silent) {
202 if(!silent) {
203 server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
204 }
205 int state = runTarget(STOP_CMD);
206
207 if (state != RunTarget.SUCCESS){
208 recordError(STOP_CMD);
209 }
210 else{
211 recordSuccess(STOP_CMD);
212 }
213
214 }
215
216 public void reloadBuildProperties() {
217 try {
218 FileInputStream in = new FileInputStream(build_properties_file);
219
220 if (in != null) {
221 logger_.info("loading build properties");
222 build_properties.load(in);
223 in.close();
224 } else {
225 logger_.error("Couldn't load build properties!");
226 }
227 } catch (Exception e) {
228 logger_.error("Exception trying to reload build.properties: "+e);
229 }
230
231 }
232
233}
Note: See TracBrowser for help on using the repository browser.