/*
 * Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hawkular;

import java.lang.management.ManagementFactory;
import javax.management.ObjectName;

import org.hawkular.commons.log.MsgLogger;
import org.hawkular.commons.log.MsgLogging;
import org.hawkular.commons.properties.HawkularProperties;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;

/**
 * Lightweight HawkularServer based on Vert.X/Netty.
 *
 * @author Jay Shaughnessy
 * @author Lucas Ponce
 */
public class HawkularServer implements HawkularServerMBean {
    private static final MsgLogger log = MsgLogging.getMsgLogger(HawkularServer.class);

    private static final String BIND_ADDRESS = "hawkular.bind-address";
    private static final String BIND_ADDRESS_DEFAULT = "127.0.0.1";
    private static final String PORT = "hawkular.port";
    private static final String PORT_DEFAULT = "8080";
    private static final String JMX_NAME = "org.hawkular:name=HawkularServer";

    private Vertx vertx;
    private HttpServer server;
    private HandlersManager handlers;

    public void start() {
        long start = System.currentTimeMillis();
        String bindAdress = HawkularProperties.getProperty(BIND_ADDRESS, BIND_ADDRESS_DEFAULT);
        Integer port = Integer.valueOf(HawkularProperties.getProperty(PORT, PORT_DEFAULT));

        try {
            vertx = Vertx.vertx();
            handlers = new HandlersManager(vertx);
            handlers.start();
            server = vertx.createHttpServer();
            log.infof("Starting Server at http://%s:%s in [%s ms] ", bindAdress, port, (System.currentTimeMillis() - start));
            server.requestHandler(handlers::handle).listen(port, bindAdress);
        } catch (Exception e) {
            log.error(e);
            log.error("Forcing exit");
            handlers.stop();
            server.close();
            System.exit(1);
        }
    }

    public String getStatus() {
        return server != null ? "STARTED" : "STOPPED";
    }

    public void stop() {
        log.info("Stopping Server");
        handlers.stop();
        server.close();
        log.info("Server stopped");
        System.exit(0);
    }

    public static void registerMBean(HawkularServer server) {
        try {
            ObjectName jmxName = new ObjectName(JMX_NAME);
            ManagementFactory.getPlatformMBeanServer().registerMBean(server, jmxName);
        } catch (Exception exception) {
            log.error("Unable to register JMX Bean");
        }
    }

    public static void main(String[] args) {
        HawkularServer server = new HawkularServer();
        registerMBean(server);
        server.start();
    }
}

@@ -746,16 +746,17 @@
 actory;%0A
+%0A
 import j
@@ -996,16 +996,61 @@
 pServer;
+%0Aimport io.vertx.core.http.HttpServerOptions;
 %0A%0A/**%0A *
@@ -2042,24 +2042,25 @@
 tx.vertx();%0A
+%0A
             
@@ -2123,24 +2123,153 @@
 ers.start();
+%0A%0A            HttpServerOptions serverOptions = new HttpServerOptions();%0A            serverOptions.setCompressionSupported(true);
 %0A           
@@ -2297,27 +2297,41 @@
 eHttpServer(
+serverOptions
 );%0A
+%0A
             

Enable gzip compression when the client supports it.