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; 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.DocumentFactory;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
@ -22,23 +23,34 @@ import org.xml.sax.EntityResolver;
public final class XMLHelper { public final class XMLHelper {
private final DocumentFactory documentFactory; private final DocumentFactory documentFactory;
public XMLHelper(ClassLoaderService classLoaderService) { public XMLHelper() {
this.documentFactory = classLoaderService.workWithClassLoader( PrivilegedAction<DocumentFactory> action = new PrivilegedAction<DocumentFactory>() {
new ClassLoaderService.Work<DocumentFactory>() { public DocumentFactory run() {
@Override final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
public DocumentFactory doWork(ClassLoader classLoader) { try {
final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader(); // We need to make sure we get DocumentFactory
try { // loaded from the same ClassLoader that loads
Thread.currentThread().setContextClassLoader( classLoader ); // Hibernate classes, to make sure we get the
return DocumentFactory.getInstance(); // proper version of DocumentFactory, This class
} // is "internal", and should only be used for XML
finally { // files generated by Envers.
Thread.currentThread().setContextClassLoader( originalTccl );
}
}
}
);
// 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() { public DocumentFactory getDocumentFactory() {

View File

@ -393,7 +393,7 @@ public class JPAOverriddenAnnotationReaderTest extends BaseUnitTestCase {
} }
private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException { 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 ); InputStream is = ClassLoaderServiceTestingImpl.INSTANCE.locateResourceStream( ormfile );
assertNotNull( "ORM.xml not found: " + ormfile, is ); assertNotNull( "ORM.xml not found: " + ormfile, is );
XMLContext context = new XMLContext( BootstrapContextImpl.INSTANCE ); 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.internal.util.xml.XMLHelper;
import org.hibernate.testing.boot.BootstrapContextImpl; import org.hibernate.testing.boot.BootstrapContextImpl;
import org.hibernate.testing.boot.ClassLoaderAccessTestingImpl;
import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl; import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
/** /**
@ -31,7 +30,7 @@ import org.hibernate.testing.boot.ClassLoaderServiceTestingImpl;
public class XMLContextTest { public class XMLContextTest {
@Test @Test
public void testAll() throws Exception { public void testAll() throws Exception {
final XMLHelper xmlHelper = new XMLHelper( ClassLoaderServiceTestingImpl.INSTANCE ); final XMLHelper xmlHelper = new XMLHelper();
final XMLContext context = new XMLContext( BootstrapContextImpl.INSTANCE ); final XMLContext context = new XMLContext( BootstrapContextImpl.INSTANCE );
InputStream is = ClassLoaderServiceTestingImpl.INSTANCE.locateResourceStream( InputStream is = ClassLoaderServiceTestingImpl.INSTANCE.locateResourceStream(

View File

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