HHH-9446 - ClassCastException on loaded entities under GlassFish 4.0

This commit is contained in:
Steve Ebersole 2015-03-27 15:13:53 -05:00
parent d5d066cb3d
commit e9230758b4
4 changed files with 7 additions and 117 deletions

View File

@ -42,6 +42,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.ClassLoaderHelper;
import org.jboss.logging.Logger;
/**
@ -355,66 +356,4 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
}
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// completely temporary !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/**
* Perform some discrete work with with the TCCL set to our aggregated ClassLoader
*
* @param work The discrete work to be done
* @param <T> The type of the work result
*
* @return The work result.
*/
@Override
public <T> T withTccl(Work<T> work) {
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
boolean set = false;
try {
Thread.currentThread().setContextClassLoader(
new TcclSafeAggregatedClassLoader( aggregatedClassLoader, tccl ) );
set = true;
}
catch (Exception ignore) {
}
try {
return work.perform();
}
finally {
if ( set ) {
Thread.currentThread().setContextClassLoader( tccl );
}
}
}
// TODO: Remove in ORM 5! See HHH-8818
private class TcclSafeAggregatedClassLoader extends ClassLoader {
private final AggregatedClassLoader aggregatedClassLoader;
private TcclSafeAggregatedClassLoader(AggregatedClassLoader aggregatedClassLoader, ClassLoader tccl) {
super(tccl);
this.aggregatedClassLoader = aggregatedClassLoader;
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
return aggregatedClassLoader.getResources( name );
}
@Override
protected URL findResource(String name) {
return aggregatedClassLoader.findResource( name );
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
return aggregatedClassLoader.findClass( name );
}
}
}

View File

@ -88,31 +88,4 @@ public interface ClassLoaderService extends Service, Stoppable {
* @return The ordered set of discovered services.
*/
public <S> LinkedHashSet<S> loadJavaServices(Class<S> serviceContract);
/**
* Hack around continued (temporary) need to sometimes set the TCCL for code we call that expects it.
*
* @param <T> The result type
*/
@Deprecated
public static interface Work<T> {
/**
* The work to be performed with the TCCL set
*
* @return The result of the work
*/
public T perform();
}
/**
* Perform some discrete work with with the TCCL set to our aggregated ClassLoader
*
* @param work The discrete work to be done
* @param <T> The type of the work result
*
* @return The work result.
*/
@Deprecated
public <T> T withTccl(Work<T> work);
}

View File

@ -54,8 +54,6 @@ import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.boot.spi.MetadataImplementor;
@ -195,22 +193,11 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
this.standardServiceRegistry = ssrBuilder.build();
configure( standardServiceRegistry, mergedSettings );
// Build the Metadata object
// NOTE : because we still use hibernate-commons-annotations, we still need to
// use the TCCL hacks because hibernate-commons-annotations uses TCCL
final ClassLoaderService classLoaderService = bsr.getService( ClassLoaderService.class );
this.metadata = ( (ClassLoaderServiceImpl) classLoaderService ).withTccl(
new ClassLoaderServiceImpl.Work<MetadataImplementor>() {
@Override
public MetadataImplementor perform() {
final MetadataSources metadataSources = new MetadataSources( bsr );
List<AttributeConverterDefinition> attributeConverterDefinitions = populate( metadataSources, mergedSettings, standardServiceRegistry );
final MetadataBuilder metamodelBuilder = metadataSources.getMetadataBuilder( standardServiceRegistry );
populate( metamodelBuilder, mergedSettings, standardServiceRegistry, attributeConverterDefinitions );
return (MetadataImplementor) metamodelBuilder.build();
}
}
);
final MetadataSources metadataSources = new MetadataSources( bsr );
List<AttributeConverterDefinition> attributeConverterDefinitions = populate( metadataSources, mergedSettings, standardServiceRegistry );
final MetadataBuilder metamodelBuilder = metadataSources.getMetadataBuilder( standardServiceRegistry );
populate( metamodelBuilder, mergedSettings, standardServiceRegistry, attributeConverterDefinitions );
this.metadata = (MetadataImplementor) metamodelBuilder.build();
withValidatorFactory( configurationValues.get( AvailableSettings.VALIDATION_FACTORY ) );

View File

@ -120,16 +120,7 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
this.classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
// NOTE : we use the TCCL here for hibernate-commons-annotations
classLoaderService.withTccl(
new ClassLoaderService.Work<Void>() {
@Override
public Void perform() {
doInitialize( metadata, mappingCollector, serviceRegistry, classLoaderService );
return null;
}
}
);
doInitialize( metadata, mappingCollector, serviceRegistry, classLoaderService );
}
private void doInitialize(