From f593a9c14ac39d8c2d37a46e420f369fc85f1ac4 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Fri, 19 Jun 2015 16:55:52 +0100 Subject: [PATCH] HHH-9874 Perform Infinispan configuration parsing using the classloader of the cache integration module --- .../infinispan/InfinispanRegionFactory.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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 490fb7cd69..b8330a3af8 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 @@ -427,6 +427,8 @@ public class InfinispanRegionFactory implements RegionFactory { DEF_INFINISPAN_CONFIG_RESOURCE ); final FileLookup fileLookup = FileLookupFactory.newInstance(); + //The classloader of the current module: + final ClassLoader infinispanClassLoader = InfinispanRegionFactory.class.getClassLoader(); return serviceRegistry.getService( ClassLoaderService.class ).workWithClassLoader( new ClassLoaderService.Work() { @@ -438,12 +440,12 @@ public class InfinispanRegionFactory implements RegionFactory { if ( is == null ) { // when it's not a user-provided configuration file, it might be a default configuration file, // and if that's included in [this] module might not be visible to the ClassLoaderService: - classLoader = this.getClass().getClassLoader(); + classLoader = infinispanClassLoader; // This time use lookupFile*Strict* so to provide an exception if we can't find it yet: is = FileLookupFactory.newInstance().lookupFileStrict( configLoc, classLoader ); } - final ParserRegistry parserRegistry = new ParserRegistry( classLoader ); - final ConfigurationBuilderHolder holder = parserRegistry.parse( is ); + final ParserRegistry parserRegistry = new ParserRegistry( infinispanClassLoader ); + final ConfigurationBuilderHolder holder = parseWithOverridenClassLoader( parserRegistry, is, infinispanClassLoader ); // Override global jmx statistics exposure final String globalStats = extractProperty( @@ -462,10 +464,28 @@ public class InfinispanRegionFactory implements RegionFactory { throw new CacheException( "Unable to create default cache manager", e ); } } + } ); } + private static ConfigurationBuilderHolder parseWithOverridenClassLoader(ParserRegistry configurationParser, InputStream is, ClassLoader infinispanClassLoader) { + // Infinispan requires the context ClassLoader to have full visibility on all + // its components and eventual extension points even *during* configuration parsing. + final Thread currentThread = Thread.currentThread(); + final ClassLoader originalContextClassLoader = currentThread.getContextClassLoader(); + try { + currentThread.setContextClassLoader( infinispanClassLoader ); + ConfigurationBuilderHolder builderHolder = configurationParser.parse( is ); + // Workaround Infinispan's ClassLoader strategies to bend to our will: + builderHolder.getGlobalConfigurationBuilder().classLoader( infinispanClassLoader ); + return builderHolder; + } + finally { + currentThread.setContextClassLoader( originalContextClassLoader ); + } + } + protected EmbeddedCacheManager createCacheManager(ConfigurationBuilderHolder holder) { return new DefaultCacheManager( holder, true ); }