Hi,
I'm working on a 'themeable' (does this word exist ?) application were
some string resources differ based on the currently active user's theme.
Since (at least during development) each user can freely switch between
different themes, I need Wicket to not cache these string resources.
I implemented a custom IStringResourceLoader to locate the .properties
file associated with the current user's UI theme but it seems Wicket is
caching the String property values and only calls my
IStringResourceLoader once for every String property.
How can I disable this caching for my IStringResourceLoader ? I suspect
Wicket thinks of .properties files as static resources so maybe I'm
stretching the design a bit ;)
Cheers,
Tobias
Currently my WicketApplication class looks like this:
--->8------>8------>8------>8------>8------>8---
getResourceSettings().getStringResourceLoaders().add( new
UIThemeStringResourceLoader() );
--->8------>8------>8------>8------>8------>8---
...and the the IStringResourceLoader looks like this
--->8------>8------>8------>8------>8------>8---
protected final class UIThemeStringResourceLoader implements
IStringResourceLoader
{
// cache to avoid reloading properties over and over when not
in debug mode
private final Map<String,CacheEntry> properties = new
HashMap<>(); // Key is UI theme name, value is properties file
@Override
public String loadStringResource(Class<?> clazz, String key,
Locale locale, String style, String variation)
{
return loadStringResource( (Component) null , key , locale
,style , variation );
}
@Override
public String loadStringResource(Component component, String
key, Locale locale, String style, String variation)
{
synchronized( properties )
{
CacheEntry props = properties.get(
getUITheme().getName() );
if ( props == null || ( isDebugModeEnabled() &&
props.isOlderThan( java.time.Duration.ofSeconds( 2 ) ) ) ) // discard
stale properties when running in debug mode
{
final UserSession session = (UserSession)
Session.get();
final Locale locale = session.isUserLoggedIn() ?
session.getCurrentUser().getLocale() :
applicationSettings.getDefaultLocale();
final String path =
getUITheme().getStringPropertiesPath( locale );
try ( final InputStream stream =
getServletContext().getResourceAsStream( path ) )
{
if ( stream == null ) {
return null;
}
final Properties tmp = new Properties();
tmp.load( stream );
props = new CacheEntry( tmp );
properties.put( getUITheme().getName() , props );
}
catch (IOException e) {
LOG.warn("getString(): Failed to load string
properties for UI theme '"+getUITheme().getName()+"' from '"+path+"'",e);
}
}
return props.properties.getProperty( key );
}
}
}
--
Tobias Gierke
Development
VOIPFUTURE GmbH Wendenstraße 4 20097 Hamburg, Germany
Phone +49 40 688 900 164 Fax +49 40 688 900 199
Email [email protected] Web http://www.voipfuture.com
CEO Jan Bastian
Commercial Court AG Hamburg HRB 109896, VAT ID DE263738086
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]