revisited

119
REVISITED KAZUHIROFUJIE

Upload: shunsaku-kudo

Post on 05-Dec-2014

3.321 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Revisited

REVISITED

KAZUHIRO FUJIE

Page 2: Revisited

intro code

Page 3: Revisited

intro digit

Page 4: Revisited

intro digit

Page 5: Revisited

Try to Create a WebServices Client

Kazuhiro FujieItochu techno-science corporation

Page 6: Revisited

Follow the white rabbit.

Page 7: Revisited

Follow the white rabbit.

Page 8: Revisited

Follow the white rabbit.

Page 9: Revisited

" 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

Page 10: Revisited

" 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

Page 11: Revisited

Red pill or Blue pill.

Page 12: Revisited

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

Nothing more. "

Morpheus

Page 13: Revisited

グラサン

Page 14: Revisited

Web Services Architecture

• よくみる図

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

Requester

Broker

Provider

find publish

bind

Page 15: Revisited

" There is no spoon. "

Neo

Page 16: Revisited

There is no spoon.

Page 17: Revisited

Program

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

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

• ローカルなプログラム

Main program Sub program

Page 18: Revisited

Program Code

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

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

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

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

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

return result;}

Page 19: Revisited

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

Persephone

Page 20: Revisited

Persephone

Page 21: Revisited

Separate Ways

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

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

Client ServerSocket…

Page 22: Revisited

RPC Remote Procedure Call

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

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

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

return result;}

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

処理結果送信

Page 23: Revisited

distributed environment

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

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

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

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

Page 24: Revisited

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

Page 25: Revisited

" 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

Page 26: Revisited

Stub / Skeleton

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

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

Sub programMain program

Page 27: Revisited

Stub / Skeleton ( Tie )

• そのための仕組み。

Main program Sub program

Stub Skeleton

Runtime Runtime

Transport

Interface Language

Interface Language

Page 28: Revisited

Web Services

• JAX-RPC

Web Service

ImplementationStub

Tie (Skeleton)

JAX-RPC Runtime

SOAP/HTTP

JAX-RPC Runtime

Web Service Client

Application

WSDL WSDL

Page 29: Revisited

Twins 1

Page 30: Revisited

Twins 2

Page 31: Revisited

Twins 3

Page 32: Revisited

Twins 4

Page 33: Revisited

" That’s a nice trick. "

Trinity

Page 34: Revisited

Trinity

Page 35: Revisited

JAX-RPC

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

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

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

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

Page 36: Revisited

" I Love Candy. "

The Oracle

Page 37: Revisited

The Oracle

Page 38: Revisited

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

I see providence.I see purpose. "

Morpheus

Page 39: Revisited

3 objectives,3 captains, 3 ships

Page 40: Revisited

The Magi

• 東方の三賢人

–Gaspar (Caspar), Melchior, Balthazar

• 三種の神器

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

• Webサービス

–SOAP, WSDL, UDDI

Page 41: Revisited

Web Services Standards

• SOAP–Simple Object Access Protocol

• WSDL–Web Services Description Language

• UDDI–Universal Description, Discovery, and

Integration

Page 42: Revisited

" Free your mind "

Morpheus

Page 43: Revisited

Morpheus

Page 44: Revisited

Web Services Architecture

• もういちど。

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

Client

UDDI registry

Service

find publish

SOAP

WSDL

Page 45: Revisited

" That means buckle your seat belt, Dorothy,

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

Cypher

Page 46: Revisited

Cypher

Page 47: Revisited

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

Google Web APIs

Page 48: Revisited

" 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

Page 49: Revisited

Google Web Services

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

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

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

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

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

Page 50: Revisited

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

Page 51: Revisited

Google Step 1/3

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

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

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

• アカウントの登録

• ライセンスの取得

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

Page 52: Revisited

Google Step 2/3

• スタブクラスの作成

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

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

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

–wscompile で半自動生成。

Page 53: Revisited

Google Step 3/3

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

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

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

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

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

• コンパイル

• 実行

Page 54: Revisited

Execution Result

Page 55: Revisited

" Okey, dokey. "

Neo

Page 56: Revisited

Neo Jump

Page 57: Revisited

やってみましょう。

Demonstration

Page 58: Revisited

" I know Kung Fu. "

Neo

Page 59: Revisited

Neo

Page 60: Revisited

" Show me. "

Morpheus

Page 61: Revisited

Show me

Page 62: Revisited

いまみたこと

Revisited

Page 63: Revisited

WSDLをちょっとのぞく。

Look into Web Services Description Language

Page 64: Revisited

WSDL Document Structure

TypesWSDL Document

Message

Port Type

Binding

ServicePort

Operation

Page 65: Revisited

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>:

Page 66: Revisited

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">:

Page 67: Revisited

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>

Page 68: Revisited

" 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)

Page 69: Revisited

スタブを用意する。

Generate Static Stub

Page 70: Revisited

Generate Stub

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

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

–ex. config-wsdl.xml

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

Page 71: Revisited

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>

Page 72: Revisited

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

creating a new effect, just like … poetry. "

Merovingian

Page 73: Revisited

starts so simply

Page 74: Revisited

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

Coding a Static Stub Client

Page 75: Revisited

Static Stub 1/4Creates a Stub object

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

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

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

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

Page 76: Revisited

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");

Page 77: Revisited

Static Stub 3/4 Cast stub to the service

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

型変換する。

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

GoogleSearchPort google = (GoogleSearchPort)stub;

Page 78: Revisited

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 );

Page 79: Revisited

" I wrote it myself. "

Merovingian

Page 80: Revisited

Merovingian

Page 81: Revisited

" Have fun. "

Persephone

Page 82: Revisited

Persephone

Page 83: Revisited

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

Reloaded

Page 84: Revisited

" 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

Page 85: Revisited

Councillor Hamann

Page 86: Revisited

tcpmon

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

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

• Apache AXIS に付属。

• 多少難在り。

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

Page 87: Revisited

Actual tcpmon

Page 88: Revisited

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>

Page 89: Revisited

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>

Page 90: Revisited

" Do you proceed? "

Agent (not Smith)

Page 91: Revisited

Agents

Page 92: Revisited

ふろく

Appendix

Page 93: Revisited

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

Call Model of Web Services Client with JAX-RPC

Page 94: Revisited

JAX-RPC Client Types

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

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

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

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

– 複雑なコーディング。

Page 95: Revisited

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

Coding a Dynamic Proxy Client

Page 96: Revisited

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));

Page 97: Revisited

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);

Page 98: Revisited

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

Coding a DII Client

Page 99: Revisited

DII 1/6Creates a Service object

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

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

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

Page 100: Revisited

DII 2/6Creates a Call object

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

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

String qnamePort = "GoogleSearchPort";

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

Page 101: Revisited

DII 3/6 Set a endpoint address

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

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

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

Page 102: Revisited

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/");

Page 103: Revisited

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);

Page 104: Revisited

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);

Page 105: Revisited

" There is a difference between knowing the road

and following it. "

Morpheus

Page 106: Revisited

Morpheus and Neo

Page 107: Revisited

終わりに

Conclusion

Page 108: Revisited

Round up

• WebServices は、RPCとも云える

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

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

• WSDLは、一番だいじ。

• WSDLからStubができる

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

Page 109: Revisited

Just after this

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

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

• WSDLを詳しくみる

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

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

Page 110: Revisited

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

through it. "

Morpheus

Page 111: Revisited

Morpheus and Neo

Page 112: Revisited

Bibliography

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

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

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

– ISBN4-87311-101-3

Page 113: Revisited

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

Page 114: Revisited

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

Page 115: Revisited

Resources

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

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

Page 116: Revisited

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/

Page 117: Revisited

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

Means, ‘Know thyself’. "

The Oracle

Page 118: Revisited

‘Know thyself’

Page 119: Revisited

TO BE CONCLUDED.