@Override public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) { if (creationalContext == null) { return null; } checkActive(); if (passivatingScope) { if (!(bean instanceof PassivationCapable)) { throw new IllegalStateException( bean.toString() + " doesn't implement " + PassivationCapable.class.getName()); } } ContextualStorage storage = getContextualStorage(bean, true); Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean)); if (contextualInstanceInfo != null) { @SuppressWarnings("unchecked") final T instance = (T) contextualInstanceInfo.getContextualInstance(); if (instance != null) { return instance; } } return storage.createContextualInstance(bean, creationalContext); }
@Override public <T> T get(Contextual<T> bean) { checkActive(); ContextualStorage storage = getContextualStorage(bean, false); if (storage == null) { return null; } Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean)); if (contextualInstanceInfo == null) { return null; } return (T) contextualInstanceInfo.getContextualInstance(); }
/** * @param bean * @param creationalContext * @param <T> * @return */ public <T> T createContextualInstance( Contextual<T> bean, CreationalContext<T> creationalContext) { Object beanKey = getBeanKey(bean); if (isConcurrent()) { // locked approach ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>(); ConcurrentHashMap<Object, ContextualInstanceInfo<?>> concurrentMap = (ConcurrentHashMap<Object, ContextualInstanceInfo<?>>) contextualInstances; ContextualInstanceInfo<T> oldInstanceInfo = (ContextualInstanceInfo<T>) concurrentMap.putIfAbsent(beanKey, instanceInfo); if (oldInstanceInfo != null) { instanceInfo = oldInstanceInfo; } synchronized (instanceInfo) { T instance = instanceInfo.getContextualInstance(); if (instance == null) { instance = bean.create(creationalContext); instanceInfo.setContextualInstance(instance); instanceInfo.setCreationalContext(creationalContext); } return instance; } } else { // simply create the contextual instance ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>(); instanceInfo.setCreationalContext(creationalContext); instanceInfo.setContextualInstance(bean.create(creationalContext)); contextualInstances.put(beanKey, instanceInfo); return instanceInfo.getContextualInstance(); } }
public static void destroyBean( Contextual bean, ContextualInstanceInfo<?> contextualInstanceInfo) { bean.destroy( contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext()); }