/**
 * Copyright 2007-2012 Jordi Hernndez Sells, Ibrahim Chaehoi
 * 
 * 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 net.jawr.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jawr.web.JawrConstant;

import org.apache.log4j.Logger;

/**
 *
 * Main Jawr servlet. Maps logical URLs to script bundles, which are generated on the fly (may 
 * be cached), and served as a single file. 
 * 
 * 
 * @author Jordi Hernndez Sells
 * @author Ibrahim Chaehoi
 */
public class JawrServlet extends HttpServlet {
	
	/** The serial version UID */ 
	private static final long serialVersionUID = -4551240917172286444L;

	/** The logger */
	private static final Logger LOGGER = Logger.getLogger(JawrServlet.class);

	/** The request handler */
	protected JawrRequestHandler requestHandler;

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() throws ServletException {
		try {
			String type = getServletConfig().getInitParameter(JawrConstant.TYPE_INIT_PARAMETER);
			if(JawrConstant.IMG_TYPE.equals(type)){
				requestHandler = new JawrImageRequestHandler(getServletContext(),getServletConfig());
			}else{
				requestHandler = new JawrRequestHandler(getServletContext(),getServletConfig());
			}
			//getServletConfig().getServletContext().
		}catch (ServletException e) {
			LOGGER.fatal("Jawr servlet with name " +  getServletConfig().getServletName() +" failed to initialize properly. ");
			LOGGER.fatal("Cause:");
			LOGGER.fatal(e.getMessage(),e);
			throw e;
		}catch (RuntimeException e) {
			LOGGER.fatal("Jawr servlet with name " +  getServletConfig().getServletName() +" failed to initialize properly. ");
			LOGGER.fatal("Cause: ");
			LOGGER.fatal(e.getMessage(),e);
			throw new ServletException(e);
		}
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		requestHandler.doGet(req, resp);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#destroy()
	 */
	public void destroy() {		
		super.destroy();
		requestHandler.destroy();
	}
     
}

@@ -23,32 +23,33 @@
 -2012 Jordi Hern
+%EF%BF%BD
 ndez Sells, Ibra
@@ -41,16 +41,17 @@
 dez Sell
+%EF%BF%BD
 s, Ibrah
@@ -689,24 +689,113 @@
 ception;%0D%0A%0D%0A
+import javax.servlet.ServletContextEvent;%0D%0Aimport javax.servlet.ServletContextListener;%0D%0A
 import javax
@@ -1214,16 +1214,17 @@
 rdi Hern
+%EF%BF%BD
 ndez Sel
@@ -1224,16 +1224,17 @@
 dez Sell
+%EF%BF%BD
 s%0D%0A * @a
@@ -1306,16 +1306,50 @@
 Servlet 
+implements ServletContextListener 
 %7B%0D%0A%09%0D%0A%09/
@@ -3036,28 +3036,8 @@
 %09%09%0D%0A
-%09%09super.destroy();%0D%0A
 %09%09re
@@ -3064,16 +3064,455 @@
 ();%0D%0A%09%7D%0D
+%0A%0D%0A%09/* (non-Javadoc)%0D%0A%09 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)%0D%0A%09 */%0D%0A%09@Override%0D%0A%09public void contextInitialized(ServletContextEvent sce) %7B%0D%0A%09%09%0D%0A%09%7D%0D%0A%0D%0A%09/* (non-Javadoc)%0D%0A%09 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)%0D%0A%09 */%0D%0A%09@Override%0D%0A%09public void contextDestroyed(ServletContextEvent sce) %7B%0D%0A%09%09requestHandler.destroy();%09%09%0D%0A%09%7D%0D
 %0A     %0D%0A

Fix issue with threadLocal on servletContext destruction