org.verwandlung.voj.judger.application.ApplicationBootstrap.java Source code

Java tutorial

Introduction

Here is the source code for org.verwandlung.voj.judger.application.ApplicationBootstrap.java

Source

/* Verwandlung Online Judge - A cross-platform judge online system
 * Copyright (C) 2018 Haozhe Xie <cshzxie@gmail.com>
 *
 * 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *                              _ooOoo_  
 *                             o8888888o  
 *                             88" . "88  
 *                             (| -_- |)  
 *                             O\  =  /O  
 *                          ____/`---'\____  
 *                        .'  \\|     |//  `.  
 *                       /  \\|||  :  |||//  \  
 *                      /  _||||| -:- |||||-  \  
 *                      |   | \\\  -  /// |   |  
 *                      | \_|  ''\---/''  |   |  
 *                      \  .-\__  `-`  ___/-. /  
 *                    ___`. .'  /--.--\  `. . __  
 *                 ."" '<  `.___\_<|>_/___.'  >'"".  
 *                | | :  `- \`.;`\ _ /`;.`/ - ` : | |  
 *                \  \ `-.   \_ __\ /__ _/   .-` /  /  
 *           ======`-.____`-.___\_____/___.-`____.-'======  
 *                              `=---=' 
 *
 *                          HERE BE BUDDHA
 *
 */
package org.verwandlung.voj.judger.application;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.verwandlung.voj.judger.mapper.LanguageMapper;
import org.verwandlung.voj.judger.model.Language;

/**
 * ??.
 * 
 * @author Haozhe Xie
 */
public class ApplicationBootstrap {
    /**
     * ??.
     */
    public static void main(String[] args) {
        LOGGER.info("Starting Verwandlung Online Judge Judger...");
        ApplicationBootstrap app = new ApplicationBootstrap();
        app.getApplicationContext();
        app.setupHeartBeat();
        app.getSystemEnvironment();
        app.setUpShutdownHook();
        LOGGER.info("Verwandlung Online Judge Judger started.");
    }

    /**
     * ??.
     */
    private void getApplicationContext() {
        applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
    }

    /**
     * ?Web?.
     * ?Web???Keep-Alive?.
     */
    private void setupHeartBeat() {
        final int INITIAL_DELAY = 0;
        final int PERIOD = 25;

        ApplicationHeartbeat heartbeat = applicationContext.getBean(ApplicationHeartbeat.class);
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(heartbeat, INITIAL_DELAY, PERIOD, TimeUnit.MINUTES);
    }

    /**
     * ShutdownHook.
     * ???.
     */
    private void setUpShutdownHook() {
        final Thread mainThread = Thread.currentThread();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                try {
                    LOGGER.info("Verwandlung Online Judge Judger is shutting down...");
                    mainThread.join();
                } catch (InterruptedException ex) {
                    LOGGER.catching(ex);
                }
            }
        });
    }

    /**
     * ???.
     * Bug?.
     */
    private void getSystemEnvironment() {
        LOGGER.info("System Information: ");
        LOGGER.info("\tOperating System Name: " + System.getProperty("os.name"));
        LOGGER.info("\tOperating System Version: " + System.getProperty("os.version"));
        LOGGER.info("\tJava VM Name: " + System.getProperty("java.vm.name"));
        LOGGER.info("\tJava Runtime Version: " + System.getProperty("java.runtime.version"));

        LOGGER.info("Compiler Information: ");
        LanguageMapper languageMapper = applicationContext.getBean(LanguageMapper.class);
        List<Language> languages = languageMapper.getAllLanguages();
        for (Language language : languages) {
            String languageName = language.getLanguageName();
            String compileProgram = getCompileProgram(language.getCompileCommand());
            LOGGER.info("\t" + languageName + ": " + getCompilerVersion(languageName, compileProgram));
        }
    }

    /**
     * ??.
     * @param compileCommand - 
     * @return ?
     */
    private String getCompileProgram(String compileCommand) {
        int firstSpaceIndex = compileCommand.indexOf(" ");
        String compileProgram = compileCommand.substring(0, firstSpaceIndex);

        if ("javac".equalsIgnoreCase(compileProgram)) {
            return "java";
        }
        return compileProgram;
    }

    /**
     * ??.
     * @param languageName - ??
     * @param compileProgram -  
     * @return ?
     */
    private String getCompilerVersion(String languageName, String compileProgram) {
        String versionCommand = getVersionCommand(languageName);
        StringBuilder compilerVersion = new StringBuilder();

        try {
            String command = compileProgram + versionCommand;
            Process process = Runtime.getRuntime().exec(command);

            compilerVersion.append("Command Line: " + command + "\n");
            compilerVersion.append(IOUtils.toString(process.getInputStream()));
            compilerVersion.append(IOUtils.toString(process.getErrorStream()));
        } catch (Exception ex) {
            return "Not Found";
        }
        return compilerVersion.toString();
    }

    /**
     * ??.
     * @param languageName - ??
     * @return ??
     */
    private String getVersionCommand(String languageName) {
        if ("Java".equalsIgnoreCase(languageName)) {
            return " -version";
        }
        return " --version";
    }

    /**
     * ??.
     */
    private ApplicationContext applicationContext;

    /**
     * .
     */
    private static final Logger LOGGER = LogManager.getLogger(ApplicationBootstrap.class);
}