package com.ctrip.hermes.rest.common;

import java.io.File;
import java.io.IOException;

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

import com.ctrip.hermes.core.utils.PlexusComponentLocator;
import com.ctrip.hermes.rest.service.SubscriptionRegisterService;
import com.dianping.cat.Cat;
import com.yammer.metrics.Metrics;

public class GatewayListener implements ServletContextListener {

	private SubscriptionRegisterService subsribeRegistry = PlexusComponentLocator
	      .lookup(SubscriptionRegisterService.class);

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		File catConfigFile = new File("/opt/ctrip/data/cat/client.xml");
		if (!catConfigFile.isFile() || !catConfigFile.canRead()) {
			try {
				throw new IllegalStateException(
						  String.format("Cat config file %s not found", catConfigFile.getCanonicalPath()));
			} catch (IOException e) {
				// todo: handle exception
				e.printStackTrace();
			}
		}
		Cat.initialize(catConfigFile);

		subsribeRegistry.start();
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		subsribeRegistry.stop();

		Metrics.defaultRegistry().shutdown();
		
	}

}

@@ -631,399 +631,8 @@
 ) %7B%0A
-%09%09File catConfigFile = new File(%22/opt/ctrip/data/cat/client.xml%22);%0A%09%09if (!catConfigFile.isFile() %7C%7C !catConfigFile.canRead()) %7B%0A%09%09%09try %7B%0A%09%09%09%09throw new IllegalStateException(%0A%09%09%09%09%09%09  String.format(%22Cat config file %25s not found%22, catConfigFile.getCanonicalPath()));%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09// todo: handle exception%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09Cat.initialize(catConfigFile);%0A%0A
 %09%09su

remove Cat.initialize