HHH-13285 : ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory after dom4j update

This commit is contained in:
Gail Badner 2019-02-24 14:25:32 -08:00 committed by gbadner
parent a938d5838c
commit 6fba4c1035
4 changed files with 32 additions and 21 deletions

View File

@ -6,7 +6,8 @@
*/
package org.hibernate.internal.util.xml;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;
@ -22,23 +23,34 @@ import org.xml.sax.EntityResolver;
public final class XMLHelper {
private final DocumentFactory documentFactory;
public XMLHelper(ClassLoaderService classLoaderService) {
this.documentFactory = classLoaderService.workWithClassLoader(
new ClassLoaderService.Work<DocumentFactory>() {
@Override
public DocumentFactory doWork(ClassLoader classLoader) {
public XMLHelper() {
PrivilegedAction<DocumentFactory> action = new PrivilegedAction<DocumentFactory>() {
public DocumentFactory run() {
final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader( classLoader );
// 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() {

View File

@ -393,7 +393,7 @@ public class JPAOverriddenAnnotationReaderTest extends BaseUnitTestCase {
}
private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException {
XMLHelper xmlHelper = new XMLHelper( ClassLoaderServiceTestingImpl.INSTANCE );
XMLHelper xmlHelper = new XMLHelper();
InputStream is = ClassLoaderServiceTestingImpl.INSTANCE.locateResourceStream( ormfile );
assertNotNull( "ORM.xml not found: " + ormfile, is );
XMLContext context = new XMLContext( BootstrapContextImpl.INSTANCE );

View File

@ -22,7 +22,6 @@ import org.hibernate.internal.util.xml.ErrorLogger;
import org.hibernate.internal.util.xml.XMLHelper;
import org.hibernate.testing.boot.BootstrapContextImpl;
import org.hibernate.testing.boot.ClassLoaderAccessTestingImpl;
import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
/**
@ -31,7 +30,7 @@ import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
public class XMLContextTest {
@Test
public void testAll() throws Exception {
final XMLHelper xmlHelper = new XMLHelper( ClassLoaderServiceTestingImpl.INSTANCE );
final XMLHelper xmlHelper = new XMLHelper();
final XMLContext context = new XMLContext( BootstrapContextImpl.INSTANCE );
InputStream is = ClassLoaderServiceTestingImpl.INSTANCE.locateResourceStream(

View File

@ -111,7 +111,7 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
this.serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
this.classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
this.xmlHelper = new XMLHelper( classLoaderService );
this.xmlHelper = new XMLHelper();
doInitialize( metadata, mappingCollector, serviceRegistry );
}