package com.persagy.framework.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.persagy.core.mvc.service.CoreService;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.PropertyUtil;
import com.persagy.functions.weather.service.DayHisRecordService;
import com.persagy.functions.weather.service.DayStaticService;
import com.persagy.functions.weather.service.DependApiStaticService;
import com.persagy.functions.weather.service.DisasterWarningService;
import com.persagy.functions.weather.service.HourRecordService;
import com.persagy.functions.weather.thread.DayDataRecordThread;
import com.persagy.functions.weather.thread.DayStaticThread;
import com.persagy.functions.weather.thread.DependAPIStaticThread;
import com.persagy.functions.weather.thread.DisasterWarningThread;
import com.persagy.functions.weather.thread.HourDataRecordThread;
import com.persagy.functions.weather.thread.Min5LongHuThread;
import com.persagy.functions.weather.thread.RequestIpStatisticThread;

public class SystemInitServlet extends HttpServlet {
	private static final long serialVersionUID = 8077103762776343464L;
	private static final Logger log = Logger.getLogger(SystemInitServlet.class);

	@Override
	public void init() throws ServletException {
		log.info("system init start......");
		
		ServletContext sc = this.getServletContext();
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);

		CoreService coreService = (CoreService) context.getBean("coreService");
		DisasterWarningService disasterService = (DisasterWarningService) context.getBean("disasterWarningService");
		HourRecordService hourRecordService = (HourRecordService) context.getBean("hourRecordService");
		DayHisRecordService dayHisRecordService = (DayHisRecordService) context.getBean("dayHisRecordService");
		DayStaticService dayStaticService = (DayStaticService) context.getBean("dayStaticService");
		DependApiStaticService dependApiStaticService = (DependApiStaticService) context.getBean("dependApiStaticService");
		
		log.info("config info init start...");
		try {
			ConfigUtil.cache_refresh_all();
		} catch (Exception e) {
			log.error("load config info error");
			System.exit(1);
		}
		
		String threadRun = PropertyUtil.getSystemProperty("thread.model");
		if("true".equals(threadRun)) {
			log.info("business thread objects start...");
			new Thread(new Min5LongHuThread(coreService)).start();
			
			new Thread(new HourDataRecordThread(hourRecordService)).start();
			
			new Thread(new DisasterWarningThread(disasterService)).start();
			
			new Thread(new DayDataRecordThread(dayHisRecordService)).start();
			
			new Thread(new DayStaticThread(dayStaticService)).start();
			
			new Thread(new DependAPIStaticThread(dependApiStaticService, "Thread")).start();
		}else {
			RequestIpStatisticThread rist = new RequestIpStatisticThread(coreService);
			rist.init();
			new Thread(rist).start();
			
			new Thread(new DependAPIStaticThread(dependApiStaticService, "Business")).start();
		}
		
		log.info("system init end.");
	}
}
