me.bulat.jivr.core.bootstrap.config.RootConfig.java Source code

Java tutorial

Introduction

Here is the source code for me.bulat.jivr.core.bootstrap.config.RootConfig.java

Source

/*
 * Licensed to JIVR under one or more contributor
 *  license agreements. See the NOTICE file distributed with
 *  this work for additional information regarding copyright
 *  ownership. JIVR licenses this file to you 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 me.bulat.jivr.core.bootstrap.config;

import me.bulat.jivr.consul.manager.ConsulManager;
import me.bulat.jivr.consul.manager.ServiceState;
import me.bulat.jivr.consul.manager.SimpleConsulManager;
import me.bulat.jivr.core.agi.AgiScriptEngine;
import me.bulat.jivr.core.agi.SimpleAgiScriptEngine;
import me.bulat.jivr.core.node.Node;
import me.bulat.jivr.core.node.SimpleNode;
import me.bulat.jivr.core.updater.SimpleUpdater;
import me.bulat.jivr.core.updater.UpdateInfo;
import me.bulat.jivr.core.updater.Updater;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Alex Bogatikov
 *         Created on 03/09/16.
 */
@Configuration
@PropertySource("file:${ext.properties.file}")
public class RootConfig {

    /**
     * From configuration file. Consul connection host.
     */
    @Value("${consul.host}")
    private String consulHost;

    /**
     * From configuration file. Consul connection port.
     */
    @Value("${consul.port}")
    private String consulPort;

    /**
     * From configuration file. Node name.
     */
    @Value("${consul.node.name}")
    private String nodeName;

    /**
     * Main Node bean factory.
     * @param consul consul manager.
     * @return constructed node.
     */
    @Bean(name = "node")
    public Node getNode(@Qualifier("consul_manager") ConsulManager consul) {
        return new SimpleNode() {

            @Override
            protected void updateServices() {
                LinkedBlockingQueue<UpdateInfo> updates = getUpdater().getUpdates();
                try {
                    while (true) {
                        UpdateInfo update = updates.poll(10, TimeUnit.SECONDS);
                        if (update != null) {
                            AgiScriptEngine engine;
                            switch (update.type) {
                            case ADD:
                                engine = new SimpleAgiScriptEngine(update.service, getConsulManager());
                                services.put(update.service.getServiceName(), engine);
                                break;
                            case ADD_AND_START:
                                engine = new SimpleAgiScriptEngine(update.service, getConsulManager());
                                services.put(update.service.getServiceName(), engine);
                                engine.start();
                                break;
                            case DELETE:
                                services.remove(update.service.getServiceName()).stop();
                                break;
                            case REPLACE_AND_STOP:
                                services.get(update.service.getServiceName()).updateService(update.service);
                                break;
                            case REPLACE_AND_START:
                                services.get(update.service.getServiceName()).updateService(update.service).start();
                                break;
                            case START:
                                services.get(update.service.getServiceName()).start();
                                break;
                            case STOP:
                                services.get(update.service.getServiceName()).stop();
                                break;
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    // TODO: 10/09/16 LOG stop get updates
                    System.out.println("Stop get updates.");
                }
            }

            @Override
            protected void nodeRegister() {
                consul.registerNode();
            }

            @Override
            protected void nodeUnRegister() {
                consul.unRegisterNode();
            }

            @Override
            protected void setNodeState(final ServiceState state) {
                consul.setNodeState(state);

            }

            @Override
            protected void setNodeState(final ServiceState state, final String note) {
                consul.setNodeState(state, note);
            }
        };
    }

    /**
     * Consul manager.
     * @return constructed consul manager bean.
     */
    @Bean(name = "consul_manager")
    public ConsulManager getConsulManager() {
        return new SimpleConsulManager(consulHost, nodeName, Integer.valueOf(consulPort));
    }

    /**
     * Node config updater.
     * @return constructed updater.
     */
    @Bean(name = "updater")
    public Updater getUpdater() {
        return new SimpleUpdater(getConsulManager());
    }

}