/**
 * Copyright © 2011-2012 EMBL - European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.ac.ebi.phenotype.util;

import java.util.Set;

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

public class ContextFinalizer implements ServletContextListener {

	public void contextInitialized(ServletContextEvent sce) {
	}

	public void contextDestroyed(ServletContextEvent sce) {

		// Try to kill the TOMCAT abandoned connection clean up thread
		// to prevent it from holding the whole context in memory causing
		// a leak

		Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
		Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);

		for (Thread t : threadArray) {
			if (t.getName().contains("Abandoned connection cleanup thread")) {
				synchronized (t) {
					t.stop(); // don't complain, it works
				}
			}
		}
	}

}

@@ -20,17 +20,17 @@
 2011-201
-2
+3
  EMBL - 
@@ -680,16 +680,163 @@
 ava.
-util.Set
+sql.Driver;%0Aimport java.sql.DriverManager;%0Aimport java.sql.SQLException;%0Aimport java.util.Enumeration;%0Aimport java.util.Set;%0Aimport java.util.logging.Level
 ;%0A%0Ai
@@ -922,16 +922,49 @@
 tener;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 public c
@@ -1022,16 +1022,93 @@
 ener %7B%0A%0A
+%09private Logger LOG = Logger.getLogger(this.getClass().getCanonicalName());%0A%0A
 %09public 
@@ -1222,16 +1222,617 @@
 e) %7B%0A%0A%09%09
+%0A        // This manually deregisters JDBC driver, which prevents Tomcat 7 from complaining about memory leaks wrto this class%0A        Enumeration%3CDriver%3E drivers = DriverManager.getDrivers();%0A        while (drivers.hasMoreElements()) %7B%0A            Driver driver = drivers.nextElement();%0A            try %7B%0A                DriverManager.deregisterDriver(driver);%0A                LOG.info(String.format(%22deregistering jdbc driver: %25s%22, driver));%0A            %7D catch (SQLException e) %7B%0A                LOG.warn(String.format(%22Error deregistering driver %25s%22, driver), e);%0A            %7D%0A        %7D%0A%0A        
 // Try t

Change copyright in Apache 2 license to 2013