Grails Zipping Files

Currently, I am working on a project where I need to prompt a download a zip file to the user.

I needed to create a zip file on the fly.

Here is how we can create a zip file in Groovy.

def zipFiles(String inputDir) {
		String zipFileName = ""
		ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(zipFileName))
		new File(inputDir).eachFile() { file ->
			zipFile.putNextEntry(new ZipEntry(file.getName()))
			def buffer = new byte[1024]
			file.withInputStream { i ->
				def l =
				if (l > 0) {
					zipFile.write(buffer, 0, l)



Gestures on Android SDK

I am working on a android project where I need to detect gestures like, fling, scroll etc.
I thought I would share on how I was able to achieve gesture detection.


Here are the main guts.

1. have to implement OnGestureListener (this contract will bind you to implement many methods including “onFling”)

2. You have to define a gesture detector:

 gestureDetector = new GestureDetector(this);          // initialize GestureDetector

3. That’s it now you can detect touch, fling etc.

import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;

public class SpinTheBottle extends Activity implements OnGestureListener{
private GestureDetector gestureDetector;

    public void onCreate(Bundle savedInstanceState) {
        gestureDetector = new GestureDetector(this);          // initialize GestureDetector

     public boolean onTouchEvent(final MotionEvent event){
    	          return gestureDetector.onTouchEvent(event); // bubble it up

     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		System.out.println("Fling Detected ##########################"); // terrible thing to do but for this demo, we can check the logcat to see how fling is detected
		System.out.println("e1.X: "+e1.getX() + " e1.Y: "+e1.getY()+" e2.X: "+e2.getX()+" e2.Y: "+ e2.getY());
		System.out.println("X Vel: "+ velocityX+ " Y Vel: "+ velocityY);
		return true;


Groovy Testing using MockFor to test MultipartFile

Recently, I came across a situation where I had to test a service that took a file and uploaded it to a server NAS. service was being passed a “MultipartFile” (org.springframework.web.multipart.MultipartFile)
I wanted to test the service and mock up MultipartFile, as I performed multiple search on “Bingle”, they all pointed to MockMultipartFile. For the unit test I had, I only had to make sure few methods in MultipartFile was called.
I have been using Mockito for a while now and I really loved the simplicity that Mockito provided. However, Mockito won’t support Groovy classes yet :(.
So went back to Groovy’s “MockFor” feature.
Here is how I created a mocked MultipartFile using MockFor and was able to verify the method calls.

import groovy.mock.interceptor.MockFor
import org.springframework.web.multipart.MultipartFile
void testsample(){
		def mockMultipartFile = new MockFor(MultipartFile)
		mockMultipartFile.demand.isEmpty{return false}

		def mockMPF = mockMultipartFile.proxyInstance()
		assertEquals(false, mockMPF.isEmpty())
		mockMultipartFile.verify(mockMPF)  // verifying that .isEmpty was actually called

Ubuntu updating system time

Today is the DST time, however, I found out that my Ubuntu did not update the time accordingly. (Thank god I checked my cell phone network time, else I would have missed my flight).
Here is how to update system time in ubuntu
$: sudo ntpdate
This will update the system time accordingly. you can use any time server you want just replace with the server you desire.

Adding New virtual HDD on Wubi

In one of my laptops, I have been using Ubuntu under Wubi. It is great as I get to use full blown Ubuntu and still can get access to Windows when needed. One of the limitation is though that virtual HDD assigned under Ubuntu can get full really quickly (specially with the system updates and other PPA updates). So, I am going to talk about how we can get pass it by creating a virtual HDD and attaching it to Ubuntu.

Here are the steps to create and attach the HDD.
Step 1: Creating a virtual HDD
Run following command to create a 20 GB hdd (I will use this hdd as my primary home directory)
$: /host/ubuntu/disks$ dd if=/dev/zero of=extra.disk bs=1MB count=20000 seek=1
NOTE: here i am calling it a “extra.disk”, you can name it anything you want.

Step 2: Formatting the virtual HDD to ext3
$: /host/ubuntu/disks$ mkfs.ext3 -F extra.disk
NOTE: make sure you use the right disk in our case its “extra.disk”

Step 3: Creating a backup
$: mv home home.backup
$: mkdir home
$: mount home
$: rsync -av –exclude=home.backup/.gvfs home.backup/* home


Grails Gotcha’s

I have been playing around with Grails for a while now. It is a great framework. One of the best alternatives for rapid application development under java stack.
Its idea of Convention over configuration is the seller. However, As I started developing apps on grails, it was hard to get use to the “convention” as sometimes the “convention” was not clear. When I searched the issue online, I could hardly find any relevant information. So I am starting to keep track of things I learned the hard way.

1. Plugin-Installation:
I wanted to install jQuery plug-in on my app so I followed instructions found at
I was having hard time installing it (will talk about that in bullet 2). So finally I installed jQuery Plug-in 1.4.2.
Later on I wanted to upgrade it to jQuery plug-in 1.4.4.
So this is what i did
a. uninstalled jquery plugin
b. ran grails install-plugin jquery

So issue so far, then I ran
c. grails install-jquery
but it kept adding jquery 1.4.2.

After hunting my file system down I figured out that apparently, Grails caches the script in \.grails\1.3.6\projects\\scriptCache
so when i installed the 1.4.2 it had kept scripts there but then when i wanted to upgrade, it still had the old script cached so it did not over write with script.
So the solution was to delete all the scripts in scriptCache folder and run through steps a-c and walla.. it works!
Lesson learned: clear the scriptCache

2. Proxy Server Setup:
I was trying to install grails plug-ins at my work. I am behind a proxy. So when I tried to install a plug-in it basically gave me error saying “Host could not be reached”.
Then I simply went to the “Network Settings” in the SpringsourceTool Set and fixed the proxy. But still I kept on getting the same error.
After much of research I found out that we need to exclusively set up Proxy in Grails. And here is how we set it up:
a. grails add-proxy myProxy “–host=someproxyserver” “–port= 8080″
b. grails set-proxy myProxy

here you can call anything you want. and dont forget the part b ie set-proxy. Also, dont forget the ” around the host and port, for some reason in the OS i am using if i did not put ” it did not work correctly.
more info could be found at