package com.ctrip.platform.dal.dao.helper;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ctrip.platform.dal.dao.DalClientFactory;

public class DalClientFactoryListener implements ServletContextListener {
	private Logger logger = LoggerFactory.getLogger(DalClientFactoryListener.class);
	
	public void contextInitialized(ServletContextEvent sce) {
		logger.info("Dal Factory Listener is about to start");
		ServletContext context = sce.getServletContext();
		
		String DalConfigPath = context.getInitParameter("com.ctrip.platform.dal.dao.DalConfigPath");
		String warmUp = context.getInitParameter("com.ctrip.platform.dal.dao.DalWarmUp");

		try {
			if(DalConfigPath == null || DalConfigPath.trim().length() == 0)
				DalClientFactory.initClientFactory();
			else
				DalClientFactory.initClientFactory(DalConfigPath.trim());
			
			if(Boolean.parseBoolean(warmUp))
				DalClientFactory.warmUpConnections();
			
			/* The startup and shutdown of ConfigBeanFactory must be done here.
			 * Because only web app can have classes folder for bean.properties
			 */
//			ConfigBeanFactory.init();

		} catch (Exception e) {
			logger.error("Error when init client factory", e);
		}
	}
	
	public void contextDestroyed(ServletContextEvent sce) {
		DalClientFactory.shutdownFactory();

//		ConfigBeanFactory.shutdown();
	}
}

@@ -35,17 +35,19 @@
 .helper;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -75,16 +75,17 @@
 Context;
+%0D
 %0Aimport 
@@ -118,16 +118,17 @@
 xtEvent;
+%0D
 %0Aimport 
@@ -164,17 +164,19 @@
 istener;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -192,16 +192,17 @@
 .Logger;
+%0D
 %0Aimport 
@@ -225,17 +225,19 @@
 Factory;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -280,17 +280,19 @@
 Factory;
-%0A
+%0D%0A%0D
 %0Apublic 
@@ -357,16 +357,17 @@
 stener %7B
+%0D
 %0A%09privat
@@ -440,18 +440,20 @@
 .class);
+%0D
 %0A%09
+%0D
 %0A%09public
@@ -503,16 +503,17 @@
 t sce) %7B
+%0D
 %0A%09%09logge
@@ -561,16 +561,17 @@
 start%22);
+%0D
 %0A%09%09Servl
@@ -614,19 +614,21 @@
 ntext();
+%0D
 %0A%09%09
+%0D
 %0A%09%09Strin
@@ -714,16 +714,17 @@
 gPath%22);
+%0D
 %0A%09%09Strin
@@ -803,17 +803,19 @@
 p%22);
-%0A
+%0D%0A%0D
 %0A%09%09try %7B
 %0A%09%09%09
@@ -810,16 +810,17 @@
 %0A%09%09try %7B
+%0D
 %0A%09%09%09if(D
@@ -878,16 +878,17 @@
 () == 0)
+%0D
 %0A%09%09%09%09Dal
@@ -925,16 +925,17 @@
 y();
+%0D
 %0A%09%09%09else
 %0A%09%09%09
@@ -930,16 +930,17 @@
 %0A%09%09%09else
+%0D
 %0A%09%09%09%09Dal
@@ -993,20 +993,22 @@
 trim());
+%0D
 %0A%09%09%09
+%0D
 %0A%09%09%09if(B
@@ -1035,16 +1035,17 @@
 warmUp))
+%0D
 %0A%09%09%09%09Dal
@@ -1082,219 +1082,38 @@
 s();
-%0A%09%09%09%0A%09%09%09/* The startup and shutdown of ConfigBeanFactory must be done here.%0A%09%09%09 * Because only web app can have classes folder for bean.properties%0A%09%09%09 */%0A//%09%09%09ConfigBeanFactory.init();%0A%0A%09%09%7D catch (Exception
+%0D%0A%0D%0A%09%09%7D catch (Throwable
  e) %7B
+%0D
 %0A%09%09%09
@@ -1166,17 +1166,56 @@
  e);
+%0D%0A%09%09%09throw new RuntimeException(e);%0D
 %0A%09%09%7D
+%0D
 %0A%09%7D
+%0D
 %0A%09
+%0D
 %0A%09pu
@@ -1267,16 +1267,17 @@
 t sce) %7B
+%0D
 %0A%09%09DalCl
@@ -1310,45 +1310,13 @@
 y();
-%0A%0A//%09%09ConfigBeanFactory.shutdown();
+%0D
 %0A%09%7D
+%0D
 %0A%7D
+%0D
 %0A

revise exception handling