配置文件和发布服务时候的一样,其实编程的思路也差不多~~只不过我这多了一步解析wsdl的过程。首先放上解析wsdl的代码,将wsdl文件可以利用的信息映射到uddi上,这样,少了不少人工编写的步骤。
这里的映射关系是根据IBM上的一篇文章来的,感觉这篇比较权威,附上链接http://www.ibm.com/developerworks/cn/webservices/ws-uwsdl/part1/index.html
下面直接放上解析wsdl的代码:
package com.publish;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.wsdl.xml.WSDLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class AnalyWsdl{
public String readWsdl(String wsdl){
String result =null;
try {
String targetNamespace = "";
String serviceName = "";
String sdescription = "";
String location = "";
String bindingdescription = "";
String tdescription = "";
String bindingname = "";
String overviewURL = "";
String address = "";
WSDLFactory factory = WSDLFactory.newInstance();
WSDLReader reader = factory.newWSDLReader();
reader.setFeature("javax.wsdl.verbose", false);
reader.setFeature("javax.wsdl.importDocuments", true);
Definition def = reader.readWSDL(wsdl);
targetNamespace=def.getTargetNamespace(); //获取definitions中的targetNamespace,作为tmodel name
//System.out.println(targetNamespace);
if(def.getDocumentationElement()!=null)
tdescription = def.getDocumentationElement().getTextContent();//获取definitions中的documentation,作为tmodel description
else
tdescription = def.getQName().getLocalPart();//definitions中的documentation不存在,将name作为tmodel description
//System.out.println(tdescription);
WSDLWriter writer = factory.newWSDLWriter();
Document doc = writer.getDocument(def);
Element elem=doc.getDocumentElement();
NodeList bindingList = elem.getElementsByTagName("wsdl:binding");
Element binding = (Element)bindingList.item(0);
bindingname = binding.getAttribute("name");
//System.out.println(bindingname);
NodeList list=elem.getElementsByTagName("wsdl:service");
Element service=(Element)list.item(0);
serviceName = service.getAttribute("name"); //获取service name
//System.out.println(serviceName);
NodeList serviceList = service.getChildNodes();
sdescription = "null";
for(int i = 0;i<serviceList.getLength();i++){
Node childNode = serviceList.item(i);
if(childNode instanceof Element&&(childNode.getNodeName().equals("wsdl:documentation"))){ sdescription = childNode.getTextContent(); //获取service下的documentation,作为service description
}
}
//System.out.println(sdescription);
NodeList wsdlAddressList = elem.getElementsByTagName("soap:address");
NodeList wsdlAddressList1 = elem.getElementsByTagName("wsdlsoap:address");
if(wsdlAddressList.getLength()==1){
Element wsdlAddress = (Element)wsdlAddressList.item(0);
address = wsdlAddress.getAttribute("location");
location = address+"?wsdl";
}
if(wsdlAddressList1.getLength()==1){
Element wsdlAddress1 = (Element)wsdlAddressList.item(0);
address = wsdlAddress1.getAttribute("location");
location = address+"?wsdl"; //获取location,作为accesspoint
}
//System.out.println(location);
NodeList portList = elem.getElementsByTagName("wsdl:port");
Element portType = (Element)portList.item(0);
NodeList port = portType.getChildNodes();
bindingdescription = "null";
for(int j=0;j<port.getLength();j++){
Node childNode = port.item(j);
if(childNode instanceof Element&&(childNode.getNodeName().equals("wsdl:documentation"))){
bindingdescription = childNode.getTextContent(); //获取service_port的documentation,作为bindingTemplate description
}
}
//System.out.println(bindingdescription);
overviewURL =address+"#"+bindingname;
//System.out.println(overviewURL);
result = targetNamespace+"|"+tdescription+"|"+overviewURL+"|"+serviceName+"|"+sdescription+"|"+location+"|"+bindingdescription;
} catch (WSDLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
Publish.java
package com.publish;
import java.io.IOException;
import junit.framework.Assert;
import org.apache.juddi.v3.client.config.UDDIClerkManager;
import org.apache.juddi.v3.client.transport.Transport;
import org.uddi.api_v3.AccessPoint;
import org.uddi.api_v3.AuthToken;
import org.uddi.api_v3.BindingTemplate;
import org.uddi.api_v3.BindingTemplates;
import org.uddi.api_v3.BusinessEntity;
import org.uddi.api_v3.BusinessService;
import org.uddi.api_v3.BusinessServices;
import org.uddi.api_v3.Description;
import org.uddi.api_v3.GetAuthToken;
import org.uddi.api_v3.Name;
import org.uddi.api_v3.OverviewDoc;
import org.uddi.api_v3.OverviewURL;
import org.uddi.api_v3.SaveBusiness;
import org.uddi.api_v3.SaveTModel;
import org.uddi.api_v3.TModel;
import org.uddi.api_v3.TModelDetail;
import org.uddi.api_v3.TModelInstanceDetails;
import org.uddi.api_v3.TModelInstanceInfo;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.uddi.v3_service.UDDISecurityPortType;
public class Publish {
public void publish(String businessname,String bdescription,String wsdladdress) throws IOException{
String clazz;
String authinfo;
String information = null;
try{
clazz = UDDIClerkManager.getClientConfig().getUDDINode("default").getProxyTransport();
Class<?> transportClass = Class.forName(clazz, true, Thread.currentThread().getContextClassLoader());
if(transportClass!=null){
AnalyWsdl ana = new AnalyWsdl();
information = ana.readWsdl(wsdladdress);
String[] section = information.split("\\|");
//将AnalyWsdl返回的结果进行分割,从而分离出需要注册到uddi上的信息
Transport transport = (Transport)transportClass.newInstance();
UDDISecurityPortType securityService = transport.getUDDISecurityService();
GetAuthToken getauthToken = new GetAuthToken();
getauthToken.setUserID("root");
getauthToken.setCred("root");
AuthToken authtoken = securityService.getAuthToken(getauthToken);
authinfo = authtoken.getAuthInfo();
System.out.println("获得AuthToken");
System.out.println(authinfo);
UDDIPublicationPortType publication = transport.getUDDIPublishService();
//添加BusinessEntity
SaveBusiness sb = new SaveBusiness();
BusinessEntity be = new BusinessEntity();
Name name = new Name();
name.setValue(businessname);
be.getName().add(name);
Description description = new Description();
description.setValue(bdescription);
be.getDescription().add(description);
//添加tModel
SaveTModel ST = new SaveTModel();
ST.setAuthInfo(authinfo);
TModel tm = new TModel();
Name name1 = new Name();
System.out.println("tmodel name:"+section[0]);
name1.setValue(section[0]);
tm.setName(name1);
if(!(section[1].equals("null")))
{
Description description1 = new Description();
description1.setValue(section[1]);
System.out.println("tmodel description:"+section[1]);
tm.getDescription().add(description1);
}
OverviewDoc od = new OverviewDoc();
OverviewURL ou = new OverviewURL();
ou.setValue(section[2]);
System.out.println("overviewURL:"+section[2]);
od.setOverviewURL(ou);
tm.getOverviewDoc().add(od);
ST.getTModel().add(tm);
TModelDetail Tt = publication.saveTModel(ST);
//添加BusinessService
BusinessServices bss = new BusinessServices();
BusinessService bs = new BusinessService();
Name name2 = new Name();
name2.setValue(section[3]);
System.out.println("service name:"+section[3]);
bs.getName().add(name2);
if(!(section[4].equals("null")))
{
Description description2 = new Description();
description2.setValue(section[4]);
System.out.println("service description:"+section[4]);
bs.getDescription().add(description2);
}
BindingTemplate bt = new BindingTemplate();
BindingTemplates bts = new BindingTemplates();
AccessPoint ap = new AccessPoint();
ap.setUseType("wsdlDeployment");
ap.setValue(section[5]);
System.out.println("accesspoint:"+section[5]);
bt.setAccessPoint(ap);
if(!(section[6].equals("null")))
{
Description description3 = new Description();
description3.setValue(section[6]);
System.out.println("binding description:"+section[6]);
bt.getDescription().add(description3);
}
TModelInstanceDetails TD = new TModelInstanceDetails();
TModelInstanceInfo TI = new TModelInstanceInfo();
TI.setTModelKey(Tt.getTModel().get(0).getTModelKey());
TD.getTModelInstanceInfo().add(TI);
bt.setTModelInstanceDetails(TD);
bts.getBindingTemplate().add(bt);
bs.setBindingTemplates(bts);
bss.getBusinessService().add(bs);
be.setBusinessServices(bss);
sb.setAuthInfo(authinfo);
sb.getBusinessEntity().add(be);
publication.saveBusiness(sb);
System.out.println("服务注册成功!!");
}else{
Assert.fail();
}
}catch(Exception e){
e.printStackTrace();
Assert.fail("Could not obtain authInfo token.");
}
//out.println("</HTML></BODY>");
}
public static void main(String[] args) throws IOException{
Publish pub = new Publish();
pub.publish("TestPublish", "This is a test", "http://localhost:9000/Test_10/services/HelloWorld?wsdl");
}
}
其中要往juddi中注册的地址http://localhost:9000/Test_10/services/HelloWorld?wsdl是之前发布的服务,再放上一下wsdl文件,这样可以便于对比是把哪些信息映射到uddi中。
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="HelloWorld" targetNamespace="http://server.ws.demo/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.ws.demo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:documentation>My top level documentation</wsdl:documentation>
<wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="http://server.ws.demo/" version="1.0" xmlns:tns="http://server.ws.demo/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sayHi" type="tns:sayHi"/>
<xs:element name="sayHiResponse" type="tns:sayHiResponse"/>
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element minOccurs="0" name="text" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHiResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHiResponse">
<wsdl:part element="tns:sayHiResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHi">
<wsdl:part element="tns:sayHi" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:documentation>My PortType documentation</wsdl:documentation>
<wsdl:operation name="sayHi">
<wsdl:input message="tns:sayHi" name="sayHi">
</wsdl:input>
<wsdl:output message="tns:sayHiResponse" name="sayHiResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:documentation>My service documentation</wsdl:documentation>
<wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort">
<wsdl:documentation>My service port documentation</wsdl:documentation>
<soap:address location="http://localhost:9000/Test_10/services/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
运行publish.java,提示注册服务成功,进入http://localhost:8080/juddiv3/,查看刚刚注册的服务,多出一个TestPublish节点,则服务注册成功
查看它的服务的详细信息,显示如下