升级到 JDK 5.0 Update 1 定制部署 WebServices - Axis 终于正常了!
JDK 1.4.x 是正常的!
但是 JDK 5.0 定制部署 WebServices - Axis 时,执行命令行:
java org.apache.axis.client.AdminClient deploy.wsdd
总不正常!
升级到 Update 1:
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)
终于又可以了!
相关下载:
JDK 5.0 Update 1:
http://dlc.sun.com/jdk/jdk-1_5_0_01-windows-i586-p.exe
Resin 2.1.16 (推荐,因为相对于 Tomcat 小巧)
http://www.caucho.com/download/resin-2.1.16.zip
Tomcat 5.5.7
http://apache.justdn.org/jakarta/tomcat-5/v5.5.7/bin/jakarta-tomcat-5.5.7.zip
Apache WebServices - Axis v1.1: (June 16, 2003)
http://apache.freelamp.com/ws/axis/1_1/axis-1_1.zip
解压后,将 axis-1_1\webapps\ 目录下的 axis 目录复制到 Resin 或 Tomcat 的 webapps 目录下!
JavaMail API 1.3.2 FCS:
JavaBeans(TM) Activation Framework 1.0.2:
XML Security Version 1.2 of the Java library:
http://xml.apache.org/security/dist/java-library/xml-security-bin-1_2_0.zip
清单:
activation.jar
axis-ant.jar
axis.jar
commons-discovery.jar
commons-logging-api.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
stylebook-1.0-b3_xalan-2.jar
wsdl4j.jar
xalan.jar
xercesImpl.jar
xml-apis.jar
xmlParserAPIs.jar
xmlsec.jar
xmlsecSamples.jar
xmlsecTests.jar
Class1.java 文件保存到 \webapps\axis\WEB-INF\classes\Class1.java
执行: javac Class1.java 编译成 Class1.class:
public class Class1
{
public static void main()
{
System.out.println("Hello World!");
//System.out.println(new jwsClass1().SayHelloTo("世界!"));
}
public String SayHelloTo(String Who)
{
return "你好: " + Who;
}
public String SayHelloToo(String Who)
{
return "你好: " + Who;
}
}
http://localhost:1080/axis/services/Class1Service?wsdl
另存为: xx.wsdl
deploy.wsdd (undeploy.wsdd) 文件,可由命令行:
java org.apache.axis.wsdl.WSDL2Java --skeletonDeploy true xx.wsdl
生成:
deploy.wsdd (undeploy.wsdd) 部署文件
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="Class1Service" provider="java:RPC">
<parameter name="className" value="Class1"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
客户端:
(a)从 WSDL 文件生成客户端存根 (stub) 代码,服务端框架 (skeleton) 代码以及 WSDL 中的数据类型文件(生成与之对应的 Java 代码)
1. deploy.wsdd : MyService 服务的部署描述文件
2. MyService.java : MyService 服务的接口文件
3. MyServiceService.java : 获得 MyService 服务的接口文件
4. MyServiceServiceLocator.java : 实现 MyServiceService 接口
5. MyServiceSoapBindingImpl.java : 实现 MyService 接口,应向其中补充业务逻辑
6. MyServiceSoapBindingSkeleton.java : MyService 服务的服务端框架代码,实现 MyService, org.apache.axis.wsdl.Skeleton 接口
7. MyServiceSoapBindingStub.java :MyService 服务的客户端存根代码,实现 MyService 接口
8. undeploy.wsdd : 注销 MyService 服务的部署描述文件
(b)
动态调用 WebServices :
参阅: 王咏刚
wsCaller:Web Service通用客户端和测试工具
http://www.contextfree.net/wangyg/c/wsCaller/wsCaller.html
import org.apache.axis.Constants;
import org.apache.axis.encoding.ser.SimpleDeserializer;
import org.apache.axis.wsdl.gen.Parser;
import org.apache.axis.wsdl.symbolTable.*;
import javax.wsdl.*;
//import javax.wsdl.extensions.soap.SOAPAddress;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.encoding.Deserializer;
import javax.xml.rpc.encoding.DeserializerFactory;
import java.util.*;
//import localhost.axis.services;
// Web Service Dynamic Invoker
public class DynamicInvoker
{
private Parser wsdlParser = null;
private Map services = null;
public DynamicInvoker(String wsdlURL) throws Exception
{
wsdlParser = new Parser();
wsdlParser.run(wsdlURL);
services = enumSymTabEntry(ServiceEntry.class);
}
public Map invoke(String serviceName,
String portName,
String operationName,
Vector parameterValues) throws Exception
{
Vector inputs = new Vector();
String returnName = null;
ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Service service = serviceEntry.getService();
org.apache.axis.client.Service clientService = new
org.apache.axis.client.Service(wsdlParser, service.getQName());
Call call = clientService.createCall(QName.valueOf(portName), QName.valueOf(operationName));
((org.apache.axis.client.Call) call).setTimeout(new Integer(15 * 1000));
BindingEntry bindingEntry = getBindingEntry(serviceName, portName);
Operation o = getOperation(bindingEntry, operationName);
Parameters parameters = bindingEntry.getParameters(o);
if (parameters.returnParam != null)
{
//QName returnType = org.apache.axis.wsdl.toJava.Utils.getXSIType(parameters.returnParam);
QName returnQName = parameters.returnParam.getQName();
returnName = returnQName.getLocalPart();
}
int size = parameters.list.size();
for (int i = 0; i < size; i++)
{
Parameter p = (Parameter) parameters.list.get(i);
switch (p.getMode())
{
case Parameter.IN :
inputs.add(getParamData((org.apache.axis.client.Call) call
, p
, (String) parameterValues.elementAt(i)));
break;
case Parameter.OUT :
break;
case Parameter.INOUT :
inputs.add(getParamData((org.apache.axis.client.Call) call,
p,
(String) parameterValues.elementAt(i)));
break;
}
}
Object ret = call.invoke(inputs.toArray());
Map outputs = call.getOutputParams();
HashMap map = new HashMap();
if (ret != null && returnName != null)
{
map.put(returnName, ret);
}
if (outputs != null)
{
for (Iterator i = outputs.keySet().iterator(); i.hasNext();)
{
Object obj = i.next();
String name;
Object value;
if (obj.getClass().getName().equals("java.lang.String"))
{
name = (String) obj;
}
else
{
name = ((QName) obj).getLocalPart();
}
value = outputs.get(obj);
map.put(name, value);
}
}
return map;
}
public Vector enumServiceNames()
{
Vector v = new Vector();
Iterator i = services.keySet().iterator();
while (i.hasNext())
{
String s = (String) i.next();
v.addElement(s);
}
return v;
}
public Vector enumPortNames(String serviceName)
{
Vector v = new Vector();
ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Map ports = serviceEntry.getService().getPorts();
Iterator i = ports.keySet().iterator();
while (i.hasNext())
{
String s = (String) i.next();
v.addElement(s);
}
return v;
}
public Vector enumOperationNames(String serviceName, String portName)
{
Vector v = new Vector();
BindingEntry entry = getBindingEntry(serviceName, portName);
Set operations = entry.getOperations();
Iterator i = operations.iterator();
while (i.hasNext())
{
Operation o = (Operation) i.next();
String s = o.getName();
v.addElement(s);
}
return v;
}
public Parameters enumParameters(String serviceName
, String portName
, String operationName)
{
BindingEntry entry = getBindingEntry(serviceName, portName);
Operation o = getOperation(entry, operationName);
Parameters parameters = entry.getParameters(o);
return parameters;
}
public String getParameterModeString(Parameter p)
{
String ret = null;
switch (p.getMode())
{
case Parameter.IN:
ret = "[IN]";
break;
case Parameter.INOUT:
ret = "[IN, OUT]";
break;
case Parameter.OUT:
ret = "[OUT]";
break;
}
return ret;
}
private BindingEntry getBindingEntry(String serviceName, String portName)
{
ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Port port = serviceEntry.getService().getPort(portName);
Binding binding = port.getBinding();
SymbolTable table = wsdlParser.getSymbolTable();
return table.getBindingEntry(binding.getQName());
}
private Operation getOperation(BindingEntry entry, String operationName)
{
Set operations = entry.getOperations();
Iterator i = operations.iterator();
while (i.hasNext())
{
Operation o = (Operation) i.next();
if (operationName.equals(o.getName()))
{
return o;
}
}
return null;
}
// return Map of <QName.getLocalPart, SymTabEntry>
private Map enumSymTabEntry(Class cls)
{
HashMap ret = new HashMap();
HashMap map = wsdlParser.getSymbolTable().getHashMap();
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry entry = (Map.Entry) iterator.next();
QName key = (QName) entry.getKey();
Vector v = (Vector) entry.getValue();
int size = v.size();
for (int i = 0; i < size; ++i)
{
SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
if (cls.isInstance(symTabEntry))
{
ret.put(key.getLocalPart(), symTabEntry);
}
}
}
return ret;
}
private Object getParamData(org.apache.axis.client.Call c, Parameter p,
String arg) throws Exception
{
// Get the QName representing the parameter type
QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
TypeEntry type = p.getType();
if (type instanceof BaseType && ((BaseType) type).isBaseType())
{
DeserializerFactory factory = c.getTypeMapping().getDeserializer(paramType);
Deserializer deserializer = factory.getDeserializerAs(Constants.
AXIS_SAX);
if (deserializer instanceof SimpleDeserializer)
{
return ((SimpleDeserializer) deserializer).makeValue(arg);
}
}
throw new RuntimeException("not know how to convert '" + arg
+ "' into " + c);
}
}
class AppTest
{
public static void main(String[] args) throws Exception
{
System.out.println("Hello World!");
//动态调用 WebServics
String location = "http://localhost:1080/axis/services/Class1Service?wsdl";
DynamicInvoker invoker = new DynamicInvoker(location);
Vector vServices = invoker.enumServiceNames();
Object[] Services = vServices.toArray();
Arrays.sort(Services);
for (int i = 0; i < Services.length; i++)
{
String serviceName = (String) Services[i];
System.out.println(serviceName);
Vector V = invoker.enumPortNames(serviceName);
String portName = (String) V.elementAt(0);
V = invoker.enumOperationNames(serviceName, portName);
Object[] OperationNames = V.toArray();
Arrays.sort(OperationNames);
for (int j = 0; j < OperationNames.length; j++)
{
String operationName = (String) OperationNames[j];
System.out.println("\t" + (String) OperationNames[j]);
Parameters parameters = invoker.enumParameters(serviceName, portName, operationName);
Vector v = parameters.list;
Vector parameterValues = new Vector();
for (int k = 0; k < v.size(); k++)
{
Parameter para = (Parameter) v.elementAt(k);
System.out.print("\t\t" + para.getQName().getLocalPart());
if (para.getType().getQName().getLocalPart().equals("string"))
{
parameterValues.addElement("asdasdas");
}
else
{
parameterValues.addElement(null);
}
System.out.print("\t" + para.getType().getQName().getLocalPart());
System.out.println("\t" + invoker.getParameterModeString(para));
}
Map result = invoker.invoke(serviceName, portName, operationName, parameterValues);
for (Iterator it = result.keySet().iterator(); it.hasNext();)
{
String name = (String) it.next();
Object value = result.get(name);
String line;
if (value != null)
{
line = name + "=" + result.get(name).toString() + "\n";
}
else
{
line = name + "=(null)\n";
}
System.out.println(line);
}
}
}
//调用代理类调用 WebServices
localhost.axis.services.Class1Service.Class1 x = new localhost.axis.services.Class1Service.Class1ServiceLocator().getClass1Service();
//((localhost.axis.services.Class1Service.Class1ServiceSoapBindingStub) x).setMaintainSession(true);
System.out.println(x.sayHelloTo("萨达三 adas"));
}
}