deleted project from trunk to work on branch
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / CDMServer.java
diff --git a/eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java b/eu.etaxonomy.taxeditor.remoting/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java
deleted file mode 100644 (file)
index b4b16cc..0000000
+++ /dev/null
@@ -1,351 +0,0 @@
-// $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 java.net.Socket;
-
-
-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;
-
-/**
- *
- * (Singleton) Server instance which manages a compatible cdmlib-webapp-war.
- * This is done by launching a jetty instance (using jetty-runner) as an
- * executed process. The choice of the external process over a more
- * preferable 'embedded jetty' instance is due to problems arising from the
- * class loading of classes (e.g. from hibernate core) which are needed
- * for both the webapp as well as the remoting client.
- *
- * @author cmathew
- * @date 23 Sep 2014
- *
- */
-
-public class CDMServer {
-
-    public static final Logger logger = Logger.getLogger(CDMServer.class);
-
-    @TestDataSource
-    protected DataSource dataSource;
-
-    private final String name = "default";
-    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 = false;
-
-    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.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 {
-
-        /**
-         * First check if the CDM server responds to a service request, which implies that
-         * the server has started properly. If no response is received then check if the
-         * server is listening on specific host / port, which implies that the server
-         * has started but incorrectly, in which case we try to force stop it (if we can)
-         * and start a new server.
-         */
-        if(isRunning(1)) {
-            logger.info("[CDM-Server] Server already running @ " + host + ":" + port );
-            return;
-        } else if (isAvailable(1)){
-            logger.info("[CDM-Server] Server available, but not started correctly @ " + host + ":" + port );
-            logger.info("[CDM-Server] .... trying to force stop server @ " + host + ":" + port );
-            try {
-                stop(true);
-            } catch (Exception e) {
-                throw new CDMServerException("CDM Server could not be stopped : " + e.getMessage());
-            }
-        }
-
-        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] Starting server with 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");
-            // making sure to kill server if it is not started correctly
-            try {
-                stop(true);
-            } catch (Exception e) {
-                throw new CDMServerException("CDM Server could not be stopped : " + e.getMessage());
-            }
-            throw new CDMServerException("CDM Server not started : ");
-        }
-
-    }
-
-    public boolean isAvailable(int checkingIntervals) throws CDMServerException {
-        int intervalsCount = 0;
-        do {
-            try {
-                Socket s = new Socket(host, port);
-                logger.info("[CDM-Server] Available @ " + host + ":" + port );
-                return true;
-            } catch (IOException ioe) {
-
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException ie) {
-                throw new CDMServerException("Error checking CDM Server status", ie);
-            }
-            intervalsCount++;
-        } while (intervalsCount < checkingIntervals);
-
-        return false;
-    }
-
-    public boolean isStopped(int checkingIntervals) throws CDMServerException {
-        int intervalsCount = 0;
-        do {
-            try {
-                Socket s = new Socket(host, port);
-            } catch (IOException ioe) {
-                logger.info("[CDM-Server] Stopped @ " + host + ":" + port );
-                return true;
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException ie) {
-                throw new CDMServerException("Error checking CDM Server status", ie);
-            }
-            intervalsCount++;
-        } while (intervalsCount < checkingIntervals);
-
-        return false;
-    }
-
-    public boolean isRunning(int checkingIntervals) throws CDMServerException  {
-        CdmRemoteSourceBase crsb = new CdmRemoteSourceBase("local-cdm-server",
-                host,
-                port,
-                contextPath,
-                null);
-        int intervalsCount = 0;
-        do {
-            try {
-                if(cdmse != null) {
-                    return false;
-                }
-                if(crsb.checkConnection()) {
-                    logger.info("[CDM-Server] Running @ " + host + ":" + port );
-                    return true;
-                }
-            } catch (Exception e) {
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException ie) {
-                throw new CDMServerException("Error checking CDM Server status", ie);
-            }
-            intervalsCount++;
-        } while (intervalsCount < checkingIntervals);
-        return false;
-    }
-
-    public void stop() throws Exception {
-        stop(false);
-    }
-    public void stop(boolean force) throws Exception {
-
-        if(!force) {
-            if(!getInstance().isAvailable(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");
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-
-            }
-        };
-
-        t.setDaemon(true);
-        t.start();
-
-        if(isStopped(5)) {
-            logger.info("[CDM-Server] Stopped Server @ " + host + ":" + port );
-        } else {
-            logger.info("[CDM-Server] Could not stop @ " + host + ":" + port + ". Please kill it manually");
-        }
-    }
-}