现在的位置: 首页 > 综合 > 正文

在UDDI注册中心查找和发布wsdl

2014年02月23日 ⁄ 综合 ⁄ 共 6116字 ⁄ 字号 评论关闭

在开始编写用于发布应用程序的代码之前,您必须理解这类应用程序的两个基本需求。首先,发布应用程序必须读取和理解 WSDL 文档的内容。其次,它们必须向 UDDI 注册中心发送请求,然后处理任何响应。幸运的是,有两个现成的 Java 类库提供这种功能,它们是:Web Services Description Language for Java(WSDL4J)和 UDDI Java
API(UDDI4J)。

WSDL4J 提供可以用于解析现有 WSDL 文档或通过编程创建新 WSDL 文档的标准 Java 接口。WSDL4J 是定位在 IBM developerWorks 网站上的一个开放源码项目(请参阅参考资料),其目的是为“Java 规范请求 110(Java Specification Request 110(JSR 110))”Java
APIs for WSDL,提供一个参考实现(有关详细信息,也请参阅参考资料)。这个 JSR 是通过 Java Community Process 开发的。

本文中的编程示例使用 WSDL4J V0.8。大多数 WSDL4J 类表示能够在 WSDL 文档中出现的元素。例如,由 Definition 类表示 元素,而由 Service 类表示 元素。也有使 WSDL 文档变得易于读取和解析以及将 WSDL4J 对象的内容作为 XML 文档写出的实用类。

UDDI4J 提供一个 Java 客户机端接口,该接口可以用来在 UDDI 注册中心中发布和查找服务描述。 这也是 IBM developerWorks 网站上主办的一个开放源码项目(请再次参阅参考资料以获得链接)。

对于发布应用程序,我将使用 UDDI4J V1.03。这个版本提供了 UDDI V1 应用程序编程接口的一个完整实现。UDDI4J 类包含 UDDI 数据实体的一个完整表示。例如,TModel 类表示 UDDI tModel 而 BusinessService 类表示 businessService。UDDI4J 还包含一个
UDDI 注册中心代理类。这个类用于向 UDDI 注册中心发送 publish 和 find 请求。

将 WSDL 服务接口作为 UDDI tModel 发布

我将通过开发一些代码开始,这些代码将用于发布作为 UDDI tModel 的 WSDL 服务接口描述。在本系列的第一部分,我回顾了正确发布一个 WSDL 服务接口定义所需的步骤。作为开发发布应用程序的需求,我将执行这些步骤。这里是这些步骤的一个简短摘要:

通过 WSDL 文档的 targetNamespace 设置 tModel 名称。

如果描述元素内有一个文档元素,那么就使用它来创建 tModel 描述。

将 WSDL 服务接口文档的网络可访问位置放到 overviewDoc 中。

至少添加一个 keyedReference,它指出该 tModel 包含对 WSDL 服务描述的引用。

下面将详细描述与每个步骤相关联的代码。您也可以查看和下载完整的 PublishServiceInterface 应用程序(参请阅参考资料以获得链接)。

步骤 1:创建 tModel 并通过 targetNamespace 设置名称

在处理这个步骤之前,您需要读取和解析 WSDL 文档。使用 WSDL4J 中的 WSDLReader 类来完成这个操作。在读取 WSDL 文档之后,创建一个新的 tModel 对象并通过 WSDL 文档中的 targetNamespace 设置它的名称。

清单 1:首先读取和解析 WSDL 文档

// Read WSDL service interface document
Definition def = WSDLReader.readWSDL(null, wsdlURL);

...

// Create tModel from WSDL service interface
TModel tModel = new TModel();

// [STEP 1: tModel} Set the businessService name from targetNamespace
tModel.setName(def.getTargetNamespace());

步骤 2:通过 WSDL 文档元素设置 tModel 描述

设置 tModel 描述是可选的。 如果文档元素是在定义元素内指定的,那么它可以用于设置 tModel 描述。

清单 2:设置 tModel 描述

// Get documentation element
Element element = def.getDocumentationElement();

// [STEP 2: tModel] Set default tModel description
String desc = DOMUtils.getChildCharacterData(element);
tModel.setDefaultDescriptionString(desc);

步骤 3:为 tModel 创建 overviewDoc

overviewDoc 将包含 WSDL 服务接口文档的网络可访问位置。在这个代码段中,将 overviewURL 设置成 WSDL 文档的位置。

清单 3:设置 overviewURL

// Create overview doc
OverviewDoc overviewDoc = new OverviewDoc();

// Create overview URL
OverviewURL overviewURL = new OverviewURL(wsdlURL);

// Set overviewURL
overviewDoc.setOverviewURL(overviewURL);

// [STEP 3: tModel] Set the overviewDoc
tModel.setOverviewDoc(overviewDoc);

步骤 4:将 tModel 归类为 WSDL 服务描述

因为 tModel 能够引用可以在任何标记语言中指定的服务描述,所以指出该 tModel 表示一个 WSDL 服务描述很重要。在 tModel 的 categoryBag 中添加一个附加项将完成这个操作。在这个样本代码中,有两个 keyedReference。其中,一个指出服务描述的类型,而另一个指出实现这个服务接口的服务的商业用途。

清单 4:编辑 tModel 的 categoryBag

// Create a categoryBag and get the CategoryBag 
categoryBag = new CategoryBag();

// Create a keyedReference for wsdlSpec 
KeyedReference kr = new KeyedReference("uddi-org:types", "wsdlSpec");
kr.setTModelKey("UUID:C1ACF26D-9672-4404-9D70-39B756E62AB4");
krList.add(kr);

// Create a keyedReference for the category of service
Vector krList = new Vector();
kr = new KeyedReference("Stock market trading services", "84121801");
kr.setTModelKey("UUID:DB77450D-9FA8-45D4-A7BC-04411D14E384");
krList.add(kr);

// Set keyed reference vector
categoryBag.setKeyedReferenceVector(krList);

// [STEP 4: tModel] Set the category bag
tModel.setCategoryBag(categoryBag);

将 WSDL 服务实现作为 UDDI businessService 发布

第二个应用程序将 UDDI 中的 WSDL 服务实现描述作为 businessService发布。要完成这个操作,您必须既创建 businessService 又创建 bindingTemplate。下列步骤是基于本系列第一部分中定义的过程的。这些步骤显示了如何既创建 businessService 又创建 bindingTemplate,并将用作构建您的发布应用程序的基础。与每个步骤相关联的代码是从完整的 PublishServiceImplementation 应用程序中抽取出来的。

首先我将回顾创建 businessService 的步骤。这里是这些步骤的摘要:

通过服务元素的名称设置 businessService 名称。

如果指定了名称,使用文档元素的内容来创建 businessService 的描述。

步骤 1:创建 businessService 并通过服务名称设置名称

通过读取和解析 WSDL 文档的内容开始。您使用 WSDL4J 中的 WSDLReader 类来完成这个操作。在读取 WSDL 文档之后,您创建了一个新的 businessService 对象并通过 WSDL 文档中的服务名称设置它的名称。

清单 5:创建和命名新的 businessService 对象

// Read WSDL service implementation document Definition
wsdlDefinition = WSDLReader.readWSDL(null, wsdlURL);

// Get the first service element only
Service wsdlService = 
((Service[]) wsdlDefinition.getServices().values().toArray(new Service[0]))[0];

...

// Create businessService from WSDL service interface
BusinessService businessService = new BusinessService();

...

// [STEP 1: businessService] Set the businessService name from service name
businessService.setName(wsdlService.getQName().getLocalPart());

步骤 2:通过 WSDL 文档元素设置 businessService 描述

服务元素内的 WSDL 文档元素是用来设置 businessService 的缺省描述的:

清单 6:设置缺省 businessService 描述

// Get documentation element
element = wsdlService.getDocumentationElement();

// [STEP 2: businessService] Set default businessService description
businessService.setDefaultDescriptionString(DOMUtils.getChildCharacterData(element));

现在您已经创建了一个 businessService,您可以创建 bindingTemplate。这里是创建 bindingTemplateHere 的四个步骤的摘要:

如果端口元素包含文档元素,那么使用它的内容来创建 bindingTemplate 的描述。

使用端口元素内的位置来设置 accessPoint。

向 bindingTemplate 添加一个 tModelInstanceInfo。该元素包含 tModelKey。

创建一个包含到 WSDL 服务实现文档的引用的 overviewDoc 元素。

步骤 1:创建 bindingTemplate 并通过 WSDL 文档元素设置描述

创建一个新的 bindingTemplate 对象,然后,通过端口元素内的 WSDL 文档元素设置它的缺省描述。

清单 7:创建和设置 bindingTemplate 对象

// Create a bindingTemplate
BindingTemplate bindingTemplate = new BindingTemplate();

// Get the first port element
Port wsdlPort = ((Port[]) wsdlService.getPorts().values().toArray(new Port[0]))[0];

// Get documentation element
element = wsdlPort.getDocumentationElement();

// [STEP 1: bindingTemplate] Set default bindingTemplate description
bindingTemplate.setDefaultDescriptionString(DOMUtils.getChildCharacterData(element));

步骤 2:在 bindingTemplate 内设置访问点

通过端口元素内的可扩展性元素设置访问点。对于这个样本代码,我假设使用了 SOAP 绑定并且访问点使用 HTTP 协议。

清单 8:设置 bindingTemplate 访问点

// Get first extensibility elementExtensibility
Element ext = (ExtensibilityElement) wsdlPort.getExtensibilityElements().get(0);

// Create access point (assume that it is always SOAP binding and HTTP protocol)
AccessPoint accessPoint = new AccessPoint(((SOAPAddress)ext).getLocationURI(), "http");

// [STEP 2: bindingTemplate] Set the access point
bindingTemplate.setAccessPoint(accessPoint);

步骤 3:创建引用服务接口的 tModelInstanceInfo

bindingTemplate 必须包含一个对与服务接口定义相关联的 tModel 的引用。通过创建一个包含该 tModel 的 tModelKey 的 tModelInstanceInfo 来完成这个操作。

清单 9 

// [STEP 3: bindingTemplate] Create tModelInstanceInfo using the tModelKey
tModelInstanceInfo = new TModelInstanceInfo(tModelKey);

步骤 4:在 instanceDetails 中创建 overviewDoc

除了 overviewDoc 将包含 WSDL 服务实现文档的位置以外,这个步骤与用于服务接口文档的步骤类似。

清单 10 

// Create overview URLOverviewURL 
overviewURL = new OverviewURL(wsdlURL);

// Set overviewURL
overviewDoc.setOverviewURL(overviewURL);

// [STEP 4: bindingTemplate] Set the overview doc
instanceDetails.setOverviewDoc(overviewDoc);

抱歉!评论已关闭.