JAXR提供者为发布web服务到UDDI注册中心提供了便利。本节讲述了发布现有web服务到注册中心的步骤:
发布一个服务到注册中心包括以下步骤:
创建一个Organization 创建它的类别 创建服务及服务绑定 保存信息到注册中心为了创建发布Web服务到注册中心的JAXR客户端,请导入下面这些必需的程序包:
import javax.xml.registry.*;
import javax.xml.registry.infomodel.*;
import java.net.*;
import java.security.*;
import java.util.*;
创建一个类,其中包含一个main方法,一个makeConnection方法,用来建立到注册中心的连接,一个executePublish方法,用来发布所有与服务相关的信息到注册中心。以下代码演示了主类JAXRPublish的创建:
public class JAXRPublish {
Connection connection = null;
public JAXRPublish() {}
public static void main(String[] args) {
String queryURL = "http://www-3.ibm.com/services/uddi/v2beta/inquiryapi";
String publishURL = "https://www-3.ibm.com/services/uddi/v2beta/protect/publishapi";
String username = "";
String password = "";
JAXRPublish jp = new JAXRPublish();
jp.makeConnection(queryURL, publishURL);
jp.executePublish(username, password);
}
JAXR客户端必须建立一个到UDDI注册中心的连接,并设置连接配置属性。详细信息请参见"建立连接"。
创建连接,传入配置属性。详细信息请参见"创建连接"。
创建一个机构,以及它的类别、服务,把它保存到注册中心。
更多信息请参见以下几节:
创建一个机构 添加类别 向机构添加服务和服务绑定以下代码演示了发布web服务的步骤:
public void executePublish(String username, String password) { RegistryService rs = null;
BusinessLifeCycleManager blcm = null;
BusinessQueryManager bqm = null;
String orgName = "The Coffee Break";
String orgDesc = "Purveyor of the finest coffees. Established 1895"; String contactName = "Jane Doe";
String contactPhone = "(800) 555-1212";
String contactEmail = "[email protected]";
String serviceName = "My Service Name";
String serviceDesc = "My Service Description";
String serviceBindingDesc = "My Service Binding Description";
String serviceBindingURI = "http://localhost:1024";
String scheme = "ntis-gov:naics";
String conceptName = "Snack and Nonalcoholic Beverage Bars";
String conceptCode = "722213";
try {
java.io.BufferedInputStream bfInput = null;
Properties propTemp = new Properties();
bfInput = new java.io.BufferedInputStream (new java.io.FileInputStream("jaxr.properties"));
propTemp.load(bfInput);
bfInput.close();orgName = propTemp.getProperty("org-name");
orgDesc = propTemp.getProperty("org-desc");
contactName = propTemp.getProperty("contact-name");
contactPhone = propTemp.getProperty("contact-phone");
contactEmail = propTemp.getProperty("contact-email");
serviceName = propTemp.getProperty("service-name");
serviceDesc = propTemp.getProperty("service-desc");
serviceBindingDesc = propTemp.getProperty("service-binding-desc");
serviceBindingURI = propTemp.getProperty("service-binding-uri");
scheme = propTemp.getProperty("scheme");
conceptName = propTemp.getProperty("concept");
conceptCode = propTemp.getProperty("concept-code");
}
try {
rs = connection.getRegistryService();
blcm = rs.getBusinessLifeCycleManager();
bqm = rs.getBusinessQueryManager();
System.out.println("Got registry service, query " + "manager, and life cycle manager");
// Get authorization from the registry
PasswordAuthentication passwdAuth = new PasswordAuthentication(username,password.toCharArray());
Set creds = new HashSet();
creds.add(passwdAuth);
connection.setCredentials(creds);
System.out.println("Established security credentials");
// Create organization name and description
Organization org = blcm.createOrganization(orgName);
InternationalString s = blcm.createInternationalString(orgDesc);
org.setDescription(s);
// Create primary contact, set name
User primaryContact = blcm.createUser();
PersonName pName = blcm.createPersonName(contactName);
primaryContact.setPersonName(pName);
// Set primary contact phone number
TelephoneNumber tNum = blcm.createTelephoneNumber();
tNum.setNumber(contactPhone);
Collection phoneNums = new ArrayList();
phoneNums.add(tNum);
primaryContact.setTelephoneNumbers(phoneNums);
// Set primary contact email address
EmailAddress emailAddress = blcm.createEmailAddress(contactEmail);
Collection emailAddresses = new ArrayList();
emailAddresses.add(emailAddress);
primaryContact.setEmailAddresses(emailAddresses);
// Set primary contact for organization
org.setPrimaryContact(primaryContact);
// Set classification scheme to NAICS
ClassificationScheme cScheme = bqm.findClassificationSchemeByName(null,scheme);
if (cScheme != null) {
// Create and add classification
Classification classification = (Classification)
blcm.createClassification(cScheme, conceptName, conceptCode);
Collection classifications = new ArrayList();
classifications.add(classification);
org.addClassifications(classifications);
}
// Create services and service
Collection services = new ArrayList();
Service service = blcm.createService(serviceName);
InternationalString is = blcm.createInternationalString(serviceDesc);
service.setDescription(is);
// Create service bindings
Collection serviceBindings = new ArrayList();
ServiceBinding binding = blcm.createServiceBinding();
is = blcm.createInternationalString(serviceBindingDesc);
binding.setDescription(is);
// allow us to publish a bogus URL without an error
binding.setValidateURI(false);
binding.setAccessURI(serviceBindingURI);
serviceBindings.add(binding);
// Add service bindings to service
service.addServiceBindings(serviceBindings);
// Add service to services, then add services to organization
services.add(service);
org.addServices(services);
// Add organization and submit to registry
// Retrieve key if successful
Collection orgs = new ArrayList();
orgs.add(org);
BulkResponse response = blcm.saveOrganizations(orgs);
Collection exceptions = response.getExceptions();
if (exceptions == null) {
System.out.println("Organization saved");
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);
org.setKey(orgKey);
}
}
}
}
本文地址:http://com.8s8s.com/it/it16956.htm