Friday, April 23, 2010

Groovy Actors example

Groovy also implements Actors through the GPars library.

Groovy and Scala are both languages on top of the JVM and as i understand on some articles, they  both suffer on a JVM limitation and implement the Actor model nearly the same way and by using concurrency features that will be available in Java 7 (JSR166y).

I've reworked the little Groovy sample by using Grape: simple copy and paste the following code in a file actors.groovy for example and run:

chmod u+x actors.groovy
./actors.groovy

#!/usr/bin/env groovy

@Grab(group='org.codehaus.gpars', module='gpars', version='0.10-beta-1')

import groovyx.gpars.actor.AbstractPooledActor

class SecuredServer extends AbstractPooledActor {
    def password
    void afterStart() { 
        password = new Random().nextInt(10) 
        println "password set to ${password}"
    }
    void act() {
        loop {
            react {
                if (it > password) reply 'too large'
                else if (it < password) reply 'too small'
                else { reply 'autorized'; stop(); System.exit 0; }
            }
        }
    }
}

class Client extends AbstractPooledActor {
    def server
    void act() {
        loop {
            def guess = new Random().nextInt(10)
            server.send guess
            react {
                switch(it) {
                    case 'too large': println "$guess was too large"; break
                    case 'too small': println "$guess was too small"; break
                    case 'autorized': println "Access granted with: $guess"; stop(); break
                }
            }
        }
    }
}

def myServer = new SecuredServer().start()
def client = new Client(server: myServer).start()
[myServer, client]*.join()

Groovy vs Scala

After having read some Groovy books (Programming Groovy and Groovy Recipes) and programmed in Groovy, i am currently reading two Scala books (Programming in Scala and Pro Scala) and decided to practice with this language.

Here are summary the pro/cons i feel about... Feel free to comment and bring some clue, tips, ideas, comments ... I will include them in the list :-)

Groovy:

  1. Better Syntax: i find the Groovy syntax shorter and less verbose than Scala
  2. Type Conversion: i really prefer the way Groovy handles type conversion, with the ability to override / redefine the asType method on a metaclass. The Scala implicit keyword seems to much more act at compile time wherease Groovy's asType method is completely dynamic.
  3. Easier to learn: being a strong Java developer and also using a lot Javascript, i find Groovy easier to learn.
  4. Harder and longer to test: probably due to it being dynamic, i find that my Groovy code was really more harder to test than Java code or any other statically types language
  5. Not suitable for complex tasks: I was about to write a plugin which bridges an in-process event system of an event-driven system to JMS so that we can interact with JMS by sending events. The bridge should handle exceptions correctly, JMS responses, event replies, ... This was a lot of stuff and i find it really hard to test and get a code coverage using Groovy. I completely had to rewrite it in Java to be sure i don't miss anything and they won't be runtime errors that will trigger somewhere.
  6. Really good for simple tasks: in the complex monitoring application i work on, we use Groovy a lot to write agent modules for cpu, memory, processes ... to get system information. These scripts are short, simple, reloadable and easier to read and understand than any other language i've ever seen !

Scala:

  1. Functional:  all the people i know having already worked with functional languages prefer Scala
  2. Types language: Scala uses types and its compiler will at least prevent a lot of potential errors that we can make by using dynamic code such as Groovy. In Groovy, we can only remain on tests.
  3. Verbose: for sure it is less verbose than Java, but compared to Groovy, Groovy syntax is far lighter

Thursday, April 15, 2010

Subversion password recovery

I often don't know the password i use and it is especially the case for Subversion.
I made a Groovy script which is able to show me all the Subversion credentials i use on my computer. It takes about 10 lines:

#!/usr/bin/env groovy
@Grapes([
@Grab(group = 'com.sun.jna', module = 'jna', version = '3.0.9', transitive = false),
@Grab(group = 'org.tmatesoft.svnkit', module = 'svnkit', version = '1.3.2', transitive = false)])
import org.tmatesoft.svn.core.wc.SVNWCUtil
import org.tmatesoft.svn.core.internal.wc.SVNWCProperties
import org.tmatesoft.svn.core.*
def auth = new File(SVNWCUtil.defaultConfigurationDirectory, "auth/svn.simple");
def auths = auth.listFiles({!it.getName().contains(".")} as FileFilter)
auths.each {file ->
    SVNProperties values = new SVNWCProperties(file, "").asMap();
    def username = SVNPropertyValue.getPropertyAsString(values.getSVNPropertyValue("username"))
    def password = SVNPropertyValue.getPropertyAsString(values.getSVNPropertyValue("password"))
    if(username && password) println "${username}:${password}"
}

If you have installed Groovy, you can simply save this script in a file called SvnPasswordRecovery.groovy and then type:

chmod u+x SvnPasswordRecovery.groovy
./SvnPasswordRecovery.groovy

It will first automatically download required libraries using Grape, an internal Groovy feature, and then show you your credentials
This password recovery uses the very good library from SVNKit