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 committed Jul 23, 2014
1 parent 67fdff1 commit 7e71319
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 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 java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand All @@ -34,12 +35,12 @@
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
import org.hibernate.validator.internal.util.privilegedactions.GetResource;
import org.hibernate.validator.internal.util.privilegedactions.NewSchema;

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

Expand Down Expand Up @@ -137,6 +138,14 @@ private synchronized XMLEventReader createXmlEventReader(InputStream xmlStream)
return xmlInputFactory.createXMLEventReader( xmlStream );
}

/**
* Returns the XML schema identified by the given resource name.
*
* @param schemaResource
* the resource name identifying the schema.
* @return the schema identified by the given resource name or {@code null} if the resource was not found or could
* not be loaded.
*/
public Schema getSchema(String schemaResource) {
Schema schema = schemaCache.get( schemaResource );

Expand All @@ -145,9 +154,14 @@ public Schema getSchema(String schemaResource) {
}

schema = loadSchema( schemaResource );
Schema previous = schemaCache.putIfAbsent( schemaResource, schema );

return previous != null ? previous : schema;
if ( schema != null ) {
Schema previous = schemaCache.putIfAbsent( schemaResource, schema );
return previous != null ? previous : schema;
}
else {
return null;
}
}

private Schema loadSchema(String schemaResource) {
Expand All @@ -157,9 +171,9 @@ private Schema loadSchema(String schemaResource) {
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( schemaResource, e.getMessage() );
}
return schema;
Expand All @@ -174,4 +188,8 @@ private Schema loadSchema(String schemaResource) {
private <T> T run(PrivilegedAction<T> action) {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

private <T> T run(PrivilegedExceptionAction<T> action) throws Exception {
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
}

0 comments on commit 7e71319

Please sign in to comment.