/** * Get the value from the view map (or null if not found). * * @param <T> the type. * @param facesContext the faces context. * @param contextual the contextual. * @return the value or null if not found. */ public <T> T getBean(FacesContext facesContext, Contextual<T> contextual) { T result = null; Map<String, ViewScopeContextObject> contextMap = getContextMap(facesContext); if (contextMap != null) { if (!(contextual instanceof PassivationCapable)) { throw new IllegalArgumentException( "ViewScoped bean " + contextual.toString() + " must be PassivationCapable, but is not."); } ViewScopeContextObject contextObject = contextMap.get(((PassivationCapable) contextual).getId()); if (contextObject != null) { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log( Level.FINEST, "Getting value for @ViewScoped bean with name: {0}", contextObject.getName()); } result = (T) facesContext.getViewRoot().getViewMap(true).get(contextObject.getName()); } } return result; }
@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); }
/** * Create the bean. * * @param <T> the type. * @param facesContext the faces context. * @param contextual the contextual. * @param creational the creational. * @return the value or null if not found. */ public <T> T createBean( FacesContext facesContext, Contextual<T> contextual, CreationalContext<T> creational) { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "Creating @ViewScoped CDI bean using contextual: {0}", contextual); } if (!(contextual instanceof PassivationCapable)) { throw new IllegalArgumentException( "ViewScoped bean " + contextual.toString() + " must be PassivationCapable, but is not."); } T result = contextual.create(creational); if (result != null) { String name = getName(result); facesContext.getViewRoot().getViewMap(true).put(name, result); String passivationCapableId = ((PassivationCapable) contextual).getId(); getContextMap(facesContext) .put(passivationCapableId, new ViewScopeContextObject(passivationCapableId, name)); } return result; }