Skip to content

Commit

Permalink
HV-1283 Not using ResourceBundle.Control when running as a Java 9 nam…
Browse files Browse the repository at this point in the history
…ed module
  • Loading branch information
gunnarmorling authored and gsmet committed Mar 15, 2017
1 parent 968570b commit 9ea0ea3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Expand Up @@ -86,9 +86,9 @@ public interface Messages {
String parameterizedTypesWithMoreThanOneTypeArgument();

@Message(value = "Hibernate Validator cannot instantiate AggregateResourceBundle.CONTROL. " +
"This can happen most notably in a Google App Engine environment. " +
"This can happen most notably in a Google App Engine environment or when running Hibernate Validator as Java 9 named module. " +
"A PlatformResourceBundleLocator without bundle aggregation was created. " +
"This only effects you in case you are using multiple ConstraintDefinitionContributor jars. " +
"This only affects you in case you are using multiple ConstraintDefinitionContributor JARs. " +
"ConstraintDefinitionContributors are a Hibernate Validator specific feature. All Bean Validation " +
"features work as expected. See also https://hibernate.atlassian.net/browse/HV-1023.")
String unableToUseResourceBundleAggregation();
Expand All @@ -101,5 +101,3 @@ public interface Messages {
format = Message.Format.NO_FORMAT)
String annotationTypeMustBeAnnotatedWithConstraint();
}


Expand Up @@ -6,7 +6,11 @@
*/
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;
Expand All @@ -20,13 +24,11 @@

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;

import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;

/**
* A resource bundle locator, that loads resource bundles by invoking {@code ResourceBundle.loadBundle(String, Local, ClassLoader)}.
* <p>
Expand Down Expand Up @@ -164,21 +166,42 @@ private static <T> T run(PrivilegedAction<T> action) {
}

/**
* Check whether ResourceBundle.Control is available, which is needed for bundle aggregation. If not, we'll skip
* resource aggregation.
* <p>
* It is *not* available
* <ul>
* <li>in the Google App Engine environment</li>
* <li>when running HV as Java 9 named module (which would be the case when adding a module-info descriptor to the
* HV JAR)</li>
* </ul>
*
* In an Google App Engine environment bundle aggregation is not possible, since ResourceBundle.Control
* is not on the list of white listed classes in this environment.
* to create AggregateResourceBundle.CONTROL proactively, if it fails skip resource aggregation.
*
* @see <a href="http://code.google.com/appengine/docs/java/jrewhitelist.html">JRE whitelist</a>
* @see <a href="http://code.google.com/appengine/docs/java/jrewhitelist.html">GAE JRE whitelist</a>
* @see <a href="https://hibernate.atlassian.net/browse/HV-1023">HV-1023</a>
* @see <a href="http://download.java.net/java/jdk9/docs/api/java/util/ResourceBundle.Control.html>ResourceBundle.Control</a>
*/
private static boolean determineAvailabilityOfResourceBundleControl() {
try {
@SuppressWarnings("unused")
ResourceBundle.Control dummyControl = AggregateResourceBundle.CONTROL;
return true;

if ( dummyControl == null ) {
return false;
}

Method getModule = run( GetMethod.action( Class.class, "getModule" ) );
// not on Java 9
if ( getModule == null ) {
return true;
}

// on Java 9, check whether HV is a named module
Object module = getModule.invoke( PlatformResourceBundleLocator.class );
Method isNamedMethod = run( GetMethod.action( module.getClass(), "isNamed" ) );
boolean isNamed = (Boolean) isNamedMethod.invoke( module );

return !isNamed;
}
catch (NoClassDefFoundError e) {
catch (Throwable e) {
log.info( MESSAGES.unableToUseResourceBundleAggregation() );
return false;
}
Expand Down

0 comments on commit 9ea0ea3

Please sign in to comment.