Title: [waffle-scm] [598] trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico: WAFFLE-60 : updated HttpSessionComponentAdapter to hide session instance behind an dynamicproxy
Revision
598
Author
mward
Date
2008-03-27 22:11:16 -0500 (Thu, 27 Mar 2008)

Log Message

WAFFLE-60 : updated HttpSessionComponentAdapter to hide session instance behind an dynamicproxy

Modified Paths

Diff

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapter.java (597 => 598)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapter.java	2008-03-26 02:25:18 UTC (rev 597)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapter.java	2008-03-28 03:11:16 UTC (rev 598)
@@ -17,14 +17,10 @@
 import org.picocontainer.PicoVisitor;
 import org.codehaus.waffle.context.CurrentHttpServletRequest;
 
-/**
- * This class allows for components to be dependent on an HttpSession without actually adding the session to the
- * picocontainer.  Without this serilaization issue can occur when an application server tries to serialize a
- * users session (<a href=""  
- *
- * @author Michael Ward
- */
 import javax.servlet.http.HttpSession;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 
 public class HttpSessionComponentAdapter implements ComponentAdapter {
     private final Class componentImplementation = HttpSession.class;
@@ -38,7 +34,9 @@
     }
 
     public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException {
-        return CurrentHttpServletRequest.get().getSession();
+        return Proxy.newProxyInstance(HttpSession.class.getClassLoader(),
+                                      new Class[] {HttpSession.class},
+                                      new HttpSessionProxy());
     }
 
     public void verify(PicoContainer container) throws PicoIntrospectionException {
@@ -48,4 +46,13 @@
     public void accept(PicoVisitor visitor) {
         // do nothing
     }
+
+    private static class HttpSessionProxy implements InvocationHandler {
+
+        public Object invoke(Object proxy, Method method, Object... args) throws Throwable {
+            HttpSession session = CurrentHttpServletRequest.get().getSession();
+
+            return method.invoke(session, args);
+        }
+    }
 }

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapterTest.java (597 => 598)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapterTest.java	2008-03-26 02:25:18 UTC (rev 597)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/context/pico/HttpSessionComponentAdapterTest.java	2008-03-28 03:11:16 UTC (rev 598)
@@ -10,13 +10,12 @@
  *****************************************************************************/
 package org.codehaus.waffle.context.pico;
 
-import org.jmock.Expectations;
+import org.codehaus.waffle.context.CurrentHttpServletRequest;
 import org.jmock.Mockery;
 import org.jmock.integration.junit4.JMock;
-import static org.junit.Assert.assertSame;
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.codehaus.waffle.context.CurrentHttpServletRequest;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
@@ -30,21 +29,12 @@
     private Mockery mockery = new Mockery();
 
     @Test
-    public void doIt() {
+    public void shouldReturnProxyToHttpSession() {
         HttpSessionComponentAdapter componentAdapter = new HttpSessionComponentAdapter();
 
         // Mock ServletContext
         final HttpServletRequest httpServletRequest = mockery.mock(HttpServletRequest.class);
-        final HttpSession httpSession = mockery.mock(HttpSession.class);
         CurrentHttpServletRequest.set(httpServletRequest);
-        
-        mockery.checking(new Expectations() {
-            {
-                one(httpServletRequest).getSession();
-                will(returnValue(httpSession));
-            }
-        });
-
-        assertSame(httpSession, componentAdapter.getComponentInstance(null));
+        Assert.assertTrue(componentAdapter.getComponentInstance(null) instanceof HttpSession);
     }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to