@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); }
/** * Destroys all the Contextual Instances in the specified ContextualStorage. This is a static * method to allow various holder objects to cleanup properly in @PreDestroy. */ public static Map<Object, ContextualInstanceInfo<?>> destroyAllActive(ContextualStorage storage) { // drop all entries in the storage before starting with destroying the original entries Map<Object, ContextualInstanceInfo<?>> contextMap = new HashMap<Object, ContextualInstanceInfo<?>>(storage.getStorage()); storage.getStorage().clear(); for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet()) { Contextual bean = storage.getBean(entry.getKey()); ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue(); destroyBean(bean, contextualInstanceInfo); } return contextMap; }
@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(); }
/** * Destroy the Contextual Instance of the given Bean. * * @param bean dictates which bean shall get cleaned up * @return <code>true</code> if the bean was destroyed, <code>false</code> if there was no such * bean. */ public boolean destroy(Contextual bean) { ContextualStorage storage = getContextualStorage(bean, false); if (storage == null) { return false; } ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().remove(storage.getBeanKey(bean)); if (contextualInstanceInfo == null) { return false; } destroyBean(bean, contextualInstanceInfo); return true; }