Saturday, January 13, 2007

Groovy man...

I recently posted a comment about JDK 7 in this blog. The entry was about how the proposals for JDK 7 are starting to move Java more towards a language like Groovy. My comments were along the lines of Sun needs to back more languages running on the JVM (specifically dynamically typed languages--like Groovy and Beanshell) and improving the overall quality of the core libraries.

As a result of this entry, I decided to see what Groovy was all about. The day Groovy 1.0 was released I downloaded it because I was curious. I installed it, but didn't do much more than that. When I read the aforementioned blog (which I was tipped off to on the groovy-users mailing list), it really got me more interested in Groovy.

The next thing I did was download Eclipse and the Groovy plugin. Normally I use Netbeans to do development, but I chose Eclipse this time (Netbeans is excellent for other things though). I went to work with the test script on the page that's on the plugin install page, namely:


class GTest {
static void main(args) {
def list = ["Rod", "Phil", "James", "Chris"]
def shorts = list.findAll {
it.size() < 5
}
shorts.each {
println it
}
}
}

And the first question I had was "What is it?" Secondly I'm a Java developer so Closures, while not a completely foreign concept to me, are different. I decided to experiment with it.

I ran the script and I thought "why are only the first 2 names being printed to the console?" I was dying to know what this thing was doing and how it was doing it.

The first thing I did was step through the script in the debugger without ever having the thought occur to me that you can't possibly debug a script in the JVM. I guess you can because I was able to step through and see what was going on. Unfortunately, the debugger didn't reveal anything obvious about the mysterious it.

Then I changed it to shit and then run the script. This got a different result. Instead of Rod and Phil being printed to the console, an exception was thrown. I knew immediately that although Groovy is dynamically typed, it wasn't going to take any shit. Especially any uninitialized shit.

Finally I broke down and do what I always do in situations like this. I read the documentation. That's right, I read it. And in the documentation for Closures, I finally found:
Closures may have 1...N arguments, which may be statically typed or untyped. The first parameter is available via an implicit untyped argument named it if no explicit arguments are named. If the caller does not specify any arguments, the first parameter (and, by extension, it) will be null.
The it has a meaning! Ha! Who knew??? Seriously, it (both the explanation and implementation) makes sense.

So, I'm starting. I'm hoping to find out some good stuff with all this. I'll be sure to share.

Tuesday, June 27, 2006

Cygwin + Windows + SSH + USENET == Happiness

Finally got Cygwin working. The biggest obstacle I overcame was figuring out where my $HOME directory should go. I wanted it to go to my regular WinXP home. But whenver Cygwin started, it wanted to set the $HOME to /usr/bin"C:\" and would then error out and start me in /etc/skel. This wasn't a huge deal for me because I would normally start the X server and use PuTTY to log in. Then, I'd set the DISPLAY to point back to my laptop's IP address and away I would go.

Well, after doing some research I found out that pointing to your laptop's IP address isn't safe. In fact, it's not safe at all. This makes complete sense. But I was (and still am) confident that the fact that since I was using SSH to begin with would not compromise any sensitive data (passwords and such).

Being a guy who likes to follow the rules, I decided I should go the safe route. This would be a combo of Cygwin, OpenSSH and X. The goal was simple: Start X Windows then log in to the server via an 'ssh -Y me@theserver'. To do that, I needed to set up a .ssh directory in my $HOME directory which meant overcoming the problem I was having with Cygwin wanting to set $HOME to /usr/bin"C:\". The search for a solution began.

First, I screwed around with /etc/passwd to see why $HOME was being set as such. After a few minutes, it became clear that it wasn't the problem.

Next I tried Google. There were a few promising leads, but no real solution yet.

Finally, I turned to the venerable USENET (which for me means going to groups.google.com). A quick search again didn't give me any real answers so I posted my dilemma to comp.unix.shell and comp.arch.embedded. I was underwhelmed with the responses I received. Except for one.
Someone suggested I set my Windows %HOME% to %USERPROFILE% at the system level. That's when I realized my %HOME% variable (which Cygwin uses to set its $HOME) was quoted and that the quotes were causing my $HOME to not be set right. I removed the quotes and viola!!! I was in business!

After some additional tinkering, I got X running correctly and safely. The moral of the story is that USENET is still useful. It only takes one right answer or suggestion to solve your problem. I'll be using it in the future.