Service Locator _pattern_ in EJB3 and the old JNDI look up issue!(Struts, Seam)
EJB 3 is great, I am not going to list all the advantages that offers and not to forget simplicity after all - YES THERE IS SIMPLICITY - it's not a Spring's advantage anymore (my friend J. Cherouvim :P).
One of the most enjoyable things in EJB 3 spec is dependency injection and the use of annotations! You want from a SLSB to call another SLSB, yeap you just annotate it and there you go, the same applies with every resource that can be access through this way and is managed in an J2EE5 manner!
I met Adam Bien @ JavaOne2007, Adam is a very respected and very good J2EE expert and architect. Writer of few books - too bad still only available in German. Adam Blogs about several J2EE related topics but one of his post's yesterday was really interesting. He has been uploading to a java.net project a set of patterns / workarounds / utilities for J2EE 5 application development! Extra extra useful resource and according to his blog he will be updating the project soon enough. So please bookmark his project @ java.net and have a look at the code, there are a couple of nice ideas in there that for sure can be applied in your J2EE5 enabled project.The project is called p4j5 and can be found here.
I browsed through the available listings and of course I was very curious for the ServiceLocator Pattern (is it a Pattern ? anyway). In J2EE 1.4 days, you can still remember or you can still be using it, JNDI, getting interface references, narrowing and obtaining finally the proxy object to call methods. (This narrowing word - always reminds me of CORBA :P ). Ok the first time you see this Look up thing, the first time you learn about the LocalHome and Remote Interface etc etc, we all had some sort of revelation- right (or you are too old and too experienced that you cant remember this moment - ok respect).
So, every time you need to access a SLSB lets say, in 1.4 world you were using the service locator pattern usually a helper class to retrieve the actual proxy and not writing duplicate code in your business layer. When j2EE5 came this changed no more JNDI look up no more narrowing - let the application server do it let him inject the reference / interface to your code (yeah nice isn't it?)
But .there is always a but! In an J2EE5 aware framework (as Adam indicates) such as JBoss Seam lets say (more on this on future posts), where we have a web framework very very close integrated with the actually business layer framework, when you want to call a SLSB from the web layer e.g. JSF component .you just inject the damn thing and you are done! Injection even works for plain Servlets - YES you can use these old - full of code Servlets - that they are actually printing HTML on out.println statements (in the era of advanced web frameworks - this is indeed looks old school), and at some point you can inject your business logic. But where's the but
I have made a post earlier this year (or last year actually). Non J2EE5 web frameworks like Struts 1.x and event Struts 2.x, injection does not work! Saaaaaaaaaad .very saaaaaaaad! So you have the all brand new Struts 2.x Actions and at some point you want to call your business layer - ooh yeah lets go back to JNDI, lets conform to the app server naming convention - lets acquire this damn Interface.
It seems with the new internals and specs of J2EE5 any caching tricks of .caching Interfaces so that you don't do so many look ups, can not be applied (am I right or wrong here?).
I really can't get this .thing! You can inject with IoC SLSB in plain servlets but you can't do it in Struts. I don't demand Struts 1.x to be able to do it, but I can't understand why Struts 2 did not make it happen. I have been attending an Intro to Struts 2 session by Don Johnson if I am correct @ JavaOne 2007. During question time, I stood up and asked WHY NO IoC on Struts 2? He said, not yet implemented yes I confirm this is an issue, we are actually looking @ stuff like WebBeans from Gavin King and it's related JSR!
Ohh well. At some point I wanted to go through the code base of Struts and to examine how difficult or easy it is to provide such functionality. I have to admit due to lack of free time still I have not managed to do it - I imagine it would take me some time to understand all the mechanics of Struts and then go through and experiment with such a change.
it's this simple thing, that actually makes Seam to me .even though I really don't fully trust JSF, more and more compelling as an alternative. With an EJB3 enabled business layer it's really not that difficult to remove Struts and glue Seam. Lets see food for thought!
yes yes I love J2EE 5!