/*
 * 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.weld.environment.servlet.jsf;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;


/**
 * @author pmuir
 * @author alesj
 */
public class WeldApplicationFactory extends ForwardingApplicationFactory
{

   private final ApplicationFactory applicationFactory;
   
   private Application application;

   private ReadWriteLock lock = new ReentrantReadWriteLock();

   public WeldApplicationFactory(ApplicationFactory applicationFactory)
   {
      this.applicationFactory = applicationFactory;
   }
   
   @Override
   protected ApplicationFactory delegate()
   {
      return applicationFactory;
   }
   
   @Override
   public Application getApplication()
   {
      lock.readLock().lock();
      try
      {
         if (application == null)
            application = new WeldApplication(delegate().getApplication());

         return application;
      }
      finally
      {
         lock.readLock().unlock();
      }
   }

   @Override
   public void setApplication(Application application)
   {
      lock.writeLock().lock();
      try
      {
         this.application = null; // invalidate the instance, so it picks up new application
         super.setApplication(application);
      }
      finally
      {
         lock.writeLock().unlock();
      }
   }
}

@@ -592,18 +592,16 @@
 %22 BASIS,
-  
 %0A * WITH
@@ -831,164 +831,8 @@
 f;%0A%0A
-import java.util.concurrent.locks.ReadWriteLock;%0Aimport java.util.concurrent.locks.ReentrantLock;%0Aimport java.util.concurrent.locks.ReentrantReadWriteLock;%0A
 impo
@@ -1098,19 +1098,16 @@
 actory;%0A
-   
 %0A   priv
@@ -1110,16 +1110,25 @@
 private 
+volatile 
 Applicat
@@ -1149,71 +1149,8 @@
 n;%0A%0A
-   private ReadWriteLock lock = new ReentrantReadWriteLock();%0A%0A
    p
@@ -1271,35 +1271,32 @@
 onFactory;%0A   %7D%0A
-   
 %0A   @Override%0A  
@@ -1379,19 +1379,16 @@
 y;%0A   %7D%0A
-   
 %0A   @Ove
@@ -1447,42 +1447,72 @@
     
-lock.readLock().lock();%0A      try%0A
+if(application == null)%0A      %7B%0A         synchronized (this)%0A   
     
@@ -1528,11 +1528,13 @@
     
+   
 if
- 
 (app
@@ -1550,24 +1550,35 @@
 == null)%0A   
+ 
+        %7B%0A
          app
@@ -1574,16 +1574,22 @@
         
+      
 applicat
@@ -1648,38 +1648,14 @@
 ));%0A
-%0A
       
-   return application;%0A
     
@@ -1668,66 +1668,46 @@
     
-finally
+   %7D
 %0A      
-%7B
+%7D
 %0A      
-   lock.readLock().unlock();%0A      %7D
+return application;
 %0A   
@@ -1792,42 +1792,27 @@
     
-lock.writeLock().lock();%0A      try
+synchronized (this)
 %0A   
@@ -1965,74 +1965,8 @@
   %7D%0A
-      finally%0A      %7B%0A         lock.writeLock().unlock();%0A      %7D%0A
    %7D

Fix locking in WeldApplicationFactory