com.apipulse.bastion.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.apipulse.bastion.Main.java

Source

/*
 * Bastion Data Ingestor
 * Copyright (c) 2016 API Fortress Inc.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.apipulse.bastion;

import akka.actor.ActorRef;
import com.apipulse.bastion.actors.BastionActors;
import com.apipulse.bastion.actors.messages.StopMessage;
import com.apipulse.bastion.controllers.ApiController;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * The main class. Launches the configured chains and the servlet container
 */
@SpringBootApplication
public class Main {

    private static final Logger log = Logger.getLogger(Main.class);

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        log.info("Bastion starting. Loading chains");
        final File dir = new File("etc/chains");

        final LinkedList<ActorRef> chains = new LinkedList<ActorRef>();
        for (File file : dir.listFiles()) {
            if (!file.getName().startsWith(".") && file.getName().endsWith(".yaml")) {
                log.info("Loading chain: " + file.getName());
                ChainConfig config = new ChainConfig(IOUtils.toString(new FileReader(file)));
                ActorRef ref = BastionActors.getInstance().initChain(config.getName(),
                        Class.forName(config.getQualifiedClass()));
                Iterator<StageConfig> iterator = config.getStages().iterator();
                while (iterator.hasNext())
                    ref.tell(iterator.next(), null);
                chains.add(ref);
            }
        }
        SpringApplication app = new SpringApplication();
        HashSet<Object> objects = new HashSet<Object>();
        objects.add(ApiController.class);
        final ConfigurableApplicationContext context = app.run(ApiController.class, args);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    log.info("Bastion shutting down");
                    Iterator<ActorRef> iterator = chains.iterator();
                    while (iterator.hasNext())
                        iterator.next().tell(new StopMessage(), null);
                    Thread.sleep(2000);
                    context.stop();
                    log.info("Bastion shutdown complete");
                } catch (Exception e) {
                }
            }
        });
    }
}