建立簡單Eclipse外掛程式實現Axis WebService用戶端

來源:互聯網
上載者:User

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,在<Runtime>裡加上運行調用Web Service所需jar包。內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
   id="colimas_plugin"
   name="Colimas_plugin Plug-in"
   version="1.0.0"
   provider-name="nova"
   class="colimas_plugin.Colimas_pluginPlugin">

   <runtime>
      <library name="colimas_plugin.jar">
         <export name="*"/>
      </library>
      <library name="lib/activation.jar">
         <export name="*"/>
      </library>
      <library name="lib/axis.jar">
         <export name="*"/>
      </library>
      <library name="lib/commons-beanutils.jar">
         <export name="*"/>
      </library>
      <library name="lib/commons-discovery-0.2.jar">
         <export name="*"/>
      </library>
      <library name="lib/commons-logging-1.0.4.jar">
         <export name="*"/>
      </library>
      <library name="lib/jaxrpc.jar">
         <export name="*"/>
      </library>
      <library name="lib/xalan.jar">
         <export name="*"/>
      </library>
      <library name="lib/xerces.jar">
         <export name="*"/>
      </library>
      <library name="lib/saaj.jar">
         <export name="*"/>
      </library>
      <library name="lib/mail.jar">
         <export name="*"/>
      </library>
   </runtime>

   <requires>
      <import plugin="org.eclipse.ui"/>
      <import plugin="org.eclipse.core.runtime"/>
   </requires>

   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="colimas_plugin.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="colimas_plugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="colimas_plugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

</plugin>
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並返回控制台結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.