revisited

Post on 05-Dec-2014

3.321 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

2004/09/10 Talk about Try to create a Client of Web Services

TRANSCRIPT

REVISITED

KAZUHIRO FUJIE

intro code

intro digit

intro digit

Try to Create a WebServices Client

Kazuhiro FujieItochu techno-science corporation

Follow the white rabbit.

Follow the white rabbit.

Follow the white rabbit.

" Have you ever had a dream, Neo, that you were so sure

was real?What if you were unable to

wake from that dream, Neo?How would you know the difference between the

dream world and the real. "

Morpheus

" You take the blue pill, the story ends; you wake up in

your bed and believe... whatever you want to believe.

You take the red pill, you stay in Wonderland; and I show you how deep the

rabbit hole goes. "

Morpheus

Red pill or Blue pill.

" Remember ... all I'm offering you is the truth.

Nothing more. "

Morpheus

グラサン

Web Services Architecture

• よくみる図

• 初めて観た時は、何だか新しいモノのような気がしたけど…

Requester

Broker

Provider

find publish

bind

" There is no spoon. "

Neo

There is no spoon.

Program

• 普通のアプリケーションプログラム

• 一つのマシンで実行する

• ローカルなプログラム

Main program Sub program

Program Code

• ローカルなプログラムの場合、例えば…

• 同じマシンでしか実行できない…

/* メインプログラム。サブルーチンを呼び出す。 */String result = doSpellingSuggestion("britnay spars");:

/* サブルーチン*/String doSpellingSuggestion(String phrase) {

特定の機能を担うサブルーチン。例えば、スペル候補の処理などを行い結果を返り値として返す。

return result;}

" He was so different.He was like you. "

Persephone

Persephone

Separate Ways

• 分散環境のアプリケーションでは?– 別々のマシンで実行するために…

• Socket, PIPE, …– 自分で通信のやり取りの中味を考える必要がある。

Client ServerSocket…

RPC Remote Procedure Call

• 基本的に同じように簡単に行いたい。/* メインプログラム。サブルーチンを呼び出す。 */String result = doSpellingSuggestion("britnay spars");:

/* サブルーチン*/String doSpellingSuggestion(String phrase) {

特定の機能を担うサブルーチン。例えば、スペル候補の処理などを行い結果を返り値として返す。

return result;}

APIのフック通信処理 サーバー受信

処理結果送信

distributed environment

• 分散環境コンピューティング

• プログラムが離れたマシンの上にあるサブルーチンを、同じマシン上にあるかのように簡易に呼び出すことができる。

• Remote Procedure Call (RPC)– 遠隔手続き呼び出し

• Distributed Object– 分散オブジェクト

Genealogy of distributed environment

• Sun RPC– ONC-RPC Open Network Computing– XDR (eXternal Data Representations)

• DCE RPC (OSF)– Distributed Computing Environment (DCE)

• CORBA (OMG)– CORBA (Common Object Request Broker Architecture)– CORBA IDL (Interface Definition Language) – IIOP (Internet Inter-ORB Protocol)

• Java RMI– Remote Method Invocation (RMI)– Java Interface

" I’ll let you in on a little secret.Being the one is just like being

love.No one can tell you you’re in

love.You just knew it…

through and through.Balls to bones. "

The Oracle

Stub / Skeleton

• 離れていても同じように。

• なるべく簡単に出来るための…

Sub programMain program

Stub / Skeleton ( Tie )

• そのための仕組み。

Main program Sub program

Stub Skeleton

Runtime Runtime

Transport

Interface Language

Interface Language

Web Services

• JAX-RPC

Web Service

ImplementationStub

Tie (Skeleton)

JAX-RPC Runtime

SOAP/HTTP

JAX-RPC Runtime

Web Service Client

Application

WSDL WSDL

Twins 1

Twins 2

Twins 3

Twins 4

" That’s a nice trick. "

Trinity

Trinity

JAX-RPC

• Java APIs for XML-based RPC–JavaでSOAPとWSDLを使った開発ができる。

• Standards– JCPによる標準仕様であるJSR 101

• Interoperability– クライアントのAPI、ツールやランタイム実装な

どについて決められてポータブルなアプリケーションを実現できるようにの工夫がされている。

" I Love Candy. "

The Oracle

The Oracle

" When I see 3 objectives,3 captains, 3 ships …I do not coincidence.

I see providence.I see purpose. "

Morpheus

3 objectives,3 captains, 3 ships

The Magi

• 東方の三賢人

–Gaspar (Caspar), Melchior, Balthazar

• 三種の神器

– 洗濯機, 冷蔵庫, 白黒テレビ

• Webサービス

–SOAP, WSDL, UDDI

Web Services Standards

• SOAP–Simple Object Access Protocol

• WSDL–Web Services Description Language

• UDDI–Universal Description, Discovery, and

Integration

" Free your mind "

Morpheus

Morpheus

Web Services Architecture

• もういちど。

• UDDIを無視すれば、RPCと観ることも。

Client

UDDI registry

Service

find publish

SOAP

WSDL

" That means buckle your seat belt, Dorothy,

`cause Kansas is going` bye-bye.. "

Cypher

Cypher

クライアントを作ってみる。

Google Web APIs

" But this is not a reason,this is not a why,

The keymaker himself, his very nature, is a means, it

is not an end. "

Merovingian

Google Web Services

• Google Web APIs (beta)–Google 提供のWebサービスのベータ版

– スペルの候補を教えてくれる?みたい。

– キーワードでの検索などができる?みたい。

• Webサービスのクライアントをつくってみる。– 標準であるJAX-RPC のAPIをつかう。

– 予めStubを作成しておいて、それに面倒な事は、お任せしたプログラムを作成する。

Google Step 0/3

• JDK– http://java.sun.com/j2se/1.4/

• JWSDP–Java Web Services Developer Pack– http://java.sun.com/webservices/jwsdp/

– JAX-RPC API, Runtime, Tools

Google Step 1/3

• サービスのWSDLを入手する。

–Webサービスを利用するための情報

• Download the developer's kit– http://www.google.co.jp/apis/index.html

• アカウントの登録

• ライセンスの取得

– 検索機能を利用するためのライセンスキー

Google Step 2/3

• スタブクラスの作成

–WSDLインターフェースからスタブを生成する。

• 公開されているWSDL– http://api.google.com/GoogleSearch.wsdl

• ツールでスタブを生成。

–wscompile で半自動生成。

Google Step 3/3

• クライアントプログラムの作成

– 必要なパッケージのインポート。

– スタブクラスのインスタンス生成。

– エンドポイントアドレスの指定。

– (遠隔オブジェクトの)メッセージの呼び出し。

• コンパイル

• 実行

Execution Result

" Okey, dokey. "

Neo

Neo Jump

やってみましょう。

Demonstration

" I know Kung Fu. "

Neo

Neo

" Show me. "

Morpheus

Show me

いまみたこと

Revisited

WSDLをちょっとのぞく。

Look into Web Services Description Language

WSDL Document Structure

TypesWSDL Document

Message

Port Type

Binding

ServicePort

Operation

Google WSDL

http://api.google.com/GoogleSearch.wsdl•

<?xml version="1.0" ?>

<!-- Revision 2002-08-16 --> <definitions name="GoogleSearch"

targetNamespace="urn:GoogleSearch"xmlns:typens="urn:GoogleSearch"xmlns:xsd=http://www.w3.org/2001/XMLSchemaxmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/xmlns:soapenc=http://schemas.xmlsoap.org/soap/encoding/xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>:

Google WSDL cont.

http://api.google.com/GoogleSearch.wsdl•

:<!-- Messages for Google Web APIs - cached page, search, spelling. --><message name="doSpellingSuggestion"><part name="key" type="xsd:string" /><part name="phrase" type="xsd:string" />

</message>

<!-- Port for Google Web APIs, "GoogleSearch" --> <portType name="GoogleSearchPort">

<!-- Binding for Google Web APIs - RPC, SOAP over HTTP --><binding name="GoogleSearchBinding"

type="typens:GoogleSearchPort">:

Google WSDL cont.

http://api.google.com/GoogleSearch.wsdl•

:<!-- Endpoint for Google Web APIs --><service name="GoogleSearchService">

<port name="GoogleSearchPort"binding="typens:GoogleSearchBinding">

<soap:address location="http://api.google.com/search/beta2" />

</port></service>

</definitions>

" It is purpose that created us.Purpose that connects us.

Purpose that pull us.That guides us.That drives us.

It is purpose that defines.Purpose that bind us."

Smith (not Agent)

スタブを用意する。

Generate Static Stub

Generate Stub

• WSDLを基にスタブクラスを生成する。

• スタブクラス生成について設定ファイルで指示できる。

–ex. config-wsdl.xml

wscompile -verbose -gen:client -d build config-wsdl.xml

wscompile configuration

• wscompile 設定ファイルの例

• ダウンロード済みWSDLを指定しても可。

<?xml version="1.0" encoding="UTF-8" ?><configuration

xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"><wsdl location="http://api.google.com/GoogleSearch.wsdl"

packageName="jp.co.ctc_g.atd.google.stub" /></configuration>

" It’s starts so simply…each line of the program

creating a new effect, just like … poetry. "

Merovingian

starts so simply

クライアントのコードを書く。

Coding a Static Stub Client

Static Stub 1/4Creates a Stub object

• Stubオブジェクトを生成する。

• 実装依存のオブジェクトからスタブを生成する。

• 実装依存のオブジェクトは、wscompileによって生成されたもの。

Stub stub =(Stub) (new GoogleSearchService_Impl().getGoogleSearchPort());

Static Stub 2/4 Set a endpoint address

• サービスのアドレスを設定する。

• アドレスはURLをStringで指定する。– http://api.google.com/search/beta2

stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,

"http://api.google.com/search/beta2");

Static Stub 3/4 Cast stub to the service

endpoint interface• サービスエンドポイントインターフェースに

型変換する。

• これで該当のメソッドを呼び出す事が可能。

GoogleSearchPort google = (GoogleSearchPort)stub;

Static Stub 4/4 Coding a Client

• クライアントコードの一部Stub stub =

(Stub) (new GoogleSearchService_Impl().getGoogleSearchPort());

endpointAddress = "http://api.google.com/search/beta2";stub._setProperty

(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);

GoogleSearchPort google = (GoogleSearchPort)stub;

// Call doSpellingSuggestion of Google Web ServicesString result

= google.doSpellingSuggestion(googleLicenseKey, phrase );System.out.println( "==> Candidate : " + result );

" I wrote it myself. "

Merovingian

Merovingian

" Have fun. "

Persephone

Persephone

もういちどやってみましょう。

Reloaded

" Almost no one comes down here.Unless, of course, there’s a

problem.That’s new it is with people.

Nobody cares how it works as long as it works. "

Councillor Hamann

Councillor Hamann

tcpmon

• SOAPで通信していることを視認する。

• tcpmon モニタでやり取りを診る。

• Apache AXIS に付属。

• 多少難在り。

set CLASSPATH=%AXIS_HOME%¥lib¥axis.jarjava org.apache.axis.utils.tcpmon

Actual tcpmon

SOAP Request

<?xml version="1.0" encoding="UTF-8" ?><env:Envelope

xmlns:env=http://schemas.xmlsoap.org/soap/envelope/xmlns:xsd=http://www.w3.org/2001/XMLSchemaxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:enc=http://schemas.xmlsoap.org/soap/encoding/xmlns:ns0="urn:GoogleSearch"env:encodingStyle

="http://schemas.xmlsoap.org/soap/encoding/"><env:Body>

<ns0:doSpellingSuggestion><key xsi:type="xsd:string">XXXXX</key><phrase xsi:type="xsd:string">britnay spars</phrase>

</ns0:doSpellingSuggestion></env:Body>

</env:Envelope>

SOAP Response

<?xml version="1.0" encoding="UTF-8" ?><SOAP-ENV:Envelope

xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/xmlns:xsi=http://www.w3.org/1999/XMLSchema-instancexmlns:xsd="http://www.w3.org/1999/XMLSchema"><SOAP-ENV:Body>

<ns1:doSpellingSuggestionResponsexmlns:ns1="urn:GoogleSearch“SOAP-ENV:encodingStyle

="http://schemas.xmlsoap.org/soap/encoding/"><return xsi:type="xsd:string">britney spears</return>

</ns1:doSpellingSuggestionResponse></SOAP-ENV:Body>

</SOAP-ENV:Envelope>

" Do you proceed? "

Agent (not Smith)

Agents

ふろく

Appendix

クライアントの種類について

Call Model of Web Services Client with JAX-RPC

JAX-RPC Client Types

• Static Stub– 実行前にスタブを用意しておく。

• Dynamic Proxy– 実行前にインターフェースだけが必要。

– 実行時にプロキシクラスを作成する。

• Dynamic Invocation Interface (DII)– 直接ランタイムにアクセスする。

– 複雑なコーディング。

クライアントのコードを書く。其の弐

Coding a Dynamic Proxy Client

Dynamic Proxy 1/2Creates a Service object

• Proxy オブジェクトを生成する。

• WSDLアドレスはURLをStringで指定する。– http://api.google.com/GoogleSearch.wsdl

• サービス名、名前空間はWSDLの記載をStringで指定する。

Service googleService = serviceFactory.createService(googleWsdlUrl,

new QName(nameSpaceUri, serviceName));

Dynamic Proxy 2/2 Creates a proxy with a

service endpoint interface• インターフェースオブジェクトを生成する。

• getPort メソッドで呼び出す。

• エンドポイントインターフェースはwscompile などで事前に用意が必要。

GoogleSearchPort googleProxy = (GoogleSearchPort)googleService.getPort(new QName(nameSpaceUri, portName),GoogleSearchPort.class);

クライアントのコードを書く。其の参

Coding a DII Client

DII 1/6Creates a Service object

• Serviceオブジェクトを生成する。

• サービス名はWSDLでの service name を Stringで指定する。

Service service = factory.createService(new QName(qnameService));

DII 2/6Creates a Call object

• Call オブジェクトを生成する。

• ポート名はWSDLでの port name をStringで指定する。

String qnamePort = "GoogleSearchPort";

QName port = new QName(qnamePort);Call call = service.createCall(port);

DII 3/6 Set a endpoint address

• サービスエンドポイントアドレスを設定する。

• アドレスはURLをStringで指定する。– http://api.google.com/search/beta2

call.setTargetEndpointAddress("http://api.google.com/search/beta2");

DII 4/6 Set a properties

• プロパティを設定する。

• 詳細は、SOAP及びWSDLを参照の事。

call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true));

call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");call.setProperty(ENCODING_STYLE_PROPERTY,

"http://schemas.xmlsoap.org/soap/encoding/");

DII 5/6 Specify parameter

• 返り値や引数の型や呼び出すメソッドを指定する。

QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");

call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(

new QName(BODY_NAMESPACE_VALUE, "doSpellingSuggestion"));call.addParameter("String_1", QNAME_TYPE_STRING,

ParameterMode.IN);call.addParameter("String_2", QNAME_TYPE_STRING,

ParameterMode.IN);

DII 6/6 Invoke a remote method

• メソッドを呼び出す。

• ここではStringの配列で引数を渡す。

• ここでの返り値はString。

String googleLicenseKey = “XXXXXXXXXXXXXX";String phrase = "britnay spars";String[] params = { googleLicenseKey, phrase };String result = (String)call.invoke(params);

" There is a difference between knowing the road

and following it. "

Morpheus

Morpheus and Neo

終わりに

Conclusion

Round up

• WebServices は、RPCとも云える

• RPCは、Stub/Skeletonという仕組み

• JAX-RPCは、SOAPとWSDL• WSDLは、インターフェース

• WSDLは、一番だいじ。

• WSDLからStubができる

• JAX-RPCでクライアントをつくる

Just after this

• サーバーであるサービスをつくる

• 相互運用性について考える

• WSDLを詳しくみる

• SOAPの構造、特徴を調べる

• そして、UDDI?• BPELとは?

" But I can only show you the door, you have to walk

through it. "

Morpheus

Morpheus and Neo

Bibliography

• XMLとJavaによるWebアプリケーション開発 第2版– 丸山 宏ほか

– ISBN:4-89471-662-3• Java Webサービス

–David A.Chappell, Tyler Jewell (著), テクノロジックアート (著), 長瀬 嘉秀 (翻訳)

– ISBN4-87311-101-3

Philosophy

• EXISTENTIAL PHENOMENOLOGY AND THE BRAVE NEW WORLD OF THE MATRIX –HUBERT DREYFUS & STEPHEN

DREYFUS– http://whatisthematrix.warnerbros.com/

• PHILOSOPHY AND THE MATRIX– http://whatisthematrix.warnerbros.com/rl_cm

p/phi.html

Resources

• Java Technology and Web Services– http://java.sun.com/webservices/

• Java API for XML-Based RPC (JAX-RPC) Downloads & Specifications– http://java.sun.com/xml/downloads/jaxrpc.ht

ml

Resources

• Google Web APIs (beta)– http://www.google.co.jp/apis/index.html

• Apache AXIS– http://ws.apache.org/axis/index.html

Resources

• Web Services Description Language (WSDL) 1.1– http://www.w3.org/TR/wsdl

• Simple Object Access Protocol (SOAP) 1.1– http://www.w3.org/TR/soap/

" You knew what that mean?It’s Latin.

Means, ‘Know thyself’. "

The Oracle

‘Know thyself’

TO BE CONCLUDED.

top related