J2EE —— 第 10 章 Java API for XML Registries

Preview:

DESCRIPTION

J2EE —— 第 10 章 Java API for XML Registries. JAXR 概述. XML Registries 用于构建、部署、发现 Web 服务 (ebXML,UDDI) JAXR 体系结构. 实现 JAXR 客户端. 查找连接工厂 import javax.xml.registry.*; import javax.naming.*; ... Context context = new InitialContext(); - PowerPoint PPT Presentation

Citation preview

J2EE—— 第 10 章Java API for XML Registries

JAXR 概述 XML Registries 用于构建、部署、发现 Web 服务 (eb

XML,UDDI) JAXR 体系结构

实现 JAXR 客户端 查找连接工厂import javax.xml.registry.*; import javax.naming.*; ... Context context = new InitialContext(); ConnectionFactory connFactory = (ConnectionFactory) context.looku

p("java:comp/env/eis/JAXR"); 创建连接工厂ConnectionFactory connFactory = ConnectionFactory.newInstance();

创建连接Properties props = new Properties(); props.setProperty("javax.xml.registry.queryManagerURL", "htt

p://uddi.ibm.com/testregistry/inquiryapi"); props.setProperty("javax.xml.registry.lifeCycleManagerURL", "h

ttps://uddi.ibm.com/testregistry/publishapi"); connFactory.setProperties(props);Connection connection = connFactory.createConnection();

设置连接属性 javax.xml.registry.queryManagerURL javax.xml.registry.lifeCycleManagerURL javax.xml.registry.semanticEquivalences javax.xml.registry.security.authenticationMethod javax.xml.registry.uddi.maxRows javax.xml.registry.postalAddressScheme com.sun.xml.registry.http.proxyHost (https) com.sun.xml.registry.http.proxyPort (https) com.sun.xml.registry.http.proxyUserName com.sun.xml.registry.http.proxyPassword com.sun.xml.registry.useCache com.sun.xml.registry.userTaxonomyFilenames

获取和使用 RegistyService 对象

RegistryService rs = connection.getRegistryService(); BusinessQueryManager bqm = rs.getBusinessQueryManager(); BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager();

按名称查找组织Collection findQualifiers = new ArrayList();findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);//findQualifiers.add(FindQualifier.CASE_SENSITIVE_MATC

H);Collection namePatterns = new ArrayList();namePatterns.add(qString);//namePatterns.add("%" + qString + "%");BulkResponse response =bqm.findOrganizations(findQua

lifiers, namePatterns, null, null, null, null);Collection orgs = response.getCollection();

按类别查找组织ClassificationScheme cScheme= bqm.findClassificationSchemeByName(null, "ntis-gov:naics");Classification classification = blcm.createClassification(cScheme, “Snack and Nonalcoholic Beverage Bars", “722213");Collection classifications = new ArrayList();classifications.add(classification);BulkResponse response = bqm.findOrganizations(null, null, classificat

ions, null, null, null); Collection orgs = response.getCollection();

基于 WSDL 提供服务的组织String schemeName = “uddi-org:types";ClassificationScheme uddiOrgTypes = bqm.findClassificationScheme

ByName(null, schemeName); Classification wsdlSpecClassification = blcm.createClassification(uddiOrgTypes, "wsdlSpec", "wsdlSpec"); Collection classifications = new ArrayList();classifications.add(wsdlSpecClassification);BulkResponse br = bqm.findConcepts(null, null, classifications, null,

null);

搜查概念Collection specConcepts = br.getCollection();Iterator iter = specConcepts.iterator();while (iter.hasNext()) { Concept concept = (Concept) iter.next(); //getKey(concept) String name = getName(concept); //getDescription(concept) Collection links = concept.getExternalLinks(); if (links.size() > 0) { ExternalLink link = (ExternalLink) links.iterator().next(); System.out.println("\tURL of WSDL document: '" + link.getExternalURI() + "'"); } Collection specConcepts1 = new ArrayList(); specConcepts1.add(con

cept); br = bqm.findOrganizations(null, null, null, specConcepts1, null, nul

l);}

查找服务和服务绑定Iterator orgIter = orgs.iterator(); while (orgIter.hasNext()) { Organization org = (Organization) orgIter.next(); Collection services = org.getServices(); Iterator svcIter = services.iterator(); while (svcIter.hasNext()) { Service svc = (Service) svcIter.next(); Collection serviceBindings = svc.getServiceBindings(); Iterator sbIter = serviceBindings.iterator(); while (sbIter.hasNext()) { ServiceBinding sb = (ServiceBinding) sbIter.next(); } }}

管理注册数据 从注册获取授权 创建组织 添加类别 将服务和服务绑定添加到组织 发布组织 发布规范概念 从注册删除数据

从注册获取授权String username = "myUserName";String password = "myPassword";

// Get authorization from the registryPasswordAuthentication passwdAuth =new PasswordAuthentication(username, password.toCharArray

());

Set creds = new HashSet();creds.add(passwdAuth);connection.setCredentials(creds);

创建组织InternationalString s = blcm.createInternationalString("The Coffee Brea

k"); Organization org = blcm.createOrganization(s); s = blcm.createInternationalString("Purveyor of " + "the finest coffees. Established 1914"); org.setDescription(s); User primaryContact = blcm.createUser();PersonName pName = blcm.createPersonName("Jane Doe");primaryContact.setPersonName(pName);TelephoneNumber tNum = blcm.createTelephoneNumber();tNum.setNumber("(800) 555-1212");Collection phoneNums = new ArrayList();phoneNums.add(tNum);primaryContact.setTelephoneNumbers(phoneNums);org.setPrimaryContact(primaryContact);

添加类别ClassificationScheme cScheme= bqm.findClassificationSchemeByName(null, "ntis-gov:naics");Classification classification = blcm.createClassification(cScheme, “Snack and Nonalcoholic Beverage Bars", “722213");Collection classifications = new ArrayList();classifications.add(classification);org.addClassifications(classifications);

将服务和服务绑定添加到组织Collection services = new ArrayList();InternationalString s = blcm.createInternationalString("My Service Name"); Service service = blcm.createService(s);s = blcm.createInternationalString("My Service Description");service.setDescription(s);Collection serviceBindings = new ArrayList();ServiceBinding binding = blcm.createServiceBinding();s = blcm.createInternationalString(“My Service Binding ” +“Descriptio

n”); binding.setDescription(s); binding.setValidateURI(false); binding.setAccessURI("http://TheCoffeeBreak.com:8080/sb/"); serviceBindings.add(binding); service.addServiceBindings(serviceBindings);services.add(service);org.addServices(services);

发布组织Collection orgs = new ArrayList(); orgs.add(org); BulkResponse response = blcm.saveOrganizations(orgs); Collection exceptions = response.getException(); if (exceptions == null) { Collection keys = response.getCollection(); Iterator keyIter = keys.iterator(); if (keyIter.hasNext()) { javax.xml.registry.infomodel.Key orgKey = (javax.xml.registry.infomodel.Key) keyIter.next(); String id = orgKey.getId(); System.out.println("Organization key is " + id); } }

发布规范概念InternationalString s = blcm.createInternationalString("HelloConcept"); Concept specConcept = blcm.createConcept(null, s, ""); s = blcm.createInternationalString( "Concept for Hello Service"); specConcept.setDescription(s); s = blcm.createInternationalString("Hello WSDL document");ExternalLink wsdlLink = blcm.createExternalLink( "http://localhost:8080/hello-jaxrpc/hello?WSDL", s);specConcept.addExternalLink(wsdlLink);ClassificationScheme uddiOrgTypes= bqm.findClassificationSchemeByName(null, “uddi-org:types”);Classification wsdlSpecClassification = blcm.createClassification(uddiOrgTypes, "wsdlSpec", "wsdlSpec"); specConcept.addClassification(wsdlSpecClassification);

检索概念关键字Collection concepts = new ArrayList();concepts.add(specConcept);BulkResponse concResponse = blcm.saveConcepts(concepts);String conceptKeyId = null;Collection concExceptions = concResponse.getExceptions();javax.xml.registry.infomodel.Key concKey = null;if (concExceptions == null) { Collection keys = concResponse.getCollection(); Iterator keyIter = keys.iterator(); if (keyIter.hasNext()) { concKey = (javax.xml.registry.infomodel.Key) keyIter.next(); conceptKeyId = concKey.getId(); }}

将概念设置到服务绑定Concept specConcept = (Concept) bqm.getRegistryObject(conceptKe

yId, LifeCycleManager.CONCEPT);SpecificationLink specLink = blcm.createSpecificationLink();specLink.setSpecificationObject(specConcept);binding.addSpecificationLink(specLink); 使用名称和类别查找概念Collection namePatterns = new ArrayList();namePatterns.add("HelloConcept");BulkResponse br = bqm.findConcepts(null, namePatterns, classificati

ons, null, null);

从注册删除数据String id = key.getId();Collection keys = new ArrayList();keys.add(key);BulkResponse response = blcm.deleteOrganizations(keys);Collection exceptions = response.getException();if (exceptions == null) { Collection retKeys = response.getCollection(); Iterator keyIter = retKeys.iterator(); javax.xml.registry.infomodel.Key orgKey = null; if (keyIter.hasNext()) { orgKey = (javax.xml.registry.infomodel.Key) keyIter.next(); id = orgKey.getId(); System.out.println("Organization key was " + id); }}

添加用户定义的分类法ClassificationScheme cScheme = blcm.createClassificationScheme

("MyScheme", "A Classification Scheme");ClassificationScheme uddiOrgTypes = bqm.findClassificationScheme

ByName(null, "uddi-org:types"); if (uddiOrgTypes != null) { Classification classification = blcm.createClassification(uddiOrgTyp

es, "postalAddress", "postalAddress" ); cScheme.addClassification(classification); ExternalLink externalLink =blcm.createExternalLink( "http://www.mycom.com/myscheme.html", "My Scheme"); cScheme.addExternalLink(externalLink); Collection schemes = new ArrayList(); schemes.add(cScheme); BulkResponse br = blcm.saveClassificationSchemes(schemes); }

得到 scheme uuidif (br.getStatus() == JAXRResponse.STATUS_SUCCESS) { System.out.println("Saved ClassificationScheme"); Collection schemeKeys = br.getCollection(); Iterator keysIter = schemeKeys.iterator(); while (keysIter.hasNext()) { javax.xml.registry.infomodel.Key key = (javax.xml.registry.infomodel.Key) keysIter.next(); System.out.println("The postalScheme key is " + key.getId()); System.out.println("Use this key as the scheme" + " uuid in the taxonomy file"); }}

将分类法添加到 JAXR 提供者<JAXRClassificationSchemeid="uuid:nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn“ name="MySch

eme">

<JAXRConceptid="uuid:C0B9FE13-179F-413D-8A5B-5004DB8E5BB2/11"name="Agriculture, Forestry, Fishing and Hunting"parent="uuid:C0B9FE13-179F-413D-8A5B-5004DB8E5BB2"code="11"></JAXRConcept>

props.setProperty("com.sun.xml.registry.userTaxonomyFilenames","c:\mydir\xxx.xml|c:\mydir\xxx2.xml");

运行客户端示例 JAXRQuery.java 显示如何为组织搜索注册 JAXRQueryByNAICSClassification.java 显示如何使用

通用分类方案搜索注册 JAXRQueryByWSDLClassification.java 显示如何为使

用 WSDL 文档方法进行自我描述的 Web 服务搜索注册 JAXRPublish.java 显示如何将组织发布到注册 JAXRDelete.java 显示如何从注册删除组织 JAXRSaveClassificationScheme.java 显示如何将分类

方案发布到注册 JAXRPublishPostal.java 显示如何从组织检索邮政地址 JAXRDeleteScheme.java 显示如何从注册删除分类方案 JAXRPublishConcept.java 显示如何发布 WSDL 文档概

运行客户端示例 JAXRPublishHelloOrg.java 显示如何发布带有

引用 WSDL 文档的服务绑定的组织 JAXRDeleteConcept.java 显示如何删除概念 JAXRMyObjects.java 列出你在注册中拥有的所

有对象 build.xml JAXRExamples.properties postalconcepts.xml

在 J2EE 应用程序中使用 JAXR 客户端 编码应用程序客户端 MyAppClient.java ,它使用 JNDI loo

kup 获取一个到 PubQuery 企业 bean 的远程 home 接口的句柄,使用句柄创建 bean 的实例并调用其 executePublish 和 executeQuery 方法

编码 PubQuery 会话 bean 编译源文件 导入证书 启动 Application Server 创建 JAXR 资源 ( 通过连接器资源访问 JAXR 资源适配器 ) 创建、打包、部署应用程序 运行应用程序客户端

Recommended