3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.httpinvoker
;
12 import java
.io
.BufferedReader
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStreamReader
;
16 import javax
.sql
.DataSource
;
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
;
25 import eu
.etaxonomy
.cdm
.remote
.CdmRemoteSourceBase
;
26 import eu
.etaxonomy
.taxeditor
.exception
.CDMServerException
;
30 * Inspired by http://javabitch.blogspot.de/2012/02/junit-with-jetty-restful-cxf.html
35 @SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
36 public class CDMServer
{
38 public static final Logger logger
= Logger
.getLogger(CDMServer
.class);
41 protected DataSource dataSource
;
43 private boolean runDefaultCDMServer
;
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
= "";
52 private boolean keepServerRunning
= true;
54 public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE
=
55 new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
57 public static final Resource DEFAULT_JETTY_CONFIG_RESOURCE
=
58 new ClassPathResource("/etc/jetty/jetty.xml");
60 public static final Resource DEFAULT_JETTY_TEMP_RESOURCE
=
61 new ClassPathResource("/etc/jetty/temp");
63 public static final Resource DEFAULT_JETTY_TEMP_WAR_LIB_RESOURCE
=
64 new ClassPathResource("/etc/jetty/temp/webapp/WEB-INF/lib");
66 public static final Resource DEFAULT_DATASOURCE_FILE
=
67 new ClassPathResource("datasources.xml");
69 public static final Resource DEFAULT_JETTY_RUNNER_RESOURCE
=
70 new ClassPathResource("/etc/jetty/jetty-runner-9.2.3.v20140905.jar");
72 public static final Resource DEFAULT_JETTY_RESOURCE
=
73 new ClassPathResource("/etc/jetty/start-9.2.3.v20140905.jar");
77 private static CDMServer cdmServer
= null;
78 private static CDMServerException cdmse
= null;
80 private void CDMServer() {
81 Logger
.getRootLogger().setLevel(Level
.INFO
);
84 public static CDMServer
getInstance() {
85 if(cdmServer
== null) {
86 cdmServer
= new CDMServer();
91 public String
getName() {
95 public String
getHost() {
99 public int getPort() {
103 public String
getContextPath() {
107 public boolean getKeepServerRunning() {
108 return keepServerRunning
;
111 public void setKeepServerRunning(boolean keepServerRunning
) {
112 this.keepServerRunning
= keepServerRunning
;
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"));
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();
128 private String
getStartServerArgs() {
129 StringBuilder sb
= new StringBuilder();
130 sb
.append(" --port ");
132 return sb
.toString();
135 private String
getStopServerSettings() {
136 StringBuilder sb
= new StringBuilder();
137 sb
.append(" --stop-port ");
139 sb
.append(" --stop-key ");
141 return sb
.toString();
144 private String
getStopServerArgs() {
145 StringBuilder sb
= new StringBuilder();
146 sb
.append(" STOP.PORT=");
148 sb
.append(" STOP.KEY=");
150 return sb
.toString();
153 public void start() throws CDMServerException
{
156 logger
.info("[CDM-Server] Server already running @ " + host
+ ":" + port
);
160 Thread t
= new Thread() {
163 StringBuffer output
= new StringBuffer();
166 String command
= "java "
169 + DEFAULT_JETTY_RUNNER_RESOURCE
.getFile().getAbsolutePath()
170 + getStartServerArgs()
171 + getStopServerSettings()
173 + DEFAULT_CDM_WEBAPP_RESOURCE
.getFile().getAbsolutePath();
174 logger
.info("[CDM-Server] Start Command : " + command
);
175 p
= Runtime
.getRuntime().exec(command
);
177 BufferedReader reader
=
178 new BufferedReader(new InputStreamReader(p
.getInputStream()));
181 while ((line
= reader
.readLine())!= null) {
182 logger
.info("[CDM-Server] Start : " + line
);
185 } catch (Exception e
) {
187 cdmse
= new CDMServerException(e
);
198 logger
.info("[CDM-Server] Started Server @ " + host
+ ":" + port
);
200 logger
.info("[CDM-Server] Server not started within given interval");
201 throw new CDMServerException("CDM Server not started");
207 public boolean isRunning(int checkingIntervals
) {
208 CdmRemoteSourceBase crsb
= new CdmRemoteSourceBase("local-cdm-server",
213 int intervalsCount
= 0;
219 if(crsb
.checkConnection()) {
222 } catch (Exception e
) {
223 // TODO Auto-generated catch block
224 //e.printStackTrace();
228 } catch (InterruptedException e
) {
229 // TODO Auto-generated catch block
230 //e.printStackTrace();
233 } while (intervalsCount
< checkingIntervals
);
237 public void stop() throws Exception
{
240 if(!getInstance().isRunning(1)) {
241 logger
.info("[CDM-Server] Server already stopped @ " + host
+ ":" + port
);
245 if(getInstance().getKeepServerRunning()) {
246 logger
.info("[CDM-Server] Server @ " + host
+ ":" + port
+ " is set to keep running");
251 Thread t
= new Thread() {
254 StringBuffer output
= new StringBuffer();
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
);
262 BufferedReader reader
=
263 new BufferedReader(new InputStreamReader(p
.getInputStream()));
265 while ((line
= reader
.readLine())!= null) {
266 logger
.info("CDM-Server Stop : " + line
+ "\n");
268 logger
.info("[CDM-Server] Stopped Server @ " + host
+ ":" + port
);
269 } catch (Exception e
) {