package com.xt.hb.gis.system;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSONObject;
import com.xt.hb.gis.common.DataServerGlobal;
import com.xt.hb.gis.service.SystemService;

/**
 * 系统初始化
 * 
 * @author 袁晓冬
 *
 */
public class InitServlet extends HttpServlet {
	private final Logger log = LoggerFactory.getLogger(InitServlet.class);

	/** serialVersionUID */
	private static final long serialVersionUID = 3137976979537814808L;
	private AutowireCapableBeanFactory beanFactory;
	private RestTemplate restTemplate;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		System.err.println("system init start......");
		try {
			beanFactory = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext())
					.getAutowireCapableBeanFactory();
			SystemService systemService = beanFactory.getBean(SystemService.class);
			restTemplate = beanFactory.getBean(RestTemplate.class);
			System.err.println(systemService.reloadParam());
			StringBuilder url = new StringBuilder();
			if (!DataServerGlobal.SERVER_URL.startsWith("http")) {
				url.append("http://");
			}
			url.append(DataServerGlobal.SERVER_URL).append("/services");
			JSONObject info = restTemplate.getForObject(url.toString(), JSONObject.class);
			DataServerGlobal.initServices(info);
			log.info("system init success!!");
			System.err.println("system init success!!");
		} catch (Throwable t) {
			t.printStackTrace();
			throw t;
		}
	}
}
