Search in sources :

Example 1 with LRUMap

use of com.sun.faces.util.LRUMap in project mojarra by eclipse-ee4j.

the class ClientWindowScopeContextManager method getContextMap.

/**
 * 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.
 */
@SuppressWarnings("unchecked")
private Map<String, ClientWindowScopeContextObject> getContextMap(FacesContext facesContext, boolean create) {
    Map<String, ClientWindowScopeContextObject> result = null;
    ExternalContext externalContext = facesContext.getExternalContext();
    if (externalContext != null) {
        Map<String, Object> sessionMap = externalContext.getSessionMap();
        Object session = externalContext.getSession(create);
        if (session != null) {
            Map<Object, Map<String, ClientWindowScopeContextObject>> clientWindowScopeContexts = (Map<Object, Map<String, ClientWindowScopeContextObject>>) sessionMap.get(CLIENT_WINDOW_CONTEXTS);
            String clientWindowId = getCurrentClientWindowId(facesContext);
            if (clientWindowScopeContexts == null && create) {
                synchronized (session) {
                    Integer size = (Integer) sessionMap.get(ClientWindow.NUMBER_OF_CLIENT_WINDOWS_PARAM_NAME);
                    if (size == null) {
                        size = 10;
                    }
                    sessionMap.put(CLIENT_WINDOW_CONTEXTS, Collections.synchronizedMap(new LRUMap<String, Object>(size)));
                }
            }
            if (clientWindowScopeContexts != null && clientWindowId != null && create) {
                synchronized (clientWindowScopeContexts) {
                    if (!clientWindowScopeContexts.containsKey(clientWindowId)) {
                        clientWindowScopeContexts.put(clientWindowId, new ConcurrentHashMap<String, ClientWindowScopeContextObject>());
                        if (distributable) {
                            // If we are distributable, this will result in a dirtying of the
                            // session data, forcing replication. If we are not distributable,
                            // this is a no-op.
                            sessionMap.put(CLIENT_WINDOW_CONTEXTS, clientWindowScopeContexts);
                        }
                    }
                }
            }
            if (clientWindowScopeContexts != null && clientWindowId != null) {
                result = clientWindowScopeContexts.get(clientWindowId);
            }
        }
    }
    return result;
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext) LRUMap(com.sun.faces.util.LRUMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LRUMap(com.sun.faces.util.LRUMap) Map(java.util.Map)

Example 2 with LRUMap

use of com.sun.faces.util.LRUMap in project mojarra by eclipse-ee4j.

the class ViewScopeManager method processPostConstructViewMap.

/**
 * Process the PostConstructViewMap system event.
 *
 * @param systemEvent the system event.
 */
private void processPostConstructViewMap(SystemEvent systemEvent) {
    LOGGER.log(FINEST, "Handling PostConstructViewMapEvent");
    UIViewRoot viewRoot = (UIViewRoot) systemEvent.getSource();
    Map<String, Object> viewMap = viewRoot.getViewMap(false);
    if (viewMap != null) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (viewRoot.isTransient() && facesContext.isProjectStage(ProjectStage.Development)) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "@ViewScoped beans are not supported on stateless views", "@ViewScoped beans are not supported on stateless views");
            facesContext.addMessage(viewRoot.getClientId(facesContext), message);
            LOGGER.log(WARNING, "@ViewScoped beans are not supported on stateless views");
        }
        Object session = facesContext.getExternalContext().getSession(true);
        if (session != null) {
            Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
            Integer size = (Integer) sessionMap.get(ACTIVE_VIEW_MAPS_SIZE);
            if (size == null) {
                size = numberOfActiveViewMapsInWebXml;
                if (size == null) {
                    size = Integer.parseInt(NumberOfActiveViewMaps.getDefaultValue());
                }
            }
            if (sessionMap.get(ACTIVE_VIEW_MAPS) == null) {
                sessionMap.put(ACTIVE_VIEW_MAPS, Collections.synchronizedMap(new LRUMap<String, Object>(size)));
            }
            @SuppressWarnings("unchecked") Map<String, Object> viewMaps = (Map<String, Object>) sessionMap.get(ACTIVE_VIEW_MAPS);
            synchronized (viewMaps) {
                String viewMapId = UUID.randomUUID().toString();
                while (viewMaps.containsKey(viewMapId)) {
                    viewMapId = UUID.randomUUID().toString();
                }
                if (viewMaps.size() == size) {
                    String eldestViewMapId = viewMaps.keySet().iterator().next();
                    @SuppressWarnings("unchecked") Map<String, Object> eldestViewMap = (Map<String, Object>) viewMaps.remove(eldestViewMapId);
                    removeEldestViewMap(facesContext, eldestViewMapId, eldestViewMap);
                }
                viewMaps.put(viewMapId, viewMap);
                viewRoot.getTransientStateHelper().putTransient(VIEW_MAP_ID, viewMapId);
                viewRoot.getTransientStateHelper().putTransient(VIEW_MAP, viewMap);
                if (distributable) {
                    // If we are distributable, this will result in a dirtying of the
                    // session data, forcing replication. If we are not distributable,
                    // this is a no-op.
                    sessionMap.put(ACTIVE_VIEW_MAPS, viewMaps);
                }
            }
            if (contextManager != null) {
                contextManager.fireInitializedEvent(facesContext, viewRoot);
            }
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) LRUMap(com.sun.faces.util.LRUMap) UIViewRoot(jakarta.faces.component.UIViewRoot) FacesMessage(jakarta.faces.application.FacesMessage) LRUMap(com.sun.faces.util.LRUMap) Map(java.util.Map)

Example 3 with LRUMap

use of com.sun.faces.util.LRUMap in project mojarra by eclipse-ee4j.

the class ServerSideStateHelper method writeState.

// ------------------------------------------------ Methods from StateHelper
/**
 * <p>
 * Stores the provided state within the session obtained from the provided <code>FacesContext</code>
 * </p>
 *
 * <p>
 * If <code>stateCapture</code> is <code>null</code>, the composite key used to look up the actual and logical views
 * will be written to the client as a hidden field using the <code>ResponseWriter</code> from the provided
 * <code>FacesContext</code>.
 * </p>
 *
 * <p>
 * If <code>stateCapture</code> is not <code>null</code>, the composite key will be appended to the
 * <code>StringBuilder</code> without any markup included or any content written to the client.
 */
@Override
public void writeState(FacesContext ctx, Object state, StringBuilder stateCapture) throws IOException {
    notNull("context", ctx);
    String id;
    UIViewRoot viewRoot = ctx.getViewRoot();
    if (!viewRoot.isTransient()) {
        if (!ctx.getAttributes().containsKey("com.sun.faces.ViewStateValue")) {
            notNull("state", state);
            Object[] stateToWrite = (Object[]) state;
            ExternalContext externalContext = ctx.getExternalContext();
            Object sessionObj = externalContext.getSession(true);
            Map<String, Object> sessionMap = externalContext.getSessionMap();
            synchronized (sessionObj) {
                Map<String, Map> logicalMap = TypedCollections.dynamicallyCastMap((Map) sessionMap.get(LOGICAL_VIEW_MAP), String.class, Map.class);
                if (logicalMap == null) {
                    logicalMap = Collections.synchronizedMap(new LRUMap<String, Map>(numberOfLogicalViews));
                    sessionMap.put(LOGICAL_VIEW_MAP, logicalMap);
                }
                Object structure = stateToWrite[0];
                Object savedState = handleSaveState(stateToWrite[1]);
                String idInLogicalMap = (String) RequestStateManager.get(ctx, RequestStateManager.LOGICAL_VIEW_MAP);
                if (idInLogicalMap == null) {
                    idInLogicalMap = generateUniqueStateIds ? createRandomId() : createIncrementalRequestId(ctx);
                }
                String idInActualMap = null;
                if (ctx.getPartialViewContext().isPartialRequest()) {
                    // If partial request, do not change actual view Id, because page not actually changed.
                    // Otherwise partial requests will soon overflow cache with values that would be never used.
                    idInActualMap = (String) RequestStateManager.get(ctx, RequestStateManager.ACTUAL_VIEW_MAP);
                }
                if (null == idInActualMap) {
                    idInActualMap = generateUniqueStateIds ? createRandomId() : createIncrementalRequestId(ctx);
                }
                Map<String, Object[]> actualMap = TypedCollections.dynamicallyCastMap(logicalMap.get(idInLogicalMap), String.class, Object[].class);
                if (actualMap == null) {
                    actualMap = new LRUMap<>(numberOfViews);
                    logicalMap.put(idInLogicalMap, actualMap);
                }
                id = idInLogicalMap + ':' + idInActualMap;
                Object[] stateArray = actualMap.get(idInActualMap);
                // reuse the array if possible
                if (stateArray != null) {
                    stateArray[0] = structure;
                    stateArray[1] = savedState;
                } else {
                    actualMap.put(idInActualMap, new Object[] { structure, savedState });
                }
                // always call put/setAttribute as we may be in a clustered environment.
                sessionMap.put(LOGICAL_VIEW_MAP, logicalMap);
                ctx.getAttributes().put("com.sun.faces.ViewStateValue", id);
            }
        } else {
            id = (String) ctx.getAttributes().get("com.sun.faces.ViewStateValue");
        }
    } else {
        id = "stateless";
    }
    if (stateCapture != null) {
        stateCapture.append(id);
    } else {
        ResponseWriter writer = ctx.getResponseWriter();
        writer.startElement("input", null);
        writer.writeAttribute("type", "hidden", null);
        writer.writeAttribute("name", VIEW_STATE_PARAM.getName(ctx), null);
        if (webConfig.isOptionEnabled(EnableViewStateIdRendering)) {
            String viewStateId = Util.getViewStateId(ctx);
            writer.writeAttribute("id", viewStateId, null);
        }
        writer.writeAttribute("value", id, null);
        if (webConfig.isOptionEnabled(AutoCompleteOffOnViewState)) {
            writer.writeAttribute("autocomplete", "off", null);
        }
        writer.endElement("input");
        writeClientWindowField(ctx, writer);
        writeRenderKitIdField(ctx, writer);
    }
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) ExternalContext(jakarta.faces.context.ExternalContext) LRUMap(com.sun.faces.util.LRUMap) UIViewRoot(jakarta.faces.component.UIViewRoot) Map(java.util.Map) LRUMap(com.sun.faces.util.LRUMap)

Aggregations

LRUMap (com.sun.faces.util.LRUMap)3 Map (java.util.Map)3 UIViewRoot (jakarta.faces.component.UIViewRoot)2 ExternalContext (jakarta.faces.context.ExternalContext)2 FacesMessage (jakarta.faces.application.FacesMessage)1 FacesContext (jakarta.faces.context.FacesContext)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1