How Do I Call Methods On ColdFusion Components In Java

Posted October 9, 2006 at 6:16 PM by Ben Nadel

Tags: ColdFusion

I am experimenting with Java and ColdFusion and I am hitting a brick wall very quickly. As one of my first experiments, I tried to pass a ColdFusion component into a Java wrapper class. The idea was that method calls on the Java object would turn around and call those methods on the ColdFusion component. Now, I can pass in the object no problem. Storing it get's sticky. I am storing it as java.lang.Object since I can't store it as a ColdFusion class (don't have access to those class definitions).

I tried to call Invoke() on the ColdFusion component, but I am not sure how it is called. I tried sending the method name I wanted to call. I tried sending the method pointer that I wanted to call (as returned from the GetMethods() array). Nope, nothing but and ColdFusion errors.

I see that a component is of type coldfusion.runtime.TemplateProxy. Not sure what that is. I have found references to it on other sites, but nothing extensive.

What I want to do is something like this:

  • public java.lang.String GetFirstName(){
  •  
  • // Call this method on the ColdFusion component
  • // object and return that value. This component is
  • // currently stored as java.lang.Object (this.Handler).
  • return(
  • this.Handler.GetFirstName()
  • );
  •  
  • }

As I said, this is not working... Anyone have any ideas?




Reader Comments

Oct 9, 2006 at 10:41 PM // reply »
4 Comments

I did a lot of playing around with this some time ago. First off, what you want to do isn't "really" possible. A CFC is not one java class, but a collection of classes. For an example look under your cfclasses directory (under coldfusion). Find a class in there that looks familiar and you'll see a class for the CFC and one for each method. That's right! A CFC method gets compiled down to one java class.

Furthermore, if you reflect the Template Proxy you'll notice that it has methods on named invoke. I assume these are what CF calls to invoke methods. However, I have no idea what arguments they accept (and I've tried quite a range of options!). Furthermore, this is unsupported and I think it would be a mistake to actually implement due to risk of these APIs changing in future versions of ColdFusion.

For assistance reflecting CF classes use this tool: http://doughughes.net/reflection/index.cfm?event=ReflectClass

It can be downloaded here: http://doughughes.net/includes/reflector/reflection.zip (Requires MG 1.1)

Now, if you're using an old version of CF 7 (I forget the exact version, but before the first big update related by Macromeda. 7.0.1?), you can read a blog entry I wrote that shows a method of using an undocumented feature (for that version - which existed at that time in both CF standard and enterprise) called the CFCProxy to execute methods on CFCs from Java. Here's the entry: http://doughughes.net/index.cfm?event=viewEntry&entryId=122. There's a lot of information in there, but if you look past the technique for embedding java classes in a CFC you'll find a Java class that can call methods on a provided CFC.

Now, the problem is that from what I've been told CF removed the CFC proxy in the standard version (a risk with undocumented features) and supposedly enhanced it for enterprise only use in the first major update after the release of MX 7. So, now (for whatever stupid bloody reason) only MX 7 Enterprise users are allowed to use the CFCProxy. This rather ticks me off, personally. I refuse to use any features that aren't stupidly compelling in CF that are enterprise only features.

Anyhow, good luck with your experiments with Java! This is fun stuff with CF/Java.


Oct 10, 2006 at 8:01 AM // reply »
10,640 Comments

Doug,

Awesome information... and horrible information :) Ok, so no worries. I just need to change the way I handle this stuff. The learning will continue.

I like that reflector object, it looks like it is doing a MUCH better job of something I wrote a while back:

http://www.bennadel.com/index.cfm?dax=blog:206.view

I will keep you updated.


Jul 3, 2007 at 6:59 AM // reply »
2 Comments

You can actually do this using the cfcproxy...

http://www.forta.com/misc/cfcproxy.htm


Jul 3, 2007 at 7:06 AM // reply »
2 Comments

You can actually do this using the cfcproxy...

http://www.forta.com/misc/cfcproxy.htm


Jul 3, 2007 at 7:12 AM // reply »
10,640 Comments

@Nick,

That looks very cool. But, it looks like the CFCProxy object can only be initialized via a file location. Is there a way to use the CFCProxy to invoke arguments on CFCs that were passed into a Java method?

Or am I totally misunderstanding something?


Mar 5, 2009 at 2:11 PM // reply »
33 Comments

Ben

Did you figure this out in the end?

I spent a fair bit of time a while ago calling methods on CFCs from Java in order to write a SAX parser I could use in ColdFusion. That was fairly straight-forward as everything was done in the same request to a CFM. I've now hit a dead-end when trying to call CFC methods from Java outside of a ColdFusion request.

If everything's going to be done on the same request, such as a CFM page creating a CFC and a Java object, then having the Java object call CFML methods in the CFC, I can mail you some code to show you how it's done using Java reflection.

I, however, will carry on investigating how to do it outside of a ColdFusion request...

George.


Mar 5, 2009 at 2:36 PM // reply »
10,640 Comments

@George,

I never got this working. I can easily call Java methods from within ColdFusion. But, I was never able to figure out how to pass a ColdFusion component to Java and then, within a Java class, call a method on the passed-in ColdFusion component. Have you been able to get that to work?


Mar 9, 2009 at 7:03 AM // reply »
33 Comments

Sorry for the late reply... didn't get an email when you posted a response and only just came back to check.

Yes, I managed to call functions inside a CFC from Java. That part is (relatively) straight-forward. I did it a different way to how it's described here, but Ben Forta released some documentation on how it's done using CFCProxy. See this page:
http://www.forta.com/blog/index.cfm?mode=entry&entry=A61BD06B-3048-80A9-EF15B0E3B096BFCD

The downside of using CFCProxy is that you'll need the developer or enterprise version of CF for it to work. If you've got the standard edition running on your production systems, you're on your own. I think my implementation will work even if you're using standard edition as the licence checks are done inside CFCProxy, which I don't use.

The whole topic of calling CF from Java is incredibly poorly supported by Adobe. In that article, Ben mentions that they're going to be putting up some proper documentation on the subject but there's still absolutely nothing on the Adobe site besides the patch notes. Poor show, Adobe.

The technique he describes will also create a new instance of the CFC each time. This may be fine under some circumstances but most of the time I've needed to do this, I need to call methods on a CFC I've already initialised, and now I need to do it outside of a page request as well. I spent nearly four days figuring out how to do this and I've now got it working by mimicking the CFCProxy functionality but providing it an instance of the CFC I've created already. Works fine but it's definately not going to be supported by Adobe, that's for sure.

When I get the time, I'd quite like to write a paper on everything I've researched to get this functionality working and all the bits and pieces I've discovered in the process.


Mar 9, 2009 at 7:06 AM // reply »
33 Comments

Accidentally posted before finishing!

I'll clean the code up a little this week and email it over to you so you can see how to call CFC methods from Java on an existing CFC from inside a request.


Mar 10, 2009 at 8:50 AM // reply »
10,640 Comments

@George,

Thanks for pointing me to the CFCProxy API. At least it gives some insight into all the casting that might need to be done. I am looking forward to seeing your work.


Mar 13, 2009 at 2:48 PM // reply »
1 Comments

I'm a ColdFusion newbie and my Java experience isn't up to Doug's. I need to call a CFC from an applet.

As I understand it, CFCProxy requires that the Java program calling the CFC must be running in the ColdFusion server's JVM? Obviously that wouldn't work for an applet.

If that's the case, perhaps I should fall back to WSDL or even AJAX calls?


Mar 15, 2009 at 8:37 AM // reply »
33 Comments

Eric,

For the most part, CFCProxy does indeed need to be running in ColdFusion's JVM.

I'd use web services in your case. Very, very easy to set up from your CF code and easy to call, too.

George.


Apr 15, 2009 at 12:04 PM // reply »
1 Comments

Hello George,

You mention in your post: I'll clean the code up a little this week and email it over to you so you can see how to call CFC methods from Java on an existing CFC from inside a request.

Is it possible to have a copy of the code you mention?
We would like to call CFC methods from external Java files too.

Regards,
Zoltan


Dec 2, 2009 at 9:57 AM // reply »
10,640 Comments

I know this post is over THREE years cold, but if anyone cares, I figured out how to do this in Groovy:

http://www.bennadel.com/blog/1769-Calling-ColdFusion-Components-And-Methods-From-Groovy.htm


Apr 18, 2011 at 2:44 AM // reply »
1 Comments

It was a awe-inspiring post and it has a significant meaning and thanks for sharing the information.Would love to read your next post too......
Thanks


Sri
Nov 4, 2011 at 6:28 AM // reply »
1 Comments

Good posting...I always waiting snippet code to calling methods on CFCs from Java....:)



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »