@Override
public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> creationalContext)
{
Asserts.assertNotNull(bean, "bean parameter can not be null");
Context context = null;
Object instance = null;
if (bean instanceof SerializableBean)
{
bean = ((SerializableBean)bean).getBean();
}
//Check type if bean type is given
if(beanType != null)
{
if(!ResolutionUtil.checkBeanTypeAssignableToGivenType(bean.getTypes(), beanType, bean instanceof NewBean))
{
throw new IllegalArgumentException("Given bean type : " + beanType + " is not applicable for the bean instance : " + bean);
}
}
if(!(creationalContext instanceof CreationalContextImpl))
{
creationalContext = CreationalContextFactory.getInstance().wrappedCreationalContext(creationalContext, bean);
}
//Scope is normal
if (WebBeansUtil.isScopeTypeNormal(bean.getScope()))
{
instance = getEjbOrJmsProxyReference(bean, beanType,creationalContext);
if(instance != null)
{
return instance;
}
instance = cacheProxies.get(bean);
if (instance == null)
{
//Create Managed Bean Proxy
instance = JavassistProxyFactory.getInstance().createNormalScopedBeanProxy((AbstractOwbBean<?>)bean,creationalContext);
//Cached instance
cacheProxies.put(bean, instance);
}
}
//Create Pseudo-Scope Bean Instance
else
{
//Get bean context
context = getContext(bean.getScope());
//Get instance for ejb or jms
instance = getEjbOrJmsProxyReference(bean, beanType, creationalContext);
if(instance != null)
{
return instance;
}
//Get dependent from DependentContex that create contextual instance
instance = context.get((Bean<Object>)bean, (CreationalContext<Object>)creationalContext);
}
return instance;
}