/** * 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; }
/** * Destroy the view scoped beans for the given view and context map. * * @param viewMap the view map. * @param contextMap the context map. */ private void destroyBeans( Map<String, Object> viewMap, Map<String, ViewScopeContextObject> contextMap) { ArrayList<String> removalNameList = new ArrayList<String>(); if (contextMap != null) { for (Map.Entry<String, ViewScopeContextObject> entry : contextMap.entrySet()) { String passivationCapableId = entry.getKey(); Contextual contextual = beanManager.getPassivationCapableBean(passivationCapableId); ViewScopeContextObject contextObject = entry.getValue(); CreationalContext creationalContext = beanManager.createCreationalContext(contextual); // We can no longer get this from the contextObject. Instead we must call // beanManager.createCreationalContext(contextual) contextual.destroy(viewMap.get(contextObject.getName()), creationalContext); removalNameList.add(contextObject.getName()); } Iterator<String> removalNames = removalNameList.iterator(); while (removalNames.hasNext()) { String name = removalNames.next(); viewMap.remove(name); } } }