org.apache.webbeans.context.AbstractContext Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.webbeans.context;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.AlterableContext;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import org.apache.webbeans.container.SerializableBean;
import org.apache.webbeans.container.SerializableBeanVault;
import org.apache.webbeans.context.creational.BeanInstanceBag;
/**
* Abstract implementation of the {@link javax.enterprise.context.spi.Context} interfaces.
*
* @see javax.enterprise.context.spi.Context
* @see RequestContext
* @see DependentContext
* @see SessionContext
* @see ApplicationContext
* @see ConversationContext
*/
public abstract class AbstractContext implements AlterableContext, Serializable
{
private static final long serialVersionUID = 2357678967444477818L;
/**Context status, active or not*/
protected volatile boolean active;
/**Context contextual instances*/
protected Map, BeanInstanceBag>> componentInstanceMap = null;
/**Contextual Scope Type*/
protected Class extends Annotation> scopeType;
@SuppressWarnings("unchecked")
private BeanInstanceBag createContextualBag(Contextual contextual, CreationalContext creationalContext)
{
BeanInstanceBag bag = new BeanInstanceBag(creationalContext);
if(componentInstanceMap instanceof ConcurrentMap)
{
BeanInstanceBag> existingBag = ((ConcurrentMap, BeanInstanceBag>>) componentInstanceMap).putIfAbsent(contextual, bag);
if (existingBag != null)
{
bag = (BeanInstanceBag) existingBag;
}
}
else
{
componentInstanceMap.put(contextual, bag);
}
return bag;
}
/**
* Creates a new context with given scope type.
*
* @param scopeType context scope type
*/
protected AbstractContext(Class extends Annotation> scopeType)
{
this.scopeType = scopeType;
setComponentInstanceMap();
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public T get(Contextual component)
{
checkActive();
BeanInstanceBag bag = componentInstanceMap.get(component);
if(bag != null)
{
return (T) bag.getBeanInstance();
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public T get(Contextual contextual, CreationalContext creationalContext)
{
checkActive();
return getInstance(contextual, creationalContext);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
protected T getInstance(Contextual contextual, CreationalContext creationalContext)
{
T instance;
//Look for bag
BeanInstanceBag bag = (BeanInstanceBag)componentInstanceMap.get(contextual);
if(bag == null)
{
bag = createContextualBag(contextual, creationalContext);
}
//Look for instance
instance = bag.getBeanInstance();
if (instance != null)
{
return instance;
}
else
{
if(creationalContext == null)
{
return null;
}
else
{
instance = bag.create(contextual);
}
}
return instance;
}
@Override
public void destroy(Contextual> contextual)
{
destroyInstance(contextual);
}
/**
* Internal destroy method.
*/
public void destroyInstance(Contextual> contextual)
{
BeanInstanceBag> instance = componentInstanceMap.get(contextual);
if (instance == null)
{
// just exit if people manually invoke destroy after the bean already got ditched
return;
}
//Get creational context
CreationalContext
© 2015 - 2025 Weber Informatics LLC | Privacy Policy