EclipseでWebサービス(axis)を使ったリッチクライアント

以前つくったWebサービス(http://d.hatena.ne.jp/IZIZIZI/20100630)を使ってリッチクライアントを作成した。

  • 環境
Eclipse3.0.1
VisualEditor1.0

※VisualEditorについて

http://download.eclipse.org/tools/ve/downloads/index.php

上記URLより以下をダウンロード

EMF build 2.0.2
GEF Build 3.0.1
Visual Editor Runtime 1.0.2.
  • プロジェクトの作成

Eclipseメニューのファイル→新規→プロジェクト→JavaJavaプロジェクト

プロジェクト名	AxisClientSample

プロジェクト名を右クリック→新規→ソースフォルダー

フォルダー名	src

プロジェクトを右クリックしてプロパティー画面で「Javaのビルド・パス」タブを選択し、「ライブラリー」タブで外部JARの追加で以下を追加

	c:\axis\axis1_1\lib以下のjarファイル全部
	C:\eclipse3\plugins\org.junit_3.8.1\junit.jar
  • プロクシクラスの生成

プロジェクト名を右クリック→新規→ファイル

ファイル名	build.xml

内容は以下

<?xml version="1.0"?>
<project name="AxisClientSample" default="wsdl2java">
	<taskdef resource="axis-tasks.properties" />
	<target name="wsdl2java">
		<axis-wsdl2java
			output="${basedir}/src"
			url="http://localhost:8080/axis/JoinString.wsdl" >
		</axis-wsdl2java>
		<axis-wsdl2java
			output="${basedir}/src"
			url="http://localhost:8080/axis/ReverseString.wsdl" >
		</axis-wsdl2java>
	</target>
</project>

build.xmlを右クリック→実行→「Andビルド...」を選択

表示されたダイアログの「ターゲット」タブを選択、以下をすべてチェック

wsdl2java

「プロパティー」タブを選択

「Antランタイム設定で指定されたグローバル・プロパティーを使用」チェックをオフ

「プロパティーの追加...」ボタンにて以下追加

名前	TOMCAT_HOME
値	C:\Tomcat 5.0

「クラスパス」タブを選択

フォルダーの追加で\AxisClientSample\bin
外部JARの追加でC:\axis1_1\lib以下のJarファイル全部

適用ボタンにより設定を適用させる

実行ボタンによりプロクシが自動生成される

※実行ボタンを押す場合にはすでにTomcatが起動していること

『BUILD SUCCESSFUL』表示でOK

  • サンプルクライアントの作成

srcフォルダーを右クリック→新規→その他→JFrame Visual Class

パッケージ名	com.izizizi.client
クラス名	AxisSampleApplication
main()あり

エディター画面に表示されたウィンドウを選択後、Eclipse下のプロパティビュー項目の以下を変更

layout	Borderlayout → null

以下オブジェクト配置

タイプ		オブジェクト名		表示文字
jLabel		string1Label		文字1
jLabel		string2Label		文字2
TextField	string1TextField	null
TextField	string2TextField	null
TextField	string3TextField	null
jButton		sample1Button		サンプル1
TextField	sample1AnswerTextField	null
jButton		sample2Button		サンプル2
jLabel		sample2AnswerLabel	null

Webサービスプロクシの配置

EclipseのPaletteビューからChoose Beanを選択

表示されたダイアログの「Choose a JavaBean (....」入力エリアに"Sample"と入力

「一致する型」一覧の以下を選択

JoinStringServiceLocator

ダイアログ下の「Properties」の「Bean variable name」欄へ"myJoinString" と入力しOKボタン

VisualEditorのエディットウィンドウのフォーム外の白い背景部分へマウス範囲配置します。

EclipseのPaletteビューからChoose Beanを選択

表示されたダイアログの「Choose a JavaBean (....」入力エリアに"AxisSample" と入力

「一致する型」一覧の以下を選択

ReverseStringServiceLocator

ダイアログ下の「Properties」の「Bean variable name」欄へ"myReverseString" と入力しOKボタン

VisualEditorのエディットウィンドウのフォーム外の白い背景部分へマウス範囲配置します。

VE画面の「サンプル1」ボタンを右クリック→Events→actionPerformed

private JButton getSample1Button()メソッド内のpublic void actionPerformed()を以下へ修正

public void actionPerformed(java.awt.event.ActionEvent e) {    
	System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
	try{
		JoinStringService myJoinStringService = getMyJoinString();
		JoinString myJoinString = myJoinStringService.getJoinStringService();
		((JoinStringServiceSoapBindingStub)myJoinString).setMaintainSession(true);
		myJoinString.setStr1(string1TextField.getText());
		myJoinString.setStr2(string2TextField.getText());
		String	marge = myJoinString.marge(string3TextField.getText());
		sample1AnswerTextField.setText(marge);
	}catch(Exception e1){
		e1.printStackTrace();
	}
}

以下も追加

import com.izizizi.service.JoinStringService;
import com.izizizi.service.JoinStringServiceLocator;
import com.izizizi.service.JoinStringServiceSoapBindingStub;

VE画面の「サンプル2」ボタンを右クリック→Events→actionPerformed

private JButton getSample1Button()メソッド内のpublic void actionPerformed()を以下へ修正

public void actionPerformed(java.awt.event.ActionEvent e) {    
	System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
	try{
		ReverseStringService myReverseStringService = getMyReverseString();
		ReverseString myReverseString = myReverseStringService.getReverseStringService();
		((ReverseStringServiceSoapBindingStub)myReverseString).setMaintainSession(true);
		String	reverse = myReverseString.getReverse(sample1AnswerTextField.getText());
		sample2AnswerLabel.setText(reverse);
	}catch(Exception e1){
		e1.printStackTrace();
	}
}

念のため以下AxisSampleApplication.java

/*
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.izizizi.client;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.izizizi.service.JoinString;
import com.izizizi.service.JoinStringService;
import com.izizizi.service.JoinStringServiceLocator;
import com.izizizi.service.JoinStringServiceSoapBindingStub;
import com.izizizi.service.ReverseString;
import com.izizizi.service.ReverseStringService;
import com.izizizi.service.ReverseStringServiceLocator;
import com.izizizi.service.ReverseStringServiceSoapBindingStub;
/**
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AxisSampleApplication extends JFrame {

	private javax.swing.JPanel jContentPane = null;
	private JLabel string1Label = null;
	private JLabel string2Label = null;
	private JTextField string1TextField = null;
	private JTextField string2TextField = null;
	private JTextField string3TextField = null;
	private JButton sample1Button = null;
	private JTextField sample1AnswerTextField = null;
	private JButton sample2Button = null;
	private JLabel sample2AnswerLabel = null;
	private JoinStringServiceLocator myJoinString = null;  //  @jve:decl-index=0:visual-constraint="639,38"
	private ReverseStringServiceLocator myReverseString = null;  //  @jve:decl-index=0:visual-constraint="640,122"
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getString1TextField() {
		if (string1TextField == null) {
			string1TextField = new JTextField();
			string1TextField.setBounds(93, 11, 505, 31);
		}
		return string1TextField;
	}
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getString2TextField() {
		if (string2TextField == null) {
			string2TextField = new JTextField();
			string2TextField.setBounds(95, 52, 501, 32);
		}
		return string2TextField;
	}
	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getString3TextField() {
		if (string3TextField == null) {
			string3TextField = new JTextField();
			string3TextField.setBounds(12, 99, 585, 30);
		}
		return string3TextField;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getSample1Button() {
		if (sample1Button == null) {
			sample1Button = new JButton();
			sample1Button.setBounds(229, 132, 168, 31);
			sample1Button.setText("サンプル1");
			sample1Button.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						JoinStringService myJoinStringService = getMyJoinString();
						JoinString myJoinString = myJoinStringService.getJoinStringService();
						((JoinStringServiceSoapBindingStub)myJoinString).setMaintainSession(true);
						myJoinString.setStr1(string1TextField.getText());
						myJoinString.setStr2(string2TextField.getText());
						String	marge = myJoinString.marge(string3TextField.getText());
						sample1AnswerTextField.setText(marge);
					}catch(Exception e1){
						e1.printStackTrace();
					}
				}
			});
		}
		return sample1Button;
	}
	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getSample1AnswerTextField() {
		if (sample1AnswerTextField == null) {
			sample1AnswerTextField = new JTextField();
			sample1AnswerTextField.setBounds(13, 167, 583, 32);
		}
		return sample1AnswerTextField;
	}
	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getSample2Button() {
		if (sample2Button == null) {
			sample2Button = new JButton();
			sample2Button.setBounds(227, 211, 165, 35);
			sample2Button.setText("サンプル2");
			sample2Button.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					try{
						ReverseStringService myReverseStringService = getMyReverseString();
						ReverseString myReverseString = myReverseStringService.getReverseStringService();
						((ReverseStringServiceSoapBindingStub)myReverseString).setMaintainSession(true);
						String	reverse = myReverseString.getReverse(sample1AnswerTextField.getText());
						sample2AnswerLabel.setText(reverse);
					}catch(Exception e1){
						e1.printStackTrace();
					}
				}
			});
		}
		return sample2Button;
	}
	/**
	 * This method initializes myJoinString	
	 * 	
	 * @return com.izizizi.service.JoinStringServiceLocator	
	 */    
	private JoinStringServiceLocator getMyJoinString() {
		if (myJoinString == null) {
			myJoinString = new JoinStringServiceLocator();
		}
		return myJoinString;
	}
	/**
	 * This method initializes myReverseString	
	 * 	
	 * @return com.izizizi.service.ReverseStringServiceLocator	
	 */    
	private ReverseStringServiceLocator getMyReverseString() {
		if (myReverseString == null) {
			myReverseString = new ReverseStringServiceLocator();
		}
		return myReverseString;
	}
  	public static void main(String[] args) {
		new AxisSampleApplication().show();
	}
	/**
	 * This is the default constructor
	 */
	public AxisSampleApplication() {
		super();
		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(621, 326);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			sample2AnswerLabel = new JLabel();
			string2Label = new JLabel();
			string1Label = new JLabel();
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			string1Label.setBounds(10, 9, 76, 32);
			string1Label.setText("文字1");
			string2Label.setBounds(10, 51, 78, 33);
			string2Label.setText("文字2");
			sample2AnswerLabel.setBounds(11, 257, 580, 27);
			sample2AnswerLabel.setText("");
			jContentPane.setFont(new java.awt.Font("MS ゴシック", java.awt.Font.PLAIN, 10));
			jContentPane.add(string1Label, null);
			jContentPane.add(string2Label, null);
			jContentPane.add(getString1TextField(), null);
			jContentPane.add(getString2TextField(), null);
			jContentPane.add(getString3TextField(), null);
			jContentPane.add(getSample1Button(), null);
			jContentPane.add(getSample1AnswerTextField(), null);
			jContentPane.add(getSample2Button(), null);
			jContentPane.add(sample2AnswerLabel, null);
		}
		return jContentPane;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
  • 実行

パッケージエスクプローラにてAxisSampleApplication.java を右クリック→実行→Javaアプリケーション