Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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  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(s):
Nicklas Karlsson
Pete Muir
See also:
org.jboss.weld.contexts.SharedContext
org.jboss.weld.context.BasicContext
 
 public abstract class AbstractContext implements AlterableContext {
 
     private final boolean multithreaded;
 
     private final ServiceRegistry serviceRegistry;

    
Constructor
 
     public AbstractContext(String contextIdboolean multithreaded) {
         this. = multithreaded;
         this. = Container.instance(contextId).services();
     }

    
Get the bean if it exists in the contexts.

Returns:
An instance of the bean
Throws:
ContextNotActiveException if the context is not active
See also:
javax.enterprise.context.spi.Context.get(BaseBean, boolean)
 
     @Override
     @SuppressWarnings(value = "UL_UNRELEASED_LOCK", justification = "False positive from FindBugs")
     public <T> T get(Contextual<T> contextualCreationalContext<T> creationalContext) {
         if (!isActive()) {
             throw new ContextNotActiveException();
         }
         checkContextInitialized();
         final BeanStore beanStore = getBeanStore();
         if (beanStore == null) {
             return null;
         }
         if (contextual == null) {
             throw ..contextualIsNull();
         }
         BeanIdentifier id = getId(contextual);
         ContextualInstance<T> beanInstance = beanStore.get(id);
         if (beanInstance != null) {
             return beanInstance.getInstance();
         } else if (creationalContext != null) {
             LockedBean lock = null;
             try {
                 if () {
                     lock = beanStore.lock(id);
                     beanInstance = beanStore.get(id);
                     if (beanInstance != null) {
                         return beanInstance.getInstance();
                     }
                 }
                 T instance = contextual.create(creationalContext);
                 if (instance != null) {
                    beanInstance = new SerializableContextualInstanceImpl<Contextual<T>, T>(contextualinstancecreationalContext.get(ContextualStore.class));
                    beanStore.put(idbeanInstance);
                }
                return instance;
            } finally {
                if (lock != null) {
                    lock.unlock();
                }
            }
        } else {
            return null;
        }
    }
    @Override
    public <T> T get(Contextual<T> contextual) {
        return get(contextualnull);
    }
    @Override
    public void destroy(Contextual<?> contextual) {
        if (!isActive()) {
            throw new ContextNotActiveException();
        }
        checkContextInitialized();
        if (contextual == null) {
            throw ..contextualIsNull();
        }
        final BeanStore beanStore = getBeanStore();
        if (beanStore == null) {
            throw ..noBeanStoreAvailable(this);
        }
        BeanIdentifier id = getId(contextual);
        ContextualInstance<?> beanInstance = beanStore.remove(id);
        if (beanInstance != null) {
            RequestScopedCache.invalidate();
            destroyContextualInstance(beanInstance);
        }
    }
        final BeanStore beanStore = getBeanStore();
        if (beanStore == null) {
            throw ..noBeanStoreAvailable(this);
        }
        return beanStore.get(id);
    }
    private <T> void destroyContextualInstance(ContextualInstance<T> instance) {
        instance.getContextual().destroy(instance.getInstance(), instance.getCreationalContext());
        ..contextualInstanceRemoved(instancethis);
    }

    
Destroys the context
    protected void destroy() {
        ..contextCleared(this);
        final BeanStore beanStore = getBeanStore();
        if (beanStore == null) {
            throw ..noBeanStoreAvailable(this);
        }
        for (BeanIdentifier id : beanStore) {
        }
        beanStore.clear();
    }

    
A method that returns the actual bean store implementation

Returns:
The bean store
    protected abstract BeanStore getBeanStore();
    public void cleanup() {
        final BeanStore beanStore = getBeanStore();
        if (beanStore != null) {
            try {
                beanStore.clear();
            } catch (Exception e) {
                ..unableToClearBeanStore(beanStore);
                ..catchingDebug(e);
            }
        }
    }
    protected static <T> Contextual<T> getContextual(String contextIdString id) {
        return Container.instance(contextId).services().get(ContextualStore.class).<Contextual<T>, T>getContextual(id);
    }
    protected BeanIdentifier getId(Contextual<?> contextual) {
        if (contextual instanceof WrappedContextual<?>) {
            contextual = ((WrappedContextual<?>) contextual).delegate();
        }
        if (contextual instanceof CommonBean<?>) {
            // There is not need to call ContextualStore.putIfAbsent() because it's called for all PassivationCapable beans during deployment
            return ((CommonBean<?>) contextual).getIdentifier();
        }
        return .get(ContextualStore.class).putIfAbsent(contextual);
    }
    protected ServiceRegistry getServiceRegistry() {
        return ;
    }

    
Allows contexts that are initialized lazily to plug in additional logic.
    protected void checkContextInitialized() {
    }
New to GrepCode? Check out our FAQ X