package de.chandre.admintool.log4j2;

import java.io.IOException;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@WebListener
@Component
public class AdminToolLog4jSessionListener implements HttpSessionListener {
	
	private static final Log LOGGER = LogFactory.getLog(AdminToolLog4jSessionListener.class);
	
	public AdminToolLog4jSessionListener() {
		LOGGER.debug("Session listener to remove useless outputstreams has been initialiezed");
	}
	
	@Autowired
	private AdminToolLog4j2Util log4jUtil;

	@Override
	public void sessionCreated(HttpSessionEvent sessionEvent) {}

	@Override
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		String appenderName = (String) sessionEvent.getSession().getAttribute(AdminToolLog4j2Util.SESSION_APPENDER_NAME);
		try {
			log4jUtil.closeOutputStreamAppender(appenderName);
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
		}
	}

}

@@ -29,17 +29,19 @@
 .log4j2;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -60,17 +60,19 @@
 ception;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -108,16 +108,17 @@
 istener;
+%0D
 %0Aimport 
@@ -153,16 +153,17 @@
 onEvent;
+%0D
 %0Aimport 
@@ -201,17 +201,19 @@
 istener;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -243,16 +243,17 @@
 ing.Log;
+%0D
 %0Aimport 
@@ -290,16 +290,17 @@
 Factory;
+%0D
 %0Aimport 
@@ -354,16 +354,17 @@
 towired;
+%0D
 %0Aimport 
@@ -404,17 +404,182 @@
 mponent;
-%0A
+%0D%0A%0D%0A/**%0D%0A * Session listener to remove unused outputstream appender to free resources%0D%0A * you may have to configure @ServletComponentScan %0D%0A * @author Andr%C3%A9%0D%0A *%0D%0A */%0D
 %0A@WebLis
@@ -583,16 +583,17 @@
 Listener
+%0D
 %0A@Compon
@@ -595,16 +595,17 @@
 omponent
+%0D
 %0Apublic 
@@ -672,18 +672,20 @@
 stener %7B
+%0D
 %0A%09
+%0D
 %0A%09privat
@@ -767,18 +767,20 @@
 .class);
+%0D
 %0A%09
+%0D
 %0A%09public
@@ -813,16 +813,17 @@
 ener() %7B
+%0D
 %0A%09%09LOGGE
@@ -908,13 +908,16 @@
 d%22);
+%0D
 %0A%09%7D
+%0D
 %0A%09
+%0D
 %0A%09@A
@@ -924,16 +924,17 @@
 utowired
+%0D
 %0A%09privat
@@ -965,17 +965,19 @@
 g4jUtil;
-%0A
+%0D%0A%0D
 %0A%09@Overr
@@ -971,32 +971,33 @@
 l;%0D%0A%0D%0A%09@Override
+%0D
 %0A%09public void se
@@ -1042,17 +1042,19 @@
 vent) %7B%7D
-%0A
+%0D%0A%0D
 %0A%09@Overr
@@ -1056,16 +1056,17 @@
 Override
+%0D
 %0A%09public
@@ -1120,16 +1120,17 @@
 Event) %7B
+%0D
 %0A%09%09Strin
@@ -1241,16 +1241,17 @@
 ME);
+%0D
 %0A%09%09try %7B
 %0A%09%09%09
@@ -1246,17 +1246,48 @@
 %0A%09%09try %7B
+%0D
 %0A
+%09%09%09if (null != log4jUtil) %7B%0D%0A%09
 %09%09%09log4j
@@ -1331,16 +1331,23 @@
 erName);
+%0D%0A%09%09%09%7D%0D
 %0A%09%09%7D cat
@@ -1366,16 +1366,17 @@
 ion e) %7B
+%0D
 %0A%09%09%09LOGG
@@ -1407,15 +1407,18 @@
  e);
+%0D
 %0A%09%09%7D
+%0D
 %0A%09%7D
-%0A
+%0D
 %0A%7D
+%0D
 %0A

Fix NPE in Log4j SessionListener