You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Tobias Gierke <to...@voipfuture.com> on 2015/06/18 16:39:48 UTC
Wicket 6 + string resource caching
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 tobias.gierke@voipfuture.com Web http://www.voipfuture.com
CEO Jan Bastian
Commercial Court AG Hamburg HRB 109896, VAT ID DE263738086
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket 6 + string resource caching
Posted by Tobias Gierke <to...@voipfuture.com>.
Thanks Martin & Sven, works like a charm :)
Cheers,
Tobias
> Also see Localizer#clearCache() [1]
> When changing the theme use getApplication().getLocalizer().clearCache()
>
> 1.
> https://github.com/apache/wicket/blob/edcbd4e849378a5aba9ee2d5e4f954bce904af52/wicket-core/src/main/java/org/apache/wicket/Localizer.java#L90
>
> Martin Grigorov
> Freelancer. Available for hire!
> Wicket Training and Consulting
> https://twitter.com/mtgrigorov
>
> On Thu, Jun 18, 2015 at 9:14 PM, Sven Meier <sv...@meiers.net> wrote:
>
>> Hi,
>>
>> Wicket includes the session's style when generating the cache key*. Why
>> don't you use this to identify your current 'theme'? Then theme-specific
>> string resources should work out-of-the-box.
>>
>> Have fun
>> Sven
>>
>> *See Localizer#getCacheKey()
>>
>>
>>
>> On 18.06.2015 16:39, Tobias Gierke wrote:
>>
>>> 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 );
>>> }
>>> }
>>> }
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket 6 + string resource caching
Posted by Martin Grigorov <mg...@apache.org>.
Also see Localizer#clearCache() [1]
When changing the theme use getApplication().getLocalizer().clearCache()
1.
https://github.com/apache/wicket/blob/edcbd4e849378a5aba9ee2d5e4f954bce904af52/wicket-core/src/main/java/org/apache/wicket/Localizer.java#L90
Martin Grigorov
Freelancer. Available for hire!
Wicket Training and Consulting
https://twitter.com/mtgrigorov
On Thu, Jun 18, 2015 at 9:14 PM, Sven Meier <sv...@meiers.net> wrote:
> Hi,
>
> Wicket includes the session's style when generating the cache key*. Why
> don't you use this to identify your current 'theme'? Then theme-specific
> string resources should work out-of-the-box.
>
> Have fun
> Sven
>
> *See Localizer#getCacheKey()
>
>
>
> On 18.06.2015 16:39, Tobias Gierke wrote:
>
>> 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 );
>> }
>> }
>> }
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
Re: Wicket 6 + string resource caching
Posted by Sven Meier <sv...@meiers.net>.
Hi,
Wicket includes the session's style when generating the cache key*. Why
don't you use this to identify your current 'theme'? Then theme-specific
string resources should work out-of-the-box.
Have fun
Sven
*See Localizer#getCacheKey()
On 18.06.2015 16:39, Tobias Gierke wrote:
> 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 );
> }
> }
> }
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org