[Looking for Charlie's main web site?]

CF8 Hidden Gem: New option to save java source for web service proxy--with createobject only

Have you ever wanted to see the Java source code for the proxy/stub that's created when you invoke a web service from Coldfusion? Well, here's a hidden gem in CF8 (one of dozens I discuss in my "hidden gems in cf8" talk) that does just this.

Curiously, it's only available when you invoke a web service using createObject(), not CFOBJECT or CFINVOKE.

It's enabled using the new ArgStruct argument that I discussed last month.

<cfscript>
wsargs = structnew();
wsargs.savejava="yes";

convert=createobject("webservice","http://www.webservicex.net/CurrencyConvertor.asmx?wsdl",wsargs);

writeoutput(convert.ConversionRate(FromCurrency='USD',ToCurrency='EUR'));
</cfscript>

You may wonder why you have to put it in this argStruct when it's the only key being put in the structure. That's just the way it is. Of course, I could have created the structure using the new implicit array creation syntax, as in:

wsargs = {savejava="yes"};

which replaces 2 lines with 1.

For those who don't care for CFSCRIPT

Of course, you don't need to use CFSCRIPT to use createObject, for those not comfortable with it. I could just as well have done it all in tags, as:

<cfset wsargs = structnew()>
<cfset wsargs.savejava="yes">

<cfset convert=createobject("webservice","http://www.webservicex.net/CurrencyConvertor.asmx?wsdl",wsargs)>

<cfoutput>var="#convert.ConversionRate(FromCurrency='USD',ToCurrency='EUR')#</cfoutput>

Where the Java source is placed

So where is the Java placed? In the same directory where the java proxy stubs have been placed since CF6: [coldfusion]/stubs/. In the case of the standalone version of CF8, that might be c:\coldfusion8\stubs.

Each invocation of a web service in CF (whether you use the saveJava option or not) will create a directory there, typically in a form like WS729914123 (one for each separate web service invoked by any CFML requests), and within the subdirectories of that directory you'll find class files reflecting the name of the called web service.

If you don't use the saveJava option, you'll see only class files. If you'll see corresponding .java source files for each.

Finally, note that the Java source files will be removed automatically if the web service is refreshed (manually or in the CF Admin) and you call it without the SaveJava option (which also means if you invoke it using CFOBJECT or CFINVOKE).

(*Update*: In the original entry, I said the source would be removed if you called the web service without the SaveJava option, but I should clarify that it's if you do that and you cause the web service to be refreshed, not just any call, since that would use the compiled result of the earlier call unless you told it to do otherwise.)

Still, for those who have long wished to better understand these Java proxy stubs, it's nice that we have the option to see the source if we want to.

Still more to come

There's still more power in CF8 for those that like to tinker with the java proxy/stub generation. More on that in a later entry.

Comments
You can actually do this in CF6/7 as well. If CF is unable to compile the java files due to an error then you can browse to the stubs folder and view the java files to find out why. So all you need to do is cause an error in the web service invocation on purpose and the java files will not compile, so you will be left with the java files and not the class files.
So you could for example miss out a required argument, and coldfusion will generate the proxy stubs for the web service, but will throw an error and will not compile them.
# Posted By Russ Michaels | 10/11/07 7:17 PM
If you are on 6.1 or 7 you can use the wsdl2java utility. There are actually a couple of TechNotes on how to do this:

http://www.adobe.com...
http://www.adobe.com...
# Posted By Nathan Mische | 10/12/07 9:11 AM
Extremely cool trick Charlie. I am not sure most ColdFusion developers understand what happens under the hood in CF to make web service calls possible. ColdFusion developers can look at this stub code to help debug their web service calls.

Thanks for the tip,
David Fekke.
# Posted By David Fekke | 10/12/07 9:15 AM
@Russ, I don't find that to be the case in either 7 or 8, whether using CFINVOKE or createobject. Are you stating that observation from confirmed experience (can you do it right now?) or more just from a recollection? It could certainly be useful for folks on 6 or 7 if it's indeed so, though creating errors to cause side-effects is certainly less desirable if there are other solutions (such as with this in CF8). :-)

@Nathan, I think what you were meaning to tell people is that you can use the Wsdl2Java utility (rather than needing to rely on this option) to cause CF to save the Java source files, and that's a fair point. I was actually alluding to that in my final statement above, about "There's still more power in CF8 for those that like to tinker with the java proxy/stub generation. More on that in a later entry." :-) But I wouldn't have mentioned that it was an alternative to this savejava option, so thanks for sharing the links.

@David, thanks for the kind regards. Always appreciated.
# Posted By Charlie Arehart | 10/12/07 11:03 AM
Charlie,

I can confirm what Russ has stated for CFMX 7.0.2. If CF encounters a Java compilation error of the WSDL stubs it will leave the java source code behind. Ran into this while working with grants.gov webservices and ColdFusion.

Still, very good tip for CF8 w/o having to rely on an error to get the java source. Thanks!
# Posted By David Epler | 10/12/07 11:25 AM
Thanks, David. So you say that it has to encounter a compilation error. Ok. It's just that Russ had said, "So you could for example miss out a required argument, and coldfusion will generate the proxy stubs for the web service, but will throw an error and will not compile them."

And I tried that and didn't get the source, in either 7 or 8.

Since he's proposing it as a way to force it in 6 or 7, it would be useful for us to clarify for people exactly what they'd need to do to be able to cause such an error from the calling end. Anyone want to show how my example above (without the use of the argstruct, of course) could be changed to force such an error to save the java. That could be useful to some. Thanks.
# Posted By Charlie Arehart | 10/12/07 11:40 AM
@Charlie - That's exactly what I wanted to say :-) If you are using CF 6 or 7 and want to see the source code for the Java proxy (something that can be extremely useful when debugging webservice issues) then you can use the wsdl2java utility. Instructions on using that utility can be found in the referenced TechNotes. Sorry if that wasn't clear.
# Posted By Nathan Mische | 10/12/07 2:22 PM
So what would be to process for re-compiling the souce manually if pre say you wanted to make a change? Sometimes working with microsoft, their web services tend not to work out of the box in a java environment. Would you just manually runt the javac command to replace the auto-generated classes?
# Posted By Jay | 1/12/08 7:26 PM
Hi Jay, I would suppose so. I'll admit that I have not done it myself. My goal in the blog entry was to focus on the hidden gem in CF8 of being able to get the source easily. Perhaps others subscribed to this thread may chip in with more (if indeed there's anything more to be said). Cheers.
# Posted By Charlie Arehart | 1/14/08 12:30 AM
Copyright ©2018 Charlie Arehart
Carehart Logo
BlogCFC was created by Raymond Camden. This blog is running version 5.005.
(Want to validate the html in this page?)

Managed Hosting Services provided by
Managed Dedicated Hosting