com.zergiu.tvman.TVMan.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.TVMan.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2011.
 *
 * This file is part of TVMan.
 *
 * TVMan 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.
 *
 * TVMan 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 TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.zergiu.tvman;

import java.net.URL;
import java.util.Enumeration;

import org.apache.log4j.Level;
import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

public class TVMan {

    static {
        try {
            //configure log4j
            URL log4jConfFile = ResourceUtils.getURL("classpath:com/zergiu/tvman/web/WEB-INF/log4j.xml");
            DOMConfigurator.configure(log4jConfFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Logger log = LoggerFactory.getLogger(TVMan.class);

    public static void main(String[] args) {
        try {
            TVManOptions options = TVManOptions.getInstance();
            options.parseCommandLine(args);
            configureLoggers(options);
            log.info("Starting TVMan .... ");
            final TVManWebServer server = new TVManWebServer();
            addShutdownHook(server);
            startServer(server);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }

    private static void startServer(final TVManWebServer server) {
        Thread startupThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    server.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        startupThread.start();
    }

    private static void addShutdownHook(final TVManWebServer server) {
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                server.shutdown();
            }
        }));
    }

    private static void configureLoggers(TVManOptions options) {
        if (options.isDebug()) {
            org.apache.log4j.Logger log4JLogging = org.apache.log4j.Logger.getLogger("com.zergiu.tvman");
            log4JLogging.setLevel(Level.DEBUG);
        }

        if (options.isQuiet()) {
            org.apache.log4j.Logger log4JLogging = org.apache.log4j.Logger.getRootLogger();
            Enumeration<?> loggers = log4JLogging.getLoggerRepository().getCurrentLoggers();
            while (loggers.hasMoreElements()) {
                org.apache.log4j.Logger logger = (org.apache.log4j.Logger) loggers.nextElement();
                logger.setLevel(Level.ERROR);
            }
            log4JLogging.setLevel(Level.ERROR);
        }
    }

}