Skip to content

Commit f593a9c

Browse files
committed
HHH-9874 Perform Infinispan configuration parsing using the classloader of the cache integration module
1 parent 3d27b29 commit f593a9c

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ protected EmbeddedCacheManager createCacheManager(
427427
DEF_INFINISPAN_CONFIG_RESOURCE
428428
);
429429
final FileLookup fileLookup = FileLookupFactory.newInstance();
430+
//The classloader of the current module:
431+
final ClassLoader infinispanClassLoader = InfinispanRegionFactory.class.getClassLoader();
430432

431433
return serviceRegistry.getService( ClassLoaderService.class ).workWithClassLoader(
432434
new ClassLoaderService.Work<EmbeddedCacheManager>() {
@@ -438,12 +440,12 @@ public EmbeddedCacheManager doWork(ClassLoader classLoader) {
438440
if ( is == null ) {
439441
// when it's not a user-provided configuration file, it might be a default configuration file,
440442
// and if that's included in [this] module might not be visible to the ClassLoaderService:
441-
classLoader = this.getClass().getClassLoader();
443+
classLoader = infinispanClassLoader;
442444
// This time use lookupFile*Strict* so to provide an exception if we can't find it yet:
443445
is = FileLookupFactory.newInstance().lookupFileStrict( configLoc, classLoader );
444446
}
445-
final ParserRegistry parserRegistry = new ParserRegistry( classLoader );
446-
final ConfigurationBuilderHolder holder = parserRegistry.parse( is );
447+
final ParserRegistry parserRegistry = new ParserRegistry( infinispanClassLoader );
448+
final ConfigurationBuilderHolder holder = parseWithOverridenClassLoader( parserRegistry, is, infinispanClassLoader );
447449

448450
// Override global jmx statistics exposure
449451
final String globalStats = extractProperty(
@@ -462,10 +464,28 @@ public EmbeddedCacheManager doWork(ClassLoader classLoader) {
462464
throw new CacheException( "Unable to create default cache manager", e );
463465
}
464466
}
467+
465468
}
466469
);
467470
}
468471

472+
private static ConfigurationBuilderHolder parseWithOverridenClassLoader(ParserRegistry configurationParser, InputStream is, ClassLoader infinispanClassLoader) {
473+
// Infinispan requires the context ClassLoader to have full visibility on all
474+
// its components and eventual extension points even *during* configuration parsing.
475+
final Thread currentThread = Thread.currentThread();
476+
final ClassLoader originalContextClassLoader = currentThread.getContextClassLoader();
477+
try {
478+
currentThread.setContextClassLoader( infinispanClassLoader );
479+
ConfigurationBuilderHolder builderHolder = configurationParser.parse( is );
480+
// Workaround Infinispan's ClassLoader strategies to bend to our will:
481+
builderHolder.getGlobalConfigurationBuilder().classLoader( infinispanClassLoader );
482+
return builderHolder;
483+
}
484+
finally {
485+
currentThread.setContextClassLoader( originalContextClassLoader );
486+
}
487+
}
488+
469489
protected EmbeddedCacheManager createCacheManager(ConfigurationBuilderHolder holder) {
470490
return new DefaultCacheManager( holder, true );
471491
}

0 commit comments

Comments
 (0)