/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., 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.context;

import static org.jboss.weld.logging.Category.CONTEXT;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.ContextMessage.CONTEXTUAL_INSTANCE_REMOVED;
import static org.jboss.weld.logging.messages.ContextMessage.CONTEXTUAL_IS_NULL;
import static org.jboss.weld.logging.messages.ContextMessage.CONTEXT_CLEARED;
import static org.jboss.weld.logging.messages.ContextMessage.NO_BEAN_STORE_AVAILABLE;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;

import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.context.api.ContextualInstance;
import org.jboss.weld.context.beanstore.BeanStore;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.slf4j.cal10n.LocLogger;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

/**
 * Base for the Context implementations. Delegates calls to the abstract
 * getBeanStore and getActive to allow for different implementations (storage
 * types and ThreadLocal vs. shared)
 * 
 * @author Nicklas Karlsson
 * @author Pete Muir
 * 
 * @see org.jboss.weld.contexts.SharedContext
 * @see org.jboss.weld.context.BasicContext
 */
public abstract class AbstractContext implements Context
{
   private static final LocLogger log = loggerFactory().getLogger(CONTEXT);

   private Lock creationLock = new ReentrantLock();
   
   private final boolean multithreaded;
   
   private final ServiceRegistry serviceRegistry;
   
   /**
    * Constructor
    * 
    * @param scopeType The scope type
    */
   public AbstractContext(boolean multithreaded)
   {
      this.multithreaded = multithreaded;
      this.serviceRegistry = Container.instance().services();
   }

   /**
    * Get the bean if it exists in the contexts.
    * 
    * @param create If true, a new instance of the bean will be created if none
    *           exists
    * @return An instance of the bean
    * @throws ContextNotActiveException if the context is not active
    * 
    * @see javax.enterprise.context.spi.Context#get(BaseBean, boolean)
    */
   @SuppressWarnings(value="UL_UNRELEASED_LOCK", justification="False positive from FindBugs")
   public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext)
   {
      if (!isActive())
      {
         throw new ContextNotActiveException();
      }
      if (getBeanStore() == null)
      {
         return null;
      }
      if (contextual == null)
      {
         throw new IllegalArgumentException(CONTEXTUAL_IS_NULL);
      }
      String id = getId(contextual);
      ContextualInstance<T> beanInstance = getBeanStore().get(id);
      if (beanInstance != null)
      {
         return beanInstance.getInstance();
      }
      else if (creationalContext != null)
      {
         try
         {
            if (multithreaded)
            {
               
               creationLock.lock();
               beanInstance = getBeanStore().get(id);
               if (beanInstance != null)
               {
                  return beanInstance.getInstance();
               }
            }
            T instance = contextual.create(creationalContext);
            if (instance != null)
            {
               beanInstance = new SerializableContextualInstanceImpl<Contextual<T>, T>(contextual, instance, creationalContext, serviceRegistry.get(ContextualStore.class));
               getBeanStore().put(id, beanInstance);
            }
            return instance;
         }
         finally
         {
            if (multithreaded)
            {
               creationLock.unlock();
            }
         }
      }
      else
      {
         return null;
      }
   }

   public <T> T get(Contextual<T> contextual)
   {
      return get(contextual, null);
   }
   
   private <T> void destroy(String id)
   {
      if (getBeanStore() == null)
      {
         throw new IllegalStateException(NO_BEAN_STORE_AVAILABLE, this);
      }
      ContextualInstance<T> beanInstance = getBeanStore().get(id);
      beanInstance.getContextual().destroy(beanInstance.getInstance(), beanInstance.getCreationalContext());
      log.trace(CONTEXTUAL_INSTANCE_REMOVED, id, this);
   }
   

   /**
    * Destroys the context
    */
   protected void destroy()
   {
      log.trace(CONTEXT_CLEARED, this);
      if (getBeanStore() == null)
      {
         throw new IllegalStateException(NO_BEAN_STORE_AVAILABLE, this);
      }
      for (String id : getBeanStore())
      {
         destroy(id);
      }
      getBeanStore().clear();
   }

   /**
    * A method that returns the actual bean store implementation
    * 
    * @return The bean store
    */
   protected abstract BeanStore getBeanStore();
   
   public void cleanup()
   {
      if (getBeanStore() != null)
      {
         getBeanStore().clear();
      }
   }
   
   protected static <T> Contextual<T> getContextual(String id)
   {
      return Container.instance().services().get(ContextualStore.class).<Contextual<T>, T>getContextual(id);
   }
   
   protected String getId(Contextual<?> contextual)
   {
      return serviceRegistry.get(ContextualStore.class).putIfAbsent(contextual);
   }
   
   protected ServiceRegistry getServiceRegistry()
   {
      return serviceRegistry;
   }
   
}

@@ -3527,34 +3527,73 @@
 %7D%0A      
-if (getBeanStore()
+BeanStore beanStore = getBeanStore();%0A      if (beanStore
  == null
@@ -3819,36 +3819,33 @@
 nInstance = 
-getB
+b
 eanStore
 ().get(id);%0A
@@ -3824,34 +3824,32 @@
 ance = beanStore
-()
 .get(id);%0A      
@@ -4131,36 +4131,33 @@
 nInstance = 
-getB
+b
 eanStore
 ().get(id);%0A
@@ -4136,34 +4136,32 @@
 ance = beanStore
-()
 .get(id);%0A      
@@ -4595,28 +4595,25 @@
         
-getB
+b
 eanStore
 ().put(i
@@ -4604,18 +4604,16 @@
 eanStore
-()
 .put(id,

Call getBeanStore() only once