Anonymous avatar Anonymous committed 0288c24

Added a CacheContextListener to allow for a clean startup and shutdown of the ServletCacheAdministrator and its application scoped cache.

Comments (0)

Files changed (3)

src/core/java/com/opensymphony/oscache/web/CacheContextListener.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.oscache.web;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+/**
+ *
+ * @author <a href="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
+ * @version $Revision$
+ */
+public class CacheContextListener implements ServletContextListener {
+    /**
+     * This notification occurs when the webapp is ready to process requests.<p>
+     * We use this hook to cleanly start up the {@link ServletCacheAdministrator}
+     * and create the application scope cache (which will consequentially
+     * initialize any listeners configured for it that implement <code>LifecycleAware</code>.)<p>
+     *
+     * As of Servlet 2.4, this is guaranteed to be called before any Servlet.init()
+     * methods.
+     */
+    public void contextInitialized(ServletContextEvent servletContextEvent) {
+        ServletContext context = servletContextEvent.getServletContext();
+        ServletCacheAdministrator.getInstance(context);
+    }
+
+    /**
+     * This notification occurs when the servlet context is about to be shut down.
+     * We use this hook to cleanly shut down the cache.
+     */
+    public void contextDestroyed(ServletContextEvent servletContextEvent) {
+        ServletContext context = servletContextEvent.getServletContext();
+        ServletCacheAdministrator.destroyInstance(context);
+    }
+}

src/core/java/com/opensymphony/oscache/web/ServletCacheAdministrator.java

             context.setAttribute(CACHE_ADMINISTRATOR_KEY, admin);
 
             if (log.isInfoEnabled()) {
-                log.info("getInstance(): Created new instance of ServletCacheAdministrator");
+                log.info("Created new instance of ServletCacheAdministrator");
             }
         }
 
 
             if (cache != null) {
                 admin.finalizeListeners(cache);
+                context.removeAttribute(admin.getCacheKey());
+                context.removeAttribute(CACHE_ADMINISTRATOR_KEY);
+                cache = null;
+
+                if (log.isInfoEnabled()) {
+                    log.info("Shut down the ServletCacheAdministrator");
+                }
             }
 
             admin = null;

src/webapp/WEB-INF/web.xml

 
 <web-app>
 
-   <display-name>OSCache</display-name>
+    <display-name>OSCache</display-name>
 
-   <filter>
-       <filter-name>oscache</filter-name>
-       <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
-   </filter>
+    <filter>
+        <filter-name>oscache</filter-name>
+        <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
+    </filter>
 
-   <filter-mapping>
-       <filter-name>oscache</filter-name>
-       <url-pattern>/filter/*</url-pattern>
-   </filter-mapping>
+    <filter-mapping>
+        <filter-name>oscache</filter-name>
+        <url-pattern>/filter/*</url-pattern>
+    </filter-mapping>
 
-   <servlet>
-      <servlet-name>oscache</servlet-name>
-      <servlet-class>com.opensymphony.oscache.web.OscacheServlet</servlet-class>
-      <load-on-startup>1</load-on-startup>
-   </servlet>
+    <listener>
+        <listener-class>com.opensymphony.oscache.web.CacheContextListener</listener-class>
+    </listener>
 
-   <servlet-mapping>
-      <servlet-name>oscache</servlet-name>
-      <url-pattern>/cacheServlet/*</url-pattern>
-   </servlet-mapping>
+    <servlet>
+        <servlet-name>oscache</servlet-name>
+        <servlet-class>com.opensymphony.oscache.web.OscacheServlet</servlet-class>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
 
-   <session-config>
-      <session-timeout>5</session-timeout>
-   </session-config>
+    <servlet-mapping>
+        <servlet-name>oscache</servlet-name>
+        <url-pattern>/cacheServlet/*</url-pattern>
+    </servlet-mapping>
 
-   <taglib>
-      <taglib-uri>oscache</taglib-uri>
-      <taglib-location>/WEB-INF/lib/oscache.jar</taglib-location>
-   </taglib>
+    <session-config>
+        <session-timeout>5</session-timeout>
+    </session-config>
+
+    <taglib>
+        <taglib-uri>oscache</taglib-uri>
+        <taglib-location>/WEB-INF/lib/oscache.jar</taglib-location>
+    </taglib>
 
 </web-app>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.