Pages

Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Glide your apps on Google App Engine

I had been working on a script, which has turned into project Glide, to help me work with smallish gaelyk apps (like my homepage etc.).

While gaelyk template project provides excellent start; for simple website'ish projects, java webapp structure and the xml noise is sometimes a hindrance for non java background (web) developers.

What glide does is cut that noise off with simple defaults (while being still configurable). The only files your project is required to have is your web content (something like static html or php webapps). Plus there are some more goodies like preconfigured sitemesh etc.

I have created 4 minimal examples to give an idea of what I mean here. Each dir in the samples is a glide project and can be run locally and deployed to GAE.

Start with the `news` project a simple one filer to get sense of what I mean by no config noise. `expense` and `blog` take few steps ahead but are still very very small.

To run a glide project, just cd into the app directory and fire the 'glide' command.

Steps to install glide are mentioned in the README. It should be fairly straight forward if you have java/git/gae already installed. (if you dont use git, you can directly download the zip and extract it)

I look forward to feedback on the project.

PS :
  • Motivated to let it know to the world  by "F**k It, Ship It"
  • Content almost identical to what I posted on Gaelyk User Group

Java 7 and Groovy on Mac OS X Mountain Lion

I usually keep all my softwares and SDKs to the latest version but jdk7 was probably amongst the longest resisted update/upgrade. Mostly running groovy, I never got motivated enough to upgrade and then there have been several security and compatibility issues reported by many.

Anyways, today I just thought of doing it, primarily to reap benefits of invokedynamic, the thing I have not even looked at so far. The download was pretty simple in form of a .dmg and then the .pkg installer.

I fired up terminal with to check java and viola:
[~]
kunal@kaydee $ java -version
java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

The second natural thing was to just verify groovy, but what the heck, the JVM version I see is:
[~] 
kunal@kaydee $ groovy -version
Groovy Version: 2.0.6 JVM: 1.6.0_37 Vendor: Apple Inc. OS: Mac OS X

Apparently Java 7 got installed at
/Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home/

But the one available to groovy was something else:
[~]
kunal@kaydee $ groovysh
Groovy Shell (2.0.6, JVM: 1.6.0_37)
...
groovy:000> System.properties["java.home"]
===> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Setting JAVA_HOME Environment variable comes to rescue (in .zshrc or .bash_profile or whatever profile file you use.)
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home"

Reload the iTerm and

[~]
kunal@kaydee $ groovy -version
Groovy Version: 2.0.6 JVM: 1.7.0_11 Vendor: Oracle Corporation OS: Mac OS X


Reading RSS Feed in Groovy

The more I explore, the more I love groovy. Last week while I was demonstrating a few colleagues of mine, that how simple things are really dead-simple in groovy, I wrote this small code that fetches the rss feed and prints the headlines. Just thought of sharing with all.
// setup proxy, if any
System.properties.putAll(["http.proxyHost":"your.proxy", "http.proxyPort":"8080"])             

// define the url, change it to whatever rss url you like
def url = "http://news.google.com/news?ned=us&topic=h&output=rss"

def rss = new XmlSlurper().parse(url) 

println rss.channel.title
rss.channel.item.each {
    println "- ${it.title}"
}
That's it! You have headlines in your output. How can you not love the brevity yet expressiveness of this code:)

Groovy substring

Can't imagine any terser way of doing this than

println "teststring"[4..6]

Ain't it groovy :)

Groovy performance : iterating with closure vs loop

Toady while listening to this video I discovered an interesting fact about usage of closure vs regular loop for iterating over list. So I wrote a quick test to confirm and yes loops outperform closures in terms of performance in case of iterating over a list.

But in a slightly similar case of File.eachLine{} vs loading file in a list and then iterating with for, the closure still works out faster on the whole. I have not tested this for different file sizes, but damn, whatever it may be new File(file).eachLine{} just rocks :)

Here is the github gist, to get the latest version of code.

def file = "input.txt"
// just to heat up the jvm :)
new File(file).eachLine{
    it.toUpperCase()
}

// with closure
start = System.nanoTime()
new File(file).eachLine{
    it.toUpperCase()
}
println System.nanoTime() - start

// with loop
start = System.nanoTime()
lines = new File(file).readLines()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

// read lines into a list
lines = new File(file).readLines()

// with list.each {}
start = System.nanoTime()
lines.each { line->
    line.toUpperCase()
}
println System.nanoTime() - start

// with loop over list
start = System.nanoTime()
for (line in lines){
    line.toUpperCase()
}
println System.nanoTime() - start

Results (with groovy-1.8.0 and jdk-1.6)
 Run # 1   Run # 2   Run # 3   Run # 4
 4792229   4773791   4840280   5428064
11169576  10254934  10791595  12546008
 2638604   2424610   2477969   2466514
  465702    315403    315403    412063

Display System Properties in Groovy

It can't get any simpler than this.

System.properties.each { k,v->
    println "$k = $v"
}

Groovy rocks!!