Category Archives: Technology

CodeMirror port for Google Web Toolkit

I had been hunting for a good web based code editor when I came across CodeMirror at http://www.codemirror.net.

Great project and a lot of hard and smart work indeed.

I am a great fan of GWT and try to move anything and everything that comes my way to GWT :)

The result is gCodeMirror project at Google Code and GitHub.

If you are interested in directly jumping to a demo, browse to this test URL.

Google App Engine and GWT – Multi-Project Application

While working with Google Web Toolkit (GWT), it is a very common scenario to split the main application into multiple sub-projects and then use IDE to do a combined build and compile. However, the problem arises in scenarios where the GWT-RPC services and their implementations are kept in separate projects.

Recently working on an application, I structured it as follows:

  • MainApp: The final GWT+GAE application that will be deployed
  • AppCommon: Common classes to be used across projects
  • AppServices: Service definitions. It comprises of the GWT-RPC service definitions and their Async counterparts.
  • MainApp project contains the implementations of the services defined in the AppServices project.

When I “GWT Compile Project” or “Deploy App Engine Project“, the “shared” or “server” code of the dependencies is never copied to the war/WEB-INF/classes folder. Only the compiled JavaScript code is compiled.

So, I thought of automating all the stuff using Ant files.

GWT app-creator tool creates the “build.xml” for Ant and for GAE, there are guidelines on the web, for example here.

At an outline, the following needs to be done during the project build:

  • Enlist all the dependencies in order.
    If there are five projects – P1, P2, P3, P4, P5 – my script cannot identify which one depends on the other. It’s the developer’s responsibility to write them in the order so that independent or least dependent projects are listed and hence compiled first.
  • Compile all the projects in order with appropriate class-path…
  • Make a jar comprising of all the compiled class files and push it to war/WEB-INF/lib folder.
  • Compile the GWT module, as usual
  • Publish the application to GAE, as usual

I have made used of the following Ant tasks:

  • foreach from ant-contrib. You can download the jar from Sourceforge. Project home page is at http://ant-contrib.sourceforge.net.
  • Standard property, mkdir, delete, javac, copy etc tasks
  • App Engine task appcfg.

First step is to define a property, I named it projects, enlisting all projects in order.

<property name="projects" value="AppCore,AppServices,AppOne,AppTwo" />

Next is to expand the default task javac to compile all the dependencies as well. To take care of dynamic classpath entries arising because of the multiple projects, I copy all the class files to the <main-app>/war/WEB-INF/classes folder and that’s my standard classpath.

<target name="javac" depends="libs" description="Compile java source">
  <foreach list='${projects}' param="project.dir" target='compileDeps' />
  <mkdir dir="war/WEB-INF/classes" />
  <javac srcdir="src" includes="**" encoding="utf-8" destdir="war/WEB-INF/classes"
    source="1.5" target="1.5" nowarn="true" debug="true" debuglevel="lines,vars,source">
    <classpath refid="project.class.path" />
  </javac>
  <copy todir="war/WEB-INF/classes">
    <fileset dir="src" excludes="**/*.java" />
  </copy>
</target>

What this target does is compile all the dependencies before compiling the main application project.

Defining the compileDeps target is more straightforward now, noting that we have a helping hand from foreach task. Here’s how it looks like.

<target name='compileDeps' description='Compiles dependencies'>
  <property name="pdir" location="../${project.dir}" />
  <property name="cdir" location="." />
  <mkdir dir="${pdir}/war/WEB-INF/classes" />
  <javac srcdir="${pdir}/src" includes="**" encoding="utf-8" destdir="${pdir}/war/WEB-INF/classes"
    source="1.5" target="1.5" nowarn="true" debug="true"
    debuglevel="lines,vars,source">
    <classpath refid="project.class.path" />
  </javac>
  <copy todir="${pdir}/war/WEB-INF/classes">
    <fileset dir="${pdir}/src" excludes="**/*.java" />
  </copy>
  <copy todir="${cdir}/war/WEB-INF/classes">
    <fileset dir="${pdir}/war/WEB-INF/classes" excludes="**/*.java" />
  </copy>
</target>

This task compiles the dependencies, and copies all resources (non-java files) from the corresponding src folder and all binaries from the corresponding war/WEB-INF/classes folder to the <main-app>/war/WEB-INF/classes folder.

I further expanded the gwtc target to make a jar file rather than individual class files in the WEB-INF folder. Here’s the code for that.

<target name="gwtc" depends="javac" description="GWT compile to JavaScript">
  <jar destfile="war/WEB-INF/lib/main-app.jar">
    <fileset dir='war/WEB-INF/classes' />
  </jar>
  <delete dir="war/WEB-INF/classes" failonerror="false" />
  <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">...</java>
</target>

And that’s it!

Publishing the application to GAE requires no special change. All files are right there! Just use the <appcfg> task.

Android MVC – Forward and Backward Navigation

This weekend’s changes to the Android MVC project include the following:

  • Reorganization of the Command classes. AbstractBaseCommand has been created for simple get/set. All methods except “execute” have been implemented. This class now forms the base for IdentityCommand as well as AbstractCommand.
  • Controller now has full support for forward as well as backward navigation.
  • Controller can now record the activity navigation. Uses ActivityStackInfo.
  • Controller now support forward navigation with few more options:
    1. Can optionally mark the command to not be recorded
    2. Can optionally reset the activity stack
  • Controller now has back method for backward navigation.
  • Controller, during forward navigation, manipulates the activity stack appropriately.
    • Missing part is handling failure scenario when receiving a response. When processing the response, if it is in “error” state, IMHO, the response must be handled by the same activity rather than launching a new one.
    • More so, the command must be removed from the top of the stack if there was an error.
  • The support for IMemento, IInitializable has been removed. Not required.
  • The initialize method, henceforth, has been removed from BaseActivity.
  • BaseActivity now has two methods preProcessData and processData called before onBeforeCreate and before onCreateContent respectively.
  • BaseActivity now handles the back button (using the v2.0 function onBackPressed) by calling the back method for navigation.
    • Developers using v1.6 can override the method onKeyPressed and look for the KeyEvent.KEYCODE_BACK and call the method back.

Sample case study (showcase) now comprises of the following:

  • SplashActivity that happens to be the main launcher activity.
  • Erstwhile MainActivity has been renamed to LoginActivity to reflect its purpose.
  • HomeActivitiy continues to be the same.
  • SplashActivity launches LoginActivity using IdentityCommand and sharing some data (using Tag)
  • LoginActivity launches HomeActivity upon successful login (sharing some data using Tag and passing some piece of data to LoginCommand using Data).

The code is, as usual, available on Sourceforge at http://android-mvc.git.sourceforge.net/git/gitweb.cgi?p=android-mvc/android-mvc;a=tree. Additionally, I’ve also pushed the codebase on Github at http://github.com/gvaish/Android-MVC.

Android MVC – Stable Release and Case Study

Yes! The code has moving quite fast… now from Beta– to Beta- as I see it ;).

And soon, the Android MVC project should move up to real Beta.

The Http-Commands have been made more robust with better error handling. Controller has been enhanced to provide support for registering activities dynamically.

What still remains pending is activity-stack management by the Controller – which otherwise is done by the Android OS. The stack management is not a trivial task, however for an application it will provide some interesting bits. Firstly, it’d be great for a single-Activity-at-a-time application without too many overheads. Secondly, the Controller will some enhancements to ensure that forward-and-back work without too many duplicates in the UI as what happens in several applications as of now.

As usual… the code can be browsed at: http://bit.ly/cLHzbW.

And Oh Yes… to login, use these credentials – username: “gvaish”, password: “password” (without quotes).

Android MVC Project Updates

The latest code can be browsed at http://bit.ly/cLHzbW. A couple of critical changes have gone into the structure:

  • Removed the logic for progress or error activity from the Controller. The effect is – the fields progressActivity, errorActivity have been removed and also the code pieces that referenced them have been deleted.
  • Progress activity has been replaced by a simple, easy to use, ProgressDialog. This dialog is initialized in and launched from BaseActivity, as opposed to earlier structure where the progress activity was launched from Controller
  • Overloaded “go” methods have been added in BaseActivity. This gives ease and peace of mind to the BaseActivity-inherited classes to never be bothered about the Controller.

Pending items include:

  • Appropriate response handling in case of error from the incoming response in Controller.
  • Ensuring that if an error occurs and the same activity is shown, pressing the “back” button should not execute the “errorsome” command again (deviation from normal browsing a MVC-designed website).
  • Creating a robust case study to test out a few scenarios:
    Main (Splash) => Login => Successful => Home => Back (should exit);
    Splash => Login => Failed => Relogin (may be same activity or different) => Back (should exit);
    Splash => Login => Cancel (back button) => Should just cancel the login request.

Keep watching for more updates!

Model View Controller (MVC) for Android

Finally, I have been able to find time to push the intial code for “Model View Controller” implementation on Android.

Quite a few people told me that MVC is inbuilt in Android, with the View being represented by the Activity but I failed to visualize or tangelize the Controller. Well, the Model can always be the backend data – the business entities.

The code is hosted on Sourceforge – https://sourceforge.net/projects/android-mvc/

If are you interested in the code, you can get it using Git at git://android-mvc.git.sourceforge.net/gitroot/android-mvc/android-mvc.

Or if you just want to browse the code before downloading it on your machine, you can visit http://android-mvc.git.sourceforge.net/.

The code, by any means, is not yet ready for production use since I know various pitfalls, specially for the scenarios when the “Command” fetches the data from the network or database or from anywhere that takes long time.

In these cases, the best practice would be to hive-off the execution in a separate thread (read: Non-UI Thread). When the response is ready, you would pass it to the main thread (read: UI Thread) using a Handler. This part is implemented in the Controller, however, there can be race conditions in the scenarios where in multiple commands get issued – specially in very interactive application. Well, the bigger question there can always be – should MVC be used in that specific scenario?

Additionally, since, currently, the progress is presented using another Activity, there is an extra house keeping that is being done and I know (by use of this approach in my application), it is bound to fail in near-extreme scenarios.

The fixes are available with me… and I should soon find time to push them. Besides, I also need to a create more powerful case-study! 😀

So… keep watching!

And yes… if you feel that the code has done any good to you, please do spread the word and “Share the Love” :)

Here are initial screenshots of a trivial case-study (scenario) of initial implementation…

[showtime bgcolor=’#fcd77f’ width=’480′ height=’640′]

Model View Controller (MVC) Framework for Android

Recently, I have been working on a Model-View-Controller framework for Android. Have been having a crazy and interesting life since. :)

The infrastructure is something as follows:

  • An Activity act as a “View”
  • There’s a “Controller” – a singleton class – that keeps a track of all transactions across the “View” and the “Model”
  • Business Entity representing the “Model”
  • Then there is “Command” that represent any transaction / action triggered from the “View” to generate the “Updated Model” and result
  • A “Command Manager” to keep a track of all business operations so that, just in case, a command needs to be terminated and/or for general bookkeeping

There are quite a few interesting challenges that I came across while working on the same… and continue to evolve it:

  1. Everything happens on the client side – there’s no browser that will take care of the “Back” and “Forward”
  2. Cannot rely on the OS default handling of the launching of the new activities and the hardware back button
  3. So as to provide a support for the Back button, because that’s the only one on the device by default, the controller has to keep its own stack of multiple items – activity, data used to launch and initialize the activity, any changes that may have happened in the state while interacting with the activity itself – the last of which I still need to do
  4. Most of the new activity launches are triggered from data transactions over network. For example, after the login screen, I jump to the home screen and upon performing an action (like click) on some item on the home screen, I navigate to, say my messages activity. As such, the controller has provided a support for “progress” – not just on the top of the activity but as a full-screen activity
  5. While going “Back”, the controller had to be made smart enough to ensure that the “Progress” activity is never shown but the real previous activity is launch with appropriate data for initialization
  6. The activities talk to the controller for a business operation to be performed on a piece of data (model). As such, you can say, the controller follows the front controller pattern. For those familiar with the Struts framework, controller is something similar to ActionServlet to which *.do or /do/* is mapped or like the StrutsPrepareAndExecuteFilter in Struts2 that pre-processes all incoming requests
  7. Most of the business operations, as what I believe and least the case is in the current application that prompted me for creating the framework, will fetch data from either database or network. And in our case, we had no caching – any and all user interface when presented to the user will always get the latest data from the server. There’s no “Refresh” button. It’s always refreshed by default. And we would not like to block the UI thread while fetching the data. So, process all business operations in a non-UI thread and do a context switch just before the data is to be processed by the activity (the UI) – all that is the responsibility of the controller
  8. At some point in time, the “Back” button may need to be reset – saying that from the current point, no back is possible. Controller has the flexibility. Useful if the activity-stack gets larger or if the logic says, for example, on launching any activity from the options menu should reset the back-activity-stack
  9. It has to do all data housekeeping – what was the data used to launch the activity and initialize it. When to launch a new activity or when to update an existing one. Which activity to launch. Whether or not to terminate any previous business operations pending, for example, while navigating from one activity to another, any data communication to/from server may need termination… there’s a lot of hard work to be done by controller.

Well, this posting is just a primer for MVC for Android in terms of what are some of items to be taken care of.

I will create a project on SourceForge, publish the code there… and write the updates here.

Keep watching…

Google Web Toolkit 2.0 (GWT 2.0)

Google Web Toolkit 2.0 has finally been released! And it’s exciting. There are quite a few updates. Do have a look at http://code.google.com/webtoolkit/doc/latest/ReleaseNotes.html

Few things that I personally like:

  • Development mode as opposed to hosted mode. So, now I can ask my colleague in Stanford to test application that I host here or vice-versa –we don’t need to exchange Eclipse environment. Selenium Remote Control (Selenium RC) integration just rocks!
  • Speed Tracer is a great thing to have for Google Chrome. For starters, it’s like Firebug for Firefox.
  • Code Splitting is finally here. It was always in my wish-list, coming a Dojo background 😉
  • UiBinder – using templates to create the interface. Although, the Dojo template engine is 100x better
  • And finally… unit testing that I never made use of the way GWT originally provided has support for headless browser – HtmlUnit. HtmlUnit is something indispensable for web application developers and testers.

XMPP and Openfire

This is a great surprise for us!

Openfire 3.6.5 will be released under Apache Public License (APL) v2.0 :)

Have a look at http://svn.igniterealtime.org/svn/repos/openfire/trunk/documentation/dist/

I could not find any update on any of the mailing lists to this effect. Version 3.6.4 was release a couple of months back, under GNU General Public License (GPL). However, when I looked at the latest code that I checkout a few days back but noticed only today (while doing a diff) in the headers of one of the core files.

Great going Ignite Realtime :)

LearnG™ – The Learning Genius?

LearnG™ 2 is finally out! And I’m loving it.

LearnG™ is a plug-in for Microsoft Office PowerPoint (2003, 2007  and 2010) that can help one create learning nuggets with ease.

I now use it to record all my training sessions. Currently, it can record the presentation, audio and video and create a synchronized output. It has a simple but extensive template engine that can be used to create output in any desired structure. Well, the output is in a format that can be pushed onto web.

And the best part… it can create AICC and SCORM compliant nuggets.

I use it as a multimedia content authoring tool — no pre-production setup, no post-production editing. All that I need to do is incline my laptop screen appropriately so that it’s focussed at correct location (of course, I can change it during recording), turn on the mic and start my presentation. It records the entire slide-show as I do with the audio+video synchronized.

The default template that comes with it has support for:

  • Branding (I can have my own logo on the page)
  • Video (default – AVI file with Windows Media player for windows, VLC Media Player for Linux)
  • Presentation Slidese
  • Table of Contents (automatically inferred from the slides — of course, can customize)

On the recording options side, I have a variety of options available:

  • Select video-input device, specially if there are multiple connected
  • Select audio-input device, specially if multiple available
  • Select the audio codec with compression options
  • Select the video codec with compression options — can create DivX videos also (say, using Xvid or DivX codec)
  • Configure all device properties (brightness, contrast, blah blah blah)
  • Configure capture properties (frame rate, video size etc)
  • Well, I can do an audio mix also — from mic + audio-being-played-on-machine

I’ll try to upload a sample video output from the application. It’s simply amazing! And guess what… it’s not as pricey as TechSmith Camtasia (which costs around US$ 300)!

Contact me if you want to purchase a perpetual license. Screen-shots on the way… :)