diff --git a/engine/src/main/java/org/hibernate/validator/internal/util/privilegedactions/NewSchema.java b/engine/src/main/java/org/hibernate/validator/internal/util/privilegedactions/NewSchema.java new file mode 100644 index 0000000000..1edd27f76f --- /dev/null +++ b/engine/src/main/java/org/hibernate/validator/internal/util/privilegedactions/NewSchema.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.hibernate.validator.internal.util.privilegedactions; + +import java.net.URL; +import java.security.PrivilegedExceptionAction; + +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.xml.sax.SAXException; + +/** + * Loads a given XML schema. + * + * @author Gunnar Morling + */ +public final class NewSchema implements PrivilegedExceptionAction { + + private final SchemaFactory schemaFactory; + private final URL url; + + public static NewSchema action(SchemaFactory schemaFactory, URL url) { + return new NewSchema( schemaFactory, url ); + } + + public NewSchema(SchemaFactory schemaFactory, URL url) { + this.schemaFactory = schemaFactory; + this.url = url; + } + + @Override + public Schema run() throws SAXException { + return schemaFactory.newSchema( url ); + } +} diff --git a/engine/src/main/java/org/hibernate/validator/internal/xml/ValidationXmlParser.java b/engine/src/main/java/org/hibernate/validator/internal/xml/ValidationXmlParser.java index af444758b2..83bc4a6250 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/xml/ValidationXmlParser.java +++ b/engine/src/main/java/org/hibernate/validator/internal/xml/ValidationXmlParser.java @@ -21,6 +21,7 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import javax.validation.ConstraintValidatorFactory; import javax.validation.MessageInterpolator; import javax.validation.TraversableResolver; @@ -42,6 +43,7 @@ import org.hibernate.validator.internal.util.privilegedactions.GetResource; import org.hibernate.validator.internal.util.privilegedactions.LoadClass; import org.hibernate.validator.internal.util.privilegedactions.NewInstance; +import org.hibernate.validator.internal.util.privilegedactions.NewSchema; /** * Parser for validation.xml using JAXB. @@ -251,9 +253,9 @@ private Schema getValidationConfigurationSchema() { SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); Schema schema = null; try { - schema = sf.newSchema( schemaUrl ); + schema = run( NewSchema.action( sf, schemaUrl ) ); } - catch ( SAXException e ) { + catch ( Exception e ) { log.unableToCreateSchema( VALIDATION_CONFIGURATION_XSD, e.getMessage() ); } return schema; @@ -268,4 +270,8 @@ private Schema getValidationConfigurationSchema() { private static T run(PrivilegedAction action) { return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run(); } + + private static T run(PrivilegedExceptionAction action) throws Exception { + return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run(); + } } diff --git a/engine/src/main/java/org/hibernate/validator/internal/xml/XmlMappingParser.java b/engine/src/main/java/org/hibernate/validator/internal/xml/XmlMappingParser.java index e0c8d9d74d..32844a9a5b 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/xml/XmlMappingParser.java +++ b/engine/src/main/java/org/hibernate/validator/internal/xml/XmlMappingParser.java @@ -28,6 +28,7 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedExceptionAction; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -62,6 +63,7 @@ import org.hibernate.validator.internal.util.privilegedactions.GetMethodFromPropertyName; import org.hibernate.validator.internal.util.privilegedactions.GetResource; import org.hibernate.validator.internal.util.privilegedactions.LoadClass; +import org.hibernate.validator.internal.util.privilegedactions.NewSchema; import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList; import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap; @@ -193,6 +195,7 @@ private void parseConstraintDefinitions(List constrain } } + @SuppressWarnings( "unchecked" ) private List>> findConstraintValidatorClasses(Class annotationType) { List>> constraintValidatorDefinitionClasses = newArrayList(); if ( constraintHelper.isBuiltinConstraint( annotationType ) ) { @@ -607,9 +610,9 @@ private Schema getMappingSchema() { SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); Schema schema = null; try { - schema = sf.newSchema( schemaUrl ); + schema = run( NewSchema.action( sf, schemaUrl ) ); } - catch ( SAXException e ) { + catch ( Exception e ) { log.unableToCreateSchema( VALIDATION_MAPPING_XSD, e.getMessage() ); } return schema; @@ -653,6 +656,10 @@ private static T run(PrivilegedAction action) { return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run(); } + private static T run(PrivilegedExceptionAction action) throws Exception { + return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run(); + } + // JAXB closes the underlying input stream public class CloseIgnoringInputStream extends FilterInputStream { public CloseIgnoringInputStream(InputStream in) {