Compile and invoke your webservice client (generated by XFire) in a running JVM

0

Generating XFire client files isn’t that special, but with Java 6 it is possible to compile those files immediately and invoke the webservice within one application. When I told this idea to a colleague he was a bit worried, because there are tools out there that can do this for you. But it’s just an exercise. I don’t play enough with XFire and testing new Java 6 features is also something that’s on my list for a very long time.
What I want to do is provide a WSDL url, generate the client files, compile them and invoke a method on that client. Of course without any manual steps!

In this blog we take the following steps:

  • dissect the XFire command line WsGen application
  • generate the client files
  • compile the client files
  • invoke the web service

....

Dissecting WsGen

WsGen is the class you invoke when client files need to be generated from a WSDL. A detailed description of WsGen and the command parameters can be found here. I wanted to generate the files from within my application (without doing a Runtime.exec), so I had to find out how the XFire guys did it. Luckily XFire is open source and their subversion repository is browsable. What WsGen basically does is checking the command line parameters, returning errors or print a help message. Well that’s just what we don’t need for now, let’s throw that away. After removing that code this is the code we need:

<code><br /><font color="#000000">Wsdl11Generator&nbsp;generator&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Wsdl11Generator</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">generator.setOutputDirectory</font><font color="#000000">(</font><font color="#000000">outputDirectory</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">generator.setWsdl</font><font color="#000000">(</font><font color="#000000">wsdl</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">generator.setExplicitAnnotation</font><font color="#000000">(</font><font color="#000000">explicit</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">generator.setOverwrite</font><font color="#000000">(</font><font color="#000000">overwrite</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">generator.setGenerateServerStubs</font><font color="#000000">(</font><font color="#000000">serverStubs</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#7f0055"><strong>try&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">generator.generate</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>catch&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Exception&nbsp;e</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>throw&nbsp;new&nbsp;</strong></font><font color="#000000">BuildException</font><font color="#000000">(</font><font color="#000000">e</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">}</font></code>

The overwrite, explicitAnnotation and generateServerStubs are not needed for this example, so we can set them to false. That leaves us with the output location and the WSDL url.

Generate the client files

When we run this piece of code generation code it generates a list of files deep inside some directories. Since our compiler needs every file (with its location) we have to list all the files. Now I can show you something I learned in school (yes that happens and I think it is so special that I wanted to tell it). When things with trees (a directory tree) need to be done you can do it the difficult way or the recursive way. Let’s do it the recursive way. I have a List of File objects that needs to be filled recursively:

<code><br /><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">listFilesRecursive</font><font color="#000000">(</font><font color="#000000">List&lt;File&gt;&nbsp;fileList,&nbsp;</font><font color="#7f0055"><strong>final&nbsp;</strong></font><font color="#000000">File&nbsp;dir</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">File</font><font color="#000000">[]&nbsp;</font><font color="#000000">files&nbsp;=&nbsp;dir.listFiles</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">File&nbsp;f&nbsp;:&nbsp;files</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">f.isDirectory</font><font color="#000000">())&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">listFilesRecursive</font><font color="#000000">(</font><font color="#000000">fileList,&nbsp;f</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>else&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">f.getName</font><font color="#000000">()</font><font color="#000000">.endsWith</font><font color="#000000">(</font><font color="#2a00ff">&quot;.java&quot;</font><font color="#000000">))&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">fileList.add</font><font color="#000000">(</font><font color="#000000">f</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br /><font color="#000000">}</font></code>

The function calls itself when it finds a directory and the stop-condition is a file that is not a directory, when that file name ends with .java we add it to fileList and otherwise nothing happens.

Compile the client files

The list of files must be compiled in the next step. I found this great blog entry about compiling files in Java 6.
However, one very important part is missing. When you watch closely you see that the cl variable is not defined. This is where things can get horribly wrong. I got so stuck that I almost gave up. The Java class loader is a strange thing. There actually is a whole hierarchy that defines which class loader is loading your files. When you compiled your classes with Class Loader Y and you want to load them with Class Loader X it might not happen. It’s very complicated and I couldn’t find much information about the class loaders.

After adding the class loader
an
d my listFilesRecursive method to the Juixe code I ended up with this piece of code:

<code><font color="#000000">JavaCompiler&nbsp;jc&nbsp;=&nbsp;ToolProvider.getSystemJavaCompiler</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">ClassLoader&nbsp;cl&nbsp;=&nbsp;Thread.currentThread</font><font color="#000000">()</font><font color="#000000">.getContextClassLoader</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">StandardJavaFileManager&nbsp;sjfm&nbsp;=&nbsp;jc.getStandardFileManager</font><font color="#000000">(</font><font color="#000000">null,&nbsp;null,&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#000000">File&nbsp;dir&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">File</font><font color="#000000">(</font><font color="#000000">filesLocation</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#000000">List&lt;File&gt;&nbsp;files&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">ArrayList&lt;File&gt;</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">listFilesRecursive</font><font color="#000000">(</font><font color="#000000">files,&nbsp;dir</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#000000">Iterable&lt;?&nbsp;</font><font color="#7f0055"><strong>extends&nbsp;</strong></font><font color="#000000">JavaFileObject&gt;&nbsp;fileObjects&nbsp;=&nbsp;sjfm.getJavaFileObjects</font><font color="#000000">(</font><font color="#000000">files.toArray</font><font color="#000000">(</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">File</font><font color="#000000">[]{}))</font><font color="#000000">;</font><br /><font color="#000000">jc.getTask</font><font color="#000000">(</font><font color="#000000">null,&nbsp;sjfm,&nbsp;null,&nbsp;null,&nbsp;null,&nbsp;fileObjects</font><font color="#000000">)</font><font color="#000000">.call</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">sjfm.close</font><font color="#000000">()</font></code>

Invoking the Web Service

This last step is a bit strange because I haven’t really thought about what I want to do now. A possible idea is to extract all the methods from the webservice and display the input parameters on a form on a web page. This is a lot of work and I’m already creating an application that already exists.

I make use of a publicly available web service. The WSDL can be found here.

When you access a web service you invoke a method on a Client object. What we know from XFire is that a the class name of the client ends with Client. So we look up the client class and make an instance of that object:

<code><br /><font color="#000000">String&nbsp;cn&nbsp;=&nbsp;generator.getClientClassName</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">Object&nbsp;client&nbsp;=&nbsp;Class.forName</font><font color="#000000">(</font><font color="#000000">cn</font><font color="#000000">)</font><font color="#000000">.newInstance</font><font color="#000000">()</font><font color="#000000">;</font></code>

getClientClassName is a method that searches for the client class. The client class has a method called getEndpoints. This method must be invoked to receive a Collection with end points:

<code><br /><font color="#000000">Class&nbsp;clientClass&nbsp;=&nbsp;client.getClass</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">Method&nbsp;method&nbsp;=&nbsp;clientClass.getMethod</font><font color="#000000">(</font><font color="#2a00ff">&quot;getEndpoints&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">Collection&lt;Endpoint&gt;&nbsp;col&nbsp;=&nbsp;</font><font color="#000000">(</font><font color="#000000">Collection</font><font color="#000000">)&nbsp;</font><font color="#000000">method.invoke</font><font color="#000000">(</font><font color="#000000">client</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&quot;End&nbsp;points:&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Endpoint&nbsp;e&nbsp;:&nbsp;col</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp; </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&quot;&nbsp;&nbsp;&quot;&nbsp;</font><font color="#000000">+&nbsp;e.getName</font><font color="#000000">())</font><font color="#000000">;</font><br /><font color="#000000">}</font></code>

The web services I tried all returned an end points that ends with LocalEndPoint and another one, we need the other one. This method/endpoint needs to be invoked:

<code><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Method&nbsp;call&nbsp;=&nbsp;clientClass.getMethod</font><font color="#000000">(</font><font color="#2a00ff">&quot;getMidnightTraderSoap&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">Object&nbsp;o&nbsp;=&nbsp;call.invoke</font><font color="#000000">(</font><font color="#000000">client</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">o</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">Method</font><font color="#000000">[]&nbsp;</font><font color="#000000">methods&nbsp;=&nbsp;o.getClass</font><font color="#000000">()</font><font color="#000000">.getMethods</font><font color="#000000">()</font><font color="#000000">;</font><br /><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Method&nbsp;method1&nbsp;:&nbsp;methods</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#2a00ff">&quot;&nbsp;&nbsp;&nbsp;:&quot;&nbsp;</font><font color="#000000">+&nbsp;method1</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font></code>

From this method we need to get the correct method(s). Let’s remove the methods that belong to java.lang.Object and the methods that do something with java.lang.reflect are also not needed
We end up with the following list:

&nbsp;&nbsp; :public final com.strikeiron.ServiceInfoOutput $Proxy21.getServiceInfo(com.strikeiron.ws.LicenseInfo,javax.xml.ws.Holder)<br />&nbsp;&nbsp; :public final void $Proxy21.getRemainingHits(com.strikeiron.ws.LicenseInfo)<br />&nbsp;&nbsp; :public final com.strikeiron.StatusCodeOutput $Proxy21.getAllStatuses(com.strikeiron.ws.LicenseInfo,javax.xml.ws.Holder)<br />&nbsp;&nbsp; :public final com.strikeiron.MidnightTraderOutput<br />$Proxy21.lookupFinancialNews(com.strikeiron.MidnightTraderRequest,com.strikeiron.ws.LicenseInfo,javax.xml.ws.Holder)

These methods

can also be invoked the way I described earlier, you just have to decide where to get your parameters from and what you want to do with the result.

Conclusion

At first sight it seems a waste of time to generate something that already exists. But these kind of exercises give a good insight in what is possible with Java 6 and I learned a lot about how web services work. I also think it’s really cool that you can make changes to the classes in your JVM. It’s like changing the wheels of your car when you run at full speed on a motor way.

Sources

http://xfire.codehaus.org/Client+and+Server+Stub+Generation+from+WSDL
http://fisheye.codehaus.org/browse/~raw,r=2183/xfire/trunk/xfire/xfire-generator/src/main/org/codehaus/xfire/gen/WsGen.java
http://www.juixe.com/techknow/index.php/2006/12/13/java-se-6-compiler-api/

Share.

About Author

Comments are closed.