package org.javaosc.galaxy.web;

import java.beans.Introspector;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.javaosc.galaxy.constant.Configuration;
import org.javaosc.galaxy.context.BeanFactory;
import org.javaosc.galaxy.context.ConfigExtHandler;
import org.javaosc.galaxy.context.ConfigHandler;
import org.javaosc.galaxy.context.ScanAnnotation;
import org.javaosc.galaxy.context.ScanPackage;
import org.javaosc.galaxy.jdbc.ConnectionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @description
 * @author Dylan Tao
 * @date 2014-09-09
 * Copyright 2014 Javaosc Team. All Rights Reserved.
 */
public class ContextListener implements ServletContextListener {
	
	private static final Logger log = LoggerFactory.getLogger(ContextListener.class);

	public void contextDestroyed(ServletContextEvent event) {
		
		ActionContext.destroy();
		
		RouteNodeRegistry.clear();
		
		ConnectionHandler.destroy();
		
		ConfigHandler.clear();
		
		ConfigExtHandler.clear();
		
		BeanFactory.clear();
		
		Introspector.flushCaches();
		System.gc();
		
		log.info("====== Galaxy Framework startup failed ======");
	}

	public void contextInitialized(ServletContextEvent event) {
		
		long initTime = System.currentTimeMillis();
		
		ServletContext sc = event.getServletContext();
		
		ConfigHandler.load(sc.getInitParameter(Configuration.CONFIG_FILE_NAME));
		
		ConfigExtHandler.load(sc.getInitParameter(Configuration.EXPEND_FILE_NAME));
		
		ScanPackage scan = new ScanPackage();
		scan.load();
		scan = null;
		
		ScanAnnotation.registryAnnotation();
		
		ConnectionHandler.init();
		
		log.debug("Bean factory: {}", BeanFactory.beanMap);
		
		initTime = System.currentTimeMillis() - initTime;
		
		if(ConfigHandler.getStartedStatus()){
			log.info("    ______          __                         ");
			log.info("  .' ___  |        [  |            {}", "v1.0.0.RELEASE");
			log.info(" / .'   \\_|  ,--.   | |  ,--.   _   __   _   __ ");
			log.info(" | |   ____ `'_\\ :  | | `'_\\ : [ \\ [  ] [ \\ [  ]");
			log.info(" \\ `.___]  |// | |, | | // | |, > '  <   \\ '/ /    Startup in {} ms", initTime);
			log.info("  `._____.' \\'-;__/[___]\\'-;__/[__]`\\_] [ \\_:/  ");
			log.info("                                         \\_.'  ");
			log.info(" Thanks ~ Good Luck! | Community: www.javaosc.com  ");
		}else{
			log.info("====== Galaxy framework startup in {} ms ======", initTime);
		}
		
		ConfigHandler.clear();
		
		System.gc();
	}
}

@@ -1153,24 +1153,34 @@
 );%0A%09%09%0A%09%09
-log.info
+System.out.println
 (%22======
@@ -1847,32 +1847,242 @@
 atus())%7B%0A%09%09%09
-log.info
+String clsPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();%0A%09%09%09int index = clsPath.lastIndexOf('-');%0A%09%09%09clsPath = clsPath.substring(index+1,clsPath.length()-4);%0A%09%09%09System.out.println
 (%22    ______
@@ -2121,32 +2121,42 @@
      %22);%0A%09%09%09
-log.info
+System.out.println
 (%22  .' ___  
@@ -2184,43 +2184,43 @@
     
-%7B%7D%22, %22v1.0.0.RELEASE%22);%0A%09%09%09log.info
+%22 + clsPath);%0A%09%09%09System.out.println
 (%22 /
@@ -2273,24 +2273,34 @@
  %22);%0A%09%09%09
-log.info
+System.out.println
 (%22 %7C %7C  
@@ -2352,24 +2352,34 @@
 %5D%22);%0A%09%09%09
-log.info
+System.out.println
 (%22 %5C%5C %60.
@@ -2440,15 +2440,10 @@
  in 
-%7B%7D ms%22,
+%22+
  ini
@@ -2447,30 +2447,47 @@
 initTime
-);%0A%09%09%09log.info
++ %22 ms%22);%0A%09%09%09System.out.println
 (%22  %60.__
@@ -2539,24 +2539,34 @@
  %22);%0A%09%09%09
-log.info
+System.out.println
 (%22      
@@ -2614,24 +2614,34 @@
  %22);%0A%09%09%09
-log.info
+System.out.println
 (%22 Thank
@@ -2705,16 +2705,26 @@
 %0A%09%09%09
-log.info
+System.out.println
 (%22==
@@ -2756,18 +2756,30 @@
 rtup in 
-%7B%7D
+%22+ initTime +%22
  ms ====
@@ -2785,18 +2785,8 @@
 ===%22
-, initTime
 );%0A%09

update started status log-> system.out