RPC アダプター・ライブラリーの概要
Web リモートは、JavaScriptTM またはクライアント・サイド・コードがサーバー・サイド・ロジックを直接呼び出すのをサポートするパターンです。
このパターンは、JavaTM メソッドを JavaScript から呼び出す機能を備えています。
呼び出し方法は、JSON-RPC 呼び出しです。最も一般的な使用法は、XmlHttpRequest による非同期呼び出しです。
データは、サーバーおよびクライアント間を JSON フォーマットで転送されます。したがって、このパターンは、基本的には JSON Web サービスの形式の 1 つです。
RPC アダプターの紹介
Web リモートの IBM® 実装は、IBM では RPC アダプターと呼んでいます。RPC アダプターは、Ajax アプリケーションやその他の軽量クライアントに対するプログラミング・スタイルを補完する方法で、開発者がコマンド・ベースのサービスを迅速かつ容易に作成できるように設計されています。
汎用サーブレットとして実装されている RPC アダプターは、登録済み JavaBeans に HTTP インターフェースを提供します。
RPC アダプターは、現在、次の 2 つの RPC プロトコルをサポートします。
- HTTP RPC。クエリー・パラメーター (HTTP GET の場合) またはフォーム・パラメーター (HTTP POST の場合) を指定した URL として RPC 呼び出しをエンコードします。
- JSON-RPC。Dojo dojo.rpc.JsonService API によって使用される SMD (Simple Method Description) サービス記述子をサポートします。
HTTP RPC
HTTP RPC での呼び出しは、クエリー・パラメーターまたはフォーム・パラメーターを指定した URL で実行します。
RPC アダプターは、URL をインターセプトおよびデシリアライズして、サービス名、メソッド名、および入力パラメーターを取得します。
これらの情報を使用して、RPC アダプターは一致する JavaBeans の対応メソッドを呼び出します。
例えば、クライアントは、以下の JavaBeans で test.Example.getEcho("Hello world")
を呼び出すことができます。
package test;
public class Example {
public String getEcho(String message) {
return(message);
}
}
以下のように、HTTP GET を使用する HTTP RPC を介して test.Example.getEcho("Hello world")
を呼び出します。
GET /contextRoot/RPCAdapter/httprpc/example/echo?p0=Hello%20world HTTP/1.1
これらすべての呼び出しは、アプリケーションを処理できるように RPC アダプターを適切に構成した後に行う必要があります。
適切に構成するためには、RPCAdapter Java アーカイブ (JAR) ファイルを WAR ファイルの WEB-INF/lib に追加し、web.xml ファイルに以下のエントリーを作成する必要があります。
RPCAdapter
RPCAdapter
com.ibm.websphere.rpcadapter.RPCAdapter
RPCAdapter
/RPCAdapter
RPCAdapter
/RPCAdapter/*
WEB-INF ディレクトリーに RpcAdapterConfig.xml ファイルを指定します。
このファイルは、エクスポートするサービスを構成するために使用されます。
この場合、サンプルの RpcAdapterConfig.xml ファイルは次のようになります。
xml
サンプル
test.pojo.Sample
派生 HTTP RPC API は、最初の引数の名前に p0 を割り当てます。
このデフォルトの動作は、Java リフレクション API における現在の制約によります。Bean 記述子情報を使用して、または RpcAdapterConfig.xml ファイルのパラメーター名を指定することによって、意味のわかるパラメーター名を割り当てることができます。
例えば、次の Bean の getEcho() を見てください。
package test;
import com.ibm.websphere.rpcadapter.SelfBeanInfo;
public class Example2 implements SelfBeanInfo {
public String getEcho(String message) {
return(message);
}
public static String[][] getBeanDescriptorInfo() {
String [][] mds = {
{"method", "getEcho", "Echoes the input String.", "GET", "message", "String to be echoed."},
};
return(mds);
}
}
上の例は、次のように呼び出すことができます。
GET /contextRoot/httprpc/example/echo?message=Hello%20world HTTP/1.1
package test;
public class Example2 {
public String getEcho(String message) {
return(message);
}
}
上の例は、後述の RpcAdapterConfig.xml を使用して次のように呼び出すことができます。
GET /contextRoot/httprpc/example/echo?message=Hello%20world HTTP/1.1
xml
example
test.pojo.Example2
echo
echos the message
message
Contains the message to be echo'd.
構成ファイルの default-format エレメントで、フォーマットが要求内に指定されていない場合に使用するデフォルト・フォーマットを指定します。このフォーマットは XML または JSON にすることができます。
JSON-RPC
JSON-RPC のメソッド呼び出しは、JSON オブジェクトを使用して行われます。
生成される応答も JSON オブジェクトです。登録済み JavaBeans は、Dojo JSON-RPC API を介してアクセスすることができます。
var example = new dojo.rpc.JsonService("/contextRoot/RPCAdapter/jsonrpc/example");
example.getEcho('Hello world').addCallback(exampleCallback);
function exampleCallback(result) {
// response is available as result.getEchoReturn.String
}
Bean 記述子情報の指定
RPC アダプターでは、Bean 記述子情報を使用して、使用可能な JavaBeans に関する情報を指定します。
Bean 記述子を指定しない場合は、情報はリフレクションによって取得されます。
Bean 記述子情報は、2 つの方法で指定できます。
以下に示す Sample という JavaBeans を検討します。
package test.pojo;
import java.util.List;
import java.util.Vector;
/**
* Example showing custom Bean access specified with a separate BeanInfo class.
* This sample is instructive for cases where the Bean class cannot be modified.
*
* @see test.pojo.SampleBeanInfo
* @see test.pojo.SampleNoBeanInfo
* @see test.pojo.SampleSelfBeanInfo
*/
public class Sample {
/**
* Echoes the input String. This stateless operation works correctly
* regardless of the setting of the bean descriptor oneInstancePerUser
.
*
* @param message String to be echoed.
*/
public String echo(String message) {
return(message);
}
public String echoNoArg() {
return("Hello World");
}
public boolean echoBoolean(boolean b) {
return(b);
}
public boolean echoIntegerBean(boolean b) {
return(b);
}
List _values = new Vector();
/**
* Adds a value to a list that can be retrieve with the getValues
method.
* This stateful scenario requires that the bean descriptor oneInstancePerUser
* be set to true.
*
* @param value String added to the list.
*/
public void addValue(String value) {
_values.add(value);
}
/**
* Returns the cumulative list of values added with the addValue
method.
* This stateful scenario requires that the bean descriptor oneInstancePerUser
* be set to true.
*
* @return List of Strings previously added to the list.
*/
public List getValues() {
return(_values);
}
}
Sample JavaBeans を記述するために、SampleBeanInfo という BeanInfo オブジェクトを作成します。
BeanInfo クラスにその親として SimplerBeanInfo がどのように含まれているかに注意してください。
また、パラメーターに Sample.class を指定したコンストラクターから setBeanClass を呼び出す必要があることにも注意してください。
package test.pojo;
import com.ibm.websphere.rpcadapter.SimplerBeanInfo;
/**
* Example BeanInfo class showing declarative-type descriptor information
* supported by the RPC adapter.
*
* @see test.pojo.Sample
*/
public class SampleBeanInfo extends SimplerBeanInfo {
/**
* SimplerBeanInfo uses Java Reflection; must set the Bean class first.
*/
public SampleBeanInfo() {
setBeanClass(Sample.class);
}
public String[][] getBeanDescriptorInfo() {
String [][] mds = {
{"bean", "oneInstancePerUser", "true"},
{"method", "echo", "Echoes the input String.", "GET", "message", "String to be echoed."},
{"method", "echoNoArg", "Echoes Hello World.", "GET" },
{"method", "addValue", "Adds a value to a list.", "GET", "value", "value to add"},
{"method", "getValues", "Returns the list.", "GET"},
{"method", "echoBoolean", "Echoes a boolean value.", "GET", "b", "Boolean to be echoed."},
};
return(mds);
}
}
Bean 記述子を指定するもう 1 つの方法は、Bean 記述子情報を Bean クラス自体に含める方法です。Bean クラスが SelfBeanInfo インターフェースを実装することに注意してください。
また、JavaBeans の情報を返す静的メソッド getBeanDescriptorInfo() も含める必要があります。
package test.pojo;
import java.util.List;
import java.util.Vector;
import com.ibm.websphere.rpcadapter.SelfBeanInfo;
/**
* Example showing custom Bean access specified with embedded descriptive
* information. This sample is instructive for cases where the Bean can be
* modified.
*
* @see test.pojo.SampleNoBeanInfo
* @see test.pojo.Sample
*/
public class SampleSelfBeanInfo implements SelfBeanInfo {
/**
* Echoes the input String. This stateless operation works correctly
* regardless of the setting of the bean descriptor oneInstancePerUser
.
*
* @param message String to be echoed.
*/
public String echo(String message) {
return(message);
}
List _values = new Vector();
/**
* Adds a value to a list that can be retrieve with the getValues
method.
* This stateful scenario requires that the bean descriptor oneInstancePerUser
* be set to true.
*
* @param value String added to the list.
*/
public void addValue(String value) {
_values.add(value);
}
/**
* Returns the cumulative list of values added with the addValue
method.
* This stateful scenario requires that the bean descriptor oneInstancePerUser
* be set to true.
*
* @return List of Strings previously added to the list.
*/
public List getValues() {
return(_values);
}
public static String[][] getBeanDescriptorInfo() {
String [][] mds = {
{"bean", "oneInstancePerUser", "true"},
{"method", "echo", "Echoes the input String.", "GET", "message", "String to be echoed."},
{"method", "addValue", "Adds a value to a list.", "GET", "value", "value to add"},
{"method", "getValues", "Returns the list.", "GET"},
};
return(mds);
}
}
BeanInfo を使用したくない場合は、クラスパスに BeanInfo クラスが存在してはならないことに注意してください。これは、BeanInfo を POJO クラス名に追加することによって RPC アダプターが BeanInfo クラスを検査し、その情報を最初にロードしようとするためです。
また、BeanDescriptors を使用するかどうかに関係なく、公開する必要があるオブジェクトを RPCAdapterConfig.xml に登録する必要もあります。
パッケージ構造
このライブラリーはスタンドアロン版で配布されます。
スタンドアロン・パッケージ
WebSphere® Application Server V8.5 の場合、このパッケージは、以下のように構成されています。
|
<install_root>/optionalLibraries/web2mobile/rpcAdapter/RPCAdapter.jar
|
API を
実装する Java クラス・ファイル。Java アーカイブ (JAR) フォーマットで
パッケージされています。 |
|