import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.net.Socket;
+
import javax.sql.DataSource;
/**
*
- * 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);
@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");
private static CDMServerException cdmse = null;
private void CDMServer() {
- Logger.getRootLogger().setLevel(Level.INFO);
+ logger.setLevel(Level.INFO);
}
public static CDMServer getInstance() {
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() {
+ 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 =
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,
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);
}
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() {
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();
+
+ 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");
+ }
}
}