Author: dan.j.allen
Date: 2009-05-11 16:06:57 -0400 (Mon, 11 May 2009)
New Revision: 10853
Added:
modules/trunk/web/
modules/trunk/web/pom.xml
modules/trunk/web/src/
modules/trunk/web/src/main/
modules/trunk/web/src/main/java/
modules/trunk/web/src/main/java/org/
modules/trunk/web/src/main/java/org/jboss/
modules/trunk/web/src/main/java/org/jboss/seam/
modules/trunk/web/src/main/java/org/jboss/seam/web/
modules/trunk/web/src/main/java/org/jboss/seam/web/HttpSessionManager.java
modules/trunk/web/src/main/java/org/jboss/seam/web/SeamFilter.java
modules/trunk/web/src/main/resources/
modules/trunk/web/src/main/resources/META-INF/
modules/trunk/web/src/main/resources/META-INF/beans.xml
Log:
introduce a web module for providing web life cycle routines
initial feature is to shut down the session in a delayed fashion
Property changes on: modules/trunk/web
___________________________________________________________________
Name: svn:ignore
+ target
Added: modules/trunk/web/pom.xml
===================================================================
--- modules/trunk/web/pom.xml (rev 0)
+++ modules/trunk/web/pom.xml 2009-05-11 20:06:57 UTC (rev 10853)
@@ -0,0 +1,63 @@
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>seam-parent</artifactId>
+ <groupId>org.jboss.seam</groupId>
+ <version>3.0.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>seam-web</artifactId>
+ <packaging>jar</packaging>
+ <version>3.0.0-SNAPSHOT</version>
+ <name>Seam Web Module</name>
+
+ <build>
+ <plugins>
+
+ <!--
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <suiteXmlFiles>
+
<suiteXmlFile>src/test/resources/test-suite.xml</suiteXmlFile>
+ </suiteXmlFiles>
+ </configuration>
+ </plugin>
+ -->
+
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${seam.groupId}</groupId>
+ <artifactId>seam-bridge-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>${webbeans.groupId}</groupId>
+ <artifactId>jsr299-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>${webbeans.groupId}</groupId>
+ <artifactId>webbeans-logging</artifactId>
+ <!-- assumes use of Web Beans impl -->
+ <scope>provided</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
Added: modules/trunk/web/src/main/java/org/jboss/seam/web/HttpSessionManager.java
===================================================================
--- modules/trunk/web/src/main/java/org/jboss/seam/web/HttpSessionManager.java
(rev 0)
+++ modules/trunk/web/src/main/java/org/jboss/seam/web/HttpSessionManager.java 2009-05-11
20:06:57 UTC (rev 10853)
@@ -0,0 +1,206 @@
+package org.jboss.seam.web;
+
+import java.util.Enumeration;
+
+import javax.annotation.PreDestroy;
+import javax.context.RequestScoped;
+import javax.inject.Produces;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+
+/**
+ * A bean which manages the HttpSession for the current request. The main
+ * purpose of this bean is to allow a session to be flagged to be invalidated
+ * after the request has ended. It also exposes the HttpSession so that it's
+ * available for injection. A filter is provided in this module that can assign
+ * the HttpSession to this manager when the HttpSession is initialized.
+ *
+ * @author Dan Allen
+ */
+public
+@RequestScoped
+class HttpSessionManager
+{
+ private HttpSession session;
+
+ private boolean invalidate = false;
+
+ /**
+ * Expose the current HttpSession to be available for injection.
+ *
+ * TODO wrap the HttpSession an delegate the invalidate() call to this bean's
delayed invalidation mechanism
+ *
+ * @return HttpSession the HttpSession being managed (presumably associated
+ * with the current request.
+ */
+ public
+ @Produces
+ @RequestScoped
+ HttpSession getSession()
+ {
+ return new HttpSessionWrapper(session, this);
+ }
+
+ /**
+ * Establish the HttpSession to manage.
+ *
+ * @param session The HttpSession associated with the current request
+ */
+ public void setSession(HttpSession session)
+ {
+ this.session = session;
+ }
+
+ /**
+ * Flag the current HttpSession to be invalidated at the end of the request.
+ * The session is not terminated immediately so as to avoid disruption caused
+ * by cleanup routines by HttpSessionListener implementations monitoring for
+ * the end of session event.
+ */
+ public void invalidateAtEndOfRequest()
+ {
+ invalidate = true;
+ }
+
+ /**
+ * Is the current HttpSession scheduled to be invalidated at the end of the
+ * request.
+ *
+ * @return boolean Whether the current HttpSession has been scheduled for
+ * invalidation
+ */
+ public boolean isPendingInvalidation()
+ {
+ return invalidate;
+ }
+
+ /**
+ * Indicate whether there is currently being a session managed. The assumption
+ * is that if there is a session being managed, it's also active.
+ *
+ * @return boolean Whether there is a session being managed.
+ */
+ public boolean isManaged()
+ {
+ return session != null;
+ }
+
+ /**
+ * Observe the end of request event and invalidate the session if it has
+ * been scheduled for termination.
+ */
+ public
+ @PreDestroy
+ void afterRequest()
+ {
+ if (invalidate && session != null)
+ {
+ session.invalidate();
+ }
+ }
+
+ private class HttpSessionWrapper implements HttpSession
+ {
+ private HttpSession delegate;
+ private HttpSessionManager sessionManager;
+
+ public HttpSessionWrapper(HttpSession delegate, HttpSessionManager sessionManager)
+ {
+ this.delegate = delegate;
+ this.sessionManager = sessionManager;
+ }
+
+ public Object getAttribute(String name)
+ {
+ return delegate.getAttribute(name);
+ }
+
+ @SuppressWarnings("unchecked")
+ public Enumeration getAttributeNames()
+ {
+ return delegate.getAttributeNames();
+ }
+
+ public long getCreationTime()
+ {
+ return delegate.getCreationTime();
+ }
+
+ public String getId()
+ {
+ return delegate.getId();
+ }
+
+ public long getLastAccessedTime()
+ {
+ return delegate.getLastAccessedTime();
+ }
+
+ public int getMaxInactiveInterval()
+ {
+ return delegate.getMaxInactiveInterval();
+ }
+
+ public ServletContext getServletContext()
+ {
+ return delegate.getServletContext();
+ }
+
+ @SuppressWarnings("deprecation")
+ public HttpSessionContext getSessionContext()
+ {
+ return delegate.getSessionContext();
+ }
+
+ @SuppressWarnings("deprecation")
+ public Object getValue(String name)
+ {
+ return delegate.getValue(name);
+ }
+
+ @SuppressWarnings("deprecation")
+ public String[] getValueNames()
+ {
+ return delegate.getValueNames();
+ }
+
+ public void invalidate()
+ {
+ sessionManager.invalidateAtEndOfRequest();
+ }
+
+ public boolean isNew()
+ {
+ return delegate.isNew();
+ }
+
+ @SuppressWarnings("deprecation")
+ public void putValue(String name, Object value)
+ {
+ delegate.putValue(name, value);
+ }
+
+ public void removeAttribute(String name)
+ {
+ delegate.removeAttribute(name);
+ }
+
+ @SuppressWarnings("deprecation")
+ public void removeValue(String name)
+ {
+ delegate.removeValue(name);
+ }
+
+ public void setAttribute(String name, Object value)
+ {
+ delegate.setAttribute(name, value);
+ }
+
+ public void setMaxInactiveInterval(int interval)
+ {
+ delegate.setMaxInactiveInterval(interval);
+ }
+
+ }
+}
Added: modules/trunk/web/src/main/java/org/jboss/seam/web/SeamFilter.java
===================================================================
--- modules/trunk/web/src/main/java/org/jboss/seam/web/SeamFilter.java
(rev 0)
+++ modules/trunk/web/src/main/java/org/jboss/seam/web/SeamFilter.java 2009-05-11 20:06:57
UTC (rev 10853)
@@ -0,0 +1,77 @@
+package org.jboss.seam.web;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpSession;
+
+import org.jboss.seam.bridge.ManagerBridge;
+
+/**
+ * Filter all requests to an application using Seam and perform pre- and post-request
+ * initialization.
+ *
+ * @author Dan Allen
+ */
+public class SeamFilter implements Filter
+{
+
+ public void init(FilterConfig config) throws ServletException
+ {
+ }
+
+ /**
+ * Intercept the request and install a hook that watches for a session to be created
and registers that
+ * session with the Seam HttpSessionManager.
+ */
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException
+ {
+ if (request instanceof HttpServletRequest)
+ {
+ // TODO: may want to organize this logic into a Lifecycle class
+ request = new HttpServletRequestWrapper((HttpServletRequest) request) {
+
+ private HttpSession registeredSession;
+
+ @Override
+ public HttpSession getSession()
+ {
+ // we don't want to assume that the sibling getSession method is
called by the impl
+ return this.getSession(true);
+ }
+
+ @Override
+ public HttpSession getSession(boolean create)
+ {
+ HttpSession session = super.getSession(create);
+ if (session != null && session != registeredSession)
+ {
+ registerSession(session);
+ }
+ return session;
+ }
+
+ private void registerSession(HttpSession session)
+ {
+
ManagerBridge.getProvider().getRootManager().getInstanceByType(HttpSessionManager.class).setSession(session);
+ registeredSession = session;
+ }
+
+ };
+ }
+
+ chain.doFilter(request, response);
+ }
+
+ public void destroy()
+ {
+ }
+
+}
Added: modules/trunk/web/src/main/resources/META-INF/beans.xml
===================================================================