[webbeans-commits] Webbeans SVN: r2158 - in ri/trunk: tests/src/test/java/org/jboss/webbeans/test/unit/context and 1 other directory.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Mon Mar 23 16:09:09 EDT 2009


Author: nickarls
Date: 2009-03-23 16:09:09 -0400 (Mon, 23 Mar 2009)
New Revision: 2158

Added:
   ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplicationScopedTest.java
   ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplictionScopedObject.java
Modified:
   ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractMapContext.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractThreadLocalMapContext.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/context/ApplicationContext.java
   ri/trunk/impl/src/main/java/org/jboss/webbeans/context/RequestContext.java
Log:
WBRI-195, Thanks to Takeshi Kondo

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractMapContext.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractMapContext.java	2009-03-23 19:55:17 UTC (rev 2157)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractMapContext.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -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;
@@ -43,6 +44,8 @@
 {
    private static LogProvider log = Logging.getLogProvider(AbstractMapContext.class);
 
+   private static ReentrantLock creationLock = new ReentrantLock();
+   
    /**
     * Constructor
     * 
@@ -76,12 +79,32 @@
       }
       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;
          }
-         return instance;
+         finally
+         {
+            if (needCreationLock)
+            {
+               creationLock.unlock();
+            }
+         }
       }
       else
       {
@@ -125,5 +148,11 @@
     * @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();
 
 }

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractThreadLocalMapContext.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractThreadLocalMapContext.java	2009-03-23 19:55:17 UTC (rev 2157)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/context/AbstractThreadLocalMapContext.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -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;
+   }
+   
 }
\ No newline at end of file

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/context/ApplicationContext.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/context/ApplicationContext.java	2009-03-23 19:55:17 UTC (rev 2157)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/context/ApplicationContext.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -106,4 +106,10 @@
       return active + "application context " + beanStoreInfo;
    }
 
+   @Override
+   protected boolean isCreationLockRequired()
+   {
+      return true;
+   }
+
 }

Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/context/RequestContext.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/context/RequestContext.java	2009-03-23 19:55:17 UTC (rev 2157)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/context/RequestContext.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -51,4 +51,10 @@
       return active + "request context " + beanStoreInfo; 
    }
 
+   @Override
+   protected boolean isCreationLockRequired()
+   {
+      return false;
+   }
+   
 }

Added: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplicationScopedTest.java
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplicationScopedTest.java	                        (rev 0)
+++ ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplicationScopedTest.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -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;
+
+ at 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;
+   }
+
+}

Added: ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplictionScopedObject.java
===================================================================
--- ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplictionScopedObject.java	                        (rev 0)
+++ ri/trunk/tests/src/test/java/org/jboss/webbeans/test/unit/context/ApplictionScopedObject.java	2009-03-23 20:09:09 UTC (rev 2158)
@@ -0,0 +1,20 @@
+package org.jboss.webbeans.test.unit.context;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.context.ApplicationScoped;
+
+ at ApplicationScoped
+public class ApplictionScopedObject
+{
+   private AtomicInteger counter = new AtomicInteger();
+
+   public void increment()
+   {
+      counter.incrementAndGet();
+   }
+   public int getValue()
+   {
+      return counter.get();
+   }
+}




More information about the weld-commits mailing list