Skip to content

Commit

Permalink
WBRI-195, Thanks to Takeshi Kondo
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2158 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Mar 23, 2009
1 parent 983ed1e commit d0884d8
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 62 deletions.
Expand Up @@ -18,6 +18,7 @@
package org.jboss.webbeans.context;

import java.lang.annotation.Annotation;
import java.util.concurrent.locks.ReentrantLock;

import javax.context.ContextNotActiveException;
import javax.context.Contextual;
Expand All @@ -43,6 +44,8 @@ public abstract class AbstractMapContext extends AbstractContext
{
private static LogProvider log = Logging.getLogProvider(AbstractMapContext.class);

private static ReentrantLock creationLock = new ReentrantLock();

/**
* Constructor
*
Expand Down Expand Up @@ -76,12 +79,32 @@ public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContex
}
else if (creationalContext != null)
{
instance = contextual.create(creationalContext);
if (instance != null)
boolean needCreationLock = isCreationLockRequired();
try
{
getBeanStore().put(contextual, instance);
if(needCreationLock)
{
creationLock.lock();
instance = getBeanStore().get(contextual);
if (instance != null)
{
return instance;
}
}
instance = contextual.create(creationalContext);
if (instance != null)
{
getBeanStore().put(contextual, instance);
}
return instance;
}
finally
{
if (needCreationLock)
{
creationLock.unlock();
}
}
return instance;
}
else
{
Expand Down Expand Up @@ -125,5 +148,11 @@ public void destroy()
* @return The bean store
*/
protected abstract BeanStore getBeanStore();

/**
* If Context need to inhibit concurrent instance creation then true, else false.
* @return need lock
*/
protected abstract boolean isCreationLockRequired();

}
@@ -1,59 +1,64 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.webbeans.context;

import java.lang.annotation.Annotation;

import org.jboss.webbeans.context.api.BeanStore;

/**
* Abstract base class for representing contexts with thread local bean storage
*
* @author Pete Muir
*/
public abstract class AbstractThreadLocalMapContext extends AbstractMapContext
{
private final ThreadLocal<BeanStore> beanStore;

public AbstractThreadLocalMapContext(Class<? extends Annotation> scopeType)
{
super(scopeType);
this.beanStore = new ThreadLocal<BeanStore>();
}

/**
* Gets the bean store
*
* @returns The bean store
*/
@Override
public BeanStore getBeanStore()
{
return beanStore.get();
}

/**
* Sets the bean store
*
* @param beanStore The bean store
*/
public void setBeanStore(BeanStore beanStore)
{
this.beanStore.set(beanStore);
}

/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.webbeans.context;

import java.lang.annotation.Annotation;

import org.jboss.webbeans.context.api.BeanStore;

/**
* Abstract base class for representing contexts with thread local bean storage
*
* @author Pete Muir
*/
public abstract class AbstractThreadLocalMapContext extends AbstractMapContext
{
private final ThreadLocal<BeanStore> beanStore;

public AbstractThreadLocalMapContext(Class<? extends Annotation> scopeType)
{
super(scopeType);
this.beanStore = new ThreadLocal<BeanStore>();
}

/**
* Gets the bean store
*
* @returns The bean store
*/
@Override
public BeanStore getBeanStore()
{
return beanStore.get();
}

/**
* Sets the bean store
*
* @param beanStore The bean store
*/
public void setBeanStore(BeanStore beanStore)
{
this.beanStore.set(beanStore);
}

protected boolean isCreationLockRequired()
{
return true;
}

}
Expand Up @@ -106,4 +106,10 @@ public String toString()
return active + "application context " + beanStoreInfo;
}

@Override
protected boolean isCreationLockRequired()
{
return true;
}

}
Expand Up @@ -51,4 +51,10 @@ public String toString()
return active + "request context " + beanStoreInfo;
}

@Override
protected boolean isCreationLockRequired()
{
return false;
}

}
@@ -0,0 +1,40 @@
package org.jboss.webbeans.test.unit.context;

import java.util.concurrent.CountDownLatch;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.webbeans.test.unit.AbstractWebBeansTest;
import org.testng.annotations.Test;

@Artifact
public class ApplicationScopedTest extends AbstractWebBeansTest
{

@Test
public void testConcurrentInitilized() throws InterruptedException
{
final CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++)
{
new Thread(new Runnable()
{
public void run()
{
try
{
manager.getInstanceByType(ApplictionScopedObject.class).increment();
}
finally
{
latch.countDown();
}
}
}).start();
}
latch.await();
int value = manager.getInstanceByType(ApplictionScopedObject.class).getValue();
System.out.println(value);
assert value == 10;
}

}
@@ -0,0 +1,20 @@
package org.jboss.webbeans.test.unit.context;

import java.util.concurrent.atomic.AtomicInteger;

import javax.context.ApplicationScoped;

@ApplicationScoped
public class ApplictionScopedObject
{
private AtomicInteger counter = new AtomicInteger();

public void increment()
{
counter.incrementAndGet();
}
public int getValue()
{
return counter.get();
}
}

0 comments on commit d0884d8

Please sign in to comment.