All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.el.ScopedRunner Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
/*
 * Copyright 2020 OmniFaces
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.omnifaces.el;

import java.util.HashMap;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.omnifaces.util.Callback;

/**
 * This class helps in letting code run within its own scope. Such scope is defined by specific variables being
 * available to EL within it. The request scope is used to store the variables.
 *
 * @author Arjan Tijms
 *
 */
public class ScopedRunner {

	private FacesContext context;
	private Map scopedVariables;
	private Map previousVariables = new HashMap<>();

	public ScopedRunner(FacesContext context) {
		this(context, new HashMap());
	}

	public ScopedRunner(FacesContext context, Map scopedVariables) {
		this.context = context;
		this.scopedVariables = scopedVariables;
	}

	/**
	 * Adds the given variable to this instance. Can be used in a builder-pattern.
	 *
	 * @param key the key name of the variable
	 * @param value the value of the variable
	 * @return this ScopedRunner, so adding variables and finally calling invoke can be chained.
	 */
	public ScopedRunner with(String key, Object value) {
		scopedVariables.put(key, value);
		return this;
	}

	/**
	 * Invokes the callback within the scope of the variables being given in the constructor.
	 * @param callback The callback.
	 */
	public void invoke(Callback.Void callback) {
		try {
			setNewScope();
			callback.invoke();
		} finally {
			restorePreviousScope();
		}
	}

	private void setNewScope() {
		previousVariables.clear();

		Map requestMap = context.getExternalContext().getRequestMap();
		for (Map.Entry entry : scopedVariables.entrySet()) {
			Object previousVariable = requestMap.put(entry.getKey(), entry.getValue());
			if (previousVariable != null) {
				previousVariables.put(entry.getKey(), previousVariable);
			}
		}
	}

	private void restorePreviousScope() {
		try {
			Map requestMap = context.getExternalContext().getRequestMap();
			for (Map.Entry entry : scopedVariables.entrySet()) {
				Object previousVariable = previousVariables.get(entry.getKey());
				if (previousVariable != null) {
					requestMap.put(entry.getKey(), previousVariable);
				} else {
					requestMap.remove(entry.getKey());
				}
			}
		} finally {
			previousVariables.clear();
		}
	}

	/**
	 * Invokes the callback within the scope of the given variable.
	 * @param context The involved faces context.
	 * @param key the key name of the variable
	 * @param value the value of the variable
	 * @param callback The callback.
	 * @since 3.0
	 */
	public static void forEach(FacesContext context, String key, Object value, Runnable callback) {
		new ScopedRunner(context).with(key, value).invoke(callback::run);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy