Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2158 1c488680-804c-0410-94cd-c6b725194a0e
- Loading branch information
Showing
6 changed files
with
168 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 63 additions & 58 deletions
121
impl/src/main/java/org/jboss/webbeans/context/AbstractThreadLocalMapContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplicationScopedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplictionScopedObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |