Tuesday, November 3, 2009

JavaLoader 1.0 / CFCDynamicProxy (I Think I am in Love)

So after many weeks, I finally got a chance to work with Mark Mandel's JavaLoader 1.0 Beta today, and boy did it put a smile on my face. When I started reading the documentation on the Dynamic Proxy, I nearly fell out of my chair.

...And there on the other side, read the following words ...

In JavaLoader 1.0, I've written a Dynamic Proxy that you are able to wrap around a CFC, and thereby make Java Objects think they are interacting with a native Java object, but are in fact, talking to your CFC.

Holy cow! If you do any interaction with java from CF (or ever wanted to) I think you can appreciate this phenomenally cool feature. It means you can hook directly into a java library, from a CFC. No need to write and compile a custom java class. Now obviously it only applies to interfaces. But hey, most good libraries "program to the interface and not the implementation" anyway. So this opens up a lot of possibilities.

A good example is adding custom headers and footers with iText. (See this previous entry some background on page events). If you were using java, you would create a custom class that implements the PdfPageEvent interface. By implementing that interface, the custom class can capture certain pdf events like document opened, page added, etcetera. Then the code inside the custom class adds the desired headers, footers, watermarks, etcetera as those events are detected.

Now that is easy enough to do from java. But until now, it was not possible with ColdFusion alone, because iText requires an instance of a java class to handle the page events. Prior to now there were work-arounds, but none as elegant as using the Dynamic Proxy. With the new Proxy, you can simply write the event handler in CF. Then wrap it up in a proxy object and pass the proxy into iText. iText will think it is dealing with a real java class and simply route the events to your cfc as usual.

In a relatively short period of time, I was able to greatly simplify some old code for adding headers and footers to pdf documents using iText. I will post it shortly just so you can see how easy it was to interact with iText using the new Dynamic Proxy. I have to say it feels a lot more natural, and I am seeing endless possibilities here.

I said it before and I will say it again. That Mark Mandel is a freak...'in genius.


Mark Mandel November 3, 2009 at 5:00 PM  

Well... * blush * that just made my day :D

Glad you are enjoying using this!

cfSearching November 3, 2009 at 6:07 PM  


I am glad, because it _totally_ made mine! No more hoop jumping. Great job :)


  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep