Hopefully you’ve been able to read some articles on my blog related to using Web Services in Java/Websphere and they’ve provided you some assistance.  In relation to that topic, this article simple illustrates how you you can invoke a web service using both a context and the locator class that is generated from WSDL2Java:

InitialContext initialContext = new InitialContext();
Context context = (Context)initialContext.lookup("java:comp/env/service");
GeneratedServiceLocator locator = context.lookup("ContextNameDefinedInWAS"); 
if (locator == null) {
locator = new GeneratedServiceLocator();
}
if (locator != null) {
URL url = new URL("YourEndpointIeWSDL");
GeneratedPortType service = locator.generatedGetPortTypeName(portAddress);
// optionally return or use this service in your method
}

So lets take a look at this code to understand what’s going on.  Each bullet represents each line number:

  1. This line uses the Websphere (or similar, meaning this package is dependant on the library you are using) InitialContext class.
  2. This line looks up the context in WAS by the specified name (standard name used here)
  3. In order to locate the Locator class of the web service, this line will look for an optionally defined locator instance that is defined by a context.  Please note the string literal called ContextNameDefinedInWAS is a string that is defined by an administrator when setting this up in Websphere.  Products like DataPower would use this, otherwise do not use this line of code and keep moving on in the example.
  4. Check the locator, did we get one?
  5. Since the locator is null, we will create an instance of a ServiceLocator class, and you should see some kind of ServiceLocator class name defined from your generated WSDL2Java code.
  6. End if section
  7. Check for the Locator instance ,we should have one by now.
  8. Create an instance of a URL object since its needed on the next line.  The String literal YourEndpointIeWSDL will be the fully qualified path to your WSDL endpoint URL address.
  9. Since we should have a locator instance at this time in the code, we now look for the method that is part of our ServiceLocator instance for it’s PortType.  Take a look at the signature of the ServiceLocator class and make sure you can find the appropriate getPortType method.  Since we don’t know how the WSDL is defined, we can’t really figure out the name of the method, you will need to check this.
  10. End

Once you have a porttype, you can invoke the required web service API