org.echocat.nodoodle.server.Main.java Source code

Java tutorial

Introduction

Here is the source code for org.echocat.nodoodle.server.Main.java

Source

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****
 *
 * Version: MPL 2.0
 *
 * echocat NoDoodle, Copyright (c) 2010-2012 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

package org.echocat.nodoodle.server;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.spi.Configurator;
import org.apache.log4j.xml.DOMConfigurator;
import org.echocat.nodoodle.NodoodleInformationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.File;
import java.io.FileReader;
import java.util.logging.LogManager;

import static org.apache.log4j.LogManager.*;

public class Main {

    private static final Logger LOG = LoggerFactory.getLogger(Main.class);
    private static final NodoodleInformationFactory INFORMATION_FACTORY = new NodoodleInformationFactory(
            Main.class);

    public static void main(String[] args) {
        final File log4jConfig = new File(System.getProperty("log4j.configuration", "../config/log4j.xml"));
        if (log4jConfig.isFile()) {
            try {
                final FileReader reader = new FileReader(log4jConfig);
                try {
                    final DOMConfigurator domConfigurator = new DOMConfigurator();
                    domConfigurator.doConfigure(reader, getLoggerRepository());
                } finally {
                    IOUtils.closeQuietly(reader);
                }
            } catch (Exception e) {
                throw new RuntimeException("Could not configure log4j with " + log4jConfig + ".", e);
            }
        }

        final String applicationName = getApplicationName();
        //System.setSecurityManager(new ServerSecurityManager());
        LOG.info("Starting " + applicationName + "...");

        final File config = new File(
                System.getProperty(Main.class.getPackage().getName() + ".config", "../config/nodoodleServer.xml"));
        final AbstractApplicationContext applicationContext = new FileSystemXmlApplicationContext(config.getPath());
        LOG.info("Starting " + applicationName + "... DONE!");
        Runtime.getRuntime().addShutdownHook(new Thread("destroyer") {
            @Override
            public void run() {
                LOG.info("Stopping " + applicationName + "...");
                applicationContext.stop();
                LOG.info("Stopping " + applicationName + "... DONE!");
            }
        });
    }

    public static String getApplicationName() {
        final String title = INFORMATION_FACTORY.getImplementationTitle();
        final String version = INFORMATION_FACTORY.getImplementationVersion();
        return (title != null ? title : "server") + (version != null ? " " + version : "");
    }
}