package com.github.aureliano.achmed.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.github.aureliano.achmed.common.StatusCode;
import com.github.aureliano.achmed.common.logging.LoggingFactory;
import com.github.aureliano.achmed.server.conf.ServerConfiguration;

public class AchmedServerHandler {

	private static final Logger logger = LoggingFactory.createLogger(AchmedServerHandler.class);
	private static AchmedServerHandler instance;
	
	private ServerConfiguration configuration;
	
	private AchmedServerHandler() {}
	
	public static AchmedServerHandler instance() {
		if (instance == null) {
			instance = new AchmedServerHandler();
		}
		
		return instance;
	}
	
	public void startUp(ServerConfiguration configuration) {
		try (
			ServerSocket serverSocket = new ServerSocket(configuration.getPortNumber());
		) {
			logger.info("Achmed server started and listening on port " + configuration.getPortNumber());
			this.listen(serverSocket);
		} catch (IOException ex) {
			logger.log(Level.SEVERE, ex.getMessage(), ex);
			System.exit(StatusCode.SERVER_LISTEN_CONNECTION.getCode());
		}
	}
	
	public void shutDown() {
		logger.info("Achmed server has just shut down.");
	}
	
	private void listen(ServerSocket serverSocket) throws IOException {
		while (true) {
			Runnable thread = ThreadHandler.handle(serverSocket.accept());
			thread.run();
		}
	}
}
@@ -573,16 +573,48 @@
 ration;%0A
+%09private boolean serverRunning;%0A
 %09%0A%09priva
@@ -639,16 +639,48 @@
 dler() %7B
+%0A%09%09this.serverRunning = false;%0A%09
 %7D%0A%09%0A%09pub
@@ -969,24 +969,54 @@
 r());%0A%09%09) %7B%0A
+%09%09%09this.serverRunning = true;%0A
 %09%09%09logger.in
@@ -1095,24 +1095,28 @@
 tNumber());%0A
+%09%09%09%0A
 %09%09%09this.list
@@ -1316,55 +1316,210 @@
 %7B%0A%09%09
-logger.info(%22Achmed server has just shut down.%22
+if (this.serverRunning) %7B%0A%09%09%09this.serverRunning = false;%0A%09%09%09logger.info(%22Achmed server has just shut down.%22);%0A%09%09%7D%0A%09%7D%0A%09%0A%09public String getFileRepository() %7B%0A%09%09return this.configuration.getFileRepository(
 );%0A%09

Print shutdown message only when server is running.