Friday Dec 11, 2009

Groovy Advice Needed for Shakespeare Web Service Client

What is the most efficient way to do this? Right now, I've pared my Groovy code down to this:
import groovyx.net.ws.WSClient

class ShakesWsClient {

    String play, speaker, words

    void findQuote(searchString){
        def proxy = new WSClient("http://www.xmlme.com/WSShakespeare.asmx?WSDL", ShakesWsClient.class.classLoader)
        proxy.initialize()
        def speech = new XmlParser().parseText(proxy.GetSpeech(searchString))
        play = speech.PLAY.text()
        speaker = speech.SPEAKER.text()
        words = speech.text()
    }
    
}

That allows me to get at the play, speaker, and text from my Java code as follows:

public class Demo {

    public static void main(String[] args) {
        ShakesWsClient client = new ShakesWsClient();
        client.findQuote("fair is foul");
        System.out.println(client.getPlay());
        System.out.println(client.getSpeaker());
        System.out.println(client.getWords());
    }

}

Ant output:

12 Dec 2009 4:04:10 PM org.apache.cxf.endpoint.dynamic.DynamicClientFactory outputDebug
INFO: Created classes: com.xmlme.webservices.GetSpeech, com.xmlme.webservices.GetSpeechResponse, com.xmlme.webservices.ObjectFactory
12 Dec 2009 4:04:11 PM groovyx.net.ws.AbstractCXFWSClient getBindingOperationInfo
WARNING:  Using SOAP version: 1.1
MACBETH
ALL
Fair is foul, and foul is fair: Hover through the fog and filthy air.
BUILD SUCCESSFUL (total time: 6 seconds)

Anyone out there with suggestions for how to improve my Groovy code (even further)?

By the way, I believe that Groovy's web service support is the best thing about Groovy, especially if you mainly want to continue working in Java.

Searching for Shakespeare on Groovy and the NetBeans Platform

Since writing How to Create a Swing CRUD Application on NetBeans Platform 6.8, I've come to realize that more end-to-end scenarios like that are needed. So here's the next one I'm working on—a Shakespeare quote client. The web service is accessed via Groovy, which is also used to parse the result. All that (two Groovy JARs, plus a JAR containing the Groovy class that defines the interaction with the web service) is in one module. In a second module, the UI is provided, i.e., the window where the search string is entered and sent to the "Groovy" code (because, at compilation, your Groovy is Java):

The result is displayed in a BeanTreeView, using BeanNodes, synchronized with the Properties window. Double-click a node and you bring up a dialog containing the retrieved text, as shown above. There's also a progress bar to avoid the situation where the UI is blocked during the processing of the web service.

private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
   Thread t = new Thread(new WSRunnable());
   t.start();
}

private class WSRunnable implements Runnable {
    @Override
    public void run() {
        ProgressHandle p = ProgressHandleFactory.createHandle(
                "Fetching the Shakespeare quote for " +
                "'" +searchField.getText() + "'");
        p.start();
        QuoteBean bean = new QuoteBean();
        ShakesWsClient client = new ShakesWsClient();
        bean.setName(client.getSpeaker(searchField.getText()));
        bean.setPlay(client.getPlay(searchField.getText()));
        bean.setSpeech(client.getSpeech(searchField.getText()));
        bean.setSearch(searchField.getText());
        content.add(bean);
        p.finish();
    }
}

Above, in bold, is all the code needed to integrate with the progress bar!

And here's the same scenario as above, using an AbstractNode instead of a BeanNode, which gives you more freedom, but also more responsibility:

A full tutorial to describe all of the above will be on NetBeans Zone soon.

In other news. All the code for the above, plus a bit more, is available here on Кеnai: http://kenai.com/projects/shakespeareannotater

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« December 2009 »
SunMonTueWedThuFriSat
  
2
4
5
6
7
9
12
13
16
17
18
19
23
24
25
26
27
28
  
       
Today