portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r1101917 - in /portals/jetspeed-2/portal/trunk: jetspeed-api/src/main/java/org/apache/jetspeed/util/ jetspeed-commons/src/main/java/org/apache/jetspeed/util/
Date Wed, 11 May 2011 15:19:31 GMT
Author: ate
Date: Wed May 11 15:19:31 2011
New Revision: 1101917

URL: http://svn.apache.org/viewvc?rev=1101917&view=rev
Log:
JS2-1253: A generic and shared ServletRequestCleanupService with callback registration

Added:
    portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
  (with props)
    portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
  (with props)
    portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
  (with props)
    portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
  (with props)

Added: portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java?rev=1101917&view=auto
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
(added)
+++ portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
Wed May 11 15:19:31 2011
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.util;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @version $Id$
+ *
+ */
+public interface ServletRequestCleanupCallback
+{
+    void cleanup(ServletContext context, HttpServletRequest request, HttpServletResponse
response);
+}

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-api/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupCallback.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java?rev=1101917&view=auto
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
(added)
+++ portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
Wed May 11 15:19:31 2011
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.util;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @version $Id$
+ *
+ */
+public class ServletRequestCleanupFilter implements Filter
+{
+    private ServletContext context;
+    
+    public void init(FilterConfig config) throws ServletException
+    {
+        this.context = config.getServletContext();
+    }
+    
+    public void destroy()
+    {
+    }
+
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException
+    {
+        ServletRequestCleanupService.setCleaner(this);
+        if (filterChain != null)
+        {
+            filterChain.doFilter(request, response);
+        }
+        if (request instanceof HttpServletRequest)
+        {
+            ServletRequestCleanupService.cleanup(this, context, (HttpServletRequest)request,
(HttpServletResponse)response);
+        }
+    }
+}

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java?rev=1101917&view=auto
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
(added)
+++ portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
Wed May 11 15:19:31 2011
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.jetspeed.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @version $Id$
+ *
+ */
+public class ServletRequestCleanupService
+{
+    private static ThreadLocal<List<ServletRequestCleanupCallback>> cleanups
= new ThreadLocal<List<ServletRequestCleanupCallback>>();
+    private static ThreadLocal<Object> firstCleaner = new ThreadLocal<Object>();
+    
+    private static List<ServletRequestCleanupCallback> getCleanups(boolean create)
+    {
+        List<ServletRequestCleanupCallback> list = cleanups.get();
+        if (list == null && create)
+        {
+            list = new ArrayList<ServletRequestCleanupCallback>();
+            cleanups.set(list);
+        }
+        return list;
+    }
+    
+    public static void setCleaner(Object cleaner)
+    {
+        if (cleaner == null)
+        {
+            throw new IllegalArgumentException("Cleaner may not be null");
+        }
+        if (firstCleaner.get() == null)
+        {
+            firstCleaner.set(cleaner);
+        }
+    }
+        
+    public static void addCleanupCallback(ServletRequestCleanupCallback callback)
+    {
+        if (firstCleaner.get() == null)
+        {
+            try
+            {
+                throw new RuntimeException();
+            }
+            catch (RuntimeException jre)
+            {
+                // log missing cleaner and stacktrace for addCleanupCallback call
+                JetspeedLoggerUtil.getSharedLogger(ServletRequestCleanupService.class)
+                    .error("No request cleaner set for ServletRequestCleanupService: cleanup
callback ignored", jre);
+                return;
+            }
+        }
+        getCleanups(true).add(callback);
+    }
+    
+    public static void cleanup(Object cleaner, ServletContext context, HttpServletRequest
request, HttpServletResponse response)
+    {
+        if (cleaner != null && cleaner.equals(firstCleaner.get()))
+        {
+            List<ServletRequestCleanupCallback> list = getCleanups(false);
+            if (list != null)
+            {
+                for (ServletRequestCleanupCallback callback : cleanups.get())
+                {
+                    try
+                    {
+                        callback.cleanup(context, request, response);
+                    }
+                    catch (Exception e)
+                    {
+                        JetspeedLoggerUtil.getSharedLogger(ServletRequestCleanupService.class)
+                            .error("Request cleanup operation failed", e);
+                    }
+                }
+                cleanups.remove();
+            }
+            firstCleaner.remove();
+        }
+    }
+}

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestCleanupService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java?rev=1101917&view=auto
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
(added)
+++ portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
Wed May 11 15:19:31 2011
@@ -0,0 +1,40 @@
+package org.apache.jetspeed.util;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+/**
+ * @version $Id$
+ *
+ */
+public class ServletRequestThreadLocalCleanupCallback implements ServletRequestCleanupCallback
+{
+    private ThreadLocal<?> threadLocal;
+    
+    public ServletRequestThreadLocalCleanupCallback(ThreadLocal<?> threadLocal)
+    {
+        this.threadLocal = threadLocal;
+        ServletRequestCleanupService.addCleanupCallback(this);
+    }
+    
+    public void cleanup(ServletContext context, HttpServletRequest request, HttpServletResponse
response)
+    {
+        threadLocal.remove();
+    }
+}

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ServletRequestThreadLocalCleanupCallback.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message