tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r966287 - in /tapestry/tapestry5/trunk/tapestry-ioc/src: main/java/org/apache/tapestry5/ioc/internal/services/ main/java/org/apache/tapestry5/ioc/services/ test/java/org/apache/tapestry5/ioc/internal/services/
Date Wed, 21 Jul 2010 15:56:09 GMT
Author: hlship
Date: Wed Jul 21 15:56:08 2010
New Revision: 966287

URL: http://svn.apache.org/viewvc?rev=966287&view=rev
Log:
Add methods run() and invoke() to PerThreadManager

Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java?rev=966287&r1=966286&r2=966287&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
Wed Jul 21 15:56:08 2010
@@ -19,6 +19,7 @@ import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
+import org.apache.tapestry5.ioc.Invokable;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.internal.util.DummyLock;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
@@ -189,4 +190,29 @@ public class PerthreadManagerImpl implem
         return createValue(InternalUtils.nextUUID());
     }
 
+    public void run(Runnable runnable)
+    {
+        assert runnable != null;
+
+        try
+        {
+            runnable.run();
+        }
+        finally
+        {
+            cleanup();
+        }
+    }
+
+    public <T> T invoke(Invokable<T> invokable)
+    {
+        try
+        {
+            return invokable.invoke();
+        }
+        finally
+        {
+            cleanup();
+        }
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java?rev=966287&r1=966286&r2=966287&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
Wed Jul 21 15:56:08 2010
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.ioc.services;
 
+import org.apache.tapestry5.ioc.Invokable;
+
 /**
  * Manages per-thread data, and provides a way for listeners to know when such data should
be cleaned up. Typically,
  * data is cleaned up at the end of the request (in a web application). Tapestry IoC has
any number of objects that need
@@ -63,4 +65,16 @@ public interface PerthreadManager
      * @since 5.2.0
      */
     <T> PerThreadValue<T> createValue();
+
+    /**
+     * Invokes {@link Runnable#run()}, providing a try...finally to {@linkplain #cleanup()
cleanup} after.
+     * 
+     * @since 5.2.0
+     */
+    void run(Runnable runnable);
+
+    /**
+     * Returns the result from the invocation, providing a try...finally to {@linkplain #cleanup()
cleanup} after.
+     */
+    <T> T invoke(Invokable<T> invokable);
 }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java?rev=966287&r1=966286&r2=966287&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImplTest.java
Wed Jul 21 15:56:08 2010
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.ioc.internal.services;
 
+import org.apache.tapestry5.ioc.Invokable;
 import org.apache.tapestry5.ioc.services.PerThreadValue;
 import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
 import org.apache.tapestry5.ioc.test.IOCTestCase;
@@ -175,4 +176,41 @@ public class PerthreadManagerImplTest ex
 
         assertNull(v.get());
     }
+
+    @Test
+    public void run_performs_cleanup()
+    {
+        final PerthreadManagerImpl m = new PerthreadManagerImpl(null);
+
+        m.run(new Runnable()
+        {
+            public void run()
+            {
+                m.put("foo", "bar");
+            }
+        });
+
+        assertNull(m.get("foo"));
+    }
+
+    @Test
+    public void invoke_performs_cleanup()
+    {
+        final PerthreadManagerImpl m = new PerthreadManagerImpl(null);
+
+        String actual = m.invoke(new Invokable<String>()
+        {
+            public String invoke()
+            {
+                m.put("foo", "bar");
+
+                return "baz";
+            }
+        });
+
+        assertEquals(actual, "baz");
+
+        assertNull(m.get("foo"));
+
+    }
 }



Mime
View raw message