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/11 21:06:18 UTC

svn commit: r963133 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java

Author: hlship
Date: Sun Jul 11 19:06:17 2010
New Revision: 963133

URL: http://svn.apache.org/viewvc?rev=963133&view=rev
Log:
TAP5-1197: Refactor InternalComponentResourcesImpl to store mutable state in PerthreadManager

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java?rev=963133&r1=963132&r2=963133&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImpl.java Sun Jul 11 19:06:17 2010
@@ -39,6 +39,7 @@ import org.apache.tapestry5.ioc.internal
 import org.apache.tapestry5.ioc.internal.util.Defense;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.ioc.internal.util.TapestryException;
+import org.apache.tapestry5.ioc.services.PerThreadValue;
 import org.apache.tapestry5.model.ComponentModel;
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.runtime.PageLifecycleListener;
@@ -68,6 +69,12 @@ public class InternalComponentResourcesI
 
     private final ComponentPageElementResources elementResources;
 
+    private final boolean mixin;
+
+    private static final Object[] EMPTY = new Object[0];
+
+    private static final AnnotationProvider NULL_ANNOTATION_PROVIDER = new NullAnnotationProvider();
+
     // Case insensitive map from parameter name to binding
     private Map<String, Binding> bindings;
 
@@ -77,16 +84,11 @@ public class InternalComponentResourcesI
 
     private Messages messages;
 
-    // Case insensitive
-    private Map<String, Object> renderVariables;
-
-    private static final Object[] EMPTY = new Object[0];
-
-    private static final AnnotationProvider NULL_ANNOTATION_PROVIDER = new NullAnnotationProvider();
-
     private boolean informalsComputed;
 
-    private final boolean mixin;
+    private PerThreadValue<Map<String, Object>> renderVariables;
+
+    private Informal firstInformal;
 
     /**
      * We keep a linked list of informal parameters, which saves us the expense of determining which
@@ -128,8 +130,6 @@ public class InternalComponentResourcesI
         }
     }
 
-    private Informal firstInformal;
-
     public InternalComponentResourcesImpl(Page page, ComponentPageElement element,
             ComponentResources containerResources, ComponentPageElementResources elementResources, String completeId,
             String nestedId, Instantiator componentInstantiator, boolean mixin)
@@ -357,6 +357,12 @@ public class InternalComponentResourcesI
         if (bindings == null)
             return;
 
+        for (Informal i = firstInformal(); i != null; i = i.next)
+            i.write(writer);
+    }
+
+    private synchronized Informal firstInformal()
+    {
         if (!informalsComputed)
         {
             for (Map.Entry<String, Binding> e : getInformalParameterBindings().entrySet())
@@ -367,8 +373,7 @@ public class InternalComponentResourcesI
             informalsComputed = true;
         }
 
-        for (Informal i = firstInformal; i != null; i = i.next)
-            i.write(writer);
+        return firstInformal;
     }
 
     public Component getContainer()
@@ -394,7 +399,7 @@ public class InternalComponentResourcesI
         return element.getLocale();
     }
 
-    public Messages getMessages()
+    public synchronized Messages getMessages()
     {
         if (messages == null)
             messages = elementResources.getMessages(componentModel);
@@ -456,8 +461,24 @@ public class InternalComponentResourcesI
         return result;
     }
 
+    private synchronized Map<String, Object> getRenderVariables()
+    {
+        if (renderVariables == null)
+            renderVariables = elementResources.createPerThreadValue("tapestry.internal.RenderVariables:"
+                    + getCompleteId());
+
+        Map<String, Object> result = renderVariables.get();
+
+        if (result == null)
+            result = renderVariables.set(CollectionFactory.newCaseInsensitiveMap());
+
+        return result;
+    }
+
     public Object getRenderVariable(String name)
     {
+        Map<String, Object> renderVariables = getRenderVariables();
+
         Object result = InternalUtils.get(renderVariables, name);
 
         if (result == null)
@@ -475,16 +496,14 @@ public class InternalComponentResourcesI
         if (!element.isRendering())
             throw new IllegalStateException(StructureMessages.renderVariableSetWhenNotRendering(getCompleteId(), name));
 
-        if (renderVariables == null)
-            renderVariables = CollectionFactory.newCaseInsensitiveMap();
+        Map<String, Object> renderVariables = getRenderVariables();
 
         renderVariables.put(name, value);
     }
 
     public void postRenderCleanup()
     {
-        if (renderVariables != null)
-            renderVariables.clear();
+        getRenderVariables().clear();
     }
 
     public void addPageLifecycleListener(PageLifecycleListener listener)
@@ -502,12 +521,12 @@ public class InternalComponentResourcesI
         page.addResetListener(listener);
     }
 
-    public ParameterConduit getParameterConduit(String parameterName)
+    public synchronized ParameterConduit getParameterConduit(String parameterName)
     {
         return InternalUtils.get(conduits, parameterName);
     }
 
-    public void setParameterConduit(String parameterName, ParameterConduit conduit)
+    public synchronized void setParameterConduit(String parameterName, ParameterConduit conduit)
     {
         if (conduits == null)
             conduits = CollectionFactory.newCaseInsensitiveMap();

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java?rev=963133&r1=963132&r2=963133&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/structure/InternalComponentResourcesImplTest.java Sun Jul 11 19:06: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,
@@ -21,15 +21,38 @@ import org.apache.tapestry5.internal.Int
 import org.apache.tapestry5.internal.bindings.InternalPropBinding;
 import org.apache.tapestry5.internal.services.Instantiator;
 import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.ioc.services.PerthreadManager;
 import org.apache.tapestry5.ioc.services.TypeCoercer;
 import org.apache.tapestry5.model.ComponentModel;
 import org.apache.tapestry5.model.ParameterModel;
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.runtime.PageLifecycleListener;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class InternalComponentResourcesImplTest extends InternalBaseTestCase
 {
+    private PerthreadManager perThreadManager;
+
+    private ComponentPageElementResources elementResources;
+
+    @BeforeClass
+    public void setup()
+    {
+        perThreadManager = getService(PerthreadManager.class);
+        TypeCoercer typeCoercer = getService(TypeCoercer.class);
+
+        elementResources = new ComponentPageElementResourcesImpl(null, null, typeCoercer, null, null, null, null, null,
+                null, null, perThreadManager);
+    }
+
+    @AfterMethod
+    public void cleanup()
+    {
+        perThreadManager.cleanup();
+    }
+
     @Test
     public void render_informal_parameters_no_bindings()
     {
@@ -44,8 +67,8 @@ public class InternalComponentResourcesI
 
         replay();
 
-        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, null,
-                                                                                  null, ins, false);
+        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null,
+                elementResources, null, null, ins, false);
 
         resources.renderInformalParameters(writer);
 
@@ -69,8 +92,8 @@ public class InternalComponentResourcesI
 
         replay();
 
-        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, null,
-                                                                                  null, ins, false);
+        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null,
+                elementResources, null, null, ins, false);
 
         resources.bindParameter("fred", binding);
 
@@ -88,9 +111,7 @@ public class InternalComponentResourcesI
         MarkupWriter writer = mockMarkupWriter();
         ComponentModel model = mockComponentModel();
         Binding binding = mockBinding();
-        Object rawValue = new Object();
-        String convertedValue = "*converted*";
-        ComponentPageElementResources componentPageElementResources = mockComponentPageElementResources();
+        Object rawValue = new Long(97);
 
         train_getModel(ins, model);
 
@@ -98,15 +119,12 @@ public class InternalComponentResourcesI
 
         train_get(binding, rawValue);
 
-        train_coerce(componentPageElementResources, rawValue, String.class, convertedValue);
-
-        writer.attributes("fred", convertedValue);
+        writer.attributes("fred", "97");
 
         replay();
 
         InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null,
-                                                                                  componentPageElementResources,
-                                                                                  null, null, ins, false);
+                elementResources, "Foo.bar", null, ins, false);
 
         resources.bindParameter("fred", binding);
 
@@ -131,7 +149,8 @@ public class InternalComponentResourcesI
 
         replay();
 
-        ComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, null, null, ins, false);
+        ComponentResources resources = new InternalComponentResourcesImpl(null, element, null, elementResources, "id",
+                null, ins, false);
 
         resources.storeRenderVariable("myRenderVar", value);
 
@@ -160,8 +179,8 @@ public class InternalComponentResourcesI
 
         replay();
 
-        ComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, "Foo.bar", null,
-                                                                          ins, false);
+        ComponentResources resources = new InternalComponentResourcesImpl(null, element, null, elementResources,
+                "Foo.bar", null, ins, false);
 
         resources.storeRenderVariable("fred", "FRED");
         resources.storeRenderVariable("barney", "BARNEY");
@@ -173,8 +192,9 @@ public class InternalComponentResourcesI
         }
         catch (IllegalArgumentException ex)
         {
-            assertEquals(ex.getMessage(),
-                         "Component Foo.bar does not contain a stored render variable with name 'wilma'.  Stored render variables: barney, fred.");
+            assertEquals(
+                    ex.getMessage(),
+                    "Component Foo.bar does not contain a stored render variable with name 'wilma'.  Stored render variables: barney, fred.");
         }
 
         verify();
@@ -195,8 +215,8 @@ public class InternalComponentResourcesI
 
         replay();
 
-        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, "Foo.bar",
-                                                                                  null, ins, false);
+        InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null,
+                elementResources, "Foo.bar", null, ins, false);
 
         resources.storeRenderVariable("fred", "FRED");
         resources.storeRenderVariable("barney", "BARNEY");
@@ -211,7 +231,7 @@ public class InternalComponentResourcesI
         catch (IllegalArgumentException ex)
         {
             assertEquals(ex.getMessage(),
-                         "Component Foo.bar does not contain a stored render variable with name 'fred'.  Stored render variables: (none).");
+                    "Component Foo.bar does not contain a stored render variable with name 'fred'.  Stored render variables: (none).");
         }
 
         verify();
@@ -232,8 +252,7 @@ public class InternalComponentResourcesI
         replay();
 
         InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null, null, "Foo.bar",
-                                                                                  null, ins, false);
-
+                null, ins, false);
 
         try
         {
@@ -243,7 +262,7 @@ public class InternalComponentResourcesI
         catch (IllegalStateException ex)
         {
             assertEquals(ex.getMessage(),
-                         "Component Foo.bar is not rendering, so render variable 'fred' may not be updated.");
+                    "Component Foo.bar is not rendering, so render variable 'fred' may not be updated.");
         }
 
         verify();
@@ -266,13 +285,13 @@ public class InternalComponentResourcesI
         replay();
 
         InternalComponentResources resources = new InternalComponentResourcesImpl(page, element, null, null, null,
-                                                                                  null, ins, false);
+                null, ins, false);
 
         resources.addPageLifecycleListener(listener);
 
         verify();
     }
-    
+
     @Test
     public void get_property_name()
     {
@@ -282,21 +301,21 @@ public class InternalComponentResourcesI
         ComponentPageElement element = mockComponentPageElement();
         Page page = mockPage();
         Binding binding = mockBinding();
-        
+
         train_getModel(ins, model);
 
         replay();
 
         InternalComponentResources resources = new InternalComponentResourcesImpl(page, element, null, null, null,
-                                                                                  null, ins, false);
-        
+                null, ins, false);
+
         resources.bindParameter("bar", binding);
 
         assertNull(resources.getPropertyName("bar"));
 
         verify();
     }
-    
+
     @Test
     public void get_property_name_internal_prop_binding()
     {
@@ -306,16 +325,16 @@ public class InternalComponentResourcesI
         ComponentPageElement element = mockComponentPageElement();
         Page page = mockPage();
         InternalPropBinding binding = newMock(InternalPropBinding.class);
-        
+
         train_getModel(ins, model);
-        
+
         expect(binding.getPropertyName()).andReturn("foo");
 
         replay();
 
         InternalComponentResources resources = new InternalComponentResourcesImpl(page, element, null, null, null,
-                                                                                  null, ins, false);
-        
+                null, ins, false);
+
         resources.bindParameter("bar", binding);
 
         assertEquals(resources.getPropertyName("bar"), "foo");