From 4d68ddf7b0f71d9f387e9732cf4b2d53d4ae2e33 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Tue, 12 Feb 2013 17:38:11 -0500 Subject: [PATCH] HHH-7991 Correct all instances of Thread.currentThread().getContextClassLoader() --- .../java/org/hibernate/cfg/Configuration.java | 3 +- .../org/hibernate/engine/jdbc/BlobProxy.java | 3 +- .../org/hibernate/engine/jdbc/ClobProxy.java | 3 +- .../org/hibernate/engine/jdbc/NClobProxy.java | 4 +- .../engine/jdbc/ResultSetWrapperProxy.java | 3 +- .../engine/jdbc/SerializableBlobProxy.java | 3 +- .../engine/jdbc/SerializableClobProxy.java | 3 +- .../hibernate/hql/internal/ast/HqlLexer.java | 19 ++-------- .../internal/util/ClassLoaderHelper.java | 38 +++++++++++++++++++ .../hibernate/internal/util/ConfigHelper.java | 6 +-- .../internal/util/ReflectHelper.java | 12 +++--- .../internal/util/SerializationHelper.java | 2 +- .../internal/util/xml/XMLHelper.java | 3 +- .../DriverManagerRegistrationTest.java | 3 +- .../configuration/AuditConfiguration.java | 4 +- .../configuration/GlobalConfiguration.java | 7 ++-- .../envers/entities/EntityInstantiator.java | 11 +++++- .../mapper/ComponentPropertyMapper.java | 2 +- .../mapper/id/AbstractCompositeIdMapper.java | 2 +- .../entities/mapper/id/MultipleIdMapper.java | 2 +- .../mapper/relation/AbstractToOneMapper.java | 10 ++++- .../tools/reflection/ReflectionTools.java | 13 +------ .../infinispan/InfinispanRegionFactory.java | 7 ++-- .../osgi/HibernateBundleActivator.java | 5 ++- 24 files changed, 106 insertions(+), 62 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/internal/util/ClassLoaderHelper.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index 6dfe1ace55..c723ff7960 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -98,6 +98,7 @@ import org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory; import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.SessionFactoryImpl; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.internal.util.ConfigHelper; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.SerializationHelper; @@ -720,7 +721,7 @@ public class Configuration implements Serializable { */ public Configuration addResource(String resourceName) throws MappingException { LOG.readingMappingsFromResource( resourceName ); - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader contextClassLoader = ClassLoaderHelper.getClassLoader(); InputStream resourceInputStream = null; if ( contextClassLoader != null ) { resourceInputStream = contextClassLoader.getResourceAsStream( resourceName ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/BlobProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/BlobProxy.java index 31839ed514..54fd38430d 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/BlobProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/BlobProxy.java @@ -32,6 +32,7 @@ import java.sql.Blob; import java.sql.SQLException; import org.hibernate.engine.jdbc.internal.BinaryStreamImpl; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.type.descriptor.java.DataHelper; /** @@ -196,7 +197,7 @@ public class BlobProxy implements InvocationHandler { * @return The class loader appropriate for proxy construction. */ private static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = BlobImplementer.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ClobProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ClobProxy.java index 0ddc9455d3..97fbe4cc26 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ClobProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ClobProxy.java @@ -34,6 +34,7 @@ import java.sql.Clob; import java.sql.SQLException; import org.hibernate.engine.jdbc.internal.CharacterStreamImpl; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.type.descriptor.java.DataHelper; /** @@ -215,7 +216,7 @@ public class ClobProxy implements InvocationHandler { * @return The class loader appropriate for proxy construction. */ protected static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = ClobImplementer.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/NClobProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/NClobProxy.java index 715f57e767..9afdd80207 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/NClobProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/NClobProxy.java @@ -28,6 +28,8 @@ import java.lang.reflect.Proxy; import java.sql.Clob; import java.sql.NClob; +import org.hibernate.internal.util.ClassLoaderHelper; + /** * Manages aspects of proxying java.sql.NClobs for non-contextual creation, including proxy creation and * handling proxy invocations. We use proxies here solely to avoid JDBC version incompatibilities. @@ -86,7 +88,7 @@ public class NClobProxy extends ClobProxy { * @return The class loader appropriate for proxy construction. */ protected static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = NClobImplementer.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java index e2cb8773a2..8609ba53f8 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java @@ -34,6 +34,7 @@ import org.jboss.logging.Logger; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.internal.util.ClassLoaderHelper; /** * A proxy for a ResultSet delegate, responsible for locally caching the columnName-to-columnIndex resolution that @@ -78,7 +79,7 @@ public class ResultSetWrapperProxy implements InvocationHandler { * @return The class loader appropriate for proxy construction. */ public static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = ResultSet.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableBlobProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableBlobProxy.java index c7f16de4a7..160f49f76e 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableBlobProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableBlobProxy.java @@ -31,6 +31,7 @@ import java.lang.reflect.Proxy; import java.sql.Blob; import org.hibernate.HibernateException; +import org.hibernate.internal.util.ClassLoaderHelper; /** * Manages aspects of proxying {@link Blob Blobs} to add serializability. @@ -101,7 +102,7 @@ public class SerializableBlobProxy implements InvocationHandler, Serializable { * @return The class loader appropriate for proxy construction. */ public static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = WrappedBlob.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableClobProxy.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableClobProxy.java index a092ff710a..ef991f4871 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableClobProxy.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/SerializableClobProxy.java @@ -31,6 +31,7 @@ import java.lang.reflect.Proxy; import java.sql.Clob; import org.hibernate.HibernateException; +import org.hibernate.internal.util.ClassLoaderHelper; /** * Manages aspects of proxying {@link Clob Clobs} to add serializability. @@ -100,7 +101,7 @@ public class SerializableClobProxy implements InvocationHandler, Serializable { * @return The class loader appropriate for proxy construction. */ public static ClassLoader getProxyClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = WrappedClob.class.getClassLoader(); } diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlLexer.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlLexer.java index f56c07539c..794eadd7fd 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlLexer.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/HqlLexer.java @@ -26,11 +26,11 @@ package org.hibernate.hql.internal.ast; import java.io.InputStream; import java.io.Reader; -import antlr.Token; - import org.hibernate.QueryException; import org.hibernate.hql.internal.antlr.HqlBaseLexer; +import antlr.Token; + /** * Custom lexer for the HQL grammar. Extends the base lexer generated by ANTLR * in order to keep the grammar source file clean. @@ -50,20 +50,7 @@ class HqlLexer extends HqlBaseLexer { } public void setTokenObjectClass(String cl) { - Thread thread = null; - ClassLoader contextClassLoader = null; - try { - // workaround HHH-6536, by setting TCCL to the Hibernate classloader - thread = Thread.currentThread(); - contextClassLoader = thread.getContextClassLoader(); - thread.setContextClassLoader(HqlToken.class.getClassLoader()); - - // Ignore the token class name parameter, and use a specific token class. - super.setTokenObjectClass( HqlToken.class.getName() ); - } - finally { - thread.setContextClassLoader( contextClassLoader ); - } + this.tokenObjectClass = HqlToken.class; } protected void setPossibleID(boolean possibleID) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ClassLoaderHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ClassLoaderHelper.java new file mode 100644 index 0000000000..ea6b7fdb15 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ClassLoaderHelper.java @@ -0,0 +1,38 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.internal.util; + +/** + * This exists purely to allow custom ClassLoaders to be injected and used + * prior to ServiceRegistry and ClassLoadingService existance. This should be + * replaced in Hibernate 5. + * + * @author Brett Meyer + */ +public class ClassLoaderHelper { + + public static ClassLoader overridenClassLoader = null; + + public static ClassLoader getClassLoader() { + return overridenClassLoader != null ? + overridenClassLoader : Thread.currentThread().getContextClassLoader(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ConfigHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ConfigHelper.java index e43aea33b0..dacda973c2 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ConfigHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ConfigHelper.java @@ -78,7 +78,7 @@ public final class ConfigHelper { // First, try to locate this resource through the current // context classloader. - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader contextClassLoader = ClassLoaderHelper.getClassLoader(); if (contextClassLoader!=null) { url = contextClassLoader.getResource(path); } @@ -159,7 +159,7 @@ public final class ConfigHelper { resource.substring(1) : resource; InputStream stream = null; - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader classLoader = ClassLoaderHelper.getClassLoader(); if (classLoader!=null) { stream = classLoader.getResourceAsStream( stripped ); } @@ -182,7 +182,7 @@ public final class ConfigHelper { InputStream stream = null; - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader classLoader = ClassLoaderHelper.getClassLoader(); if ( classLoader != null ) { stream = classLoader.getResourceAsStream( resource ); if ( stream == null && hasLeadingSlash ) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java index 7048fe371c..5d0ab0f774 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java @@ -160,9 +160,9 @@ public final class ReflectHelper { */ public static Class classForName(String name, Class caller) throws ClassNotFoundException { try { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if ( contextClassLoader != null ) { - return contextClassLoader.loadClass( name ); + ClassLoader classLoader = ClassLoaderHelper.getClassLoader(); + if ( classLoader != null ) { + return classLoader.loadClass( name ); } } catch ( Throwable ignore ) { @@ -182,9 +182,9 @@ public final class ReflectHelper { */ public static Class classForName(String name) throws ClassNotFoundException { try { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if ( contextClassLoader != null ) { - return contextClassLoader.loadClass(name); + ClassLoader classLoader = ClassLoaderHelper.getClassLoader(); + if ( classLoader != null ) { + return classLoader.loadClass(name); } } catch ( Throwable ignore ) { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java index d7c1013077..541a63e4e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/SerializationHelper.java @@ -191,7 +191,7 @@ public final class SerializationHelper { * @return The current TCCL */ public static ClassLoader defaultClassLoader() { - return Thread.currentThread().getContextClassLoader(); + return ClassLoaderHelper.getClassLoader(); } public static ClassLoader hibernateClassLoader() { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/xml/XMLHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/xml/XMLHelper.java index 1d3e889c48..3c35db7563 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/xml/XMLHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/xml/XMLHelper.java @@ -29,6 +29,7 @@ import org.dom4j.io.DOMReader; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; +import org.hibernate.internal.util.ClassLoaderHelper; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; @@ -81,7 +82,7 @@ public final class XMLHelper { public static DocumentFactory getDocumentFactory() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); DocumentFactory factory; try { Thread.currentThread().setContextClassLoader( XMLHelper.class.getClassLoader() ); diff --git a/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerRegistrationTest.java b/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerRegistrationTest.java index bb91d5c66f..cb902559f3 100644 --- a/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerRegistrationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerRegistrationTest.java @@ -35,6 +35,7 @@ import java.util.logging.Logger; import org.junit.AfterClass; import org.junit.Test; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; @@ -91,7 +92,7 @@ public class DriverManagerRegistrationTest extends BaseUnitTestCase { } private static ClassLoader determineClassLoader() { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); + ClassLoader cl = ClassLoaderHelper.getClassLoader(); if ( cl == null ) { cl = DriverManagerRegistrationTest.class.getClassLoader(); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java index 19d6ef19de..eb705e47cc 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java @@ -127,9 +127,7 @@ public class AuditConfiguration { auditStrategyClass = classLoaderService.classForName( auditEntCfg.getAuditStrategyName() ); } else { - auditStrategyClass = Thread.currentThread() - .getContextClassLoader() - .loadClass( auditEntCfg.getAuditStrategyName() ); + auditStrategyClass = ReflectHelper.classForName( auditEntCfg.getAuditStrategyName() ); } strategy = (AuditStrategy) ReflectHelper.getDefaultConstructor(auditStrategyClass).newInstance(); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/GlobalConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/GlobalConfiguration.java index a7d0fd7bee..34f3d51f3f 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/GlobalConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/GlobalConfiguration.java @@ -22,12 +22,13 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.configuration; +import static org.hibernate.envers.tools.Tools.getProperty; + import java.util.Properties; import org.hibernate.MappingException; import org.hibernate.envers.RevisionListener; - -import static org.hibernate.envers.tools.Tools.getProperty; +import org.hibernate.internal.util.ReflectHelper; /** * @author Adam Warski (adam at warski dot org) @@ -134,7 +135,7 @@ public class GlobalConfiguration { String revisionListenerClassName = properties.getProperty("org.hibernate.envers.revision_listener", null); if (revisionListenerClassName != null) { try { - revisionListenerClass = (Class) Thread.currentThread().getContextClassLoader().loadClass(revisionListenerClassName); + revisionListenerClass = (Class) ReflectHelper.classForName(revisionListenerClassName); } catch (ClassNotFoundException e) { throw new MappingException("Revision listener class not found: " + revisionListenerClassName + ".", e); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java index b4e07432f2..5152539e08 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java @@ -97,7 +97,7 @@ public class EntityInstantiator { entCfg = verCfg.getEntCfg().getNotVersionEntityConfiguration(entityName); } - Class cls = ReflectionTools.loadClass(entCfg.getEntityClassName()); + Class cls = ReflectHelper.classForName(entCfg.getEntityClassName()); ret = ReflectHelper.getDefaultConstructor(cls).newInstance(); } catch (Exception e) { throw new AuditException(e); @@ -128,7 +128,14 @@ public class EntityInstantiator { final Serializable entityId = initializer.getIdentifier(); if (verCfg.getEntCfg().isVersioned(entityName)) { final String entityClassName = verCfg.getEntCfg().get(entityName).getEntityClassName(); - final ToOneDelegateSessionImplementor delegate = new ToOneDelegateSessionImplementor(versionsReader, ReflectionTools.loadClass(entityClassName), entityId, revision, verCfg); + Class entityClass; + try { + entityClass = ReflectHelper.classForName(entityClassName); + } + catch ( ClassNotFoundException e ) { + throw new AuditException( e ); + } + final ToOneDelegateSessionImplementor delegate = new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, verCfg); originalId.put(key, versionsReader.getSessionImplementor().getFactory().getEntityPersister(entityName).createProxy(entityId, delegate)); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java index dd2889cd62..871cd3da3e 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/ComponentPropertyMapper.java @@ -121,7 +121,7 @@ public class ComponentPropertyMapper implements PropertyMapper, CompositeMapperB // set the component try { Object subObj = ReflectHelper.getDefaultConstructor( - Thread.currentThread().getContextClassLoader().loadClass(componentClassName)).newInstance(); + ReflectHelper.classForName(componentClassName)).newInstance(); setter.set(obj, subObj, null); delegate.mapToEntityFromMap(verCfg, subObj, data, primaryKey, versionsReader, revision); } catch (Exception e) { diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java index 9aed9c2018..da020efd9f 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/AbstractCompositeIdMapper.java @@ -54,7 +54,7 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme Object ret; try { - final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass); + final Class clazz = ReflectHelper.classForName(compositeIdClass); ret = ReflectHelper.getDefaultConstructor(clazz).newInstance(); } catch (Exception e) { throw new AuditException(e); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java index c9a94a3fe7..1607971499 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/id/MultipleIdMapper.java @@ -76,7 +76,7 @@ public class MultipleIdMapper extends AbstractCompositeIdMapper implements Simpl Object ret; try { - final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass); + final Class clazz = ReflectHelper.classForName(compositeIdClass); ret = ReflectHelper.getDefaultConstructor(clazz).newInstance(); } catch (Exception e) { throw new AuditException(e); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/AbstractToOneMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/AbstractToOneMapper.java index f038ecd1c9..fd0f9f8508 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/AbstractToOneMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/AbstractToOneMapper.java @@ -11,8 +11,10 @@ import org.hibernate.envers.entities.EntityConfiguration; import org.hibernate.envers.entities.PropertyData; import org.hibernate.envers.entities.mapper.PersistentCollectionChangeData; import org.hibernate.envers.entities.mapper.PropertyMapper; +import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.reader.AuditReaderImplementor; import org.hibernate.envers.tools.reflection.ReflectionTools; +import org.hibernate.internal.util.ReflectHelper; import org.hibernate.property.Setter; /** @@ -59,7 +61,13 @@ public abstract class AbstractToOneMapper implements PropertyMapper { entCfg = verCfg.getEntCfg().getNotVersionEntityConfiguration(entityName); isRelationAudited = false; } - Class entityClass = ReflectionTools.loadClass(entCfg.getEntityClassName()); + Class entityClass; + try { + entityClass = ReflectHelper.classForName(entCfg.getEntityClassName()); + } + catch ( ClassNotFoundException e ) { + throw new AuditException( e ); + } return new EntityInfo(entityClass, entityName, isRelationAudited); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/tools/reflection/ReflectionTools.java b/hibernate-envers/src/main/java/org/hibernate/envers/tools/reflection/ReflectionTools.java index 23ab1e19ce..da7908aa1d 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/tools/reflection/ReflectionTools.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/tools/reflection/ReflectionTools.java @@ -22,10 +22,11 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.envers.tools.reflection; +import static org.hibernate.envers.tools.Pair.make; + import java.util.Map; import org.hibernate.envers.entities.PropertyData; -import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.tools.Pair; import org.hibernate.internal.util.collections.ConcurrentReferenceHashMap; import org.hibernate.property.Getter; @@ -33,8 +34,6 @@ import org.hibernate.property.PropertyAccessor; import org.hibernate.property.PropertyAccessorFactory; import org.hibernate.property.Setter; -import static org.hibernate.envers.tools.Pair.make; - /** * @author Adam Warski (adam at warski dot org) */ @@ -48,14 +47,6 @@ public class ReflectionTools { ConcurrentReferenceHashMap.ReferenceType.SOFT, ConcurrentReferenceHashMap.ReferenceType.SOFT); - public static Class loadClass(String name) { - try { - return Thread.currentThread().getContextClassLoader().loadClass(name); - } catch (ClassNotFoundException e) { - throw new AuditException(e); - } - } - private static PropertyAccessor getAccessor(String accessorType) { return PropertyAccessorFactory.getPropertyAccessor(accessorType); } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java index 0f1bf3be48..8faa3c9e2c 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java @@ -34,6 +34,7 @@ import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.TimestampsRegion; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.Settings; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.internal.util.config.ConfigurationHelper; import org.infinispan.AdvancedCache; import org.infinispan.commands.module.ModuleCommandFactory; @@ -386,10 +387,10 @@ public class InfinispanRegionFactory implements RegionFactory { try { String configLoc = ConfigurationHelper.getString( INFINISPAN_CONFIG_RESOURCE_PROP, properties, DEF_INFINISPAN_CONFIG_RESOURCE); - ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader classLoader = ClassLoaderHelper.getClassLoader(); InputStream is = FileLookupFactory.newInstance().lookupFileStrict( - configLoc, ctxClassLoader); - ParserRegistry parserRegistry = new ParserRegistry(ctxClassLoader); + configLoc, classLoader); + ParserRegistry parserRegistry = new ParserRegistry(classLoader); ConfigurationBuilderHolder holder = parserRegistry.parse(is); // Override global jmx statistics exposure diff --git a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java index b2cc73fb42..bb65644684 100644 --- a/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java +++ b/hibernate-osgi/src/main/java/org/hibernate/osgi/HibernateBundleActivator.java @@ -32,6 +32,7 @@ import javax.persistence.spi.PersistenceUnitInfo; import javax.persistence.spi.PersistenceUnitTransactionType; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.ClassLoaderHelper; import org.hibernate.jpa.HibernatePersistenceProvider; import org.hibernate.jpa.boot.spi.Bootstrap; import org.osgi.framework.Bundle; @@ -62,6 +63,8 @@ public class HibernateBundleActivator context.addBundleListener(this); osgiClassLoader = new OsgiClassLoader(); + + ClassLoaderHelper.overridenClassLoader = osgiClassLoader; for ( Bundle bundle : context.getBundles() ) { handleBundleChange( bundle ); @@ -106,4 +109,4 @@ public class HibernateBundleActivator return Bootstrap.getEntityManagerFactoryBuilder( info, map, osgiClassLoader ).build(); } -} \ No newline at end of file +}