Skip to content

Commit

Permalink
WELD-787 remove two thread local leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Jan 2, 2011
1 parent 8aba6cb commit e3f241b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 29 deletions.
@@ -1,7 +1,6 @@
package org.jboss.weld.bean.proxy;

import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.endInterceptorContext;
import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.getDisabledInterceptionContexts;
import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.startInterceptorContext;

import java.io.Serializable;
Expand Down Expand Up @@ -39,7 +38,7 @@ public void setOuterDecorator(Object outerDecorator)
private Set<CombinedInterceptorAndDecoratorStackMethodHandler> getDisabledHandlers()
{

return getDisabledInterceptionContexts().peek();
return InterceptionDecorationContext.peek();
}

public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
Expand All @@ -48,7 +47,7 @@ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] ar

try
{
if (getDisabledInterceptionContexts().empty())
if (InterceptionDecorationContext.empty())
{
externalContext = true;
startInterceptorContext();
Expand Down Expand Up @@ -100,7 +99,7 @@ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] ar

public boolean isDisabledHandler()
{
if (getDisabledInterceptionContexts().empty())
if (InterceptionDecorationContext.empty())
{
return false;
}
Expand Down
Expand Up @@ -17,6 +17,7 @@

package org.jboss.weld.bean.proxy;

import java.util.EmptyStackException;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
Expand All @@ -36,27 +37,77 @@
*/
public class InterceptionDecorationContext
{
private static ThreadLocal<Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>> interceptionContexts = new ThreadLocal<Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>>()
private static ThreadLocal<Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>> interceptionContexts = new ThreadLocal<Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>>();

public static Set<CombinedInterceptorAndDecoratorStackMethodHandler> pop()
{
@Override
protected Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> initialValue()
Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> stack = interceptionContexts.get();
if (stack == null)
{
return new Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>();
throw new EmptyStackException();
}
};

public static Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> getDisabledInterceptionContexts()
else
{
try
{
return stack.pop();
}
finally
{
if (stack.isEmpty())
{
interceptionContexts.remove();
}
}
}
}

public static Set<CombinedInterceptorAndDecoratorStackMethodHandler> push(Set<CombinedInterceptorAndDecoratorStackMethodHandler> item)
{
Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> stack = interceptionContexts.get();
if (stack == null)
{
stack = new Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>>();
interceptionContexts.set(stack);
}
stack.push(item);
return item;
}

public static Set<CombinedInterceptorAndDecoratorStackMethodHandler> peek()
{
Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> stack = interceptionContexts.get();
if (stack == null)
{
throw new EmptyStackException();
}
else
{
return stack.peek();
}
}

public static boolean empty()
{
return interceptionContexts.get();
Stack<Set<CombinedInterceptorAndDecoratorStackMethodHandler>> stack = interceptionContexts.get();
if (stack == null)
{
return true;
}
else
{
return stack.empty();
}
}


public static void endInterceptorContext()
{
getDisabledInterceptionContexts().pop();
pop();
}

public static void startInterceptorContext()
{
getDisabledInterceptionContexts().push(new HashSet<CombinedInterceptorAndDecoratorStackMethodHandler>());
push(new HashSet<CombinedInterceptorAndDecoratorStackMethodHandler>());
}
}
Expand Up @@ -16,6 +16,7 @@
*/
package org.jboss.weld.injection;

import java.util.EmptyStackException;
import java.util.Stack;

import javax.enterprise.inject.spi.InjectionPoint;
Expand All @@ -29,14 +30,7 @@ public class CurrentInjectionPoint implements Service

public CurrentInjectionPoint()
{
this.currentInjectionPoint = new ThreadLocal<Stack<InjectionPoint>>()
{
@Override
protected Stack<InjectionPoint> initialValue()
{
return new Stack<InjectionPoint>();
}
};
this.currentInjectionPoint = new ThreadLocal<Stack<InjectionPoint>>();
}

/**
Expand All @@ -49,12 +43,33 @@ protected Stack<InjectionPoint> initialValue()
*/
public void push(InjectionPoint injectionPoint)
{
currentInjectionPoint.get().push(injectionPoint);
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null)
{
stack = new Stack<InjectionPoint>();
currentInjectionPoint.set(stack);
}
stack.push(injectionPoint);
}

public InjectionPoint pop()
{
return currentInjectionPoint.get().pop();
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null)
{
throw new EmptyStackException();
}
try
{
return stack.pop();
}
finally
{
if (stack.isEmpty())
{
currentInjectionPoint.remove();
}
}
}

/**
Expand All @@ -64,9 +79,14 @@ public InjectionPoint pop()
*/
public InjectionPoint peek()
{
if (!currentInjectionPoint.get().empty())
Stack<InjectionPoint> stack = currentInjectionPoint.get();
if (stack == null)
{
return currentInjectionPoint.get().peek();
return null;
}
if (!stack.empty())
{
return stack.peek();
}
else
{
Expand All @@ -76,7 +96,7 @@ public InjectionPoint peek()

public void cleanup()
{
this.currentInjectionPoint.remove();

}

}
@@ -0,0 +1,8 @@
package org.jboss.weld.util.collections;

import java.util.Stack;

public class EmptyStack<E> extends Stack<E>
{

}
Expand Up @@ -64,7 +64,7 @@ public void testSelfInvokingClassWithFailingBean(@Failing SomeBean someBean)
{
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
Assert.assertEquals(0, InterceptionDecorationContext.getDisabledInterceptionContexts().size());
Assert.assertTrue(InterceptionDecorationContext.empty());
}

@Test
Expand All @@ -80,7 +80,7 @@ public void testSelfInvokingClassWithSucceedingBean(@Succeeding SomeBean someBea
Assert.assertEquals("methodA", SomeBeanDecorator.calls.get(0));
Assert.assertEquals("methodB", SomeBeanDecorator.calls.get(1));

Assert.assertEquals(0, InterceptionDecorationContext.getDisabledInterceptionContexts().size());
Assert.assertTrue(InterceptionDecorationContext.empty());
}

}
Expand Up @@ -16,6 +16,10 @@
*/
package org.jboss.weld.tests.resources;

import static org.junit.Assert.assertNotNull;

import javax.transaction.UserTransaction;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
Expand Down

0 comments on commit e3f241b

Please sign in to comment.