You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2011/04/09 02:28:50 UTC

[Tapestry Wiki] Update of "Tapestry5HowToAddBindingPrefixCycle" by DavidRees

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The "Tapestry5HowToAddBindingPrefixCycle" page has been changed by DavidRees.
http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefixCycle?action=diff&rev1=5&rev2=6

--------------------------------------------------

  }
  }}}
  
- and this also:
+ And the binding itself:
+ 
+ Note: that starting with Tapestry 5.2 PageBindings do not appear to be thread safe when running without page pools.  So now we store the index in a ThreadLocal which works around this upgrade issue. (The previous version simply used an int for the index instead of a ThreadLocal).
+ 
  {{{#!java
  import java.util.List;
  
@@ -92, +95 @@

  
  public class CycleBinding extends AbstractBinding implements PageLifecycleListener{
      private final List<Binding> delegates;
-     private int index = 0;
+     private ThreadLocal<Integer> index;
  
      public CycleBinding(List<Binding> delegates) {
          this.delegates = delegates;
      }
  
      public Object get() {
-         Object ret = delegates.get(index).get();
+         Object ret = delegates.get(getIndex()).get();
+         incrIndex();
-         index ++; 
-         if(index>=delegates.size()) index = 0;
          return ret;
      }
      
@@ -115, +117 @@

          return Object.class;
      }
  
- 
      public void containingPageDidDetach() {
-         index=0;
+         index.remove();
      }
  
-     public void containingPageDidAttach() {/*not interested*/}
+     public void containingPageDidAttach() {
+         index = createThreadLocal();
-     
+     }
+ 
-     public void containingPageDidLoad() {/*not interested*/}
+     public void containingPageDidLoad() {}
+ 
+     public void restoreStateBeforePageAttach() {}
+ 
+     private ThreadLocal<Integer> createThreadLocal() {
+         return new ThreadLocal<Integer>() {
+             @Override
+             protected Integer initialValue() {
+                 return Integer.valueOf(0);
+             };
+         };
+     }
+ 
+     private int getIndex() {
+         return index.get().intValue();
+     }
+ 
+     private void incrIndex() {
+         int i = index.get().intValue() + 1;
+         if (i >= delegates.size()) {
+             i = 0;
+         }
+         index.set(Integer.valueOf(i));
+     }
  }
- 
- 
  
  }}}
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org