Revision: 1096
          http://stripes.svn.sourceforge.net/stripes/?rev=1096&view=rev
Author:   bengunter
Date:     2009-03-04 16:17:38 +0000 (Wed, 04 Mar 2009)

Log Message:
-----------
Fixed STS-657. During a request lifecycle, a call to 
ExecutionContext.currentContext() will return the current execution context.

Modified Paths:
--------------
    trunk/stripes/src/net/sourceforge/stripes/controller/ExecutionContext.java

Added Paths:
-----------
    
trunk/tests/src/net/sourceforge/stripes/controller/ExecutionContextTests.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/controller/ExecutionContext.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/controller/ExecutionContext.java  
2009-03-04 16:02:26 UTC (rev 1095)
+++ trunk/stripes/src/net/sourceforge/stripes/controller/ExecutionContext.java  
2009-03-04 16:17:38 UTC (rev 1096)
@@ -37,7 +37,13 @@
  */
 public class ExecutionContext {
     private static final Log log = Log.getInstance(ExecutionContext.class);
+    private static final ThreadLocal<ExecutionContext> currentContext = new 
ThreadLocal<ExecutionContext>();
 
+    /** Get the execution context for the current thread. */
+    public static final ExecutionContext currentContext() {
+        return currentContext.get();
+    }
+
     private Collection<Interceptor> interceptors;
     private Iterator<Interceptor> iterator;
     private Interceptor target;
@@ -71,7 +77,19 @@
     public Resolution wrap(Interceptor target) throws Exception {
         this.target = target;
         this.iterator = null;
-        return proceed();
+
+        // Before executing RequestInit, set this as the current execution 
context
+        if (lifecycleStage == LifecycleStage.RequestInit)
+            currentContext.set(this);
+
+        try {
+            return proceed();
+        }
+        finally {
+            // Make sure the current execution context gets cleared after 
RequestComplete
+            if (LifecycleStage.RequestComplete == getLifecycleStage())
+                currentContext.set(null);
+        }
     }
 
     /**

Added: 
trunk/tests/src/net/sourceforge/stripes/controller/ExecutionContextTests.java
===================================================================
--- 
trunk/tests/src/net/sourceforge/stripes/controller/ExecutionContextTests.java   
                            (rev 0)
+++ 
trunk/tests/src/net/sourceforge/stripes/controller/ExecutionContextTests.java   
    2009-03-04 16:17:38 UTC (rev 1096)
@@ -0,0 +1,45 @@
+package net.sourceforge.stripes.controller;
+
+import java.util.Collections;
+import java.util.List;
+
+import net.sourceforge.stripes.action.Resolution;
+import net.sourceforge.stripes.util.Log;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Tests for {...@link ExecutionContext}.
+ * 
+ * @author Ben Gunter
+ */
+public class ExecutionContextTests {
+    private static final Log log = 
Log.getInstance(ExecutionContextTests.class);
+
+    @Test
+    public void testCurrentContext() throws Exception {
+        log.debug("Testing ExecutionContext.currentContext()");
+        final ExecutionContext ctx = new ExecutionContext();
+
+        for (LifecycleStage stage : LifecycleStage.values()) {
+            log.debug("Setting lifecycle stage: " + stage);
+            ctx.setLifecycleStage(stage);
+
+            List<Interceptor> interceptors = Collections.emptyList();
+            ctx.setInterceptors(interceptors);
+
+            ctx.wrap(new Interceptor() {
+                public Resolution intercept(ExecutionContext context) throws 
Exception {
+                    Assert.assertSame(ExecutionContext.currentContext(), ctx,
+                            "The current context is not what was expected!");
+                    return null;
+                }
+            });
+        }
+
+        log.debug("Lifecycle complete. Making sure current context is null.");
+        Assert.assertNull(ExecutionContext.currentContext(),
+                "The current context was not cleared at the end of the 
lifecycle.");
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to