/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.gateway.debug.vertx;

import io.gravitee.common.service.AbstractService;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author Guillaume CUSNIEUX (guillaume.cusnieux at graviteesource.com)
 * @author GraviteeSource Team
 */
public class VertxDebugService extends AbstractService {

    /**
     * Logger.
     */
    private final Logger logger = LoggerFactory.getLogger(VertxDebugService.class);

    @Autowired
    private Vertx vertx;

    private String deploymentId;

    @Value("${http.instances:0}")
    private int instances;

    @Override
    public VertxDebugService start() {
        doStart();
        return this;
    }

    @Override
    public VertxDebugService stop() {
        doStop();
        return this;
    }

    @Override
    protected void doStart() {
        instances = (instances < 1) ? VertxOptions.DEFAULT_EVENT_LOOP_POOL_SIZE : instances;
        logger.info("Starting Vertx DEBUG container and deploy Gateway Verticles [{} instance(s)]", instances);

        final DebugReactorVerticle debugReactorVerticle = new DebugReactorVerticle();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(debugReactorVerticle);

        vertx.deployVerticle(
            debugReactorVerticle,
            event -> {
                if (event.failed()) {
                    logger.warn("Unable to start Debug HTTP server", event.cause());
                } else {
                    deploymentId = event.result();
                }
            }
        );
    }

    @Override
    protected void doStop() {
        if (deploymentId != null) {
            vertx.undeploy(
                deploymentId,
                event -> {
                    lifecycle.moveToStopped();
                }
            );
        }
    }
}

/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.gateway.debug.vertx;

import io.gravitee.common.service.AbstractService;
import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Guillaume CUSNIEUX (guillaume.cusnieux at graviteesource.com)
 * @author GraviteeSource Team
 */
public class VertxDebugService extends AbstractService {

    /**
     * Logger.
     */
    private final Logger logger = LoggerFactory.getLogger(VertxDebugService.class);

    @Autowired
    private Vertx vertx;

    private String deploymentId;

    @Override
    public VertxDebugService start() {
        doStart();
        return this;
    }

    @Override
    public VertxDebugService stop() {
        doStop();
        return this;
    }

    @Override
    protected void doStart() {
        logger.info("Starting Vertx DEBUG container and deploy 1 Debug Reactor Verticle");

        final DebugReactorVerticle debugReactorVerticle = new DebugReactorVerticle();
        applicationContext.getAutowireCapableBeanFactory().autowireBean(debugReactorVerticle);

        vertx.deployVerticle(
            debugReactorVerticle,
            event -> {
                if (event.failed()) {
                    logger.warn("Unable to start Debug HTTP server", event.cause());
                } else {
                    deploymentId = event.result();
                }
            }
        );
    }

    @Override
    protected void doStop() {
        if (deploymentId != null) {
            vertx.undeploy(
                deploymentId,
                event -> {
                    lifecycle.moveToStopped();
                }
            );
        }
    }
}

clean dead code for instances number in debug service gravitee-io/issues#6073