source: greenstone3/trunk/src/java/org/greenstone/server/Server.java@ 18101

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

Dr Nichols has changed this file so that stopping the tomcat server from start() (because it first calls stop()) no longer confuses the user with its 'stopping tomcat' message.

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