Filters are a method to give several values to a property and choose one of them based on the value of one or more selectors. They have been found particularly useful in environments which have heavy personalization requirements, although thay can be very handy in a lot of other situations
As an example will take a property called skin which defines the default skin of a user in an educative portal. The users are categorized in types differentiating teachers from students from others. The students are further subdivided depending on their grade level (primary, secondary, etc). We want to let the person configuring the portal set a different default skin depending on the type of user. To do this we retrieve the property value using the user type and subtype as selectors:
getProperties().getString("skin", Filter.by(userType, userSubtype));
In the properties file the skin property may be given different values depending of the values of the user type and subtype. For example:
skin[teacher]=blue skin[student][secundary]=green skin[student]=yellow skin=grey
With this configuration the default skin is grey and it will be applied to users which are not teachers neither students. For students the default skin is yellow unless it is of subtype secundary in which case the default skin will be green. It is important to know that it is optional and up to the person writting the configuration to use selector values or not. He/she can just give a simple value to the property. Using filters from code when retrieving the property value just offers the possibility to the configurator.
EasyConf supports an unlimited number of selectors. They can be optionally used in any of the supported property types with or without a defautl value. Check the Javadoc of the Filter class for more information
New on version 0.9
While the above functionality is enough for most cases, sometimes it is desired to have value of the property which depends on a value only known by the running application. For example a servlet may wish to configure a URL but make it dependent on the current host name and port. EasyConf supports this functionality though a special type of filter. To implement the previous example the properties file should have:
external-app.url=http://${host}:${port}/external-app
Map hostAndPort = new HashMap(); hostAndPort.put("host", host); hostAndPort.put("port", port); getProperties().getString("external-app.url", Filter.usingVariables(hostAndPort));
getProperties().getString("external-app.url", Filter.usingVariables("host", host, "port", port));
Filters based on selectors and filters based on variables can be combined. An example would be:
getProperties().getString("external-app.url", Filter.by(appName).setVariables("host", host, "port", port));