您的位置:首页 > 编程语言 > Java开发

创建简单Eclipse插件实现Axis WebService客户端

2009-03-04 20:55 316 查看
Apache Axis是Apache WebService项目中的子项目,其最初起源于IBM的"SOAP4J",应该属于最早的一批用于构造基于SOAP应用的Framework。

目前Apache Axis已经发展到了第三代,其核心是一个SOAP处理器,用于开发包括客户端,服务器端,SOAP Gateway等各种应用。事实上Apache Axis在了1.0版后,其发行版本还包括了完整的J2EE服务器插件, WSDL支持和生成,TCP/IP监视器等组件,从这个意义上来说Apahce Axis已不仅仅是个SOAP框架了,它包含了除了UDDI外对整个Web Service协议栈 (Protocol Stack)的支持。

对大多数关心Macromedia产品的人来说,Macromedia是Axis小组的核心成员之一,他们在几乎所有J2EE服务器端的产品线中包含了Axis,也就是说Macromedia对WebService的支持是通过Apache Axis实现的,这其中包括了 JRun, Flex, ColdFusion。即便是Flash Remoting,由于其核心还是SOAP,所以也是使用了Axis的SOAP框架。

和.NET的WS不同,Axis是一个非常易于扩展的体系结构,其设计的核心思想是建立一个数据处理的管道,通过把handler编织成一条处理链从而无限扩展其可能性。打个比方,在输入部分开发人员既可以编写基于HTTP Basic Authentication的用户验证模块也可以插入 SOAP Auth模块用于验证请求,而输出部分既可以通过XML binding把结果输出为SOAP XML也可以象Flash Remoting那样把结果输出成binary格式。

同时Axis也提供了所有的处理模块,使得用户能在最快的时间把服务器组件发布成WS.总的来说Axis有以下几个特点:

速度 - Axis 使用 SAX 而不是 DOM 来处理XML请求,所以速度上有很大的改善。

灵活性 - 刚才也提到了,Axis的体系结构是一个全开放式的处理器,所以扩展模块极为方便。面向组件的发布方式 - 处理链上可重复使用组件来完成类似的操作。

Transport 框架 - Axis的核心和底层的transport完全分离,也就是说不管WS的Transport是HTTP, FTP, MAIL, MESSAGE QUEUE,只要插入对应的Transport模块即可,而不需改动其他部分。

WSDL1.1支持 - 可自动由Java Object生成WSDL

摘要:
本文描述了如果创建一个简单Eclipse插件来实现Axis WebService客户端,包括4个步骤:
1 建立Eclipse插件,修改plugin.xml
2 建立调用Web Service类,该类实现调用Axis的WebService
3 修改Action类的run方法
4 调试

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 &Menu"

id="sampleMenu">

<separator

name="sampleGroup">

</separator>

</menu>

<action

label="&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并返回控制台结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: