Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Clean up your wire protocol with SOAP, Part 4

Dynamic proxies make Apache SOAP client development easy

  • Print
  • Feedback
Version 1.3 of the Java 2 Platform features an extremely savvy addition to the Java Reflection API: dynamic proxy classes. A dynamic proxy class is a class that implements a list of interfaces specified at runtime. This proxy can then be used as if it actually implemented these interfaces. In other words, any method on any of these interfaces can be called directly on the proxy -- after properly type-casting it, of course. Thus, you can use a dynamic proxy class to create a type-safe proxy object for an interface list without requiring pregeneration of the proxy class as you would with compile-time tools. (For a much more detailed introduction to the dynamic proxy classes, refer to the Resources section below.)

I will now introduce you to a framework based on these classes that will make the creation of SOAP (Simple Object Access Protocol) clients easy and intuitive. SOAP is a wire protocol that uses XML for data encoding. While building SOAP services in Parts 2 and 3 of this series, you discovered that client developers end up doing a lot of extra work that they wouldn't normally have to do. If you need a refresher, take a look at the SOAP service code in Part 2. See how insignificant the SOAP code is when compared to the client code? The simple services created in earlier articles in this series demonstrate that SOAP-based services only contain code that they would have had irrespective of SOAP. The service developers have little extra code to produce, while the client developers have much more work to complete on their end. The classes introduced in this article will alleviate most of that work.

Read the whole series on SOAP:

Introducing the SOAP Proxy class

First, let me show you what the client code would look like using the framework that you'll create in this article:

package hello;
import soapproxy.*;
public class Client
{
   public static void main(String[] args)
   {
       try
             {
                 Class[] interfaces = new Class[] {hello.Hello.class};
                 Hello hello = 
(Hello)(Proxy.newInstance("urn:Hello",interfaces));
                 // Invoke the sayHelloTo method that takes a String 
name.
                 System.out.println(hello.sayHelloTo("John"));
                 // Invoke the sayHelloTo method that takes a Name 
JavaBean.          
                 Name theName = new Name();
                 theName.setName("Mala");
                 System.out.println(hello.sayHelloTo(theName));
             }
             catch(Exception e)
             {
                 e.printStackTrace();
             }       
    }
}


Now, I might be a bit biased, but I think the client code above looks much better than the client code in Parts 2 and 3. It's okay if you don't follow the code right now. Believe me, you will by the end of this section.

To understand the client code, you must delve deeper into the SOAP Proxy class, which is in the soapproxy package, available in the Proxy.java file (see Resources below). That class has a private constructor, which means that a Proxy instance cannot be created from outside Proxy. The only way to retrieve a new Proxy instance is through the static newInstance() method. That method takes two parameters: the SOAP service's object ID and an array of interface names that this proxy will implement. The object ID parameter is straightforward, but what are the interface names, and where did they come from? The SOAP service developer defines these interfaces by simply lumping together all the methods that a client can invoke on the service into a Java interface. Pretty easy, no?

  • Print
  • Feedback

Resources
  • Sign up for the JavaWorld This Week free weekly email newsletter to find out what's new on JavaWorld: http://www.idg.net/jw-subscribe