Saturday, June 29, 2013

More on CDI scopes

A couple of months ago I wrote a simple JavaEE web application to illustrate a desire for enhancements to the JAXB specification to better support custom JAXB ObjectFactory integration in relation to marshalling and unmarshalling objects in a CDI scope. You can find the project here. The premise of the application was to demonstrate using an XML file and CDI scopes to brand a site based on request parameters (request domain or in this case HTTP parameters). On startup the application used JAXB to read in an XML configuration file containing site banner related configurations. Through the use of a custom ObjectFactory when the XML was parsed the resulting Java objects were created in custom CDI scopes. Finally a custom servlet filter intercepted all the requests to the application and based on the value of a HTTP parameter the corresponding custom CDI scope was set for the desired banner. The interesting thing about this application and why I am mentioning it here is that the objects in the custom scope were referable from JSF pages without any further customizations needed, i.e. no custom JSF VariableResolver. It is really exciting to see JavaEE using CDI scoped objects without any appserver specific plugins. When CDI was first introduced I remember reading this article and the following statement:
Anybody can build an extension, you don't have to be a vendor or anything like that. You can add extra functionality to the Java EE platform or you can, in fact, replace quite a lot of the core functionality with something that you think is better. - Pete Muir, CDI Spec Lead
I was a bit sceptical at the time but it turns out he was completely right. CDI provides extreme flexibility for framework developers to push the envelope and in my opinion CDI is the greatest contribution to JavaEE ever.