-
Notifications
You must be signed in to change notification settings - Fork 565
/
ResourceBundleMessageInterpolator.java
118 lines (101 loc) · 4.43 KB
/
ResourceBundleMessageInterpolator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.messageinterpolation;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
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;
/**
* Resource bundle backed message interpolator.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
* @author Gunnar Morling
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
* @author Adam Stawicki
* @author Guillaume Smet
*/
public class ResourceBundleMessageInterpolator extends AbstractMessageInterpolator {
private static final Log LOG = LoggerFactory.make();
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 );
}
/**
* The javax.el FactoryFinder uses the TCCL to load the {@link ExpressionFactory} implementation so we need to be
* extra careful when initializing it.
*
* @return the {@link ExpressionFactory}
*/
private static ExpressionFactory buildExpressionFactory() {
// First, we try to load the instance from the TCCL.
try {
return ExpressionFactory.newInstance();
}
catch (Throwable e) {
// we ignore the error in this case as we will try the Hibernate Validator class loader.
}
// Then we try the Hibernate Validator class loader. In a fully-functional modular environment such as
// WildFly or Jigsaw, it is the way to go.
final ClassLoader originalContextClassLoader = run( GetClassLoader.fromContext() );
try {
run( SetContextClassLoader.action( ResourceBundleMessageInterpolator.class.getClassLoader() ) );
return ExpressionFactory.newInstance();
}
catch (Throwable e) {
// HV-793 - We fail eagerly in case we have no EL dependencies on the classpath
throw LOG.getUnableToInitializeELExpressionFactoryException( e );
}
finally {
run( SetContextClassLoader.action( originalContextClassLoader ) );
}
}
/**
* Runs the given privileged action, using a privileged block if required.
* <p>
* <b>NOTE:</b> This must never be changed into a publicly available method to avoid execution of arbitrary
* privileged actions within HV's protection domain.
*/
private static <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
}