final Object structure;
final Object state;
boolean recalculateLocale = false;
ResponseStateManager rsm = _getResponseStateManager(context, renderKitId);
if (_saveAsToken(context))
{
Object token = rsm.getTreeStructureToRestore(context, viewId);
if (token == null)
{
_LOG.finest("No token in the request for view \"{0}\"; " +
"probably a first view.", viewId);
return null;
}
assert(token instanceof String);
_LOG.finer("Restoring saved view state for token {0}", token);
PageState viewState;
// Load from the application cache
if (_APPLICATION_CACHE_TOKEN.equals(token))
{
Map<String, PageState> cache = _getApplicationViewCache(context);
Map<String, PageState> perSessionCache =
_getPerSessionApplicationViewCache(context);
// Synchronize on the application-level cache.
// =-=AEW This may produce excessive contention
synchronized (cache)
{
// Look first in the per-session cache
viewState = cache.get(viewId);
if (viewState == null)
{
// Nope, it's not there. Look in the application cache
viewState = (PageState) cache.get(viewId);
// And if we find it there, then push it back into
// the per-session cache (it may have expired)
if (viewState != null)
perSessionCache.put(viewId, viewState);
}
// If the view was found in the application cache then we
// know it would be unsafe to use its locale for this session.
// Same conclusion, however, even if found in the per-session
// cache, since the latter is just a mirror of the former.
recalculateLocale = true;
}
}
else
{
Map<String, PageState> stateMap = new SubKeyMap<PageState>(
context.getExternalContext().getSessionMap(),
_VIEW_CACHE_KEY + ".");
viewState = stateMap.get(token);
if (viewState != null)
_updateRequestTokenForResponse(context, (String) token);
// Make sure that if the view state is present, the cache still
// has the token, and vice versa
// NOTE: it's very important that we call through to the
// token cache here, not just inside the assert. If we don't,
// then we don't actually access the token, so it doesn't
// get bumped up to the front in the LRU Cache!
boolean isAvailable =
_getViewCache(context).isAvailable((String) token);
assert ((viewState != null) == isAvailable);
}
if (viewState == null)
{
_LOG.severe("CANNOT_FIND_SAVED_VIEW_STATE", token);
return null;
}
_LOG.fine("Successfully found view state for token {0}", token);
UIViewRoot root = viewState.popRoot(context); // bug 4712492
if (root != null)
{
_LOG.finer("UIViewRoot for token {0} already exists. Bypassing restoreState", token);
return root;
}
structure = viewState.getStructure();
state = viewState.getState();
}
else
{
structure = rsm.getTreeStructureToRestore(context, viewId);
state = rsm.getComponentStateToRestore(context);
}
if (structure == null)
{