Skip to content

Commit

Permalink
HV-1828 Aggregate the properties of resource bundles on load
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Mar 8, 2021
1 parent 513aaba commit 0650656
Showing 1 changed file with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
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.List;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.stream.Collectors;

import org.hibernate.validator.Incubating;
import org.hibernate.validator.internal.util.CollectionHelper;
Expand Down Expand Up @@ -305,38 +305,32 @@ private static boolean determineAvailabilityOfResourceBundleControl() {
private static class AggregateResourceBundle extends ResourceBundle {

protected static final Control CONTROL = new AggregateResourceBundleControl();
private final Set<PropertyResourceBundle> properties;

protected AggregateResourceBundle(Set<PropertyResourceBundle> properties) {
this.properties = properties;
@Immutable
private final Map<String, Object> properties;

protected AggregateResourceBundle(Map<String, Object> properties) {
this.properties = CollectionHelper.toImmutableMap( properties );
}

/**
* Search object key in each properties until founded. If duplicated key, first value is returned
*/
@Override
protected Object handleGetObject(String key) {
for ( PropertyResourceBundle property : properties ) {
Object object = property.handleGetObject( key );
if ( object != null ) {
return object;
}
}
return null;
return properties.get( key );
}

@Override
protected Set<String> handleKeySet() {
return properties.keySet();
}

@Override
public Enumeration<String> getKeys() {
Set<String> keySet = newHashSet();
List<String> keys = properties.stream()
.map( PropertyResourceBundle::getKeys )
.map( Collections::list )
.flatMap( List::stream )
.collect( Collectors.toList() );
keySet.addAll( keys );
Set<String> keySet = newHashSet( properties.keySet() );

if ( parent != null ) {
keySet.addAll( Collections.list( parent.getKeys() ) );
}

return Collections.enumeration( keySet );
}
}
Expand All @@ -356,18 +350,26 @@ public ResourceBundle newBundle(
}

String resourceName = toBundleName( baseName, locale ) + ".properties";
Set<PropertyResourceBundle> properties = load( resourceName, loader );
Map<String, Object> properties = load( resourceName, loader );
return properties.isEmpty() ? null : new AggregateResourceBundle( properties );
}

private static Set<PropertyResourceBundle> load(String resourceName, ClassLoader loader) throws IOException {
Set<PropertyResourceBundle> aggregatedProperties = newHashSet();
private static Map<String, Object> load(String resourceName, ClassLoader loader) throws IOException {
Map<String, Object> aggregatedProperties = new HashMap<>();

Enumeration<URL> urls = run( GetResources.action( loader, resourceName ) );
while ( urls.hasMoreElements() ) {
URL url = urls.nextElement();
PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle( url.openStream() );
aggregatedProperties.add( propertyResourceBundle );
try ( InputStream propertyStream = url.openStream() ) {
PropertyResourceBundle propertyResourceBundle = new PropertyResourceBundle( propertyStream );
Enumeration<String> keys = propertyResourceBundle.getKeys();
while ( keys.hasMoreElements() ) {
String key = keys.nextElement();
aggregatedProperties.put( key, propertyResourceBundle.getObject( key ) );
}
}
}

return aggregatedProperties;
}
}
Expand Down

0 comments on commit 0650656

Please sign in to comment.