服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

eclipse插件实现axis webservice客户端


  1 建立eclipse插件
  
  file->new->project->plug-in development的plug-in project->next,填写project名,next, 填写内容,next,选择create plug-in using one of the templates,选择hello,world,finish。
  
  在视图可看到plugin.xml,在里加上运行调用web service所需jar包。内容如下:
  
  
  
  
  
    
  id="colimas_plugin"
  
  name="colimas_plugin plug-in"
  
  version="1.0.0"
  
  provider-name="nova"
  
  class="colimas_plugin.colimas_pluginplugin">
  
  
  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  
  
  
  
  

  
  

  
  
  
  
  
  
  
  

  
    
  point="org.eclipse.ui.actionsets">
  
    
  label="sample action set"
  
  visible="true"
  
  id="colimas_plugin.actionset">
  
    
  label="sample &menu"
  
  id="samplemenu">
  
    
  name="samplegroup">
  
  
  
  
  
    
  label="&sample action"
  
  icon="http://www.itus.cnicons/sample.gif"
  
  class="colimas_plugin.actions.sampleaction"
  
  tooltip="hello, eclipse world"
  
  menubarpath="samplemenu/samplegroup"
  
  toolbarpath="samplegroup"
  
  id="colimas_plugin.actions.sampleaction">
  
  
  
  
  
  
  
  2 建立调用web service类,该类实现调用axis的webservice
  
  /*
  
  *
  
  created on 2005/07/30
  
  *
  
  * todo to change the template for this generated file go to
  
  * window - preferences - java - code style - code templates
  
  */package com.nova.colimas.plugin.eclipse;
  
  import org.apache.axis.client.call;
  
  import org.apache.axis.client.service;
  
  import javax.xml.namespace.qname;import java.io.*;
  
  /**
  
  *@author tyrone
  
  *
  
  * todo to change the template for this generated type comment go to
  
  * window - preferences - java - code style - code templates
  
  */
  
  public class sendfileclient { private call call;
  
  /**
  
  * the constructor.
  
  */
  
  public sendfileclient() {
  
  try{
  
  service service=
  
  new service();
  
  call  = (call) service.createcall();
  
  }catch(exception ex){  system.out.println(ex.getmessage());
  
  } } public void savefile(){ try {  string endpoint =  "http://localhost:8080/axis/services/documentfilemanagement";
  
  system.out.println("start web service");
  
  call.settargetendpointaddress( new java.net.url(endpoint) );
  
  call.setoperationname(new qname("http://soapinterop.org/", "savefile"));
  
  file fp=new file("d://myproject//colimas//colimas_plugin//lib//mail.jar");
  
  bufferedinputstream in=new bufferedinputstream(new fileinputstream(fp));
  
  int len=in.available();
  
  byte[] contents=new byte[len];
  
  in.read(contents,0,len);
  
  system.out.println("begin run");
  
  //开始调用web service:documentfilemanagement的savefile方法
  
  string ret = (string) call.invoke( new object[] {fp.getname(),contents} );
  
  in.close();
  
  } catch (exception e) {  system.err.println("error"+e.tostring());
  
  }
  
  }
  
  }
  
  3 修改action类的run方法
  
  action类的run方法里的内容是eclipse插件真正要做到事
  
  package colimas_plugin.actions;import org.eclipse.jface.action.iaction;
  
  import org.eclipse.jface.viewers.iselection;
  
  import org.eclipse.ui.iworkbenchwindow;import org.eclipse.ui.iworkbenchwindowactiondelegate;
  
  import org.eclipse.jface.dialogs.messagedialog;
  
  import com.nova.colimas.plugin.eclipse.*;
  
  /**
  
  * our sample action implements workbench action delegate.
  
  * the action proxy will be created by the workbench and
  
  * shown in the ui. when the user tries to use the action,
  
  * this delegate will be created and execution will be
  
  * delegated to it. * @see iworkbenchwindowactiondelegate
  
  */public class sampleaction implements iworkbenchwindowactiondelegate { private iworkbenchwindow window;
  
  /**
  
  * the constructor.
  
  */ public sampleaction() { }
  
  /**
  
  * the action has been activated. the argument of the
  
  * method represents the 'real' action sitting
  
  * in the workbench ui.
  
  * @see iworkbenchwindowactiondelegate#run
  
  */ public void run(iaction action) { sendfileclient client=new sendfileclient();
  
  client.savefile();
  
  messagedialog.openinformation(  window.getshell(),
  
  "colimas_plugin plug-in",  "colimas connected");
  
  } /** * selection in the workbench has been changed. we
  
  * can change the state of the 'real' action here
  
  * if we want, but this can only happen after
  
  * the delegate has been created.
  
  * @see iworkbenchwindowactiondelegate#selectionchanged
  
  */ public void selectionchanged(iaction action, iselection selection) { }
  
  /**
  
  * we can use this method to dispose of any system
  
  * resources we previously allocated.
  
  * @see iworkbenchwindowactiondelegate#dispose
  
  */ public void dispose() { }
  
  /**
  
  * we will cache window object in order to
  
  * be able to provide parent shell for the message dialog.
  
  * @see iworkbenchwindowactiondelegate#init
  
  */ public void init(iworkbenchwindow window) { this.window = window;
  
  }
  
  4 调试
  
  首先启动axis服务器,然后选择eclipse的run菜单的run as -〉run time workbench。
  
  这样会启动另一个eclipse workbench,在这个workbench里你会看到toolbar里新增了一个按钮,
  
  点击按钮就会调用webservice并返回控制台结果。

扫描关注微信公众号