Watch for updated properties in Wicket

ⅰ亾dé卋堺 提交于 2019-12-11 02:39:04

问题


In my current project we need to implement a way for texters to manage the wicket messages/internationalization via upload of property files.

Also see this question: Administrating internationalized wicket applications

As suggested there, I've implemented a custom IStringResourceLoader and added it at the beginning of the StringResourceLoader list to override any properties already in place:

getResourceSettings().getStringResourceLoaders().add(0, new CustomStringResourceLoader()); 

This however is not enough, because updates can happen and need to be loaded at runtime. StringResources are cached by wicket and updated only when the ResourceWatcher is triggered.

I found where Wicket adds the string resources to the watcher: the PropertiesFactory in the settings. The method to add a resource to the watcher is addToWatcher(...). However this method is protected and also the whole setup suggests this is used for development purposes and not for production.

I managed to use this method by extending PropertiesFactory and effectively creating a custom version to add to settings:

getResourceSettings().setPropertiesFactory(new CustomPropertiesFactory(getResourceSettings()));
getResourceSettings().setResourcePollFrequency(Duration.seconds(1));

So my Question is: I feel this is quite the circuitious solution. Is there another way to watch for changing properties files?


回答1:


My solution to the problem:

getResourceSettings().getStringResourceLoaders().add(0, new CustomResourceLoader());
getResourceSettings().getResourceFinders().add(new Path("/pathToResources"));
getResourceSettings().setResourcePollFrequency(Duration.seconds(1));

This inserts my CustomResourceLoader at the beginning of the list so all properties are first checked there.

The added Path tells the PropertiesFactory to look for resources in a given arbitrary directory outside of wicket.

I needed custom names for my resource files as well, I realized this in the CustomResourceLoader:

public String loadStringResource(final Class<?> clazz, final String key, final Locale locale, final String style, final String variation) {
    final String myResourceFilename = createCustomResourceFileName(locale); 
    final IPropertiesFactory pF = Application.get().getResourceSettings().getPropertiesFactory();
    final org.apache.wicket.resource.Properties props = pF.load(clazz, myResourceFilename);
    ...
}

When using the PropertiesFactory to load the files, it adds them to the internal IModificationWatcher automatically.

It turns out that part of the problem was, that the resource files are in a non-standard encoding. This can be fixed by adding a special IPropertyLoader to the PropertiesFactory in the settings:

((PropertiesFactory) getResourceSettings().getPropertiesFactory()).getPropertiesLoaders().add(0,
            new UtfPropertiesFilePropertiesLoader("properties", "your-favorite-encoding"));


来源:https://stackoverflow.com/questions/19219243/watch-for-updated-properties-in-wicket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!