package org.hibernate.validator.messageinterpolation;
import java.lang.invoke.MethodHandles;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import javax.el.ELManager;
import javax.el.ExpressionFactory;
import org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
import org.hibernate.validator.internal.util.privilegedactions.SetContextClassLoader;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
public class ResourceBundleMessageInterpolator extends AbstractMessageInterpolator {
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
private final ExpressionFactory expressionFactory;
public ResourceBundleMessageInterpolator() {
super();
this.expressionFactory = buildExpressionFactory();
}
public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator) {
super( userResourceBundleLocator );
this.expressionFactory = buildExpressionFactory();
}
public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator,
ResourceBundleLocator contributorResourceBundleLocator) {
super( userResourceBundleLocator, contributorResourceBundleLocator );
this.expressionFactory = buildExpressionFactory();
}
public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator,
ResourceBundleLocator contributorResourceBundleLocator,
boolean cachingEnabled) {
super( userResourceBundleLocator, contributorResourceBundleLocator, cachingEnabled );
this.expressionFactory = buildExpressionFactory();
}
public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator, boolean cachingEnabled) {
super( userResourceBundleLocator, null, cachingEnabled );
this.expressionFactory = buildExpressionFactory();
}
public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator, boolean cachingEnabled, ExpressionFactory expressionFactory) {
super( userResourceBundleLocator, null, cachingEnabled );
this.expressionFactory = expressionFactory;
}
@Override
public String interpolate(Context context, Locale locale, String term) {
InterpolationTerm expression = new InterpolationTerm( term, locale, expressionFactory );
return expression.interpolate( context );
}
private static ExpressionFactory buildExpressionFactory() {
if ( canLoadExpressionFactory() ) {
ExpressionFactory expressionFactory = ELManager.getExpressionFactory();
LOG.debug( "Loaded expression factory via original TCCL" );
return expressionFactory;
}
final ClassLoader originalContextClassLoader = run( GetClassLoader.fromContext() );
try {
run( SetContextClassLoader.action( ResourceBundleMessageInterpolator.class.getClassLoader() ) );
if ( canLoadExpressionFactory() ) {
ExpressionFactory expressionFactory = ELManager.getExpressionFactory();
LOG.debug( "Loaded expression factory via HV classloader" );
return expressionFactory;
}
run( SetContextClassLoader.action( ELManager.class.getClassLoader() ) );
if ( canLoadExpressionFactory() ) {
ExpressionFactory expressionFactory = ELManager.getExpressionFactory();
LOG.debug( "Loaded expression factory via EL classloader" );
return expressionFactory;
}
}
catch (Throwable e) {
throw LOG.getUnableToInitializeELExpressionFactoryException( e );
}
finally {
run( SetContextClassLoader.action( originalContextClassLoader ) );
}
throw LOG.getUnableToInitializeELExpressionFactoryException( null );
}
private static boolean canLoadExpressionFactory() {
try {
ExpressionFactory.newInstance();
return true;
}
catch (Throwable e) {
return false;
}
}
private static <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
}