Groovy, Grapes, and Google

Groovy is an amazing language. It’s java without most of the cruft. There is one great feature of Groovy called Grapes that lets you manage dependencies within your script. No sbt config, no pom, no ivy config, just business.

Google Maps has an API, and it’s fun to use.

I decided to come up with a script that takes one from Google HQ to the White House.



def http = new HTTPBuilder('')

http.get(path: 'json',
    query: [
        origin: '1600 Amphitheatre Parkway, Mountain View, CA 94043',
        destination: '1600 Pennsylvania Ave NW, Washington D.C., DC 20500',
        sensor: 'true'
    ]) { resp, json ->

  assert resp.status == 200

  json.routes[0].legs.each { leg ->
    leg.steps.each { step ->
      println "\n" + step.html_instructions
      println "\tfrom: " + step.start_location + " to: " + step.end_location + " (" + step.distance + ") "
      println "\t" + decodePoly(step.polyline.points).join(" --> ")


class GeoPoint {
  double lat;
  double lng;

  public String toString() {
    "( lat: $lat, lng: $lng)";

def List<GeoPoint> decodePoly(String encoded) {

  List<GeoPoint> poly = new ArrayList<GeoPoint>();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;

  while (index < len) {
    int b, shift = 0, result = 0;
    while (true) {
      b = encoded.charAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
      if (b < 0x20) {
    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lat += dlat;

    shift = 0;
    result = 0;
    while (true) {
      b = encoded.charAt(index++) - 63;
      result |= (b & 0x1f) << shift;
      shift += 5;
      if (b < 0x20) {
    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lng += dlng;

    GeoPoint p = new GeoPoint(lat: (double) lat / 1E5, lng: (double) lng / 1E5);

  return poly;

The LinkedHashMap

I was converting hl7 to html and was getting tags printed out in my html because of a HashMap. My HashMap was used for escaping some special characters.

substitutions.put("&", "&amp;");
substitutions.put(">", "&gt;");
substitutions.put("<", "&lt;");

substitutions.put("\"", "&quot;");
substitutions.put("'", "&apos;");

I never realized that I can’t pick the order while iterating through the keys till I looked at Then I switched to LinkedHashMap and that fixed everything.

Its comcastic

In a time when even Sun had given up on Applets, there’s one company that still uses them. They didn’t work on my mac. I had to borrow a PC to access Comcast’s signup app. And then I had to go through this BS.
If a website has an online shopping interface and offer you a price there, and then you are declined that price by a “fulfilment” analyst, is that misleading advertising, Or unfair business practice?

And why can’t they mail me the transcript?

From Java to Jython to Java and all the way back

This was an interesting thing I ran into last week:

I have some java code in a library called LightHL7Lib. I was using it to process some messages. And I was using Java to do this. However, when I needed to change the way something worked, since I was integrating this with N different systems which had their own understanding of HL7 [ HL7 is more of a guideline than a rule ], I had to go through one whole release cycle, and that meant half a day spent on each of coding, testing, release, qa, deployment, and so on.

Not too late, I realized this wouldn’t work.

So I decided to put in some solution that would let me mess around with HL7 messages faster.

  • XML. Rejected! I would have to come up with my own language to get the processing right.
  • Put a new process to do all that. Rejected! Too complex to deploy and maintain at multiple sites.
  • Launch processes from java. Rejected! Not a great design.
  • Use a scripting language that runs inside the JVM. Whoo hoo!!

The next question was which language to use. The most popular ones were JRuby, Groovy and Jython. I work with a team of Java developers. Everything we’ve known and used is Java. And Java is not fully interpreted as scripting language. Not yet.

Parameter JRuby Groovy Jython
Do I know it? No No Kind of – I can dabble a bit in python since I downloaded GAE when it was python only
How concise is it Very Very Very very
Problems Not sure A little formal (Java’s vestige) Not so great performance

Based on this, I decided to implement this in Jython.
This is to initialize the PythonInterpreter

String javaClassPath = properties.getProperty("java.class.path");
String separator = properties.getProperty("path.separator");
javaClassPath += separator + getClassPathSource(Hl7Record.class);
properties.setProperty("java.class.path", javaClassPath);
PythonInterpreter.initialize(properties, properties, null);

This is to do the actual processing assuming the script name is in the variable jythonProcessor

fileName = this.getClass().getClassLoader().getResource(jythonProcessor).getFile();
PythonInterpreter jython = new PythonInterpreter();
PyFunction func = (PyFunction) jython.get("process",PyFunction.class);
String output = ((PyString) func.__call__(new PyString(input))).toString();

And the function getClassPathSource()

 private static final String getClassPathSource(final Class clazz) {
    final ProtectionDomain protectedDomain = clazz
    final CodeSource codeSource = protectedDomain.getCodeSource();
    if (codeSource != null) {
      final URL url = codeSource.getLocation();
      try {
        File f = new File(url.toURI());
        return f.getAbsolutePath();
        // return url.toURI().getPath();
      } catch (URISyntaxException e) {
        return null;
    } else {
      return null;

I’m really sorry about the code tags in WordPress/this specific theme messing up the display, but I hope you get what you wanted here.

JDK 1.5/32 vs JDK 1.6/64

Keeping up with a promise decision I made a while back, here’s a post on JRE5 and JRE6. Actually it’s more about JDK5 and JDK6. I was fiddling with working on a grails application, and I decided to run some tests using JMeter. I didn’t know that grails was using JDK5 by default on my computer [ Default on Leopard. ]. I ran 400 threads against the homepage of my app. The error rate was about 6%.

Out of curiosity, I opened up the Activity Monitor, and there was this column mentioning that I was running my grails app was of the Intel kind. And there were a few processes that were of the Intel (64 bit) kind.

So I decided to set the JAVA_HOME to JDK 1.6 [ On Leopard 1.6 is always 64 bit, and 1.5 always 32 bit. ], and ran the tests again. Looked like the error rate was down to about 1.8%.


What does this mean?


Should I start using Java 6?

Java 5 is past End Of Life. So you better do that.

What other tests have you performed?

A very similar test on Ubuntu+Apache+Tomcat+mod_jk and RedHat+Apache+JBoss+mod_proxy_ajp. And the first combination seems to do a better job at running an app based on AppFuse. Read fewer dropped connections.

Your votes do matter!

Why shouldn’t they? If this is a reality show, there SHOULD be audience participation.

That is one of the reasons, watching Bachelor and Bachelorette on ABC wasn’t as much fun for me. But yuttonudder time, an Indian show has proved that the american shows that they ape, can be made better. This was my feedback on the Bachelor some time back.

I heard some tweets about Rakhi Ka Swayamvar, the Indian equivalent of The Bachelorette. And I checked out their facebook page.

This is what I saw. Please read the text in ALL CAPS carefully.

RKS Facebook Page

Google Maps made a booboo

I like google. Most of the time.

I decided to go to Alcatraz. So what do I do? Google up Alcatraz and find how to get there. I book a cruise to Alcatraz after Google search. I’ve got my ticket. So far, so good.

Now comes the day I have to go to Pier 33, San Francisco to board the cruise. What do I do? I fire up google maps, to see the best way to get to Pier 33 from Fremont, using public transport. Good? No! Bad!

Picture 1

Picture 2

I almost went to the wrong place when common sense suddenly struck me. A little late, but not late enough to miss the cruise.

Pier 33 is not 33 Pier. A bay area company should do better at this.

As for the answer, the closest BART station to Pier 33 is Embarcadero. Not Glen Park. You can walk to Pier 33 from there in about 15-20 minutes. Or you can take the F Street Car from there.

And it should not take you 1h59 to get there. My guess would be 1h15.