/**
 * Copyright (c) 2016 Bosch Software Innovations GmbH.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Bosch Software Innovations GmbH - initial creation
 */
package org.eclipse.hono;

import java.util.List;

import javax.annotation.PostConstruct;

import org.eclipse.hono.authorization.AuthorizationService;
import org.eclipse.hono.registration.impl.BaseRegistrationAdapter;
import org.eclipse.hono.server.Endpoint;
import org.eclipse.hono.server.HonoServer;
import org.eclipse.hono.server.HonoServerFactory;
import org.eclipse.hono.telemetry.TelemetryAdapter;
import org.eclipse.hono.util.ComponentFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import io.vertx.core.Vertx;

/**
 * The Hono server main application class.
 * <p>
 * This class uses Spring Boot for configuring and wiring up Hono's components (Verticles).
 * By default there will be as many instances of each verticle created as there are CPU cores
 * available. The {@code hono.maxinstances} config property can be used to set the maximum number
 * of instances to create. This may be useful for executing tests etc.
 * </p>
 */
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application {

    @Value(value = "${hono.maxinstances:2}")
    private int                                    maxInstances;
    @Autowired
    private Vertx                                  vertx;
    @Autowired
    private ComponentFactory<TelemetryAdapter>     adapterFactory;
    @Autowired
    private BaseRegistrationAdapter                registration;
    @Autowired
    private ComponentFactory<AuthorizationService> authServiceFactory;
    @Autowired
    private HonoServerFactory                      serverFactory;
    @Autowired
    private List<ComponentFactory<Endpoint>>       endpointFactories;

    @PostConstruct
    public void registerVerticles() {
        if (vertx == null) {
            throw new IllegalStateException("no Vert.x instance has been configured");
        }
        int instanceCount = Runtime.getRuntime().availableProcessors();
        if (maxInstances > 0 && maxInstances < instanceCount) {
            instanceCount = maxInstances;
        }
        deployTelemetryAdapter(instanceCount);
        deployAuthorizationService(instanceCount);
        deployServer(instanceCount);
        vertx.deployVerticle(registration);
    }

    private void deployAuthorizationService(final int instanceCount) {
        for (int i = 1; i <= instanceCount; i++) {
            vertx.deployVerticle(authServiceFactory.newInstance(i, instanceCount));
        }
    }

    private void deployTelemetryAdapter(final int instanceCount) {
        for (int i = 1; i <= instanceCount; i++) {
            vertx.deployVerticle(adapterFactory.newInstance(i, instanceCount));
        }
    }

    private void deployServer(final int instanceCount) {
        for (int i = 1; i <= instanceCount; i++) {
            HonoServer server = serverFactory.newInstance(i, instanceCount);
            for (ComponentFactory<Endpoint> ef : endpointFactories) {
                server.addEndpoint(ef.newInstance(i, instanceCount));
            }
            vertx.deployVerticle(server);
        }
    }

    public static void main(final String[] args) {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
        SpringApplication.run(Application.class, args);
    }
}

@@ -1817,17 +1817,17 @@
 stances:
-2
+0
 %7D%22)%0A    

use o for default maxinstance value, so we have the same number of instances as available cores