com.sun.faces.application.view.ViewScopeContextManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jakarta.faces Show documentation
Show all versions of jakarta.faces Show documentation
EE4J Compatible Implementation for Jakarta Faces API
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.faces.application.view;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.Util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
/**
* The manager that deals with CDI ViewScoped beans.
*/
public class ViewScopeContextManager {
private static final Logger LOGGER = FacesLogger.APPLICATION_VIEW.getLogger();
private boolean isCdiOneOneOrGreater;
private Class viewScopedCDIEventFireHelperImplClass;
/**
* Stores the constant to keep track of all the active view scope contexts.
*/
private static final String ACTIVE_VIEW_CONTEXTS = "com.sun.faces.application.view.activeViewContexts";
/**
* Stores the constants to keep track of the active view maps.
*/
private static final String ACTIVE_VIEW_MAPS = "com.sun.faces.application.view.activeViewMaps";
private final BeanManager beanManager;
public ViewScopeContextManager() {
FacesContext facesContext = FacesContext.getCurrentInstance();
isCdiOneOneOrGreater = Util.isCdiOneOneOrLater(facesContext);
try {
viewScopedCDIEventFireHelperImplClass = Class.forName("com.sun.faces.application.view.ViewScopedCDIEventFireHelperImpl");
} catch (ClassNotFoundException ex) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "CDI 1.1 events not enabled", ex);
}
}
beanManager = (BeanManager) Util.getCdiBeanManager(facesContext);
}
/**
* Clear the current view map using the Faces context.
*
* @param facesContext the Faces context.
*/
public void clear(FacesContext facesContext) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Clearing @ViewScoped CDI beans for current view map");
}
Map viewMap = facesContext.getViewRoot().getViewMap(false);
Map contextMap = getContextMap(facesContext, false);
if (contextMap != null) {
destroyBeans(viewMap, contextMap);
}
}
/**
* Clear the given view map.
*
* @param facesContext the Faces context.
* @param viewMap the given view map.
*/
public void clear(FacesContext facesContext, Map viewMap) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Clearing @ViewScoped CDI beans for given view map: {0}");
}
Map contextMap = getContextMap(facesContext, viewMap);
if (contextMap != null) {
destroyBeans(viewMap, contextMap);
}
}
/**
* Create the bean.
*
* @param 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 createBean(FacesContext facesContext, Contextual contextual, CreationalContext 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;
}
/**
* 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 viewMap, Map contextMap) {
ArrayList removalNameList = new ArrayList<>();
if (contextMap != null) {
for (Map.Entry 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 removalNames = removalNameList.iterator();
while (removalNames.hasNext()) {
String name = removalNames.next();
viewMap.remove(name);
}
contextMap.clear();
}
}
/**
* Get the value from the view map (or null if not found).
*
* @param the type.
* @param facesContext the faces context.
* @param contextual the contextual.
* @return the value or null if not found.
*/
public T getBean(FacesContext facesContext, Contextual contextual) {
T result = null;
Map 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;
}
/**
* Get the context map.
*
* @param facesContext the Faces context.
* @return the context map.
*/
private Map getContextMap(FacesContext facesContext) {
return getContextMap(facesContext, true);
}
/**
* Get the context map.
*
* @param facesContext the Faces context.
* @param create flag to indicate if we are creating the context map.
* @return the context map.
*/
private Map getContextMap(FacesContext facesContext, boolean create) {
Map result = null;
ExternalContext externalContext = facesContext.getExternalContext();
if (externalContext != null) {
Map sessionMap = externalContext.getSessionMap();
Object session = externalContext.getSession(create);
if (session != null) {
Map