HHH-14096 Adapt the Hibernate Envers module to use its private copy of XMLHelper

(cherry picked from commit ef5d277df9)
This commit is contained in:
Sanne Grinovero 2020-07-03 13:01:18 +01:00 committed by gbadner
parent eeb8225b6f
commit 3a716477e8
4 changed files with 90 additions and 14 deletions

View File

@ -17,7 +17,6 @@ import org.hibernate.envers.internal.revisioninfo.RevisionInfoNumberReader;
import org.hibernate.envers.internal.revisioninfo.RevisionInfoQueryCreator;
import org.hibernate.envers.internal.synchronization.AuditProcessManager;
import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.internal.util.xml.XMLHelper;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
@ -56,8 +55,6 @@ public interface EnversService extends Service {
void initialize(MetadataImplementor metadata, MappingCollector mappingCollector);
XMLHelper getXmlHelper();
GlobalConfiguration getGlobalConfiguration();
AuditEntitiesConfiguration getAuditEntitiesConfiguration();

View File

@ -31,7 +31,6 @@ import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.envers.strategy.ValidityAuditStrategy;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.internal.util.xml.XMLHelper;
import org.hibernate.property.access.spi.Getter;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.Configurable;
@ -76,8 +75,6 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
private RevisionInfoNumberReader revisionInfoNumberReader;
private ModifiedEntityNamesReader modifiedEntityNamesReader;
private XMLHelper xmlHelper;
@Override
public void configure(Map configurationValues) {
if ( configurationValues.containsKey( LEGACY_AUTO_REGISTER ) ) {
@ -113,7 +110,6 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
this.serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
this.classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
this.xmlHelper = new XMLHelper();
doInitialize( metadata, mappingCollector, serviceRegistry );
}
@ -195,11 +191,6 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
return strategy;
}
@Override
public XMLHelper getXmlHelper() {
return xmlHelper;
}
/**
* Load a class by name, preferring our ClassLoader and then the ClassLoaderService.
*

View File

@ -55,6 +55,8 @@ public class RevisionInfoConfiguration {
private Type revisionInfoTimestampType;
private GlobalConfiguration globalCfg;
private XMLHelper xmlHelper;
private String revisionPropType;
private String revisionPropSqlType;
@ -75,7 +77,7 @@ public class RevisionInfoConfiguration {
}
private Document generateDefaultRevisionInfoXmlMapping() {
final Document document = globalCfg.getEnversService().getXmlHelper().getDocumentFactory().createDocument();
final Document document = getXmlHelper().getDocumentFactory().createDocument();
final Element classMapping = MetadataTools.createEntity(
document,
@ -120,6 +122,13 @@ public class RevisionInfoConfiguration {
return document;
}
private XMLHelper getXmlHelper() {
if ( this.xmlHelper == null ) {
this.xmlHelper = new XMLHelper();
}
return this.xmlHelper;
}
/**
* Generates mapping that represents a set of primitive types.<br />
* <code>
@ -158,7 +167,7 @@ public class RevisionInfoConfiguration {
}
private Element generateRevisionInfoRelationMapping() {
final Document document = globalCfg.getEnversService().getXmlHelper().getDocumentFactory().createDocument();
final Document document = getXmlHelper().getDocumentFactory().createDocument();
final Element revRelMapping = document.addElement( "key-many-to-one" );
revRelMapping.addAttribute( "type", revisionPropType );
revRelMapping.addAttribute( "class", revisionInfoEntityName );

View File

@ -0,0 +1,79 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.configuration.internal;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;
import org.hibernate.internal.util.xml.ErrorLogger;
import org.xml.sax.EntityResolver;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Small helper class that lazily loads DOM and SAX reader and keep them for fast use afterwards.
*
* This was part of Hibernate ORM core, but moved into the testsuite helpers to not expose
* access to the dom4j types. It's also used by Hibernate Envers, so we will need two copies
* until Envers is able to remove its reliance on dom4j.
* The rest of Hibernate uses StAX now for XML processing. See {@link org.hibernate.boot.jaxb.internal.stax}
*/
public final class XMLHelper {
private final DocumentFactory documentFactory;
public XMLHelper() {
PrivilegedAction<DocumentFactory> action = new PrivilegedAction<DocumentFactory>() {
public DocumentFactory run() {
final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
try {
// We need to make sure we get DocumentFactory
// loaded from the same ClassLoader that loads
// Hibernate classes, to make sure we get the
// proper version of DocumentFactory, This class
// is "internal", and should only be used for XML
// files generated by Envers.
// Using the (Hibernate) ClassLoader that loads
// this Class will avoid collisions in the case
// that DocumentFactory can be loaded from,
// for example, the application ClassLoader.
Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
return DocumentFactory.getInstance();
}
finally {
Thread.currentThread().setContextClassLoader( originalTccl );
}
}
};
this.documentFactory = System.getSecurityManager() != null
? AccessController.doPrivileged( action )
: action.run();
}
public DocumentFactory getDocumentFactory() {
return documentFactory;
}
public SAXReader createSAXReader(ErrorLogger errorLogger, EntityResolver entityResolver) {
SAXReader saxReader = new SAXReader();
try {
saxReader.setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false );
saxReader.setFeature( "http://xml.org/sax/features/external-general-entities", false );
saxReader.setFeature( "http://xml.org/sax/features/external-parameter-entities", false );
}
catch (Exception e) {
throw new RuntimeException( e );
}
saxReader.setMergeAdjacentText( true );
saxReader.setValidation( true );
saxReader.setErrorHandler( errorLogger );
saxReader.setEntityResolver( entityResolver );
return saxReader;
}
}