You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2010/07/07 20:27:18 UTC

svn commit: r961453 - 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/

Author: hlship
Date: Wed Jul  7 18:27:17 2010
New Revision: 961453

URL: http://svn.apache.org/viewvc?rev=961453&view=rev
Log:
TAP5-1197: Make it easier to store a per-thread value inside the PerThreadManager

Added:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java   (with props)
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=961453&r1=961452&r2=961453&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  7 18:27:17 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2010 The Apache Software Foundation
 //
 // Licensed 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
+// 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,
@@ -22,6 +22,7 @@ import java.util.concurrent.locks.Reentr
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.internal.util.DummyLock;
 import org.apache.tapestry5.ioc.internal.util.JDKUtils;
+import org.apache.tapestry5.ioc.services.PerThreadValue;
 import org.apache.tapestry5.ioc.services.PerthreadManager;
 import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
 import org.slf4j.Logger;
@@ -62,7 +63,7 @@ public class PerthreadManagerImpl implem
         try
         {
             lock.lock();
-            
+
             return holder.get();
         }
         finally
@@ -118,6 +119,8 @@ public class PerthreadManagerImpl implem
         try
         {
             lock.lock();
+
+            // Discard the per-thread map of values.
             
             holder.remove();
         }
@@ -127,6 +130,7 @@ public class PerthreadManagerImpl implem
         }
     }
 
+    @SuppressWarnings("unchecked")
     public void put(Object key, Object value)
     {
         getPerthreadMap().put(key, value);
@@ -136,4 +140,22 @@ public class PerthreadManagerImpl implem
     {
         return getPerthreadMap().get(key);
     }
+
+    public <T> PerThreadValue<T> createValue(final Object key)
+    {
+        return new PerThreadValue<T>()
+        {
+            @SuppressWarnings("unchecked")
+            public T get()
+            {
+                return (T) PerthreadManagerImpl.this.get(key);
+            }
+
+            public void set(T newValue)
+            {
+                put(key, newValue);
+            }
+        };
+    }
+
 }

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java?rev=961453&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java Wed Jul  7 18:27:17 2010
@@ -0,0 +1,29 @@
+// Copyright 2010 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.ioc.services;
+
+/**
+ * A wrapper around {@link PerthreadManager#get(Object)} and {@link PerthreadManager#put(Object, Object)}.
+ * 
+ * @since 5.2.0
+ */
+public interface PerThreadValue<T>
+{
+    /** Reads the current per-thread value, or returns null if no value has been stored. */
+    T get();
+
+    /** Sets the current per-thread value. */
+    void set(T newValue);
+}

Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerThreadValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=961453&r1=961452&r2=961453&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  7 18:27:17 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2010 The Apache Software Foundation
 //
 // Licensed 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
+// 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,
@@ -15,20 +15,21 @@
 package org.apache.tapestry5.ioc.services;
 
 /**
- * Manages per-thread data, and provides a way for listeners to know when such data should be cleaned up.  Typically,
+ * 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
  * to know when this event occurs, so that they can clean up any per-thread/per-request state.
  * <p/>
  * Due to <a href="https://issues.apache.org/jira/browse/TAPESTRY-2141">TAPESTRY-2141<a> (and the underlying JDK 1.5 bug
  * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025230">5025230</a>), this service has expanded to
- * manager per-thread data (not just end-of-request listeners).
+ * manage per-thread data (not just end-of-request listeners).
  */
 public interface PerthreadManager
 {
     /**
-     * Adds a listener to the hub.  All listeners are discarded at the {@link #cleanup()}.
-     *
-     * @param listener to add
+     * Adds a listener to the hub. All listeners are discarded at the {@link #cleanup()}.
+     * 
+     * @param listener
+     *            to add
      */
     void addThreadCleanupListener(ThreadCleanupListener listener);
 
@@ -38,12 +39,12 @@ public interface PerthreadManager
      */
     void cleanup();
 
-
     /**
-     * Returns an object stored in the per-thread map.    When the object is a string, the expected name is <em>service
-     * id</em>.<em>subkey</em>.  Unlike most of Tapestry, such keys <em>will</em> be case sensitive.
-     *
-     * @param key key used to retrieve object
+     * Returns an object stored in the per-thread map. When the object is a string, the expected name is <em>service
+     * id</em>.<em>subkey</em>. Unlike most of Tapestry, such keys <em>will</em> be case sensitive.
+     * 
+     * @param key
+     *            key used to retrieve object
      * @return corresponding per-thread object, or null
      */
     Object get(Object key);
@@ -52,4 +53,7 @@ public interface PerthreadManager
      * Stores a value into the per-thread map.
      */
     void put(Object key, Object value);
+
+    /** Creates an object that captures the key, making it easier to get or set per-thread values. */
+    <T> PerThreadValue<T> createValue(Object key);
 }

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=961453&r1=961452&r2=961453&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  7 18:27:17 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2010 The Apache Software Foundation
 //
 // Licensed 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
+// 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,
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.ioc.internal.services;
 
+import org.apache.tapestry5.ioc.services.PerThreadValue;
 import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
 import org.apache.tapestry5.ioc.test.IOCTestCase;
 import org.slf4j.Logger;
@@ -117,4 +118,23 @@ public class PerthreadManagerImplTest ex
     //
     // verify();
     // }
+
+    @Test
+    public void per_thread_value()
+    {
+        Object key = new Object();
+        Object value = "Tapestry";
+
+        PerthreadManagerImpl m = new PerthreadManagerImpl(null);
+
+        PerThreadValue<Object> v = m.createValue(key);
+
+        assertNull(m.get(key));
+        assertNull(v.get());
+
+        v.set(value);
+
+        assertSame(m.get(key), value);
+        assertSame(v.get(), value);
+    }
 }