--- /dev/null
+// $Id$
+/**
+ * Copyright (C) 2014 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
+package eu.etaxonomy.taxeditor.httpinvoker;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import javax.sql.DataSource;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.unitils.database.annotations.TestDataSource;
+import org.unitils.spring.annotation.SpringApplicationContext;
+
+import eu.etaxonomy.cdm.remote.CdmRemoteSourceBase;
+import eu.etaxonomy.taxeditor.exception.CDMServerException;
+
+/**
+ *
+ * Inspired by http://javabitch.blogspot.de/2012/02/junit-with-jetty-restful-cxf.html
+ * @author cmathew
+ * @date 23 Sep 2014
+ *
+ */
+@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
+public class CDMServer {
+
+ public static final Logger logger = Logger.getLogger(CDMServer.class);
+
+ @TestDataSource
+ protected DataSource dataSource;
+
+ private boolean runDefaultCDMServer;
+
+ private final String name = "test-cdm-server";
+ private final String host = "127.0.0.1";
+ private final int port = 9090;
+ private final int stopPort = 9191;
+ private final String stopKey = "jetty-cdm-server";
+ private final String contextPath = "";
+
+ private boolean keepServerRunning = true;
+
+ public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE =
+ new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
+
+ public static final Resource DEFAULT_JETTY_CONFIG_RESOURCE =
+ new ClassPathResource("/etc/jetty/jetty.xml");
+
+ public static final Resource DEFAULT_JETTY_TEMP_RESOURCE =
+ new ClassPathResource("/etc/jetty/temp");
+
+ public static final Resource DEFAULT_JETTY_TEMP_WAR_LIB_RESOURCE =
+ new ClassPathResource("/etc/jetty/temp/webapp/WEB-INF/lib");
+
+ public static final Resource DEFAULT_DATASOURCE_FILE =
+ new ClassPathResource("datasources.xml");
+
+ public static final Resource DEFAULT_JETTY_RUNNER_RESOURCE =
+ new ClassPathResource("/etc/jetty/jetty-runner-9.2.3.v20140905.jar");
+
+ public static final Resource DEFAULT_JETTY_RESOURCE =
+ new ClassPathResource("/etc/jetty/start-9.2.3.v20140905.jar");
+
+
+
+ private static CDMServer cdmServer = null;
+ private static CDMServerException cdmse = null;
+
+ private void CDMServer() {
+ Logger.getRootLogger().setLevel(Level.INFO);
+ }
+
+ public static CDMServer getInstance() {
+ if(cdmServer == null) {
+ cdmServer = new CDMServer();
+ }
+ return cdmServer;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public String getContextPath() {
+ return contextPath;
+ }
+
+ public boolean getKeepServerRunning() {
+ return keepServerRunning;
+ }
+
+ public void setKeepServerRunning(boolean keepServerRunning) {
+ this.keepServerRunning = keepServerRunning;
+ }
+
+ public static boolean isRunningInEclipse() {
+ return (System.getProperty("sun.java.command") != null &&
+ System.getProperty("sun.java.command").startsWith("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner"));
+ }
+
+ private String getVMArgs() throws IOException {
+ StringBuilder sb = new StringBuilder();
+ sb.append(" -Dspring.profiles.active=remoting");
+ sb.append(" -Dcdm.beanDefinitionFile=" + DEFAULT_DATASOURCE_FILE.getFile().getAbsolutePath());
+ sb.append(" -Dcdm.datasource=cdmTest");
+ return sb.toString();
+ }
+
+ private String getStartServerArgs() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(" --port ");
+ sb.append(port);
+ return sb.toString();
+ }
+
+ private String getStopServerSettings() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(" --stop-port ");
+ sb.append(stopPort);
+ sb.append(" --stop-key ");
+ sb.append(stopKey);
+ return sb.toString();
+ }
+
+ private String getStopServerArgs() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(" STOP.PORT=");
+ sb.append(stopPort);
+ sb.append(" STOP.KEY=");
+ sb.append(stopKey);
+ return sb.toString();
+ }
+
+ public void start() throws CDMServerException {
+
+ if(isRunning(1)) {
+ logger.info("[CDM-Server] Server already running @ " + host + ":" + port );
+ return;
+ }
+
+ Thread t = new Thread() {
+ @Override
+ public void run() {
+ StringBuffer output = new StringBuffer();
+ try{
+ Process p;
+ String command = "java "
+ + getVMArgs()
+ + " -jar "
+ + DEFAULT_JETTY_RUNNER_RESOURCE.getFile().getAbsolutePath()
+ + getStartServerArgs()
+ + getStopServerSettings()
+ + " "
+ + DEFAULT_CDM_WEBAPP_RESOURCE.getFile().getAbsolutePath();
+ logger.info("[CDM-Server] Start Command : " + command);
+ p = Runtime.getRuntime().exec(command);
+
+ BufferedReader reader =
+ new BufferedReader(new InputStreamReader(p.getInputStream()));
+
+ String line = "";
+ while ((line = reader.readLine())!= null) {
+ logger.info("[CDM-Server] Start : " + line);
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ cdmse = new CDMServerException(e);
+ }
+
+ }
+ };
+
+ t.setDaemon(true);
+ cdmse = null;
+ t.start();
+
+ if(isRunning(50)) {
+ logger.info("[CDM-Server] Started Server @ " + host + ":" + port );
+ } else {
+ logger.info("[CDM-Server] Server not started within given interval");
+ throw new CDMServerException("CDM Server not started");
+ }
+
+ }
+
+
+ public boolean isRunning(int checkingIntervals) {
+ CdmRemoteSourceBase crsb = new CdmRemoteSourceBase("local-cdm-server",
+ host,
+ port,
+ contextPath,
+ null);
+ int intervalsCount = 0;
+ do {
+ try {
+ if(cdmse != null) {
+ return false;
+ }
+ if(crsb.checkConnection()) {
+ return true;
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ //e.printStackTrace();
+ }
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ //e.printStackTrace();
+ }
+ intervalsCount++;
+ } while (intervalsCount < checkingIntervals);
+ return false;
+ }
+
+ public void stop() throws Exception {
+
+
+ if(!getInstance().isRunning(1)) {
+ logger.info("[CDM-Server] Server already stopped @ " + host + ":" + port );
+ return;
+ }
+
+ if(getInstance().getKeepServerRunning()) {
+ logger.info("[CDM-Server] Server @ " + host + ":" + port + " is set to keep running");
+ return;
+ }
+
+
+ Thread t = new Thread() {
+ @Override
+ public void run() {
+ StringBuffer output = new StringBuffer();
+ try{
+ Process p;
+ String command = "java -jar " + DEFAULT_JETTY_RESOURCE.getFile().getAbsolutePath()
+ + getStopServerArgs() + " --stop ";
+ logger.info("[CDM-Server] Stop Command : " + command);
+ p = Runtime.getRuntime().exec(command);
+ p.waitFor();
+ BufferedReader reader =
+ new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ while ((line = reader.readLine())!= null) {
+ logger.info("CDM-Server Stop : " + line + "\n");
+ }
+ logger.info("[CDM-Server] Stopped Server @ " + host + ":" + port );
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+ };
+
+ t.setDaemon(true);
+ t.start();
+ }
+}