CF8 Hidden Gem: New option to save java source for web service proxy--with createobject only
Note: This blog post is from 2007. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.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.
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:
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.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.
For more content like this from Charlie Arehart:Need more help with problems?
- Signup to get his blog posts by email:
- Follow his blog RSS feed
- View the rest of his blog posts
- View his blog posts on the Adobe CF portal
- If you may prefer direct help, rather than digging around here/elsewhere or via comments, he can help via his online consulting services
- See that page for more on how he can help a) over the web, safely and securely, b) usually very quickly, c) teaching you along the way, and d) with satisfaction guaranteed
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.
http://www.adobe.com...
http://www.adobe.com...
Thanks for the tip,
David Fekke.
@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.
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!
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.