package org.hibernate.validator.resourceloading;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
import org.hibernate.validator.internal.util.privilegedactions.GetMethod;
import org.hibernate.validator.internal.util.privilegedactions.GetResources;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import org.jboss.logging.Logger;
public class PlatformResourceBundleLocator implements ResourceBundleLocator {
private static final Logger log = Logger.getLogger( PlatformResourceBundleLocator.class.getName() );
private static final boolean RESOURCE_BUNDLE_CONTROL_INSTANTIABLE = determineAvailabilityOfResourceBundleControl();
private final String bundleName;
private final ClassLoader classLoader;
private final boolean aggregate;
public PlatformResourceBundleLocator(String bundleName) {
this( bundleName, null );
}
public PlatformResourceBundleLocator(String bundleName, ClassLoader classLoader) {
this( bundleName, classLoader, false );
}
public PlatformResourceBundleLocator(String bundleName, ClassLoader classLoader, boolean aggregate) {
Contracts.assertNotNull( bundleName, "bundleName" );
this.bundleName = bundleName;
this.classLoader = classLoader;
this.aggregate = aggregate && RESOURCE_BUNDLE_CONTROL_INSTANTIABLE;
}
@Override
public ResourceBundle getResourceBundle(Locale locale) {
ResourceBundle rb = null;
if ( classLoader != null ) {
rb = loadBundle(
classLoader, locale, bundleName
+ " not found by user-provided classloader"
);
}
if ( rb == null ) {
ClassLoader classLoader = run( GetClassLoader.fromContext() );
if ( classLoader != null ) {
rb = loadBundle(
classLoader, locale, bundleName
+ " not found by thread context classloader"
);
}
}
if ( rb == null ) {
ClassLoader classLoader = run( GetClassLoader.fromClass( PlatformResourceBundleLocator.class ) );
rb = loadBundle(
classLoader, locale, bundleName
+ " not found by validator classloader"
);
}
if ( rb != null ) {
log.debugf( "%s found.", bundleName );
}
else {
log.debugf( "%s not found.", bundleName );
}
return rb;
}
private ResourceBundle loadBundle(ClassLoader classLoader, Locale locale, String message) {
ResourceBundle rb = null;
try {
if ( aggregate ) {
rb = ResourceBundle.getBundle(
bundleName,
locale,
classLoader,
AggregateResourceBundle.CONTROL
);
}
else {
rb = ResourceBundle.getBundle(
bundleName,
locale,
classLoader
);
}
}
catch (MissingResourceException e) {
log.trace( message );
}
return rb;
}
private static <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
private static boolean determineAvailabilityOfResourceBundleControl() {
try {
ResourceBundle.Control dummyControl = AggregateResourceBundle.CONTROL;
if ( dummyControl == null ) {
return false;
}
Method getModule = run( GetMethod.action( Class.class, "getModule" ) );
if ( getModule == null ) {
return true;
}
Object module = getModule.invoke( PlatformResourceBundleLocator.class );
Method isNamedMethod = run( GetMethod.action( module.getClass(), "isNamed" ) );
boolean isNamed = (Boolean) isNamedMethod.invoke( module );
return !isNamed;
}
catch (Throwable e) {
log.info( MESSAGES.unableToUseResourceBundleAggregation() );
return false;
}
}
private static class AggregateResourceBundle extends ResourceBundle {
protected static final Control CONTROL = new AggregateResourceBundleControl();
private final Properties properties;
protected AggregateResourceBundle(Properties properties) {
this.properties = properties;
}
@Override
protected Object handleGetObject(String key) {
return properties.get( key );
}
@Override
public Enumeration<String> getKeys() {
Set<String> keySet = newHashSet();
keySet.addAll( properties.stringPropertyNames() );
if ( parent != null ) {
keySet.addAll( Collections.list( parent.getKeys() ) );
}
return Collections.enumeration( keySet );
}
}
private static class AggregateResourceBundleControl extends ResourceBundle.Control {
@Override
public ResourceBundle newBundle(
String baseName,
Locale locale,
String format,
ClassLoader loader,
boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
if ( !"java.properties".equals( format ) ) {
return super.newBundle( baseName, locale, format, loader, reload );
}
String resourceName = toBundleName( baseName, locale ) + ".properties";
Properties properties = load( resourceName, loader );
return properties.size() == 0 ? null : new AggregateResourceBundle( properties );
}
private Properties load(String resourceName, ClassLoader loader) throws IOException {
Properties aggregatedProperties = new Properties();
Enumeration<URL> urls = run( GetResources.action( loader, resourceName ) );
while ( urls.hasMoreElements() ) {
URL url = urls.nextElement();
Properties properties = new Properties();
properties.load( url.openStream() );
aggregatedProperties.putAll( properties );
}
return aggregatedProperties;
}
}
}