The Mind Electric's GLUE product is a Web service toolkit for Java programmers that offers an easy way to implement SOAP messaging. You can use GLUE's internal Web server to publish your Web service, but GLUE can also be run as a servlet inside your existing application server. Let's take a look at how you can build a Web services server and client using GLUE.
The GLUE server
Almost any Java class can be exposed as a Web service server with GLUE. I recommend using a design pattern where a facade class calls methods on the business logic Java class you're trying to expose. The facade class should contain only the public methods you want to expose to your SOAP clients. You can use GLUE to publish your facade class as a SOAP service. For this example, I won't use a facade class—for simplicity, I'll use the business logic class as the Web service. The facade class would contain only a method that called the echo method on our business logic class.
The server has two parts: a Java class that performs the business logic and a Java class that starts GLUE's HTTP Server and publishes the business logic object as a SOAP service. The business logic class is named StringEcho, and the publisher class is named EchoServer. Both are in the default package.
The business logic class in this example will be as simple as possible—just a public method on a class that takes a string as an argument and returns the same string to its caller. The method is called echo, and it has one argument, a String called test. The code doesn't need to reference any GLUE classes to be published in a GLUE server. The source code for the business logic class can be found in Listing A.
The server class makes two method calls from its main method. Invoking the startup method on the HTTP class will start a Web server inside the Java process that listens on a port for SOAP requests to services published with GLUE. It takes a URL as an argument to determine what port and path to use. For example, the server uses http://localhost:8080/glue.
Use the GLUE registry to expose your Java objects to the outside world. The publish method takes the name of your Web service and the object you would like published. All public methods are made available to clients.
After you run the EchoServer class from the command line, the WSDL for the new SOAP Service is available at http://localhost:8080/glue/EchoServer.wsdl. Listing B contains the server class source code.
The GLUE client
You can use GLUE to write a client that talks to the SOAP service you just wrote. When you create the simplest type of GLUE Web services client, you will need to also use a Java interface. This interface has the same method names and arguments as the object you are binding to on the server.
The interface's Java class source file can be created with GLUE's wsdl2java utility. If you have GLUE installed, this utility can be found in your /electric/bin directory.
Running wsdl2java http://localhost:8080/glue/EchoServer.wsdl generates two Java source files, IStringEcho.java, and StringEchoHelper.java. The IStringEcho.java file is the interface, and StringEchoHelper.java is a helper class that you can use to make GLUE bind to your service.
The file IStringEcho.java is shown in Listing C; Listing D contains the source code for StringEchoHelper.java.
The bind method takes a WSDL URL and a Java interface as its arguments and returns an object that implements the interface. You'll have to cast this object back to the interface you gave the method. You can then call Java methods on this object as if it were a local Java class, even though the actual server could be written in .NET, Perl, or Java. GLUE hides most of the complexity behind SOAP from you, but you can certainly use lower level APIs if you want.
Our client class calls the bind method on the StringEchoHelper class that was automatically generated by GLUE. The bind method returns an object that implements the IStringEcho interface, and you can call the echo method on that object directly. The client code is shown in Listing E.
Let it work for you
Although the example developed in this article is simple, it does provide a glimpse of the power provided by GLUE. Download GLUE and take a closer look at how it can help you with your next project.