KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > RequestContextListener


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet;
4
5 import javax.servlet.ServletRequestListener JavaDoc;
6 import javax.servlet.ServletRequestEvent JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8
9 /**
10  * Store request in current and all child threads, so it can be easily accessible.
11  */

12 public class RequestContextListener implements ServletRequestListener JavaDoc {
13
14     private static final ThreadLocal JavaDoc requestHolder = new InheritableThreadLocal JavaDoc();
15
16     public void requestInitialized(ServletRequestEvent JavaDoc requestEvent) {
17         if ((requestEvent.getServletRequest() instanceof HttpServletRequest JavaDoc) == false) {
18             throw new IllegalArgumentException JavaDoc("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
19         }
20         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestEvent.getServletRequest();
21         requestHolder.set(request);
22     }
23
24     public void requestDestroyed(ServletRequestEvent JavaDoc servletRequestEvent) {
25         requestHolder.set(null);
26     }
27
28     /**
29      * Returns current http servlet request.
30      */

31     public static HttpServletRequest JavaDoc getRequest() {
32         return (HttpServletRequest JavaDoc) requestHolder.get();
33     }
34 }
Popular Tags