merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / CDMServer.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.httpinvoker;
11
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15
16 import javax.sql.DataSource;
17
18 import org.apache.log4j.Level;
19 import org.apache.log4j.Logger;
20 import org.springframework.core.io.ClassPathResource;
21 import org.springframework.core.io.Resource;
22 import org.unitils.database.annotations.TestDataSource;
23 import org.unitils.spring.annotation.SpringApplicationContext;
24
25 import eu.etaxonomy.cdm.remote.CdmRemoteSourceBase;
26 import eu.etaxonomy.taxeditor.exception.CDMServerException;
27
28 /**
29 *
30 * Inspired by http://javabitch.blogspot.de/2012/02/junit-with-jetty-restful-cxf.html
31 * @author cmathew
32 * @date 23 Sep 2014
33 *
34 */
35 @SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
36 public class CDMServer {
37
38 public static final Logger logger = Logger.getLogger(CDMServer.class);
39
40 @TestDataSource
41 protected DataSource dataSource;
42
43 private boolean runDefaultCDMServer;
44
45 private final String name = "test-cdm-server";
46 private final String host = "127.0.0.1";
47 private final int port = 9090;
48 private final int stopPort = 9191;
49 private final String stopKey = "jetty-cdm-server";
50 private final String contextPath = "";
51
52 private boolean keepServerRunning = true;
53
54 public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE =
55 new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
56
57 public static final Resource DEFAULT_JETTY_CONFIG_RESOURCE =
58 new ClassPathResource("/etc/jetty/jetty.xml");
59
60 public static final Resource DEFAULT_JETTY_TEMP_RESOURCE =
61 new ClassPathResource("/etc/jetty/temp");
62
63 public static final Resource DEFAULT_JETTY_TEMP_WAR_LIB_RESOURCE =
64 new ClassPathResource("/etc/jetty/temp/webapp/WEB-INF/lib");
65
66 public static final Resource DEFAULT_DATASOURCE_FILE =
67 new ClassPathResource("datasources.xml");
68
69 public static final Resource DEFAULT_JETTY_RUNNER_RESOURCE =
70 new ClassPathResource("/etc/jetty/jetty-runner-9.2.3.v20140905.jar");
71
72 public static final Resource DEFAULT_JETTY_RESOURCE =
73 new ClassPathResource("/etc/jetty/start-9.2.3.v20140905.jar");
74
75
76
77 private static CDMServer cdmServer = null;
78 private static CDMServerException cdmse = null;
79
80 private void CDMServer() {
81 Logger.getRootLogger().setLevel(Level.INFO);
82 }
83
84 public static CDMServer getInstance() {
85 if(cdmServer == null) {
86 cdmServer = new CDMServer();
87 }
88 return cdmServer;
89 }
90
91 public String getName() {
92 return name;
93 }
94
95 public String getHost() {
96 return host;
97 }
98
99 public int getPort() {
100 return port;
101 }
102
103 public String getContextPath() {
104 return contextPath;
105 }
106
107 public boolean getKeepServerRunning() {
108 return keepServerRunning;
109 }
110
111 public void setKeepServerRunning(boolean keepServerRunning) {
112 this.keepServerRunning = keepServerRunning;
113 }
114
115 public static boolean isRunningInEclipse() {
116 return (System.getProperty("sun.java.command") != null &&
117 System.getProperty("sun.java.command").startsWith("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner"));
118 }
119
120 private String getVMArgs() throws IOException {
121 StringBuilder sb = new StringBuilder();
122 sb.append(" -Dspring.profiles.active=remoting");
123 sb.append(" -Dcdm.beanDefinitionFile=" + DEFAULT_DATASOURCE_FILE.getFile().getAbsolutePath());
124 sb.append(" -Dcdm.datasource=cdmTest");
125 return sb.toString();
126 }
127
128 private String getStartServerArgs() {
129 StringBuilder sb = new StringBuilder();
130 sb.append(" --port ");
131 sb.append(port);
132 return sb.toString();
133 }
134
135 private String getStopServerSettings() {
136 StringBuilder sb = new StringBuilder();
137 sb.append(" --stop-port ");
138 sb.append(stopPort);
139 sb.append(" --stop-key ");
140 sb.append(stopKey);
141 return sb.toString();
142 }
143
144 private String getStopServerArgs() {
145 StringBuilder sb = new StringBuilder();
146 sb.append(" STOP.PORT=");
147 sb.append(stopPort);
148 sb.append(" STOP.KEY=");
149 sb.append(stopKey);
150 return sb.toString();
151 }
152
153 public void start() throws CDMServerException {
154
155 if(isRunning(1)) {
156 logger.info("[CDM-Server] Server already running @ " + host + ":" + port );
157 return;
158 }
159
160 Thread t = new Thread() {
161 @Override
162 public void run() {
163 StringBuffer output = new StringBuffer();
164 try{
165 Process p;
166 String command = "java "
167 + getVMArgs()
168 + " -jar "
169 + DEFAULT_JETTY_RUNNER_RESOURCE.getFile().getAbsolutePath()
170 + getStartServerArgs()
171 + getStopServerSettings()
172 + " "
173 + DEFAULT_CDM_WEBAPP_RESOURCE.getFile().getAbsolutePath();
174 logger.info("[CDM-Server] Start Command : " + command);
175 p = Runtime.getRuntime().exec(command);
176
177 BufferedReader reader =
178 new BufferedReader(new InputStreamReader(p.getInputStream()));
179
180 String line = "";
181 while ((line = reader.readLine())!= null) {
182 logger.info("[CDM-Server] Start : " + line);
183 }
184
185 } catch (Exception e) {
186 e.printStackTrace();
187 cdmse = new CDMServerException(e);
188 }
189
190 }
191 };
192
193 t.setDaemon(true);
194 cdmse = null;
195 t.start();
196
197 if(isRunning(50)) {
198 logger.info("[CDM-Server] Started Server @ " + host + ":" + port );
199 } else {
200 logger.info("[CDM-Server] Server not started within given interval");
201 throw new CDMServerException("CDM Server not started");
202 }
203
204 }
205
206
207 public boolean isRunning(int checkingIntervals) {
208 CdmRemoteSourceBase crsb = new CdmRemoteSourceBase("local-cdm-server",
209 host,
210 port,
211 contextPath,
212 null);
213 int intervalsCount = 0;
214 do {
215 try {
216 if(cdmse != null) {
217 return false;
218 }
219 if(crsb.checkConnection()) {
220 return true;
221 }
222 } catch (Exception e) {
223 // TODO Auto-generated catch block
224 //e.printStackTrace();
225 }
226 try {
227 Thread.sleep(1000);
228 } catch (InterruptedException e) {
229 // TODO Auto-generated catch block
230 //e.printStackTrace();
231 }
232 intervalsCount++;
233 } while (intervalsCount < checkingIntervals);
234 return false;
235 }
236
237 public void stop() throws Exception {
238
239
240 if(!getInstance().isRunning(1)) {
241 logger.info("[CDM-Server] Server already stopped @ " + host + ":" + port );
242 return;
243 }
244
245 if(getInstance().getKeepServerRunning()) {
246 logger.info("[CDM-Server] Server @ " + host + ":" + port + " is set to keep running");
247 return;
248 }
249
250
251 Thread t = new Thread() {
252 @Override
253 public void run() {
254 StringBuffer output = new StringBuffer();
255 try{
256 Process p;
257 String command = "java -jar " + DEFAULT_JETTY_RESOURCE.getFile().getAbsolutePath()
258 + getStopServerArgs() + " --stop ";
259 logger.info("[CDM-Server] Stop Command : " + command);
260 p = Runtime.getRuntime().exec(command);
261 p.waitFor();
262 BufferedReader reader =
263 new BufferedReader(new InputStreamReader(p.getInputStream()));
264 String line = "";
265 while ((line = reader.readLine())!= null) {
266 logger.info("CDM-Server Stop : " + line + "\n");
267 }
268 logger.info("[CDM-Server] Stopped Server @ " + host + ":" + port );
269 } catch (Exception e) {
270 e.printStackTrace();
271 }
272
273 }
274 };
275
276 t.setDaemon(true);
277 t.start();
278 }
279 }