Friday, June 6, 2008

Betcha Can't Break This: Spoiler

If you haven't read the previous post, and would like to give it a go, skip this post.

SPOILER Ahead:

Here is one way to print 'something nefarious' in the Groovy program:


groovy ez " String() ; println 'something nefarious' ; new String"
It is clearly inspired by SQL injection. I tried using categories and some other tricks but everything else failed.

I'm writing this right after the original post, and scheduling publication for this afternoon. I'm looking forward to seeing solutions.

Betcha Can't Break This: A Groovy Puzzler

The last post featured a little program:


def input = args[0]
def theObject = evaluate( " new ${input} () " )

println theObject.toString()

Ricky posted an excellent comment that exposed a security problem. Consider this:

// replace the println with something mean
// e.g. deleting files

groovy ez " Object(){ println 'something nefarious' }"
A strong attempt, but it won't work because, as best I can tell, Groovy doesn't support anonymous classes. (This is not a problem, thanks to closures).

Challenge

Can you find a value for args[0] that prints 'something nefarious' ? I tried for a long time before I saw an answer. The exploration is a great way to test your Groovy Kung Fu.

I will schedule an "answer post" for 2 pm Central US.

About the Title

In truth, I have no doubt that someone will break this.

The title is a play on "Betcha Can't Play This", a video segment by the magazine Guitar World. In the segment, virtuoso players show off a lick that is usually extremely fast and highly technical. Though there are wonderful exceptions, most are musically vacuous and are technical scales or arppeggios: you can tell when they play it slowly.

Interestingly, the parallel between music and software is strong: these puzzlers are pure "syntactic studies" (etudes!) that don't really offer much use to real end users with real problem domains.

Thursday, June 5, 2008

Beyond Reflection

Consider this problem:

Write a program that takes a fully qualified Java class name as input. The program should call its no-arg constructor and print out the object's toString method to prove the object was constructed.

Think on that one. I'll wait.

One Solution

You might think "I could do that with reflection, but I can't be bothered looking it up right now".

Righto: me too. So I didn't write the program that way. Note that the problem statement doesn't say write a Java program. I used Groovy. Here it is with some executions:


$ cat ez.groovy

def input = args[0]
def theObject = evaluate( " new ${input} () " )

println theObject.toString()

$ groovy ez java.util.Random
java.util.Random@9478c7

$ groovy ez java.util.concurrent.atomic.AtomicInteger
0
Executing Data

The key here is that evaluate allows us to execute data. This idea is hardly new, but one of the most powerful features for any language.

Note that this program will work with any class available on the classpath (provided that it has a no-arg ctor).

At Work

I've used a broader example recently at work. I wanted to give domain experts access to an OO database, using an existing, proprietary Java framework that provides a standard API.

The idea is roughly like so:
  • Ensure the classpath contains the OODB, the framework, etc
  • The program accepts a domain object and desired value field
  • The program builds an evalString as input to evaluate
  • The evalString contains all potentially-necessary imports
  • The evalString opens a transaction, accesses the object to print the field, and closes the transaction
In pseudocode, the template looks like this:

def importString = " import com.abc.foo.* ; ETC"
def evalString = importString + """

def txn = new Transaction()
txn.open()

def theObject = ${args[0]}Factory.build()

println theObject.get${args[1]} ()

txn.close()
"""
Note that the above isn't true Groovy, but you get the idea.

The Upshot

This is a wonderful example of the power of executing data: particularly on the JVM. Note that this isn't some obscure academic exercise. I have seen at least 2 people look at this code and say "I need that program".

And yet it isn't rocket science: anyone could have written it, before Groovy came along. But the friction of Java's reflection was just too much of a deterrent.

This is exhibit #37 in the case of "why it is not wasteful to learn new programming languages".

Kudos to Ivy Tutorial

Recently, I wanted a quick example of using Ivy with Ant.

I had been reading through the newer Ant in Action book but felt like I was on a big yak shaving expedition.

Then, I came across this tutorial in the docs. Note the self-sufficient build.xml file that has an embedded HelloWorld program and targets that make a working Ivy example, out-of-the-box.

Fantastic. Thank you, Ivy. This is a nice example of a foothold, and an object lesson in effective examples for other projects.

Sunday, June 1, 2008

What to do with old hardware?

The Java Posse recently had an interesting spot on the topic of old hardware: recycling and/or donating to schools etc.

This is a timely topic for me as I have an aging, beastly CRT monitor. I am dreading the thought of moving it from my apartment: it is heavy enough to be a bugger for one person, yet too small for me to actually ask for help lifting it.

I can probably hoist the thing into my car, but it would be nice to know that I am risking major back pain for a good cause.

For the locals: does anyone know of a place in St Louis that accepts old hardware for either recycling or re-purposing (pardon my verbing) ?

For others: what do you do? I'd be willing to consider shipping charges to send it somewhere in the US.

Hmmm.... The real question: is there a website that co-ordinates such organizations? A site where you click on the map and it lists the various places for this kind of thing. (Aside from straight-up Google, Google Maps, etc).

Tuesday, May 20, 2008

The Sound of Music: Podcast Themes

For a random sidebar...

What are some favourite podcast themes? No, not the content per se, but the catchy bumpers at the beginning and end of the casts.

Some thoughts on podcasts on my playlist:

  • I'm a guitar fan so the theme at Software Engineering is a winner, and the inspiration for this post. The music is punchy, upbeat, and they always seem to fade it in and out just right. By the way, this week's guest is Anders Hejlsberg.
  • I really like the end theme/outro for Java Posse. The retro synthesized voice is delightful. The famous opener is sufficiently catchy and irritating to burrow deep into your consciousness, which is really the point.
  • Man, the band phoned it in for CNet's Buzz Out Loud. The theme is musically bankrupt, and they barely let it play, which may be merciful. However, I'm always suckered in by Molly Wood's brisk whirl through the subtitle "podcasts of indeterminate length". Nice.
  • For the Google Developer Podcast, the immediate opening isn't for me: it's punchy but something is not quite there. But when the wah guitar kicks and the woman begins to sing, I'm completely on board. Great voice and a cool vibe. The fade in is very erratic in terms of length, but at least they give the tune lots of space as the outro.
Yours?

Monday, May 19, 2008

Fitness as a Competitive Advantage

Jeff Atwood blogged recently about cleaning the monitor and the keyboard.

Some quick thoughts:

  • A big +1 on not touching the monitor. I cringe every time anyone touches any monitor. It actually distracts me during code reviews.
  • Keyboard manufacturers should figure out a way that the electronics can be removed easily (like a cartridge) to enable dishwasher use.
  • Jeff mentions "my" keyboard: I guess he doesn't work in a public war-room. Believe me, that is a whole new, fascinating ballgame. More to come sometime.
  • I don't condone filth, but I'm not convinced the neat-freaks have it right, especially with those cleaners that clean 99.5% of bacteria. Hmmmm.... no training for the immune system and we leave The Nasties to party. Is that a good idea? (As an aside, check out this article on germ factories (aka children) and the beneficial effects on parents and pediatricians.)
On to the point: presumably, Jeff wants to clean his keyboard so that he doesn't get sick. However, that is only half of the equation.

It is well-documented that moderate exercise can boost the immune system. So one could argue that we owe it to our teams, our careers, and our families to get out there and work out.

Maybe you do so. Maybe you do more than me. But if not, I'm not talking about doing an Ironman (though a sprint triathlon is a fantastic accomplishment, and much easier). I'm suggesting a 30 minute session 3 times a week, where the heartrate is elevated. Don't go for a walk: that's ok, but if you want to fight germs, aim higher.

There are other benefits as well:
  • You know those "aha" moments in the shower? Well, athletes shower more often: more aha moments.
  • It's not quite as magical as the shower, but if you swim or run, it is a phenomenal way to work out problems, without trying. The whole subconscious/background cooking thing.
  • I hate the pseudo-verb "network" but if you find your niche, you'll meet all kinds of new people. Bonus.
  • I have friends/colleagues who train in mixed martial arts: that's gotta be a stress reliever.
I ain't preachin'. I ain' teachin'.

I'm just sayin'.... Something to consider.

For those who are active: what do you like to do?