/** * We get PreDestroyViewMapEvent events from the JSF servlet and destroy our contextual instances. * This should (theoretically!) also get fired if the webapp closes, so there should be no need to * manually track all view scopes and destroy them at a shutdown. * * @see javax.faces.event.SystemEventListener#processEvent(javax.faces.event.SystemEvent) */ @Override public void processEvent(final SystemEvent event) { if (event instanceof PreDestroyViewMapEvent) { Map<Contextual<?>, Object> componentInstanceMap = getComponentInstanceMap(); Map<Contextual<?>, CreationalContext<?>> creationalContextMap = getCreationalInstanceMap(); if (componentInstanceMap != null) { for (Map.Entry<Contextual<?>, Object> componentEntry : componentInstanceMap.entrySet()) { /* * No way to inform the compiler of type <T> information, so * it has to be abandoned here :( */ Contextual contextual = componentEntry.getKey(); Object instance = componentEntry.getValue(); CreationalContext creational = creationalContextMap.get(contextual); contextual.destroy(instance, creational); } } } }
/** * 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); } } }
public static void destroyBean( Contextual bean, ContextualInstanceInfo<?> contextualInstanceInfo) { bean.destroy( contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext()); }