首页 > 代码库 > activiti designer源码的研究(一) 采用Axis2调用Webservice

activiti designer源码的研究(一) 采用Axis2调用Webservice

       最近由于项目的需要,需要对eclipse中的Eclipse Activiti designer插件进行扩充,使其能够调用的webservice,但是遇到了classNotFoundException,我的方法如下:

<1> 在org.activiti.designer.gui中添加了调用webserviced的类

<2> 在org.activiti.designer.lib中添加了axis2的jar

<3> 在org.activiti.designer.lib的manifest.mf中export了axis2

<4>在org.activiti.designer.gui中manifest.mf中的dependency中添加了lib

在eclipse中测试run eclipse application时,可以运行,但是导出之后放到rcp rich client中时,会发现访问不到类,说明,不能访问到第三方的jar包


解决方法:把axis2放到某个目录下,然后直接加载,然后使用反射调用webservice

public class ServiceUtil {       //object(Array)转化为String       public String makeArrayObject(Object array) {
		List tem = new ArrayList();
		for (int i = 0; i < Array.getLength(array); i++) {
			tem.add(Array.get(array, i));
		}
		
		for(int i=0;i<tem.size();i++)
			System.out.println(tem.get(i));
		
		return String.valueOf(tem.get(0));
	}
	
	public String getService(String socketAddress) {
		String result = null;
		
		URL[] urls = new URL[] {};
		MyClassLoader classLoader = new MyClassLoader(urls, null);
		
        try {
        	 classLoader.addJar(new File("c:/jar/AXIS2.jar").toURI().toURL());
        	
			Class rpcserviceclient = classLoader.loadClass("org.apache.axis2.rpc.client.RPCServiceClient");
			Object rpcserviceclientInstance = rpcserviceclient.newInstance();               
				
			Method getoptions = rpcserviceclient.getMethod("getOptions", null);
			Object c = getoptions.invoke(rpcserviceclientInstance, null);        
			
			Class options = c.getClass();
			
			Class endpointreference = classLoader.loadClass("org.apache.axis2.addressing.EndpointReference"); 
			Constructor endpointreferenceCons = endpointreference.getConstructor(String.class);
			Object endpointreferenceInstance = endpointreferenceCons.newInstance(socketAddress+"ServiceUtil");
			Method setTo = options.getMethod("setTo", endpointreferenceInstance.getClass());
			setTo.invoke(c, endpointreferenceInstance);
			
			Object[] objects = new Object[] {};
			Class<?>[] classes = new Class[] { String.class };
			QName qname = new QName("http://servicemanagement/", "getServiceInfo");
			
			
			Method invokebloking = rpcserviceclient.getMethod("invokeBlocking",qname.getClass(),objects.getClass(),classes.getClass());
			Object obj = invokebloking.invoke(rpcserviceclientInstance,qname,objects,classes);

			result =  makeArrayObject(obj);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return result;
	}        class MyClassLoader extends URLClassLoader {

	    public MyClassLoader(URL[] urls) {
	        super(urls);
	    }

	    public MyClassLoader(URL[] urls, ClassLoader parent) {
	        super(urls, parent);
	    }

	    public void addJar(URL url) {
	        this.addURL(url);
	    }

	}
	
	public static void main(String[]args){
		String str = new ServiceUtil().getService("http://localhost:8010/ServiceUtil");
		System.out.println(str);
	}
}


activiti designer源码的研究(一) 采用Axis2调用Webservice