Author: epbernard
Date: 2009-08-11 15:31:41 -0400 (Tue, 11 Aug 2009)
New Revision: 17265
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/GetClassLoader.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LoadClass.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/priviledgedactions/
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/ValidationXmlParser.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/XmlMappingParser.java
Log:
HV-171 finishing implementation adding due doPriviledge when needed
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ResourceBundleMessageInterpolator.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -24,11 +24,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.security.AccessController;
import javax.validation.MessageInterpolator;
import org.slf4j.Logger;
import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.GetClassLoader;
/**
* Resource bundle backed message interpolator.
@@ -155,13 +157,18 @@
*/
private ResourceBundle getFileBasedResourceBundle(Locale locale) {
ResourceBundle rb = null;
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ boolean isSecured = System.getSecurityManager() != null;
+ GetClassLoader action = GetClassLoader.fromContext();
+ ClassLoader classLoader = isSecured ? AccessController.doPrivileged( action ) :
action.run();
+
if ( classLoader != null ) {
rb = loadBundle( classLoader, locale, USER_VALIDATION_MESSAGES + " not found by
thread local classloader" );
}
if ( rb == null ) {
+ action = GetClassLoader.fromClass(ResourceBundleMessageInterpolator.class);
+ classLoader = isSecured ? AccessController.doPrivileged( action ) : action.run();
rb = loadBundle(
- this.getClass().getClassLoader(),
+ classLoader,
locale,
USER_VALIDATION_MESSAGES + " not found by validator classloader"
);
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -11,6 +11,7 @@
import org.hibernate.validation.util.LoggerFactory;
import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.NewInstance;
+import org.hibernate.validation.util.LoadClass;
/**
* A JPA 2 aware <code>TraversableResolver</code>.
@@ -47,10 +48,10 @@
*/
private void detectJPA() {
try {
- ReflectionHelper.classForName( PERSISTENCE_UTIL_CLASS_NAME, this.getClass() );
+ loadClass( PERSISTENCE_UTIL_CLASS_NAME, this.getClass() );
log.debug( "Found {} on classpath.", PERSISTENCE_UTIL_CLASS_NAME );
}
- catch ( ClassNotFoundException e ) {
+ catch ( ValidationException e ) {
log.debug(
"Cannot find {} on classpath. All properties will per default be
traversable.",
PERSISTENCE_UTIL_CLASS_NAME
@@ -61,7 +62,7 @@
try {
@SuppressWarnings( "unchecked" )
Class<? extends TraversableResolver> jpaAwareResolverClass = (Class<? extends
TraversableResolver>)
- ReflectionHelper.classForName(JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME,
this.getClass() );
+ loadClass(JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME, this.getClass() );
NewInstance<? extends TraversableResolver> newInstance = NewInstance.action(
jpaAwareResolverClass, "" );
if ( System.getSecurityManager() != null ) {
jpaTraversableResolver = AccessController.doPrivileged( newInstance );
@@ -73,20 +74,24 @@
"Instantiated an instance of {}.",
JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
);
}
- catch ( ClassNotFoundException e ) {
- log.info(
- "Unable to load JPA aware resolver {}. All properties will per default be
traversable.",
- JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
- );
- }
catch ( ValidationException e ) {
log.info(
- "Unable to instantiate JPA aware resolver {}. All properties will per default
be traversable.",
+ "Unable to load or instanciate JPA aware resolver {}. All properties will per
default be traversable.",
JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
);
}
}
+ private Class<?> loadClass(String className, Class<?> caller) {
+ LoadClass action = LoadClass.action( className, caller );
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged( action );
+ }
+ else {
+ return action.run();
+ }
+ }
+
public boolean isReachable(Object traversableObject, Path.Node traversableProperty,
Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return jpaTraversableResolver == null || jpaTraversableResolver.isReachable(
traversableObject, traversableProperty, rootBeanType, pathToTraversableObject,
elementType
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/GetClassLoader.java
(from rev 17263,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ContainsField.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/GetClassLoader.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/GetClassLoader.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -0,0 +1,32 @@
+package org.hibernate.validation.util;
+
+import java.security.PrivilegedAction;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class GetClassLoader implements PrivilegedAction<ClassLoader> {
+ private final Class<?> clazz;
+
+ public static GetClassLoader fromContext() {
+ return new GetClassLoader( null );
+ }
+
+ public static GetClassLoader fromClass(Class<?> clazz) {
+ if ( clazz == null ) throw new IllegalArgumentException("Class is null");
+ return new GetClassLoader( clazz );
+ }
+
+ private GetClassLoader(Class<?> clazz) {
+ this.clazz = clazz;
+ }
+
+ public ClassLoader run() {
+ if (clazz != null) {
+ return clazz.getClassLoader();
+ }
+ else {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ }
+}
\ No newline at end of file
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LoadClass.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LoadClass.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/LoadClass.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -0,0 +1,39 @@
+package org.hibernate.validation.util;
+
+import java.security.PrivilegedAction;
+import javax.validation.ValidationException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class LoadClass implements PrivilegedAction<Class<?>> {
+ private final String className;
+ private final Class<?> caller;
+
+ public static LoadClass action(String className, Class<?> caller) {
+ return new LoadClass( className, caller );
+ }
+
+ private LoadClass(String className, Class<?> caller) {
+ this.className = className;
+ this.caller = caller;
+ }
+
+ public Class<?> run() {
+ try {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( contextClassLoader != null ) {
+ return contextClassLoader.loadClass( className );
+ }
+ }
+ catch ( Throwable e ) {
+ // ignore
+ }
+ try {
+ return Class.forName( className, true, caller.getClassLoader() );
+ }
+ catch ( ClassNotFoundException e ) {
+ throw new ValidationException("Unable to load class: " + className, e);
+ }
+ }
+}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -198,23 +198,6 @@
}
}
- public static Class<?> loadClass(String name, Class<?> caller) throws
ClassNotFoundException {
- try {
- //try context classloader, if fails try caller classloader
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if ( loader != null ) {
- return loader.loadClass( name );
- }
- }
- catch ( ClassNotFoundException e ) {
- //trying caller classloader
- if ( caller == null ) {
- throw e;
- }
- }
- return Class.forName( name, true, caller.getClassLoader() );
- }
-
/**
* Determines the type of elements of an <code>Iterable</code>, array or the
value of a <code>Map</code>.
*
@@ -382,19 +365,6 @@
}
}
- public static Class<?> classForName(String name, Class<?> caller) throws
ClassNotFoundException {
- try {
- ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
- if ( contextClassLoader != null ) {
- return contextClassLoader.loadClass( name );
- }
- }
- catch ( Throwable e ) {
- // ignore
- }
- return Class.forName( name, true, caller.getClassLoader() );
- }
-
/**
* Returns the autoboxed type of a primitive type.
*
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -25,6 +25,7 @@
import java.security.AccessController;
import org.hibernate.validation.util.GetConstructor;
+import org.hibernate.validation.util.GetClassLoader;
/**
@@ -38,7 +39,9 @@
@SuppressWarnings("unchecked")
public static <T extends Annotation> T create(AnnotationDescriptor<T>
descriptor) {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ boolean isSecured = System.getSecurityManager() != null;
+ GetClassLoader action = GetClassLoader.fromContext();
+ ClassLoader classLoader = isSecured ? AccessController.doPrivileged( action ) :
action.run();
//TODO round 34ms to generate the proxy, hug! is Javassist Faster?
Class<T> proxyClass = (Class<T>) Proxy.getProxyClass( classLoader,
descriptor.type() );
InvocationHandler handler = new AnnotationProxy( descriptor );
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/ValidationXmlParser.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/ValidationXmlParser.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/ValidationXmlParser.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -39,6 +39,8 @@
import org.hibernate.validation.util.LoggerFactory;
import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.NewInstance;
+import org.hibernate.validation.util.GetClassLoader;
+import org.hibernate.validation.util.LoadClass;
import org.hibernate.validation.xml.PropertyType;
import org.hibernate.validation.xml.ValidationConfigType;
@@ -79,7 +81,7 @@
if ( constraintFactoryClass != null ) {
try {
@SuppressWarnings("unchecked")
- Class<ConstraintValidatorFactory> clazz = (
Class<ConstraintValidatorFactory> ) ReflectionHelper.classForName(
+ Class<ConstraintValidatorFactory> clazz = (
Class<ConstraintValidatorFactory> ) loadClass(
constraintFactoryClass, this.getClass()
);
NewInstance<ConstraintValidatorFactory> newInstance = NewInstance.action(
clazz, "constraint factory class" );
@@ -91,7 +93,7 @@
}
log.info( "Using {} as constraint factory.", constraintFactoryClass );
}
- catch ( ClassNotFoundException e ) {
+ catch ( ValidationException e ) {
throw new ValidationException(
"Unable to instantiate constraint factory class " +
constraintFactoryClass + ".", e
);
@@ -99,6 +101,16 @@
}
}
+ private Class<?> loadClass(String className, Class<?> caller) {
+ LoadClass action = LoadClass.action( className, caller );
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged( action );
+ }
+ else {
+ return action.run();
+ }
+ }
+
private void setPropertiesFromXml(ValidationConfigType config,
ValidationBootstrapParameters xmlParameters) {
for ( PropertyType property : config.getProperty() ) {
if ( log.isDebugEnabled() ) {
@@ -132,13 +144,13 @@
if ( messageInterpolatorClass != null ) {
try {
@SuppressWarnings("unchecked")
- Class<MessageInterpolator> clazz = ( Class<MessageInterpolator> )
ReflectionHelper.classForName(
+ Class<MessageInterpolator> clazz = ( Class<MessageInterpolator> )
loadClass(
messageInterpolatorClass, this.getClass()
);
xmlParameters.messageInterpolator = clazz.newInstance();
log.info( "Using {} as message interpolator.", messageInterpolatorClass );
}
- catch ( ClassNotFoundException e ) {
+ catch ( ValidationException e ) {
throw new ValidationException(
"Unable to instantiate message interpolator class " +
messageInterpolatorClass + ".", e
);
@@ -161,13 +173,13 @@
if ( traversableResolverClass != null ) {
try {
@SuppressWarnings("unchecked")
- Class<TraversableResolver> clazz = ( Class<TraversableResolver> )
ReflectionHelper.classForName(
+ Class<TraversableResolver> clazz = ( Class<TraversableResolver> )
loadClass(
traversableResolverClass, this.getClass()
);
xmlParameters.traversableResolver = clazz.newInstance();
log.info( "Using {} as traversable resolver.", traversableResolverClass );
}
- catch ( ClassNotFoundException e ) {
+ catch ( ValidationException e ) {
throw new ValidationException(
"Unable to instantiate traversable resolver class " +
traversableResolverClass + ".", e
);
@@ -190,7 +202,7 @@
String providerClassName = config.getDefaultProvider();
if ( providerClassName != null ) {
try {
- xmlParamters.providerClass = ( Class<? extends ValidationProvider<?>> )
ReflectionHelper.classForName(
+ xmlParamters.providerClass = ( Class<? extends ValidationProvider<?>> )
loadClass(
providerClassName, this.getClass()
);
log.info( "Using {} as validation provider.", providerClassName );
@@ -233,25 +245,36 @@
if ( path.startsWith( "/" ) ) {
path = path.substring( 1 );
}
+
+
+ boolean isSecured = System.getSecurityManager() != null;
+ boolean isContextCL = true;
// try the context class loader first
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- boolean isContextCL = true;
+ GetClassLoader action = GetClassLoader.fromContext();
+ ClassLoader loader = isSecured ? AccessController.doPrivileged( action ) :
action.run();
+
if (loader == null) {
log.debug( "No default context class loader, fallbacking to Bean Validation's
loader" );
- loader = ValidationXmlParser.class.getClassLoader();
+ action = GetClassLoader.fromClass(ValidationXmlParser.class);
+ loader = isSecured ? AccessController.doPrivileged( action ) : action.run();
isContextCL = false;
}
InputStream inputStream = loader.getResourceAsStream( path );
// try the current class loader
if ( isContextCL && inputStream == null ) {
- inputStream = ValidationXmlParser.class.getClassLoader().getResourceAsStream( path );
+ action = GetClassLoader.fromClass(ValidationXmlParser.class);
+ loader = isSecured ? AccessController.doPrivileged( action ) : action.run();
+ inputStream = loader.getResourceAsStream( path );
}
return inputStream;
}
private Schema getValidationConfigurationSchema() {
- URL schemaUrl = this.getClass().getClassLoader().getResource(
VALIDATION_CONFIGURATION_XSD );
+ boolean isSecured = System.getSecurityManager() != null;
+ GetClassLoader action = GetClassLoader.fromClass( ValidationXmlParser.class );
+ ClassLoader loader = isSecured ? AccessController.doPrivileged( action ) :
action.run();
+ URL schemaUrl = loader.getResource( VALIDATION_CONFIGURATION_XSD );
SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = null;
try {
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/XmlMappingParser.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/XmlMappingParser.java 2009-08-11
19:16:08 UTC (rev 17264)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/xml/XmlMappingParser.java 2009-08-11
19:31:41 UTC (rev 17265)
@@ -59,6 +59,8 @@
import org.hibernate.validation.util.ContainsMethod;
import org.hibernate.validation.util.GetMethod;
import org.hibernate.validation.util.GetDeclaredField;
+import org.hibernate.validation.util.GetClassLoader;
+import org.hibernate.validation.util.LoadClass;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
import org.hibernate.validation.xml.AnnotationType;
@@ -173,14 +175,9 @@
for ( ConstraintDefinitionType constraintDefinition : constraintDefinitionList ) {
String annotationClassName = constraintDefinition.getAnnotation();
Class<? extends Annotation> annotationClass;
- try {
- annotationClass = ( Class<? extends Annotation> )
ReflectionHelper.classForName(
- annotationClassName, this.getClass()
- );
- }
- catch ( ClassNotFoundException e ) {
- throw new ValidationException( "Unable to load class " +
annotationClassName );
- }
+ annotationClass = ( Class<? extends Annotation> ) loadClass(
+ annotationClassName, this.getClass()
+ );
if ( !annotationClass.isAnnotation() ) {
throw new ValidationException( annotationClassName + " is not an
annotation" );
@@ -193,16 +190,12 @@
}
for ( JAXBElement<String> validatorClassName : validatedByType.getValue() ) {
Class<? extends ConstraintValidator<?, ?>> validatorClass;
- try {
- validatorClass = ( Class<? extends ConstraintValidator<?, ?>> )
ReflectionHelper.classForName(
- validatorClassName.getValue(),
- this.getClass()
- );
- }
- catch ( ClassNotFoundException e ) {
- throw new ValidationException( "Unable to load class " +
validatorClassName );
- }
+ validatorClass = ( Class<? extends ConstraintValidator<?, ?>> )
loadClass(
+ validatorClassName.getValue(),
+ this.getClass()
+ );
+
if ( !ConstraintValidator.class.isAssignableFrom( validatorClass ) ) {
throw new ValidationException( validatorClass + " is not a constraint validator
class" );
}
@@ -215,6 +208,16 @@
}
}
+ private Class<?> loadClass(String className, Class<?> caller) {
+ LoadClass action = LoadClass.action( className, caller );
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged( action );
+ }
+ else {
+ return action.run();
+ }
+ }
+
private List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> findConstraintValidatorClasses(Class<? extends Annotation>
annotationType) {
List<Class<? extends ConstraintValidator<? extends Annotation, ?>>>
constraintValidatorDefinitonClasses = new ArrayList<Class<? extends
ConstraintValidator<? extends Annotation, ?>>>();
if ( constraintHelper.isBuiltinConstraint( annotationType ) ) {
@@ -569,12 +572,7 @@
returnValue = value;
}
else if ( returnType.getName().equals( Class.class.getName() ) ) {
- try {
- returnValue = ReflectionHelper.classForName( value, this.getClass() );
- }
- catch ( ClassNotFoundException e ) {
- throw new ValidationException( "Unable to instantiate class: " + value );
- }
+ returnValue = loadClass( value, this.getClass() );
}
else {
try {
@@ -615,12 +613,7 @@
else {
fullyQualifiedClass = defaultPackage + PACKAGE_SEPERATOR + clazz;
}
- try {
- return ReflectionHelper.classForName( fullyQualifiedClass, this.getClass() );
- }
- catch ( Exception e ) {
- throw new ValidationException( "Unable to instantiate class " +
fullyQualifiedClass );
- }
+ return loadClass( fullyQualifiedClass, this.getClass() );
}
private boolean isQualifiedClass(String clazz) {
@@ -647,7 +640,10 @@
}
private Schema getMappingSchema() {
- URL schemaUrl = this.getClass().getClassLoader().getResource( VALIDATION_MAPPING_XSD
);
+ boolean isSecured = System.getSecurityManager() != null;
+ GetClassLoader action = GetClassLoader.fromClass( XmlMappingParser.class );
+ ClassLoader loader = isSecured ? AccessController.doPrivileged( action ) :
action.run();
+ URL schemaUrl = loader.getResource( VALIDATION_MAPPING_XSD );
SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = null;
try {