package org.jbake.launcher;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.jbake.app.configuration.JBakeConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;


/**
 * Provides Jetty server related functions
 *
 * @author Jonathan Bullock <a href="mailto:jonbullock@gmail.com">jonbullock@gmail.com</a>
 */
public class JettyServer implements Closeable {
    private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);

    private Server server;

    @Deprecated
    public void run(String resourceBase, String port) {
        LOGGER.warn("DEPRECATED. Will be removed in the future. Use run(String resourceBase, JBakeConfiguration config) instead");
        run(resourceBase, "/", "localhost", Integer.parseInt(port));
    }

    public void run(String resourceBase, JBakeConfiguration configuration) {
        run(resourceBase, configuration.getServerContextPath(), configuration.getServerHostname(), configuration.getServerPort());
    }
    /**
     * Run Jetty web server serving out supplied path on supplied port
     *
     * @param resourceBase Base directory for resources to be served
     * @param port Required server port
     */
    private void run(String resourceBase, String contextPath, String hostname, int port) {
        server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setHost(hostname);
        connector.setPort(port);
        server.addConnector(connector);

        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[]{"index"});
        resource_handler.setResourceBase(resourceBase);

        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath(contextPath);
        contextHandler.setHandler(resource_handler);

        HandlerList handlers = new HandlerList();

        handlers.setHandlers(new Handler[]{contextHandler, new DefaultHandler()});
        server.setHandler(handlers);

        LOGGER.info("Serving out contents of: [{}] on http://{}:{}{}", resourceBase, hostname, port, contextHandler.getContextPath());
        LOGGER.info("(To stop server hit CTRL-C)");

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            LOGGER.error("unable to start server", e);
        }
    }

    public boolean isStarted() {
        return server != null && server.isStarted();
    }

    @Override
    public void close() throws IOException {
        if (server.isRunning()) {
            try {
                server.stop();
            } catch (Exception e) {
                LOGGER.error("unable to stop server");
            }
        }

    }
}

@@ -955,17 +955,29 @@
 ECATED. 
-W
+This method w
 ill be r
@@ -994,13 +994,25 @@
 the 
-futur
+next major releas
 e. U
@@ -1073,16 +1073,17 @@
  instead
+.
 %22);%0A    

change deprecated log message