package com.uas.report;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.uas.report.util.ContextUtils;
import com.uas.report.util.ReportUtils;

/**
 * 不使用spring boot内嵌的容器，部署在自定义Tomcat下时的程序入口
 * 
 * @author sunyj
 * @since 2017年1月11日 下午5:18:41
 */
@SpringBootApplication(scanBasePackages = "com.uas.report")
@EnableWebMvc
public class Application extends SpringBootServletInitializer implements ApplicationContextAware {

	private static Logger logger = LoggerFactory.getLogger(Application.class);

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		try {
			System.setErr(new PrintStream(new FileOutputStream("logs/log.log", true)));
		} catch (FileNotFoundException e) {
			logger.error("", e);
		}
		// 程序入口
		return builder.sources(Application.class);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// 注册spring上下文对象
		ContextUtils.setApplicationContext(applicationContext);
		logger.info("ApplicationContext registed");
		// 开启定时任务
		ReportUtils.startTask();
	}
}
