Can I get down now dad?

Tramps like us, baby we were born to code

links for 2008-04-26

Is it wrong?



DSC00281.JPG, originally uploaded by Spuggy.

For a grown man to have a Jiminy Cricket badge thing on his shoes?

Groovy, SQL and accessing Java libraries

The beauty of Groovy is that it combines the lovely scripty-ness with the java integration. Here is an example form this morning, I needed to encode some passwords in a database table and wanted a quick script to do the job.

What this script demonstrates is that Groovy can get at databases out of the box and talk to regular Java. Note the postgres driver below - this ran straight away and is the same driver we use in regular java dev. Also note the function “encodePassword”. This calls a load of regular Java stuff from the import java.security.MessageDigest packge.

Yet I can whack all this together and still do the crazy forEach closure thing.

Apologies for shonky code but hey:


import groovy.sql.*
import java.security.MessageDigest
/*
* function to encode a string/password
*/
public String encodePassword(String password, String algorithm) {

byte[] unencodedPassword = password.getBytes();
MessageDigest md = null;

try {
// first create an instance, given the provider
md = MessageDigest.getInstance(algorithm);
} catch (Exception e) {
print ("Exception: " + e);

return password;
}

md.reset();

// call the update method one or more times
// (useful when you don't know the size of your data, eg. stream)
md.update(unencodedPassword);

// now calculate the hash
byte[] encodedPassword = md.digest();

StringBuffer buf = new StringBuffer();

for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
}

buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
}

return buf.toString();
}

/*
* Main bit of groovy starts here
*/
def DB='jdbc:postgresql://localhost/callbriefdb?useUnicode=true&characterEncoding=utf-8'
def USER='chutney'
def PASSWORD='pants'
def DRIVER='org.postgresql.Driver'

def sql = Sql.newInstance(DB,USER,PASSWORD,DRIVER)

//not really sure how to process a result set so just hacked it ;-)
sql.eachRow('select * from app_user where id <> 0′) { n -> newPw =encodePassword(n.pinNumber,”SHA”) ; sql.execute(”update app_user set password = ${newPw} where id = ${n.id}” )}

Naked Winky Man

nakedwinkyman.JPG

During a pause in conversation my son, the Baron Von Pud, demonstrates his “Naked Winky Man” - genius.

links for 2008-04-06

  • Ajax but simpler, very elegant and simple. nice
    (tags: ajax ahah)

Still Life

stilllife.JPG

Dads cooking Saturday afternoon. Sports report on the radio, Newcastle win, bliss.

Groovy and Closures

Groovy and Grails (Ruby too) make a big thing of closures. A closure is defined, slightly unhelpfully, as:

A Groovy closure is like a “code block” or a method pointer. It is a piece of code that is defined and then executed at a later point.

If you are not use to them they can be tricky to grasp but they are hugely powerful and in many cases make code easier to read. See my cheesey code to print out the files in a directory:

groovyexp.png

This is using the Groovy eclipse plugin of course

The only problem with closures is that they are manna are from heaven to the lets-write-the-code-in-least-amount-of-lines-brigade. You know the sort of people, they chain a load of functions and objects together in one line and then stick a regular expression on the end. The technical term for these people, I believe, is bastards.

Sexy mobile groovers



Sexy mobile groovers, originally uploaded by Spuggy.

- Taken at 7:25 PM on April 04, 2008 - cameraphone upload by ShoZu

Over the Air



Over the Air, originally uploaded by Spuggy.

Sweating at the back of the Google Gears pres.
Hanging with the Paxmo possie

- Taken at 4:38 PM on April 04, 2008 - cameraphone upload by ShoZu