var obj =
{
“key1” : “val1” ,
“key2” : { “key21” : “val21” , “key22” : “val22” } ,
“key3” : [ “val31” , “val32” , “val33” ]
}
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name> <servlet-class>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name> <url-pattern>/JSON-RPC</url-pattern> </servlet-mapping> </web-app>
JSON-RPC-JAVA-ORB のほとんどの機能を検証できる練習プログラムを作りました。
public final class ExampleBean implements Serializable{
private static final long serialVersionUID = -8306703160077279386L;
private ExampleBean father;
private ExampleBean mother;
private String name;
private int age;
…(以下 setter/getter )…
}
最初に、JSON Bridgeを初期化して、クライアント(Webブラウザ)とサーバ(J2EEコンテナ)で通信が出来るようにします。
(HelloJSON.jsp)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="JSONRPCBridge" scope="session"
class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
<% JSONRPCBridge.registerObject("hello", new com.snail.web.json.HelloJSON() ); %>
<html>
<head>
<title>JSON-RPC-Java Hello</title>
<script type="text/javascript" src="jsonrpc.js"></script>
<script language="javascript">
<!--
function init(){
jsonrpc = new JSONRpcClient("JSON-RPC");
}
…(その他のJavascriptプログラム)…
//-->
</script>
</head>
<body bgcolor="#ffffff" onLoad="init();">
<h2>JSON-RPC-Java Examinations</h2>
…(HTML)…
</body>
</html>
public String sayHello(String name) {
return "Hello " + name + "!";
}
function clickHello(){
var whoNode = document.getElementById("who");
var result = jsonrpc.hello.sayHello(whoNode.value);
alert("The server replied: " + result);
}
public String setMap(Map map) {
String name = (String) map.get("Name");
String age = (String) map.get("Age");
String father = (String) map.get("Father");
String mother = (String) map.get("Mother");
return name + " is " + age + " years old. His father is " + father
+ " and mother is " + mother + ".";
}
function clickSendMap(){
try{
var age = document.getElementById("age").value;
var name = document.getElementById("name").value;
var father = document.getElementById("father").value;
var mother = document.getElementById("mother").value;
var jsonMsg =
{"javaClass" : "java.util.HashMap",
"map": {"Age": age, "Name": name, "Father": father, "Mother": mother}
};
alert( jsonrpc.hello.setMap(jsonMsg) );
}catch(e){
alert(e);
}
}
public String setBean(ExampleBean bean) {
String name = bean.getName();
int age = bean.getAge();
String mother = bean.getMother().getName();
return mother + " brought " + name + " into the world " + age
+ " years ago.";
}
function clickSendBean(){
try{
var age = document.getElementById("age").value;
var name = document.getElementById("name").value;
var father = document.getElementById("father").value;
var mother = document.getElementById("mother").value;
var jsonMsg =
{"javaClass": "com.snail.web.json.ExampleBean",
"age": age,
"name": name,
"father":
{"javaClass": "com.snail.web.json.ExampleBean",
"name": father},
"mother":
{"javaClass": "com.snail.web.json.ExampleBean",
"name": mother}
};
alert( jsonrpc.hello.setBean(jsonMsg) );
}catch(e){
alert(e);
}
}
public String setList(List list) {
return "Argument List is" + list;
}
function clickSendList(){
try{
var age = document.getElementById("age").value;
var name = document.getElementById("name").value;
var father = document.getElementById("father").value;
var mother = document.getElementById("mother").value;
var jsonMsg =
{"javaClass": "java.util.LinkedList",
"list": [age,name,father,mother] };
alert( jsonrpc.hello.setList(jsonMsg) );
}catch(e){
alert(e);
}
}
public Map sayMap() {
Map<String, String> m = new LinkedHashMap<String, String>();
m.put("FirstElement", "Enterprise");
m.put("SecondElement", "Constitution");
m.put("ThirdElement", "Yamato");
m.put("FourthElement", "Melbourne");
return m;
}
function clickGetMap(){
var ret = jsonrpc.hello.sayMap();
document.getElementById("jsonMsg").innerHTML=toJSON(ret);
document.getElementById("javaClass").innerHTML=ret.javaClass;
document.getElementById("obj").innerHTML=
"FirstElement= " + ret.map.FirstElement + ", " +
"SecondElement=" + ret.map.SecondElement + ", " +
"ThirdElement= " + ret.map.ThirdElement + ", " +
"ForuthElement=" + ret.map.FourthElement;
}
public ExampleBean sayBean() {
ExampleBean father = new ExampleBean();
father.setName("Bob");
father.setAge(34);
ExampleBean mother = new ExampleBean();
mother.setName("Alice");
mother.setAge(32);
ExampleBean me = new ExampleBean();
me.setName("Kent");
me.setAge(5);
me.setFather(father);
me.setMother(mother);
return me;
}
function clickGetBean(){
var ret = jsonrpc.hello.sayBean();
document.getElementById("jsonMsg").innerHTML=toJSON(ret);
document.getElementById("javaClass").innerHTML=ret.javaClass;
document.getElementById("obj").innerHTML=
"Name = " + ret.name + ", " +
"Age =" + ret.age + ", " +
"Father= " + ret.father.name + ", " +
"Mother=" + ret.mother.name;
}
public List sayList() {
List<String> list = new LinkedList<String>();
list.add("linux");
list.add("apache");
list.add("php");
list.add("mysql");
return list;
}
function clickGetList(){
var ret = jsonrpc.hello.sayList();
document.getElementById("jsonMsg").innerHTML=toJSON(ret);
document.getElementById("javaClass").innerHTML=ret.javaClass;
var str = "";
for( cnt = 0 ; cnt < ret.list.length ; cnt++ ){
str = str + cnt + " " + ret.list[ cnt ] + ", ";
}
document.getElementById("obj").innerHTML=str;
}
public String sayErrId() throws Exception {
throw new Exception("ERR01");
}
function clickErrMsg(){
try{
jsonrpc.hello.sayErrId();
}catch(e){
document.getElementById("eName").innerHTML=e.name;
document.getElementById("eCode").innerHTML=e.code;
document.getElementById("eMsg").innerHTML=e.message;
document.getElementById("eStack").innerHTML=e.javaStack;
}
}
JSONRpcClient.Exception.CODE_ERR_PARSE = 590 JSONRpcClient.Exception.CODE_ERR_NOMETHOD = 591 JSONRpcClient.Exception.CODE_ERR_UNMARSHALL = 592 JSONRpcClient.Exception.CODE_ERR_MARSHALL = 593
throw new Exception("エラーコード文字列");を使い、Java Script側では e.message で "エラーコード文字列" を受け取るようにするのが吉。
<jsp:useBean id="JSONRPCBridge" scope="session"
class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
<% JSONRPCBridge.registerClass("myClass", myClass); %>
registerObject() の代わりに registreClass() を使う <% JSONRPCBridge.getGlobalBridge().registerObject("myObject", myObject); %>
<% JSONRPCBridge.getGlobalBridge().registerClass("myClass", myClass); %>
getGloablBridge?() で Singleton が得られる。 var jsonrpc = new JSONRpcClient("JSON-RPC", user, pass)
Java Script で、JSONRPCClient を初期化する際に ユーザ名とパスワードを第二引数、第三引数にいれる function clickHello(){
var whoNode = document.getElementById("who");
var result = jsonrpc.hello.sayHello(whoNode.value);
alert( 'Synchronous Call Result :' + result);
}
function clickHello(){
var whoNode = document.getElementById("who");
jsonrpc.hello.sayHello(cb, whoNode.value);
alert( 'Called Remote Procedure Asynchronously' );
}
function cb(result, exception) {
if( exception ){
// exception != null
alert( 'Asynchronous Call Failed :' + exception );
return;
}
alert( 'Asynchronous Call Result :' + result );
}