One of the commenters to the blog here bring me to the nice idea to use configuration properties to configure the linker instead of sub classing the linker. I found the idea very nice and decide to play around this feature. I did this example just to demonstrate what you can do to extend the linker, which I created in my previously articles to be able to use properties as well.
First thing you need to know is how to use actually GWT configuration properties. Here is a link which describes the multi value properties but also gives you the overview about how to declare your own property. I decide to have a new property called cache.manifest which will be used to define the static files needed to be cached, which normally are not affected by the linker and for which I used sub classing before. Here the steps you need to proceed, to define your own linker:
- Inside your <module>.gwt.xml define the configuration property by adding the following:
<define-configuration-property name="cache.manifest"
is-multi-valued="true" />
- After this line you can now specify the values of the property. As you can see from the definition above I define the property as multi valued, which means the property can have more then one value. To add properties do the following:
<extend-configuration-property name="cache.manifest"
value="/Gwt2go.html" />
<extend-configuration-property name="cache.manifest"
value="/images/*" />
<extend-configuration-property name="cache.manifest"
value="/css/*" />
- Make sure that you extend the property after you defined, otherwise you will get warning message.
Now the most important part here how to use this property from the linker. The linker gives you the current LinkerContext which has a function getConfigurationProperties(). This function contains all defined properties. Knowing this you can do the following:
- Create a new method which reads the new configuration property:
protected String[] getPropsExtraFiles(TreeLogger logger,
LinkerContext context) {
// get the configured external properties, if any
// try to list the configuration properties here
// this files should be static external files
if (context.getConfigurationProperties().isEmpty()) {
logger.log(
TreeLogger.INFO,
"Info: No external static options "
+ " have been configured, configuration-property cache.manifest "
+ " is empty!");
return new String[0];
} else {
Iterator<ConfigurationProperty> it = context
.getConfigurationProperties().iterator();
while (it.hasNext()) {
ConfigurationProperty prop = it.next();
if (prop.getName().equalsIgnoreCase(CONFIG_PROPERTY)) {
List<String> props = prop.getValues();
return (String[]) props.toArray(new String[0]);
}
}
}
return new String[0];
}
- now you can use the new function like this for example:
String[] cacheExtraFiles = getPropsExtraFiles(logger, context);
if (cacheExtraFiles.length == 0) {
cacheExtraFiles = getCacheExtraFiles();
}
for (int i = 0; i < cacheExtraFiles.length; i++) {
staticResoucesSb.append(cacheExtraFiles[i]);
staticResoucesSb.append("\n");
}
This means if it’s property defined then use it, otherwise try to get the from the extended class.
You can find the full source code here and here! Also check the this and this, to see what was modified!
cheers
No comments:
Post a Comment