Subscription Options


Getting started with GWT and Google Gears

December 17th, 2007 - Written by in Using GWT

Google Gears is a library that enables your web applications to work offline. Currently it consists of three modules: LocalServer for caching and serving up your web app resources (ie. html, javascript, images), a SQLite Database for storing offline data, and a WorkerPool for performing asynchronous operations. My GWT application, a free Sudoku game called GSudokr seemed to be a good candidate for testing out the LocalServer module of Gears as it doesn’t have any complicated requirements like synchronizing back to a server. After reading all the documentation, I was able to get everything working in less than a day. In this short tutorial, I will walk you through the steps I took to get GSudokr working offline using Gears.

Installing

Download the following:

  • Google Gears
  • gwt-google-apis

On OS X, Google Gears is installed as a Firefox extension. On Windows, it’s actually a standalone installer which enables Gears support for both IE and Firefox. The gwt-google-apis project is a library for GWT that support the various Google APIs. Lucky for us, the first API they decided to support was Google Gears!

Add gwt-google-apis.jar to your project and in your GWT module xml file, you’ll need to add the line:


Working with the API

The basic class for serving up locally cached pages is called LocalServer. In your application you can instantiate the class like this.

try {    localServer = new LocalServer(); } catch (GearsException e) {    // Will throw GearsException if Gears is not installed }

If the user does not have Gears installed than LocalServer cannot be instantiated and will throw a GearsException. So be aware of this situation and display an appropriate message for your users.

LocalServer is used to create resource stores, which are a collection of URLs to cache. There are two types of supported resource stores, ResourceStore and ManagedResourceStore. With ResourceStore, you pass in the name of the urls to cache inside your application using the captureURLs() method. On the other hand, ManagedResourceStore, lets you list these urls in an external manifest file. Since GWT generates its files dynamically, ManagedResourceStore is the only approach that will work. To save the effort of trying to input all the urls by hand into the manifest, I wrote a simple php script which I run after compiling my app, that will generate the appropriate manifest. Take a look at it as an example. [generateManifest.php]

The code to create the resource store, open it and set the manifest url is straightforward. I took the code straight from the sample ManagedResourceStoreDemo in the gwt-google-api distribution. The interesting part of the code is the checkForUpdate() method.

managedResourceStore.checkForUpdate();new Timer() {   public void run() {      switch (managedResourceStore.getUpdateStatus()) {         case ManagedResourceStore.UPDATE_OK:            statusLabel.setText("Ready for offline access");            break;         case ManagedResourceStore.UPDATE_CHECKING:         case ManagedResourceStore.UPDATE_DOWNLOADING:            schedule(500);            break;         case ManagedResourceStore.UPDATE_FAILED:             statusLabel.setText("Unable to go offline");             break;         }      }}.schedule(500);

ManagedResourceStore’s checkForUpdate() method tells Gears to asynchronously check your manifest file and start downloading the resources specified in it. A timer is then created which checks the update status on the store to see if your files have been downloaded and cached. You can use the various status codes to inform the user in your UI about what’s going on. When you receive the UPDATE_OK code, your app is ready to go offline. To test this, use the Work Offline function in your browser (Firefox: File > Work Offline). Make sure to clear your cache, and try to access your application using your regular url.

As you can see, it is quite simple to get your GWT applications working offline using LocalServer. Keep in mind that Gears is still in beta and the user experience still leaves something to be desired, especially to the casual user. Still, it’s quite cool to able to add offline capabilities to your web application with a minimal amount of effort.

References

6 Comments Subscribe to this feed Stumble it!

Weekly GWT Links for 12/16

December 16th, 2007 - Written by in News

Here are some interesting links from the past week:

  • Java Shared Transactional Memory for GWT – JSTM is a library that allows you to share state between clients allowing for distributed GWT applications. [screencast]
  • GWT-REST 0.1 – Open source library for using GWT with REST web services.
  • MyGWT vs gwt-ext – Trying to figure out which library to use? This is a good thread on the developer forum discussing the pros and cons of each of these two excellent, but similar libraries.
  • IT Mill Toolkit 5 – ITMill is a server-side Ajax framework that uses GWT on the client side. They’re asking the GWT community about whether or not they should make a GWT only version of their widget set, so let them know.
  • Hands-On Google Web Toolkit – Nice article by Ed Burnette and Adam Haughton demonstrating GWT’s features to develop a mash-up photoviewer.

Got some interesting GWT stuff to share? Just send an email to .

No Comments Subscribe to this feed Stumble it!

Interviews from the GWT conference

December 15th, 2007 - Written by in News

Bill Cullifer of the WOW Technology Minute has a set of video interviews he conducted last week at the GWT conference with various members of the GWT community. It’s quite interesting to be able to put a face to the names.

  • Ryan Dewsbury – Developer of GPokr and KDice and author of the book Google Web Toolkit Applications.
  • Shanjian Li – One of the engineers at Google specializing in Internationalization.
  • Joel Webber – Tech lead and co-creator of GWT.
  • David Geary – Author of the book Google Web Toolkit Solutions: More Cool & Useful Stuff
  • Bruce Johnson – Tech lead and the other co-creator of GWT.

No Comments Subscribe to this feed Stumble it!

« Previous Entries Next Entries »