incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r635535 - in /incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl: SlingMainServlet.java helper/SlingServletContext.java
Date Mon, 10 Mar 2008 12:01:37 GMT
Author: fmeschbe
Date: Mon Mar 10 05:01:29 2008
New Revision: 635535

URL: http://svn.apache.org/viewvc?rev=635535&view=rev
Log:
SLING-310 Dispose off SlingServletContext when the SlingMainServlet is deactivated
and prevent NPE if the SlingServletContext is used after SlingMainServlet has been
deactiveted (this may be when depending components are being taken down because
the SlingServletContext is unregistered)

Modified:
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java?rev=635535&r1=635534&r2=635535&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
(original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
Mon Mar 10 05:01:29 2008
@@ -627,7 +627,11 @@
             slingAuthenticator = null;
         }
 
-        this.slingServletContext = null;
+        if (slingServletContext != null) {
+            slingServletContext.dispose();
+            slingServletContext = null;
+        }
+
         this.osgiComponentContext = null;
 
         activated = false;

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java?rev=635535&r1=635534&r2=635535&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java
(original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java
Mon Mar 10 05:01:29 2008
@@ -30,6 +30,7 @@
 import javax.servlet.RequestDispatcher;
 import javax.servlet.Servlet;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
 
 import org.apache.sling.core.impl.SlingMainServlet;
 import org.apache.sling.core.impl.request.SlingRequestDispatcher;
@@ -110,16 +111,27 @@
      * {@link SlingMainServlet} is running.
      */
     public String getServletContextName() {
-        return getServletContext().getServletContextName();
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getServletContextName();
+        }
+        
+        return null;
     }
 
     /** Returns the context path of the web application. (Servlet API 2.5) */
     public String getContextPath() {
-        try {
-            return (String) getServletContext().getClass().getMethod("getContextPath", (Class<?>[])
null).invoke(getServletContext(), (Object[]) null);
-        } catch (Exception ignore) {
-            return null;
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            try {
+                return (String) delegatee.getClass().getMethod(
+                    "getContextPath", (Class<?>[]) null).invoke(
+                    getServletContext(), (Object[]) null);
+            } catch (Throwable ignore) {
+            }
         }
+
+        return null;
     }
 
     /**
@@ -128,7 +140,12 @@
      * which the {@link SlingMainServlet} is running.
      */
     public String getInitParameter(String name) {
-        return getServletContext().getInitParameter(name);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getInitParameter(name);
+        }
+        
+        return null;
     }
 
     /**
@@ -138,7 +155,12 @@
      */
     @SuppressWarnings("unchecked")
     public Enumeration<String> getInitParameterNames() {
-        return getServletContext().getInitParameterNames();
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getInitParameterNames();
+        }
+        
+        return null;
     }
 
     // ---------- attributes ---------------------------------------------------
@@ -149,7 +171,12 @@
      * running.
      */
     public Object getAttribute(String name) {
-        return getServletContext().getAttribute(name);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getAttribute(name);
+        }
+        
+        return null;
     }
 
     /**
@@ -159,7 +186,12 @@
      */
     @SuppressWarnings("unchecked")
     public Enumeration<String> getAttributeNames() {
-        return getServletContext().getAttributeNames();
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getAttributeNames();
+        }
+        
+        return Collections.enumeration(Collections.<String>emptyList());
     }
 
     /**
@@ -168,7 +200,10 @@
      * running.
      */
     public void removeAttribute(String name) {
-        getServletContext().removeAttribute(name);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            delegatee.removeAttribute(name);
+        }
     }
 
     /**
@@ -177,7 +212,10 @@
      * {@link SlingMainServlet} is running.
      */
     public void setAttribute(String name, Object object) {
-        getServletContext().setAttribute(name, object);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            delegatee.setAttribute(name, object);
+        }
     }
 
     // ---------- Servlet Container information --------------------------------
@@ -197,7 +235,12 @@
      * running.
      */
     public int getMajorVersion() {
-        return getServletContext().getMajorVersion();
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getMajorVersion();
+        }
+        
+        return 2; // hard coded major version as fall back
     }
 
     /**
@@ -207,7 +250,12 @@
      * running.
      */
     public int getMinorVersion() {
-        return getServletContext().getMinorVersion();
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getMinorVersion();
+        }
+        
+        return 4; // hard coded minor version as fall back
     }
 
     // ---------- MIME type mapping --------------------------------------------
@@ -218,7 +266,12 @@
      * {@link SlingMainServlet} is running.
      */
     public String getMimeType(String file) {
-        return getServletContext().getMimeType(file);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getMimeType(file);
+        }
+        
+        return null;
     }
 
     // ---------- Request Dispatcher -------------------------------------------
@@ -243,7 +296,12 @@
      * {@link SlingMainServlet} is running.
      */
     public RequestDispatcher getNamedDispatcher(String name) {
-        return getServletContext().getNamedDispatcher(name);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getNamedDispatcher(name);
+        }
+        
+        return null;
     }
 
     // ---------- Resource Access ----------------------------------------------
@@ -254,7 +312,12 @@
      * running.
      */
     public URL getResource(String path) throws MalformedURLException {
-        return getServletContext().getResource(path);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getResource(path);
+        }
+        
+        return null;
     }
 
     /**
@@ -263,7 +326,12 @@
      * running.
      */
     public InputStream getResourceAsStream(String path) {
-        return getServletContext().getResourceAsStream(path);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getResourceAsStream(path);
+        }
+        
+        return null;
     }
 
     /**
@@ -273,7 +341,12 @@
      */
     @SuppressWarnings("unchecked")
     public Set<String> getResourcePaths(String parentPath) {
-        return getServletContext().getResourcePaths(parentPath);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getResourcePaths(parentPath);
+        }
+        
+        return null;
     }
 
     /**
@@ -283,7 +356,12 @@
      * is running.
      */
     public String getRealPath(String path) {
-        return getServletContext().getRealPath(path);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getRealPath(path);
+        }
+        
+        return null;
     }
 
     // ---------- logging ------------------------------------------------------
@@ -312,7 +390,12 @@
      * the {@link SlingMainServlet} is running.
      */
     public ServletContext getContext(String uripath) {
-        return getServletContext().getContext(uripath);
+        ServletContext delegatee = getServletContext();
+        if (delegatee != null) {
+            return delegatee.getContext(uripath);
+        }
+        
+        return null;
     }
 
     /** Returns <code>null</code> as defined in Servlet API 2.4 */
@@ -342,5 +425,4 @@
     private ServletContext getServletContext() {
         return slingMainServlet.getServletContext();
     }
-
 }



Mime
View raw message