HHH-7991 Correct all instances of

Thread.currentThread().getContextClassLoader()
This commit is contained in:
Brett Meyer 2013-02-12 17:38:11 -05:00
parent 56867417e2
commit 4d68ddf7b0
24 changed files with 106 additions and 62 deletions

View File

@ -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 );

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -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 ) {

View File

@ -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 ) {

View File

@ -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() {

View File

@ -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() );

View File

@ -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();
}

View File

@ -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();

View File

@ -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<? extends RevisionListener>) Thread.currentThread().getContextClassLoader().loadClass(revisionListenerClassName);
revisionListenerClass = (Class<? extends RevisionListener>) ReflectHelper.classForName(revisionListenerClassName);
} catch (ClassNotFoundException e) {
throw new MappingException("Revision listener class not found: " + revisionListenerClassName + ".", e);
}

View File

@ -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));
}

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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();
}
}
}