<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ståle&#039;s weblog &#187; GWT</title>
	<atom:link href="http://blog.staale.org/tag/gwt/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>GWT: Not enough methods, expecting 3 saw 2</title>
		<link>http://blog.staale.org/2010/10/gwt-not-enough-methods-expecting-3-saw-2.html</link>
		<comments>http://blog.staale.org/2010/10/gwt-not-enough-methods-expecting-3-saw-2.html#comments</comments>
		<pubDate>Mon, 18 Oct 2010 07:43:50 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=143</guid>
		<description><![CDATA[So, from time to time I am getting this error in GWT: &#8220;Not enough methods, expecting 3 saw 2&#8243; in relation to Serialization, and I have finally figured out what it means. Basically, when GWT sees a Async service, it will generate a TypeSerializer to serialize and deserialize objects accessible from the service. This error [...]]]></description>
			<content:encoded><![CDATA[<p>So, from time to time I am getting this error in GWT: &#8220;Not enough methods, expecting 3 saw 2&#8243; in relation to Serialization, and I have finally figured out what it means. Basically, when GWT sees a Async service, it will generate a TypeSerializer to serialize and deserialize objects accessible from the service. This error occurs client side when a type doesn&#8217;t have it&#8217;s serializer registered in the TypeSerializer.</p>
<p>For our case, we have created an AnnotationProcessor for the bean annotation ConstructorProperties, we put this on the constructor for our classes. This will result in a custom serializer being constructed, so we can have final fields in the serialized objects, and just 1 constructor. The problem is when we use writeObject in these serializers. When the normal serialization generator visits a class with an interface field, it will discover all subclasses off that interface, and create a serializer for each. But, since I use a custom serializer, GWT will not discover any subtypes of an interface. So when I use the general writeObject, GWT will not be aware that it needs to register serializers of all subtypes of a certain interface.</p>
<p>The workaround in my case, was to rewrite the class so that GWT could create a custom serializer, which caused the proper fields to get registered. It still used my custom serializer to serialize objects, but now it also discovered that it needed to register the custom serializers for each type.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2010/10/gwt-not-enough-methods-expecting-3-saw-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A GWT request que</title>
		<link>http://blog.staale.org/2009/07/a-gwt-request-que.html</link>
		<comments>http://blog.staale.org/2009/07/a-gwt-request-que.html#comments</comments>
		<pubDate>Fri, 17 Jul 2009 11:02:18 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=112</guid>
		<description><![CDATA[I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, [...]]]></description>
			<content:encoded><![CDATA[<p>I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, as I integrate with Python Django on the back end. I could create a forwarding servlet to handle the communication to deal with the same origin policy off browsers, but I have instead opted for using a Script tag approach that I found from the <a href="http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_JSONFeedsFromOtherDomain">GWT faq</a> and <a href="http://giantflyingsaucer.com/blog/?p=126">elsewhere</a>.</p>
<p>However, I had one job that resulted in over 20 additional request jobs. So I wanted a system that throttled this, and only kept a limited amount of requests live at the same time. So I needed a solution a bit beyond what I had in the examples.</p>
<h3>Circumventing the SOP when fetching JSON data</h3>
<p>The way you circumvent the SOP in browsers, is to use script tags. You add a script tag to the document with the url for the JSON service, and you also add a parameter for which callback method the service should use. The service then creates the json data, and wraps it in &#8220;callback(&lt;json-data&gt;)&#8221;, so when the page loads, that method will get invoked, giving you the data. This does of course require the server you are communicating with to support this method of data fetching.</p>
<h3>Server side Django views</h3>
<p>To ease the creation off the JSON views in Python, I created a decorator that, when applied to a method, would wrap it such that it only needed to return something that could be json encoded. Then encode and send the result. The actual code for this relies on en extension off the DecoratorBase I posted about <a href="http://blog.staale.org/2009/03/python-decorators.html">here</a>, so I will not post it here. Maybe I can update this in a seperate post.</p>
<h3>Easy request building</h3>
<p>To make it easier to code the requests, I used the builder pattern to create JsonRequestJob objects. The JsonRequrestJob objects are rather simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestJob<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> params<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestJob<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">params</span> <span style="color: #339933;">=</span> params<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">callback</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is the builder:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> StringBuilder params <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestBuilder param<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        params.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> exec<span style="color: #009900;">&#40;</span>AsyncDataCallback<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> JsonRequestJob<span style="color: #009900;">&#40;</span>url, params.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To actually create a builder, I have a DataFetcher class (which contains the above 2 classes), and it has a get method for creating a builder. The DataFetcher also need configuration in the form off a base url. This is set up by including a script tag in the root element:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script language=&quot;javascript&quot;&gt;
&nbsp;
var dataUrlBase = &quot;http://localhost:8000/etc&quot;
&nbsp;
&lt;script&gt;</pre></td></tr></table></div>

<p>And here is the get method. Keep in mind that the function argument here refers to which view/function I am calling on the Python Django side off things:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    return $wnd[&quot;dataUrlBase&quot;]
}-*/</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> JsonRequestBuilder get<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> function<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span>getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>function<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;.json?callback=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Finally, the actual DataFetcher needs to have a que for adding and dequing jobs, as well as an implementation that actually creates and inserts the script tag, sets up the callback method to call when data is loaded, as well as ensuring the max number of requests is kept at a minimum.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataFetcher <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Queue<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span> jobs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MAX_JOB_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> jobCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> requestCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jobCount <span style="color: #339933;">&gt;=</span> MAX_JOB_COUNT <span style="color: #339933;">||</span> jobs.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        jobCount<span style="color: #339933;">++;</span>
        <span style="color: #000000; font-weight: bold;">final</span> JsonRequestJob job <span style="color: #339933;">=</span> jobs.<span style="color: #006633;">poll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        makeJSONRequest<span style="color: #009900;">&#40;</span>job.<span style="color: #006633;">url</span>, job.<span style="color: #006633;">params</span>, job.<span style="color: #006633;">callback</span>, requestCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        requestCount <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>requestCount <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> makeJSONRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler, <span style="color: #000066; font-weight: bold;">int</span> requestId<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    var callback = &quot;callback&quot; + requestId;
&nbsp;
    var script = document.createElement(&quot;script&quot;);
    script.setAttribute(&quot;src&quot;, url+callback+params);
    script.setAttribute(&quot;type&quot;, &quot;text/javascript&quot;);
&nbsp;
    $wnd[callback] = function(jsonObj) {
        $wnd[callback + &quot;done&quot;] = true;
        @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(jsonObj, handler);
    }
&nbsp;
    setTimeout(function() {
        if (!$wnd[callback + &quot;done&quot;]) {
            @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(null, handler);
        }
&nbsp;
        // cleanup
        $wnd.document.getElementsByTagName(&quot;head&quot;)[0].removeChild(script);
        delete $wnd[callback];
        delete $wnd[callback + &quot;done&quot;];
    }, 1000);
&nbsp;
    $wnd.document.getElementsByTagName(&quot;head&quot;)[0].appendChild(script);
&nbsp;
    }-*/</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJSON<span style="color: #009900;">&#40;</span>JavaScriptObject jsonObj, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobCount<span style="color: #339933;">--;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        handler.<span style="color: #006633;">handleData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> jsonObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Thankfully, JavaScript client side is singlethreaded, so we don&#8217;t have to worry about race conditions or synchronizations. If we needed to do that, this could could obviously break, as it has race conditions in relation to the increment/decrement and checking off the jobCount variable. Also notice that there is a timeout added that checks that the page is loaded within 1 second. If it doesn&#8217;t, null is sent to the callback, and the actual callback method is removed from the page.</p>
<p>The code is not entirely complete. The AsyncDataCallback interface is missing (but it&#8217;s tiny and easy to implement). </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/07/a-gwt-request-que.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

