package com.emc.mongoose;

import io.prometheus.client.exporter.MetricsServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import java.util.Arrays;

/**
 @author veronika K. on 05.10.18 */
public class Server {

	final org.eclipse.jetty.server.Server server;

	public Server() {
		this(1234);  //default port
	}

	public Server(final int port) {
		server = new org.eclipse.jetty.server.Server(port);
		ServletContextHandler context = new ServletContextHandler();
		context.setContextPath("/");
		server.setHandler(context);
		context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
	}

	public Server start() {
		try {
			server.start();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return this;
	}

	public void stop() {
		try {
			server.stop();
			Arrays.asList(server.getChildHandlers()).stream().peek(handler -> {
				try {
					handler.stop();
				} catch(Exception e) {
					e.printStackTrace();
				}
			});
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -182,34 +182,8 @@
 r;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -808,176 +808,8 @@
 ();%0A
-%09%09%09Arrays.asList(server.getChildHandlers()).stream().peek(handler -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09handler.stop();%0A%09%09%09%09%7D catch(Exception e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A
 %09%09%7D 

save [skip ci]