Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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(), "<ehcache><defaultCache maxEntriesLocalHeap=\"10\" eternal=\"false\" timeToLiveSeconds=\"60\"/></ehcache>".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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading