Skip to content

Commit

Permalink
HV-912 Wrapping call to SchemaFactory#newSchema() into privileged action
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling authored and hferentschik committed Jul 24, 2014
1 parent e59d080 commit 43936f8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<Schema> {

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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <i>validation.xml</i> using JAXB.
Expand Down Expand Up @@ -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;
Expand All @@ -268,4 +270,8 @@ private Schema getValidationConfigurationSchema() {
private static <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

private static <T> T run(PrivilegedExceptionAction<T> action) throws Exception {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -193,6 +195,7 @@ private void parseConstraintDefinitions(List<ConstraintDefinitionType> constrain
}
}

@SuppressWarnings( "unchecked" )
private List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> findConstraintValidatorClasses(Class<? extends Annotation> annotationType) {
List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> constraintValidatorDefinitionClasses = newArrayList();
if ( constraintHelper.isBuiltinConstraint( annotationType ) ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -653,6 +656,10 @@ private static <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

private static <T> T run(PrivilegedExceptionAction<T> 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) {
Expand Down

0 comments on commit 43936f8

Please sign in to comment.