updated CDMServer and corresponding test files to use dbunit datasets with the possib...
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / CDMServer.java
index 9f01ae8846b8e6817b8c31f70b76d10b1d0b7399..b4b16cc57d514acade0aa1ba162e034190d75351 100644 (file)
@@ -12,6 +12,8 @@ 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;
 
@@ -27,12 +29,18 @@ import eu.etaxonomy.taxeditor.exception.CDMServerException;
 
 /**
  *
- * Inspired by http://javabitch.blogspot.de/2012/02/junit-with-jetty-restful-cxf.html
+ * (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
  *
  */
-@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
+
 public class CDMServer {
 
     public static final Logger logger = Logger.getLogger(CDMServer.class);
@@ -40,16 +48,14 @@ public class CDMServer {
     @TestDataSource
     protected DataSource dataSource;
 
-    private boolean runDefaultCDMServer;
-
-    private final String name = "test-cdm-server";
+    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 = true;
+    private boolean keepServerRunning = false;
 
     public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE =
             new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
@@ -78,7 +84,7 @@ public class CDMServer {
     private static CDMServerException cdmse = null;
 
     private void CDMServer() {
-        Logger.getRootLogger().setLevel(Level.INFO);
+        logger.setLevel(Level.INFO);
     }
 
     public static CDMServer getInstance() {
@@ -152,9 +158,24 @@ public class CDMServer {
 
     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() {
@@ -171,7 +192,7 @@ public class CDMServer {
                             + getStopServerSettings()
                             + " "
                             + DEFAULT_CDM_WEBAPP_RESOURCE.getFile().getAbsolutePath();
-                    logger.info("[CDM-Server] Start Command : " + command);
+                    logger.info("[CDM-Server] Starting server with Command : " + command);
                     p = Runtime.getRuntime().exec(command);
 
                     BufferedReader reader =
@@ -198,13 +219,59 @@ public class CDMServer {
             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");
+            // 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)  {
+    public boolean isRunning(int checkingIntervals) throws CDMServerException  {
         CdmRemoteSourceBase crsb = new CdmRemoteSourceBase("local-cdm-server",
                 host,
                 port,
@@ -217,17 +284,15 @@ public class CDMServer {
                     return false;
                 }
                 if(crsb.checkConnection()) {
+                    logger.info("[CDM-Server] Running @ " + host + ":" + port );
                     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();
+            } catch (InterruptedException ie) {
+                throw new CDMServerException("Error checking CDM Server status", ie);
             }
             intervalsCount++;
         } while (intervalsCount < checkingIntervals);
@@ -235,19 +300,21 @@ public class CDMServer {
     }
 
     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().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;
+            if(getInstance().getKeepServerRunning()) {
+                logger.info("[CDM-Server] Server @ " + host + ":" + port + " is set to keep running");
+                return;
+            }
         }
-
-
         Thread t = new Thread() {
             @Override
             public void run() {
@@ -265,7 +332,6 @@ public class CDMServer {
                     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();
                 }
@@ -275,5 +341,11 @@ public class CDMServer {
 
         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");
+        }
     }
 }