diff --git a/jspwiki-cache/src/main/java/org/apache/wiki/cache/EhcacheCachingManager.java b/jspwiki-cache/src/main/java/org/apache/wiki/cache/EhcacheCachingManager.java index bdb566b187..fd44db9bc3 100644 --- a/jspwiki-cache/src/main/java/org/apache/wiki/cache/EhcacheCachingManager.java +++ b/jspwiki-cache/src/main/java/org/apache/wiki/cache/EhcacheCachingManager.java @@ -70,11 +70,8 @@ public void initialize( final Engine engine, final Properties props ) throws Wik final String cacheEnabled = TextUtil.getStringProperty( props, PROP_CACHE_ENABLE, PROP_USECACHE_DEPRECATED, "true" ); final boolean useCache = "true".equalsIgnoreCase( cacheEnabled ); if( useCache ) { - String val = TextUtil.getStringProperty( props, PROP_CACHE_CONF_FILE, "ehcache-jspwiki.xml" ); - URL location = this.getClass().getResource( "/" + val ); - if (location == null) { - location = this.getClass().getResource( val ); - } + final String val = TextUtil.getStringProperty( props, PROP_CACHE_CONF_FILE, "ehcache-jspwiki.xml" ); + final URL location = findConfiguration( val ); LOG.info( "Reading ehcache configuration file from classpath on /{}", location ); if (location == null ) { LOG.error( "The ehcache configuration file from classpath on {} could not be loaded. Check setting {}", val , PROP_CACHE_CONF_FILE); @@ -90,6 +87,25 @@ public void initialize( final Engine engine, final Properties props ) throws Wik } } + /** + * Looks for the configuration file with the class loader of this class and, if it is not there, with the one of the + * thread. When this jar is in a library shared by several web applications, as in the portable builds, only the + * second one sees the classes folder of the web application. + */ + URL findConfiguration( final String name ) { + URL url = this.getClass().getResource( "/" + name ); + if( url == null ) { + url = this.getClass().getResource( name ); + } + if( url == null ) { + final ClassLoader context = Thread.currentThread().getContextClassLoader(); + if( context != null ) { + url = context.getResource( name.startsWith( "/" ) ? name.substring( 1 ) : name ); + } + } + return url; + } + void registerCache( final String cacheName ) { final Cache cache; if( cacheManager.cacheExists( cacheName ) ) { diff --git a/jspwiki-cache/src/test/java/org/apache/wiki/cache/EhcacheCachingManagerTest.java b/jspwiki-cache/src/test/java/org/apache/wiki/cache/EhcacheCachingManagerTest.java index 90441f5422..ca5018b897 100644 --- a/jspwiki-cache/src/test/java/org/apache/wiki/cache/EhcacheCachingManagerTest.java +++ b/jspwiki-cache/src/test/java/org/apache/wiki/cache/EhcacheCachingManagerTest.java @@ -22,6 +22,11 @@ Licensed to the Apache Software Foundation (ASF) under one import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Properties; @@ -82,4 +87,27 @@ void testPutGetRemoveAndKeys() { Assertions.assertNull( ecm.get( "trucutru", "key", () -> retrieveFromBackend ) ); } + + /** The jar is in a library shared by several web applications, so the configuration is only seen by the context class loader. */ + @Test + void testConfigurationOnlyVisibleToTheContextClassLoader() throws Exception { + final File dir = Files.createTempDirectory( "ehcache-context" ).toFile(); + final File conf = new File( dir, "ehcache-context-only.xml" ); + Files.write( conf.toPath(), "".getBytes( StandardCharsets.UTF_8 ) ); + final Properties props = new Properties(); + props.setProperty( CachingManager.PROP_CACHE_CONF_FILE, "ehcache-context-only.xml" ); + final ClassLoader original = Thread.currentThread().getContextClassLoader(); + try( URLClassLoader context = new URLClassLoader( new URL[] { dir.toURI().toURL() }, original ) ) { + Thread.currentThread().setContextClassLoader( context ); + final EhcacheCachingManager manager = new EhcacheCachingManager(); + manager.initialize( null, props ); + Assertions.assertEquals( 7, manager.cacheMap.size() ); + manager.shutdown(); + } finally { + Thread.currentThread().setContextClassLoader( original ); + conf.delete(); + dir.delete(); + } + } + } diff --git a/jspwiki-portable/src/overlay/woas/webapps/department/WEB-INF/classes/jspwiki-custom.properties b/jspwiki-portable/src/overlay/woas/webapps/department/WEB-INF/classes/jspwiki-custom.properties index 82333df29b..284fc7bf88 100644 --- a/jspwiki-portable/src/overlay/woas/webapps/department/WEB-INF/classes/jspwiki-custom.properties +++ b/jspwiki-portable/src/overlay/woas/webapps/department/WEB-INF/classes/jspwiki-custom.properties @@ -30,9 +30,8 @@ jspwiki.attachment.maxsize=4000000 jspwiki.breakTitleWithSpaces=true jspwiki.searchProvider=LuceneSearchProvider jspwiki.security=jaas -#disabled due to classpath loading issues with the portable build see JSPWIKI-1199 -jspwiki.cache.enable=false -jspwiki.cache.config-file=ehcache-woas-department +jspwiki.cache.enable=true +jspwiki.cache.config-file=ehcache-woas-department.xml # jspwiki.xmlGroupDatabaseFile=/usr/local/tomcat/lib/groupdatabase.xml # jspwiki.xmlUserDatabaseFile=/usr/local/tomcat/lib/userdatabase.xml diff --git a/jspwiki-portable/src/overlay/woas/webapps/personal/WEB-INF/classes/jspwiki-custom.properties b/jspwiki-portable/src/overlay/woas/webapps/personal/WEB-INF/classes/jspwiki-custom.properties index 9336c72292..5494350619 100644 --- a/jspwiki-portable/src/overlay/woas/webapps/personal/WEB-INF/classes/jspwiki-custom.properties +++ b/jspwiki-portable/src/overlay/woas/webapps/personal/WEB-INF/classes/jspwiki-custom.properties @@ -30,8 +30,7 @@ jspwiki.breakTitleWithSpaces=true jspwiki.pageProvider=FileSystemProvider jspwiki.searchProvider=BasicSearchProvider jspwiki.security=jaas -#disabled due to classpath loading issues with the portable build see JSPWIKI-1199 -jspwiki.cache.enable=false +jspwiki.cache.enable=true jspwiki.cache.config-file=ehcache-woas-personal.xml # jspwiki.xmlGroupDatabaseFile=/usr/local/tomcat/lib/groupdatabase.xml