Tech Chat: Mark Mandel on ColdFusion and Java Integration
DZone recently caught up with Mark Mandel, creator of JavaLoader, a library that facilitates ease of use, development and integration of Java within ColdFusion applications as well as Transfer, a ColdFusion object-relational mapping library. In this interview, recorded at Adobe MAX 2009, Mark describes some of the challenges associated with integrating Java and ColdFusion components -- JavaLoader provides several mechanisms for enabling this, including a Java dynamic proxy for ColdFusion components as well as the ability to dynamically load Java libraries without having to place them on the ColdFusion classpath.
A transcript of the interview has been provided below.
DZone: Can you tell us a little bit about the some of the work you're currently doing?
Mark: I'm doing a lot of ColdFusion development mainly. I do some Java integration stuff as well but generally speaking it's a lot of ColdFusion development. I also help out a whole bunch of different software enterprises; helping them with building better software, and techniques and methodologies for doing that sort of stuff. Helping them learn to do different things.
DZone: How did you get involved with ColdFusion?
Mark: I kind of fell into ColdFusion actually. It was really quite random. It wasn't a conscious choice. Back in University it was, around '99, 2000. I was working for an e-commerce company actually at the time doing customer service of all things. I became friends with one of the guys who was doing logistics there. He was like, "I've got an idea for a Website" and blah, blah, blah. Everyone had an idea for a dot com back then.
I was like, "Oh that sounds like a really good idea". I actually got together with a friend of mine and I said, "Let's help this guy out and see what happens". He said, "I reckon we'll do it, we'll do it with ColdFusion". I didn't know anything about it yet. He had one of those big web application construction kits. I was like, "Oh this is a really cool language".
Strangely enough, being the 90's and early 2000's that didn't go anywhere. I just really liked the language. I think about a year later, I started with small start up with another guy which again never really went anywhere. We did all right for a little while but that was all ColdFusion as well. We ended up building a project.
While I was at University I kept doing ColdFusion work. I was in a course that was fairly free form in what I was doing. It was actually a multimedia degree. They gave us a lot of leeway to kind of push stuff where we were. I ended up building several ColdFusion applications at university and then continued doing full time work as a ColdFusion developer and then started freelancing a year and half ago and yeah, I've been doing it ever since.
DZone: What options are there today for developers looking to integrate Java and ColdFusion applications?
Mark: Java and ColdFusion go together very, very, very well together. ColdFusion is essentially a J2EE application. It sits on top of a J2EE server at its core level. Although ColdFusion developers probably don't even know that or don't even need to know that, which is all a good thing. But if you are looking to integrate Java with ColdFusion at a base level it's actually pretty straight forward. You can do some stuff where you can actually basically load up any custom Java code in ColdFusion when ColdFusion starts up.
The annoying thing there is yes, you have to restart the server or you actually need to stick it on the classpath in the loop holder of ColdFusion which you may or may not have access to.
If you've actually got that happening, you can essentially write, I wouldn't say inline Java, but you can write ColdFusion code that calls directly upon the Java objects. It's very, very simple. It's like doing basic, normal ColdFusion code.
If you want to get a little bit more fancy with it, I've actually got a project called JavaLoader that's been around a few years -- I recently released a 1.0 version. I finally decided, "Hey I'll give it that 1.0 tag".
[JavaLoader] enables you to give you a bit more flexibility with loading up your own Java libraries. Basically it uses a class loader for those who know what that is.
It gives you the ability to say, "OK that library over there, I've got it on my own file, my own directory wherever I want it. Let's load it up and I'm going to start creating Java objects and interacting with them like I would normally using ColdFusion". It gives you a really nice flexible environment to work with Java and ColdFusion quite nicely. There are some interesting things there as well.
So yeah, talking from ColdFusion to Java is very easy. Talking from Java to ColdFusion is a little bit more tricky but JavaLoader gives you some ways around that as well.
DZone: What are some of the challenges? You mentioned it's a little tricky going the other way. What are some of the challenges associated with Java to ColdFusion.
Mark: Essentially there are a few challenges here and there; some of them more technical than some of the other ones. Yes, talking from ColdFusion to Java is very simple. I can get into details into how it all works but that's all fine.
DZone: Would a Java developer ever need to call a ColdFusion application or component?
Mark: Well, actually I would say to a degree yes. It's out of the power that I think ColdFusion has as a language. It's also the power ColdFusion has as a service. ColdFusion has a lot of really nice integrated stuff built into it. I mean if you're looking at PDF manipulation, or if you want to do real easy image manipulation.
Some of the encryption stuff that they've got in there, it's a lot cheaper to buy ColdFusion licenses than it would be to buy some really high end encryption licenses.
One of the classic examples, I actually do in my presentation, where it's really easy to send an email because it's a tag-based language. You actually, in ColdFusion 9 you can write in script any tags but for doing stuff like HTML output or say output of an email, it's real easy to just write it like you would in a text file essentially and wrap a CF mail tag around it. Then be like, "Oh now it's my email and it's sent".
If you wanted to say, have your model written out in Java so you've got a whole bunch of different Java objects representing your business layer and all that stuff. You're like, but I want to send an email and I just want to be able to do it like a template or I want to do like a templating instrument or anything like that.
They're like, "Oh, for that I want ColdFusion because that's easy" or I want to be like, "I just want to translate some HTML straight into PDF". Well I'll just write my HTML on a page and then stick a CF document tag around it and all of a sudden "Hey, I've got my PDF document" and I can do nice stuff that way.
There are definitely things that are available in ColdFusion that you're like, "Oh this will save me a lot of time. I don't have to go write high tech stuff so that I can create my PDF". Especially when you look at CF9's office integration where you can generate Excel files and Word files and all this stuff really, really easily. So there's some cool stuff in there.
DZone: For the benefit of some of our ColdFusion developers who may be watching this interview, can you maybe describe the whole notion of object-relational mapping and how it's now supported in ColdFusion 9? Also, in releases prior to the CF9, how were ORM use cases handled?
Mark: Object-relational mapping at its purest level is basically taking the disconnect between relational databases where you've got primary key-foreign key relationships, you've got tables and software where often you have objects that are in memory and they relate to one another. So an object-relational mapper's job is essentially simply taking those objects, being able to push them into the database so they're stored in the database, and also being able to pull them back out again.
So it enables you to do that sort of persistence by saying, OK, I've got, for example, a car and I might want to keep that copy of that car in the database but I also want to interact with it as an object without having to write all that code for yourself. It's normally some sort of configuration.
You set that up and you go, "Oh, object-relational mapper, can I please have a car?" It says, "Here's a car."
"I'm going to set it to being a Toyota, and I'm going to save it back." And the object-relational mapper's job is to say, "OK, I'm going to put that back in the database or pull that back out again." That's pretty much what an object-relational mapper does.
Prior to CF9 there were two or three object-relational mappers that were available in the ColdFusion space. It was actually interesting, back in the day when object-relational mappers came on scene with ColdFusion, there was about two or three of us that were all kind of working on them at the same time but none of us knew the other ones were doing it.
The first one, I think, that hit the scene was one actually called Reactor that came out I think it was maybe about a month before mine did. There was one actually that showed up and then disappeared. There was one called Arf that was an ActiveRecord patent that Joe Rinehart wrote that disappeared fairly quickly. It was like, "Ah, there's a couple out now. I'll leave it alone." And I was working one called Transfer that I was writing at the same time. It's actually funny how that came about. I basically started writing it because the company I was working for at that given point in time had a really strict and stringent documentation policy. So every self-replication you're writing, you're writing 10, 15 pages of documentation.
I was really interested in persistence libraries coming out of university. I'd done some stuff with EJB, the early EJB, and I was like, "OK, this is kind of cool. This is interesting. I can see how this would really save time as a software developer."
So I wrote some really bad implementations and some persistence frameworks and decided to start one all over again with Transfer. I ended up coming up with that basically because what I wanted to do was be able to say in my documentation, instead of writing five pages, I could be like, "That's what I'm working on. It's over there. All the documentation is all written as a community, " and all that sort of stuff. So that made it really easy.
And yet, it kind of just grew organically from there. I mean it was something that I was like, "This is a really cool idea. I think I'm just going to go for it and see what happens." It's one of those software projects, there's things you might want to change later and do other stuff.
But it's been doing really, really well, everyone's really been enjoying it. I've been wandering around conferences talking about it and what not. It's been a fantastic project, a fantastic learning experience, and it's grown a really nice community. I mean on the mailing list it's somewhere between 400 and 500 people sitting on the mailing list and for an accomplishment it's pretty good. Can't complain about that.
DZone: When would I use the built-in Hibernate support in CF9 versus TranferORM?
Mark: The CF9 Hibernate stuff is very, very, very slick. They've got integration with the IDE itself and the integration that they've done with Hibernate...look, Hibernate is a really fantastic thing...it's been around for a long time, it's been proven, it's really fast, it does a whole bunch of stuff that unfortunately Transfer doesn't do because it hasn't been around as long and there's some limitations in what can be done there. But yeah, they've done a really, really great job. It's easy enough to get into that you can kind of pick up and run with it but it exposes a lot of the functionality that Hibernate has so that if you have experience with Hibernate you can really extend and really build on it and really get into some of the nitty gritties.
You can get really low level with the Hibernate stuff, but if you don't want to do that, you're just like, "Oh, I'll just grab an object and I'll stick in the database and I'll pull it back out again, " and everyone's happy. So the Hibernate nine stuff, they've done a really, really good job. I've been very impressed.
DZone: So Mark, you recently made available the 1.0 Alpha release of JavaLoader. Can you tell us a little bit about what's new in this release?
Mark: When I first released JavaLoader it was really about one thing. You had a Java library that you had on your file path, and you wanted to be able to load that and interact with it. And that did it really well, and it sat, I think I called it 0.6 for a random version number for awhile. With this release I wanted to do a few more interesting things. One of the things I wanted to do was take advantage of the dynamic compilation stuff that was available in Java 1.6 to ease the Java development cycle essentially from within an IDE and then exporting a JAR file and then putting it on the server and doing all that sort of stuff.
I wanted to be able to enable a ColdFusion developer or Java developer to essentially do what you do very much in ColdFusion, which is you make some changes, you hit refresh and all of a sudden it's there. So it has that sort of stuff in it as well.
The other thing I wanted to expose was some better ability to be able to integrate ColdFusion and Java itself, more so to the point of being able to enable Java to talk to ColdFusion in an easier way. And one of the ways I wanted to do that was actually build dynamic proxies for ColdFusion components, which are the actual objects inside ColdFusion.
So that way if you are working in Java, either within ColdFusion or otherwise, you have the ability to leverage the services and the capability of what ColdFusion can bring for you, at the same time still having the incredibly extensible nature of Java and all the libraries that are out there already. So you can have a really nice integration of the two levels.
DZone: How exactly does the ColdFusion dynamic proxy work in JavaLoader?
Mark: Essentially the main purpose with the dynamic proxy in JavaLoader, and for anyone who's used dynamic proxies, at a core level really what they do is they're a way to mimic other objects for all intents and purposes. So if you set up a Java object and you're like, "Oh, this object is a..." I'm trying to think of a good object. Say like a runnable object that you could run in a thread for example. You tell it, "Your runnable object, as far as Java is concerned, doesn't know anything different."
Now, a dynamic proxy give you the power to, essentially when somebody calls a Java object, you're able to intercept that and go off and do anything you want with it. So the nice thing we're able to do with the ColdFusion component dynamic proxy is hide a ColdFusion component from whatever Java component you're using, and Java code essentially thinks its interacting with a Java object -- it doesn't know any different and it's really, really handy that way. So you can do some really cool things that you couldn't necessarily do before.
A classic example there is let's say you wanted to set up a ThreadPool in your Java code, you could now tell it that this ColdFusion component is now runnable, because its a runnable interface, so I'm going to say, "Run that," and it runs within that thread pool.
If you want to do neat stuff in Java, there's in interface for things called Comparitors. So you can set up a Comparitor to sort an array of things. The Comparitor has a method on it that says, "Here's two objects, if one's bigger than the other one, return 1, if one's less than the other one, return -1. So you can say sort an array of ColdFusion components," because, essentially, behind the scenes, everything's a ColdFusion component anyways.
You can start leveraging that sort of capability in Java because its thinking that its interacting with a Java object. It doesn't know any different. So the possibilities of Java and ColdFusion interaction are really, really cool. You can even start doing stuff say, if you're interacting with caching libraries. If you want to be told when a Java caching library expires an objec -- normally they have events that you can listen to -- normally, you'd have to write Java code that could help you do that. Using dynamic proxies, you can take advantage of the fact that it doesn't know any different. So it goes, "Oh it's an object." It thinks it's sending it to a Java object and the Java object goes, "Ah, no, I'm actually a ColdFusion component," and then you're running ColdFusion code behind the scenes. So you can do some interesting things.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)