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

org.hibernate.validator.spi.scripting.AbstractCachingScriptEvaluatorFactory Maven / Gradle / Ivy

Go to download

JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle

There is a newer version: 5.1.0
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.validator.spi.scripting;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.hibernate.validator.Incubating;

/**
 * Basic cacheable factory responsible for the creation of {@link ScriptEvaluator}s. This
 * class is thread-safe. Caches {@code ScriptEvaluator} when they are requested.
 *
 * @author Gunnar Morling
 * @author Kevin Pollet <[email protected]> (C) 2011 SERLI
 * @author Marko Bekhta
 * @since 6.0.3
 */
@Incubating
public abstract class AbstractCachingScriptEvaluatorFactory implements ScriptEvaluatorFactory {

	/**
	 * A cache of script evaluators (keyed by language name).
	 */
	private final ConcurrentMap scriptEvaluatorCache = new ConcurrentHashMap<>();

	/**
	 * Retrieves a script executor for the given language.
	 *
	 * @param languageName the name of a scripting language
	 * @return a script executor for the given language. Never null.
	 *
	 * @throws ScriptEvaluatorNotFoundException in case no compatible evaluator for the given language has been found
	 */
	@Override
	public ScriptEvaluator getScriptEvaluatorByLanguageName(String languageName) {
		return scriptEvaluatorCache.computeIfAbsent( languageName, this::createNewScriptEvaluator );
	}

	@Override
	public void clear() {
		scriptEvaluatorCache.clear();
	}

	/**
	 * Creates a new script evaluator for the given language.
	 *
	 * @param languageName the name of a scripting language
	 * @return a newly created script evaluator for the given language
	 *
	 * @throws ScriptEvaluatorNotFoundException in case no compatible engine for the given language has been found
	 */
	protected abstract ScriptEvaluator createNewScriptEvaluator(String languageName) throws ScriptEvaluatorNotFoundException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy