package com.github.mjeanroy.junit.servers.servers;

/**
 * Partial implementation of an embedded server.
 */
public abstract class AbstractEmbeddedServer implements EmbeddedServer {

	/** Flag to keep server status. */
	private boolean started;

	/** Force specific port. */
	protected final int port;

	/** Server path, default is '/' */
	protected final String path;

	/** Webapp Path. */
	protected final String webapp;

	/** Build default embedded. */
	public AbstractEmbeddedServer() {
		this(new EmbeddedServerConfiguration());
	}

	/**
	 * Build default embedded.
	 *
	 * @param configuration Server configuration.
	 */
	public AbstractEmbeddedServer(EmbeddedServerConfiguration configuration) {
		this.port = configuration.getPort();
		this.path = configuration.getPath();
		this.webapp = configuration.getWebapp();
	}

	@Override
	public void start() {
		if (!isStarted()) {
			doStart();
		}

		started = true;
	}

	@Override
	public void stop() {
		if (isStarted()) {
			doStop();
		}

		started = false;
	}

	@Override
	public boolean isStarted() {
		return started;
	}

	@Override
	public void restart() {
		stop();
		start();
	}

	/** Start embedded server. */
	protected abstract void doStart();

	/** Stop embedded server. */
	protected abstract void doStop();
}

@@ -176,16 +176,211 @@
 rver %7B%0A%0A
+%09/** Use lock object to be sure embedded server cannot be started twice. */%0A%09protected static final Object lock = new Object();%0A%0A%09// Volatile because it can be accessed from more than one thread%0A
 %09/** Fla
@@ -416,16 +416,25 @@
 private 
+volatile 
 boolean 
@@ -1055,24 +1055,49 @@
 d start() %7B%0A
+%09%09synchronized (lock) %7B%0A%09
 %09%09if (!isSta
@@ -1105,24 +1105,25 @@
 ted()) %7B%0A%09%09%09
+%09
 doStart();%0A%09
@@ -1117,31 +1117,33 @@
 %09doStart();%0A
+%09
 %09%09%7D%0A%0A%09%09
+%09
 started = tr
@@ -1146,16 +1146,20 @@
 = true;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1187,16 +1187,41 @@
 top() %7B%0A
+%09%09synchronized (lock) %7B%0A%09
 %09%09if (is
@@ -1236,16 +1236,17 @@
 )) %7B%0A%09%09%09
+%09
 doStop()
@@ -1247,23 +1247,25 @@
 Stop();%0A
+%09
 %09%09%7D%0A%0A%09%09
+%09
 started 
@@ -1273,16 +1273,20 @@
  false;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1409,16 +1409,20 @@
 %09%7D%0A%0A%09/**
+%0A%09 *
  Start e
@@ -1428,32 +1428,80 @@
 embedded server.
+%0A%09 * Must block until server is fully started.%0A%09
  */%0A%09protected a
@@ -1529,16 +1529,20 @@
 );%0A%0A%09/**
+%0A%09 *
  Stop em
@@ -1555,16 +1555,64 @@
  server.
+%0A%09 * Must block until server is fully stopped.%0A%09
  */%0A%09pro

synchronize server start / stop for multi-threading environment