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 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 = false;
|
---|
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.getState();
|
---|
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 | stop();
|
---|
91 |
|
---|
92 | // reconfigure if necessary
|
---|
93 | if (configure_required){
|
---|
94 | server_control.displayMessage(dictionary.get("ServerControl.Configuring"));
|
---|
95 | state = runAnt(CONFIGURE_CMD);
|
---|
96 |
|
---|
97 | if (state != RunAnt.SUCCESS){
|
---|
98 | recordError(CONFIGURE_CMD);
|
---|
99 | }
|
---|
100 | GlobalProperties.reload(); // properties file may have changed, so reload it
|
---|
101 | configure_required = false;
|
---|
102 | }
|
---|
103 | else{
|
---|
104 | recordSuccess(CONFIGURE_CMD);
|
---|
105 | }
|
---|
106 |
|
---|
107 | server_control.displayMessage(dictionary.get("ServerControl.Starting"));
|
---|
108 | state = runAnt(START_CMD);
|
---|
109 |
|
---|
110 | if (state != RunAnt.SUCCESS){
|
---|
111 | recordError(START_CMD);
|
---|
112 | server_state = SERVER_START_FAILED;
|
---|
113 |
|
---|
114 | }
|
---|
115 | else{
|
---|
116 | recordSuccess(START_CMD);
|
---|
117 | server_state = SERVER_STARTED;
|
---|
118 | }
|
---|
119 |
|
---|
120 | server_control.updateControl();
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void recordError(String message){
|
---|
124 | message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()});
|
---|
125 | server_control.displayMessage(message);
|
---|
126 | logger.error(dictionary.get("ServerControl.Failed",new String[]{message}));
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | private void recordError(String message, Exception e){
|
---|
131 | message = dictionary.get("ServerControl.Error",new String[]{message,GlobalProperties.getGSDL3Home()});
|
---|
132 | server_control.displayMessage(message);
|
---|
133 | logger.error(dictionary.get("ServerControl.Failed",new String[]{message}),e);
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void recordSuccess(String message){
|
---|
137 | message = dictionary.get("ServerControl.Success",new String[]{message});
|
---|
138 | server_control.displayMessage(message);
|
---|
139 | logger.info(message);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | public void launchBrowser() {
|
---|
144 | server_state = -1;
|
---|
145 | server_control.updateControl();
|
---|
146 | String message = dictionary.get("ServerControl.LaunchingBrowser");
|
---|
147 | server_control.displayMessage(message);
|
---|
148 | String url = GlobalProperties.getGSDL3WebAddress()+ build_properties.getProperty(StaticStrings.DEFAULT_SERVLET_PROPERTY);
|
---|
149 | BrowserLauncher launcher = new BrowserLauncher(build_properties.getProperty(StaticStrings.BROWSER_PATH),url);
|
---|
150 | logger.info(message);
|
---|
151 |
|
---|
152 | launcher.start();
|
---|
153 |
|
---|
154 | //wait for a while
|
---|
155 | while(launcher.getState() == -1){
|
---|
156 | try{
|
---|
157 | Thread.sleep(3000);
|
---|
158 | }
|
---|
159 | catch(Exception e){
|
---|
160 | logger.error(e);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (launcher.getState() != BrowserLauncher.LAUNCHSUCCESS ){
|
---|
165 | recordError(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
166 | server_state = BROWSER_LAUNCH_FAILED;
|
---|
167 | }
|
---|
168 | else{
|
---|
169 | recordSuccess(dictionary.get("ServerControl.LaunchBrowser"));
|
---|
170 | server_state = BROWSER_LAUNCHED;
|
---|
171 | }
|
---|
172 |
|
---|
173 | server_control.updateControl();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void restart(){
|
---|
177 | start();
|
---|
178 | if (server_state == SERVER_STARTED){
|
---|
179 | launchBrowser();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public void stop() {
|
---|
184 | server_control.displayMessage(dictionary.get("ServerControl.Stopping"));
|
---|
185 | int state = runAnt(STOP_CMD);
|
---|
186 |
|
---|
187 | if (state != RunAnt.SUCCESS){
|
---|
188 | recordError(STOP_CMD);
|
---|
189 | }
|
---|
190 | else{
|
---|
191 | recordSuccess(STOP_CMD);
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void reloadBuildProperties() {
|
---|
197 | try {
|
---|
198 | InputStream in = this.getClass().getClassLoader().getResourceAsStream("build.properties");
|
---|
199 |
|
---|
200 | if (in != null) {
|
---|
201 | logger.info("loading build properties");
|
---|
202 | build_properties.load(in);
|
---|
203 | in.close();
|
---|
204 | } else {
|
---|
205 | logger.error("couldn't load build properties!");
|
---|
206 | }
|
---|
207 | } catch (Exception e) {
|
---|
208 | logger.error("Exception trying to reload build.properties: "+e);
|
---|
209 | }
|
---|
210 |
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 |
|
---|
215 | public static void main (String[] args){
|
---|
216 | if (args.length != 1) {
|
---|
217 | System.err.println("Usage: java org.greenstone.server.ServerMain <gsdl3 src home> ");
|
---|
218 | System.exit(1);
|
---|
219 | }
|
---|
220 |
|
---|
221 | File gsdl3_src_dir = new File(args[0]);
|
---|
222 | if (!gsdl3_src_dir.isDirectory()) {
|
---|
223 | System.err.println("src directory does not exist!");
|
---|
224 | System.exit(1);
|
---|
225 | }
|
---|
226 |
|
---|
227 | new Server(args[0]);
|
---|
228 | }
|
---|
229 | }
|
---|