Thursday, November 25, 2010

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.

Tuesday, November 9, 2010

Love your work but do live life

It is 8am and I am just done watching Indira Nooyi, a lady that I admire, one-on-one with Barkha Dutt at NDTV Profit.

There were a lot of takeaways from the discussion but there is something that I want to take home and wish that you do too.

She writes mail, composes on Tuesday locking herself for 15 minutes and sends on Thursday, to 200 executives once every fortnight on various topics ranging from business to career to personal.

Here's the crux of one of her mails.

Indira and her husband would see-off their daughter daily for her school. One the first day of every grade, they would take the photos and capture some precious moments.

One fine day, carrying forward the tradition, they were to take the photos, the daughter says, "Hey! I'm driving to college".

And suddenly it was a reality check for them... their "little daughter" has grown up, time has passed by swiftly.

So, while it is good to be in business, great to be involved full heart and mind in work, do live your life to fullest and give time to family and friends because time flies quickly and it never turns back.
And it reminds me of... "If only I could turn back time".

Thursday, October 28, 2010

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.

Thursday, October 21, 2010

Google App Engine - Dealing with large number of files

One of the most common problem that one has to deal while deploying a large scale application at Google App Engine is the file limit - in terms of number of files.

As per the issue reported at http://code.google.com/p/googleappengine/issues/detail?id=161, the limit has been increased from 1000 to 3000, but there's catch. Any static file (non-JSP files outside the WEB-INF folder, unless explicitly excluded in appengine-web.xml) are counted twice. Why? They're pushed into a special folder __static__ and all such files are counted twice.

I looked at deploying the SmartGWT showcase on GAE and the "appcfg upload" gave an error about having over 9000 files. I was shocked! Literally.

And then I came across a simple yet powerful idea - zip all the static files and use a servlet to extract and serve on the fly. The solution servlet was uploaded by sindhom at http://code.google.com/p/googleappengine/issues/detail?id=161#c68.

Take that as a base, I made an update - again simple yet probably very useful. Look at the "If-Modified-Since" header and respond with with a 304 or 200 depending upon if the entry in the zip file is updated or an old one. There's one thing hardcoded in the my solution - the Cache-Control / Expires timeline is hardcoded to 365 days, and it can be easily moved into the configuration.

The solution is mentioned at http://code.google.com/p/googleappengine/issues/detail?id=161#c88. The SmartGWT showcase is available at http://1.latest.mastergaurav.appspot.com, along with the complete code of the servlet at http://1.latest.mastergaurav.appspot.com/UnzipperServlet.java.txt.

Sunday, August 29, 2010

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.

Monday, August 23, 2010

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 earlier... the code can be browsed at: http://bit.ly/cLHzbW.

And Oh Yes... to login, use these credentials - username: "gvaish", password: "password" (without quotes).

Sunday, August 22, 2010

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!

Saturday, August 14, 2010

Model View Controller (MVC) for Android

Finally, I have been able to find time to push the initial 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.

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! :D

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...




Friday, August 6, 2010

On becoming "Double Chachu"

And finally, the d-day has come. My friend and business colleague, Umakant, has been blessed with a daughter - Goddess Laksmi for the second time in his family! Sooo lucky :)

The name is Yana, meaning "Precious to God". See Yana and Ioannes.

Here are some photos...






Thursday, August 5, 2010

Home, Sweet Home

A few weeks ago, I got time to take a video of my flat (condominium or condo, if you like it that way).

And then, I got time to put it up. Here's the video...


Wednesday, April 7, 2010

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...