Skip to main content

Web services programming tips and tricks: Develop a UDDI Java application for Web services registered within a UDDI registry

Andrew Bradfield (abrad@us.ibm.com), Author, IBM,Software Group
Andrew J. Bradfield is an Advisory Software Engineer working in the IBM Software Division located at the IBM T.J. Watson Research Center in Hawthorne, New York. Andrew graduated with a degree in Computer Science from Hamilton College in Clinton, New York. His projects include J2EE development of Content Management and Data Management Solutions. He can be contacted at abrad@us.ibm.com.

Summary:  This tip establishes the case for using Universal Description, Discovery, and Integration (UDDI) to register Web services for application-level consumption. The author provides detailed code samples and an extension API based on the Universal Description, Discovery, and Integration for Java (UDDI4J) API that will enable you to use UDDI for your own development purposes.

Date:  27 Jul 2004
Level:  Advanced
Activity:  936 views

Introduction

Universal Description, Discovery, and Integration (UDDI) is fast becoming a standard for storing business processes available on the Web. Although UDDI is capable of storing many different types of data, for the purposes of this tip, I’ll focus on how UDDI can be used to register Web services, thereby making them available for application-level consumption.


Assumptions

  • You are using IBM® WebSphere® Studio Application Developer V5.0 (Application Developer) or another J2EE compliant development environment, such as the Eclipse IDE.
  • DB2® has been installed, and the sample database has been created (this sample uses the Employee and Department tables. To create the sample database, open DB2 First Steps and click on the "Create Sample Database" link).
  • UDDI registry is installed and properly configured. Please see the InstallUDDI listing for an automated setup of a UDDI registry to use the Cloudscape database. Execute installuddilibs.bat from a command window to create the necessary directory structure.


What this tip covers

This tip provides Java developers with a fast and simple way to develop their own UDDI Java applications to consume Web services registered within a UDDI registry. The sample code included contains a Payroll Web service (deployed from an entity bean) and a UDDI session bean.


Tip background

This tip is based heavily on the tutorial "Discover Web services with the WSDK V5.1: UDDI" (developerWorks, November 2003), which is cited in the Resources section. My intention is to present an extension to the basic Universal Description, Discovery, and Integration for Java (UDDI4J) classes in order to help you quickly stand up your own UDDI registry of Web services. While working through the "Discover Web services with the WSDK V5.1: UDDI" tutorial, I developed an extension API that provides a higher-level approach to the UDDI4J API. This tip extends only a small subset of the UDDI4J functionality, since its purpose is to register, discover, and consume a Web service stored within a UDDI registry. For a more broad discussion of UDDI and its uses beyond storing Web services, please see "Understanding UDDI" (developerWorks, July 2002), by Tom Bellwood.


Payroll Web service

The Payroll.ear file included contains an Enterprise JavaBeans (EJB) project named PayrollEjbPrj. Within this EJB project are two entity beans: Department and Employee, and one session bean, Payroll. The Department and Employee entity beans expose getter and setter methods for the sample database. The Payroll bean, which exposes 12 methods, uses the getter methods of the entity beans to query various pieces of information from the sample database. The sample code might easily be extended to provide setter functionality from within the Payroll bean. Exposing all of the sample database fields is beyond the scope of this tip.


UDDIClient session EJB

The UDDIClient session bean makes almost exclusive use of the UDDI utility API discussed in the next section.

A UDDI registry might contain Businesses, Services, and TModels. The UDDIClient bean provides a publish() and two delete() methods for each type of UDDI entry. As shown in Listing 1 below, method signature methods are as simple as possible, while shifting the work to the UDDI Utility API.


Listing 1. Method signatures
public String publishBusiness(String busName)
	{
		return BusinessUtilities.publishBusiness(busName);
	}
	
public void deleteBusinessByName(String busName)
{
    BusinessUtilities.deleteBusinessByName(busName);
}

public void deleteBusinessByKey(String busKey)
{
	BusinessUtilities.deleteBusinessByKey(busKey);
}

public String publishService(String serviceName, String busName, 
String tModelName, String tModelOverviewDoc, 
String accessPointStr, String description)
{
      return 
ServiceUtilities.publishService(serviceName,busName,tModelName,
tModelOverviewDoc,accessPointStr,description);
}

public void deleteServiceByName(String serviceName)
{
	ServiceUtilities.deleteServiceByName(serviceName);
}

public void deleteServiceByKey(String serviceKey)
{
	ServiceUtilities.deleteServiceByKey(serviceKey);
}

public String publishTModel(String tModelName, String overviewDocString)
{
	return 
ModelUtilities.publishTModel(tModelName,overviewDocString);
}
	
public void deleteTModelByName(String tModelName)
{
	ModelUtilities.deleteTModelByName(tModelName);
}

public void deleteTModelByKey(String tModelKey)
{
	ModelUtilities.deleteTModelByKey(tModelKey);
}
			

Methods getService and executeService

The getService and executeService methods of the UDDIClient bean demonstrate how to retrieve our Payroll Web service and invoke some of its methods. The executeService method takes a string representing the name of a service stored in the registry. In the sample code, the service registered is the Payroll Web service. For the purposes of this tip, I’ve hard coded two methods that are called from the Payroll Web service found within the registry, as shown in Listing 2.


Listing 2. Methods getService and executeService
			

/**
	* Execute a Service found in the UDDI registry
	* @param serviceName Service name used to search for
	and execute available UDDI Services
	*/
	public void executeService(String serviceName)
	{
		//Obtain all access points for services providing this 
service
		Map accessPoints = 
ServiceUtilities.getServiceAccessPoints(serviceName);
		
		//Create a set of accespoints
		Set accessSet = accessPoints.keySet();
		Iterator it = accessSet.iterator();
		
		Payroll payroll = null;
		Object obj = null;
		
		while(it.hasNext()) 
		{
			try
	          {
		          //For each access point, create an
PayrollService object
		          String accessPoint = (String)it.next();
		          System.out.println("Service Access Point: " + 
accessPoint);
		          payroll = getService(accessPoint);
				
		          if(payroll != null)
		          {
				    System.out.println("Employee 000010's 
bonus is " + payroll.getBonus("000010"));
				    System.out.println("Employee 000010's 
phone number is " + payroll.getPhoneNo("000010"));
				} 
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

/**
	* Create a Service object, to communicate with the
Web service defined by the URL
	* @param urlString the URL of the Web service
	* @return A PayrollService object.
	*/
	private Payroll getService(String urlString)
	{
		PayrollServiceLocator payrollServiceLocator =
new PayrollServiceLocator();
		Payroll payroll = null;
		
		try
		{
			URL url = new URL(urlString);
			System.out.println("Payroll URL: " + url.toString());
			System.out.println("Getting Payroll object");
			payroll = 
payrollServiceLocator.getPayroll(url);
			System.out.println("Payroll object retrieved 
successfully");
		} 
		catch (MalformedURLException e) 
		{
			System.out.println("URL Exception: " + e.toString());
		}
		catch(javax.xml.rpc.ServiceException se)
		{
			System.out.println("Service Exception: " + 
se.toString());
		}
		
		return payroll;
	}

			


UDDI utility API

The UDDI Utility API has four main classes, three of which I will cover in detail in Listings 3, 4, and 5. The fourth, Utilities.class, contains all of the setup information for the UDDI registry. The values and structure are explained in detail in the "Publishing your services on UDDI with the WSDK V5.1" (developerWorks, November 2003) tutorial.


Listing 3. BusinessUtilities
/**
	* Display business information of a BusinessEntity
	* @param businessKey The businessKey of the BusinessEntity
	whose detail is to be displayed
	*/
	public static void showBusinessDetail(String businessKey)

/**
	* Locate a Business by name
	* @param busName The business name used to search for a business
	* @return Vector: This method returns a Vector of Strings. 
	Each String represents the businessKey of a business matching the query
	*/
	public static Vector findBusinessByName(String busName)

/**
	* Locate a Business by key
	* @param businessKey The business key used to search for a business
	* @return BusinessList: This function returns a BusinessList on success.  
	In the event that no matches were located for the specified criteria,
	a BusinessList structure with zero BusinessInfo structures is returned.
	*/
	public static BusinessList findBusinessByKey(String businessKey)

/**
	* Delete a Business by name
	* @param busName Business name used to find and delete a business
	*/
	public static void deleteBusinessByName(String busName)

/**
	* Delete a Business by key
	* @param businessKey A Business key used to find and delete a business
	*/
	public static void deleteBusinessByKey(String businessKey)

/**
	* Publish a Business
	* @param busName The name of the business to be published
	* @return String: This method returns a String representing the 
	key of the newly published Business
	*/
	public static String publishBusiness(String busName)

			


Listing 4. ModelUtilities
/**
	* Locate a technical Model by name
	* @param tModelName the Name
	* @return Vector: This method returns a Vector of 
	tModelKey Strings
	*/
	public static Vector findTModelByName(String tModelName)

/**
	* Locate a technical Model by key
	* @param tModelName The TModel key used to search for a TModel 
	* @return TModelList: This method returns a TModelList 
	of TModelInfos objects
	*/
	public static TModelList findTModelByKey(String tModelKey)

/**
	* Delete a technical Model by name
	* @param tModelKey TModel name used to delete a TModel
	*/
	public static void deleteTModelByName(String tModelName)

/**
	* Delete a technical Model by key
	* @param tModelKey TModel key used to delete a TModel
	*/
	public static void deleteTModelByKey(String tModelKey)

/**
	* Publish a technical Model
	* @param tModelName The TModel name 
	* @param overviewDocString This parameter expects a 
	URL-String representation of the WSDL address. 
	EX: http://localhost:9080/MyRouter/Test.wsdl
	* @return String: This method returns a String 
	representing the key of the newly published TModel
	*/
	public static String publishTModel(String tModelName, 
	String overviewDocString)
			


Listing 5. ServiceUtilities
/**
	* Locate a Service by name
	* @param serviceNames A vector of Name objects
	* @return Map: This method returns a Map of service 
	and business key pairs 
	*/
	public static Map findServiceByName(Vector serviceNames)

/**
	* Locate a Service by key
	* @param serviceKey A key used to search for a Service
	* @return ServiceList: This method returns a ServiceList of 
	ServiceInfos objects
	*/
	public static ServiceList findServiceByKey(String serviceKey)

/**
	* Delete a Service by name
	* @param serviceName Service name used to find 
	and delete a Service
	*/
	public static void deleteServiceByName(String serviceName)

/**
	* Delete a Service by key
	* @param serviceKey Service key used to find and delete 
	a Service
	*/
	public static void deleteServiceByKey(String serviceKey)

/**
	* Publish a Service
	* @param serviceName The name of the Service to be published
	* @param businessName The Business name used to 
	associate the service to. If Business name does not exist 
	in the registry, a Business entry will be created.
	* @param tModelName The TModel that the service implements.  
	If TModel is not found in the registry and the 
	TModelOverviewDoc parameter is not null, a TModel entry 
	will be created.
	* @param tModelOverviewDoc Required only if the TModel name 
	provided in the tModelName parameter is not 
	found in the registry.
	This parameter expects a URL-String representation of the 
	WSDL address.  EX: http://localhost:9080/MyRouter/Test.wsdl
	* @param accessPointStr
	* @param description Optional Service description.
	* @return String: This method returns a String representing 
	the key of the newly published Service
	*/
	public static String publishService(String serviceName, 
	String busName, String tModelName, String tModelOverviewDoc, 
	String accessPointStr, String description)

			


Summary

Get the products used in this tip

If you are a developerWorks subscriber, you have a single user license to use WebSphere Studio Application Developer and other DB2, Lotus®, Rational®, Tivoli®, and WebSphere products -- including the Eclipse-based WebSphere Studio IDE -- to develop, test, evaluate, and demonstrate your applications. If you are not a subscriber, you can subscribe today.

As you have seen, UDDI programming can be a very powerful way to share information on the Web using the UDDI registry. This sample code, and more specifically, the UDDI Utility API are provided as a starting point from which you can develop a more customized solution.



Download

NameSizeDownload method
ws-uddiproxy.zip HTTP

Information about download methods


Resources

About the author

Andrew J. Bradfield is an Advisory Software Engineer working in the IBM Software Division located at the IBM T.J. Watson Research Center in Hawthorne, New York. Andrew graduated with a degree in Computer Science from Hamilton College in Clinton, New York. His projects include J2EE development of Content Management and Data Management Solutions. He can be contacted at abrad@us.ibm.com.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=11941
ArticleTitle=Web services programming tips and tricks: Develop a UDDI Java application for Web services registered within a UDDI registry
publish-date=07272004
author1-email=abrad@us.ibm.com
author1-email-cc=Copy email address

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers