From: <fg...@us...> - 2007-02-19 17:13:53
|
Revision: 275 http://svn.sourceforge.net/openutils/?rev=275&view=rev Author: fgiust Date: 2007-02-19 09:13:52 -0800 (Mon, 19 Feb 2007) Log Message: ----------- new WebApplicationContextListener for thread local access to string context Added Paths: ----------- trunk/openutils-spring/src/main/java/it/openutils/spring/context/ trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java Added: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java 2007-02-19 17:13:52 UTC (rev 275) @@ -0,0 +1,44 @@ +package it.openutils.spring.context; + +import org.springframework.web.context.WebApplicationContext; + + +/** + * Holder class to expose the web application context in the form of a thread-bound {@link WebApplicationContext} + * object. + * <p> + * Use {@link WebApplicationContextListener} to expose the current application context. + * @author Fabrizio Giustina + * @version $Id$ + */ +public final class WebApplicationContextHolder +{ + + private static ThreadLocal<WebApplicationContext> tl = new ThreadLocal<WebApplicationContext>(); + + /** + * Don't instantiate. + */ + private WebApplicationContextHolder() + { + // unused + } + + /** + * Returns the thread-bound WebApplicationContext. + * @return WebApplicationContext + */ + public static WebApplicationContext get() + { + return tl.get(); + } + + /** + * Bind a WebApplicationContext to the current thread. + * @param webApplicationContext WebApplicationContext + */ + public static void set(WebApplicationContext webApplicationContext) + { + tl.set(webApplicationContext); + } +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java 2007-02-19 17:13:52 UTC (rev 275) @@ -0,0 +1,36 @@ +package it.openutils.spring.context; + +import javax.servlet.ServletRequestEvent; +import javax.servlet.ServletRequestListener; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + + +/** + * Listener for Servlet 2.4+ containers. Exposes the web application context to the current thread, through + * WebApplicationContextHolder. To be registered as listener in <code>web.xml</code>. + * @author Fabrizio Giustina + * @version $Id$ + */ +public class WebApplicationContextListener implements ServletRequestListener +{ + + /** + * {@inheritDoc} + */ + public void requestInitialized(ServletRequestEvent sre) + { + WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sre.getServletContext()); + WebApplicationContextHolder.set(wac); + } + + /** + * {@inheritDoc} + */ + public void requestDestroyed(ServletRequestEvent sre) + { + WebApplicationContextHolder.set(null); + } + +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |