iRSSの日記

はてなダイアリーiRSSの日記の続き

Google、Ajaxアプリ開発ツールのβ版公開

Googleは、JavaAjaxアプリケーションを作成するためのソフト開発ツール「Google Web Toolkit」(GWT)のβ版を公開した。

 同ツールでは、Javaを使ってGoogle MapsGmailのようなAjaxアプリケーションを開発できる。自分の好きなJava開発ツールを使ってAjaxアプリケーションの作成とデバッグを行い、GWTコンパイラJavaアプリケーションをブラウザ対応のJavaScriptとHTMLに変換することが可能。

ということで、早速ダウンロードしてみた。

JavaScriptJavaのコードが混在していて、どうも混乱。
何じゃこりゃ。

ココは落ち着いて、ドキュメントを読む。

What is Google Web Toolkit?
Google Web Toolkit (GWT) is a Java software development framework that makes writing AJAX applications easy. With GWT, you can develop and debug AJAX applications in the Java language using the Java development tools of your choice. When you deploy your application to production, the GWT compiler translates your Java application to browser-compliant JavaScript and HTML.

どうやら、Ajaxのアプリを、クライアントのJava環境で動作するツール群で構築できるらしい。
しかも、擬似サーバーが提供されていて、デスクトップでも動作する模様。
コードの生成ツールもあって、なんとなく、Ruby on Railsのような感じもする。

実際のSDKのコードを読み解く。

\gwt-windows-1.0.20\samples\Hello\www\com.google.gwt.sample.hello.Hello
フォルダに、ある「gwt.js」が肝。
253行目の

// Set it up
//
__gwt_processMetas();

if (__gwt_isHosted()) {
  __gwt_onUnload.oldUnloadHandler = window.onunload;
  window.onunload = __gwt_onUnload;
  __gwt_forEachModule(__gwt_injectHostedModeFrame);
}
else {
  __gwt_hookOnLoad();
  __gwt_forEachModule(__gwt_injectWebModeFrame);
}

} // __GWT_JS_INCLUDED

がこのスクリプトのエントリポイントになるが、webモードのときは__gwt_injectWebModeFrameという、ファンクションが実行される。
このfunction __gwt_injectWebModeFrameは160行目に書かれているが、このスクリプトを呼び出した、Hello.htmlに書かれているcontentすなわち'com.google.gwt.sample.hello.Hello'を最終的に呼び出して、IFRAMEに流し込む。

<html>
	<head>
		<meta name='gwt:module' content='com.google.gwt.sample.hello.Hello'>
		<title>Hello</title>
	</head>
	<body bgcolor="white"> 
		<script language="javascript" src="gwt.js"></script>
	</body>
</html>

ここで呼び出されるのはcom.google.gwt.sample.hello.Hello.nocache.htmlである。

<html><head><script>

window["provider$user.agent"] = function() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf('opera') != -1) {
    return 'opera';
  }
   else if (ua.indexOf('safari') != -1) {
    return 'safari';
  }
   else if (ua.indexOf('msie 6.0') != -1) {
    return 'ie6';
  }
   else if (ua.indexOf('mozilla') != -1) {
    var geckoIdx = ua.indexOf('gecko/');
    if (geckoIdx == -1) 
      return 'oldmoz';
    var spaceIdx = ua.indexOf(' ', geckoIdx);
    if (spaceIdx == -1) 
      spaceIdx = ua.length;
    var version = parseInt(ua.substring(geckoIdx + 6, spaceIdx));
    if (version < 20051107) 
      return 'oldmoz';
    return 'moz';
  }
  return 'unknown';
}
;

window["prop$user.agent"] = function() {
  var v = window["provider$user.agent"]();
  switch (v) {
    case "ie6":
    case "moz":
    case "oldmoz":
    case "opera":
    case "safari":
      return v;
    default:
      parent.__gwt_onBadProperty("com.google.gwt.sample.hello.Hello", "user.agent", ['"ie6"', '"moz"', '"oldmoz"', '"opera"', '"safari"'], v);
      throw null;
  }
};

function O(a,v) {
  var answer = O.answers;
  var i = -1;
  var n = a.length - 1;
  while (++i < n) {
    if (!(a[i] in answer)) {
      answer[a[i]] = [];
    }
    answer = answer[a[i]];
  }
  answer[a[n]] = v;
}
O.answers = [];

function selectScript() {
  try {
    var F;
    var I = ["true", (F=window["prop$user.agent"],F())];

    O(["true","safari"],"1248BBC428C91EE18CF2F41BDF9E1511");
    O(["true","ie6"],"50D3154818994CF4E8B7E3A849C6484C");
    O(["true","oldmoz"],"746329F5759B466084FF38E28C09D556");
    O(["true","moz"],"746329F5759B466084FF38E28C09D556");
    O(["true","opera"],"99F0B1F0CC6563883662906EAB2E6D43");

    var strongName = O.answers[I[0]][I[1]];
    location.replace(strongName + '.cache.html');
  } catch (e) {
    // intentionally silent on property failure
  }
}
</script></head><body onload='if (parent && parent.__gwt_webModeFrameOnLoad) selectScript()'><font face='arial' size='-1'>This script is part of module</font> <code>com.google.gwt.sample.hello.Hello</code></body></html>

↑このコードの中には、ボタンを配置し、アラートを出すJavascriptがはいっていた。

そして、このcom.google.gwt.sample.hello.Hello.nocache.htmlは、
gwt-windows-1.0.20\samples\Hello\src\com\google\gwt\sample\hello\client
にある、Hello.javaである

/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.sample.hello.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Hello implements EntryPoint {

  public void onModuleLoad() {
    Button b = new Button("Click me", new ClickListener() {
      public void onClick(Widget sender) {
        Window.alert("Hello, AJAX");
      }
    });

    RootPanel.get().add(b);
  }

}

整理すると、JavaからJavascriptを生成するツールということになる。

Hello.java ----生成---->com.google.gwt.sample.hello.Hello.nocache.html

Hello.html---実行---->gwt.js --実行----> com.google.gwt.sample.hello.Hello.nocache.

結果として、IFrame内に、Window.alert("Hello, AJAX");を実現する、JavaScriptができる。

この動作確認は、クライアント上ではHostモードで動かすことができる。
動的生成もapacheなしで試せそうである(未検証だけど)

要するに

Ajaxのアプリをつくる、Java on Railsフレームワーク

とおもったけど、MVCの実装とか、どうなってるか不明なので、いったん保留。