Thursday, July 19, 2007

Quicktime / iTunes Update breaks Eclipse?

I upgraded my Quicktime and iTunes today and found that all of a sudden I couldn't compile anything in Eclipse! Apparently, the update got rid of the /System/Library/Java/Extensions/QTJSupport.jar file, and that shows up as part of the default Java 1.5 JRE definition in Eclipse. When it went missing, Eclipse freaked out.


The fix, found at note19 turned out to be simple.



Open the eclipse Preferences... menu and select Java > Installed JREs...; make sure that eclipse can locate the OS X Java 1.5. If it cannot (as was in my case), you manually add it. It is in the following folder:

/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home

Works like a charm now.


Wednesday, July 11, 2007

ProScope HR: Coolest Geek Toy Ever?

In preparation for my impending fatherhood, I've been keeping an eye out for fun things to do with my daughter once she's got a few years on her. Like any good geek, I'm looking for fun, geeky things to do that will pique her interest in the world around her and stimulate her little brain. I think I found something that fits the bill: the Pro Scope HR.


We were watching Alton Brown's Pretzel Logic episode and he was using this microscope hooked up to his PowerBook to look at the differences between various kinds of salt. Apparently it's also featured heavily on the various CSI shows.


I remember the microscope I had growing up. It was your standard light microscope; I think it went up to 200x magnification. It came with a number of prepared slides of various microorganisms, which were cool, but making new slides was kind of tedious. Plus, dealing with a microscope while wearing glasses has always been a pain. With a ProScope, you can take a look at anything you damn well please, not to mention taking high-resolution still photos and live-action and time-lapse videos. I can just see us following our daughter around the house, laptop in tow, looking at everything from pennies to raspberries to bugs in the backyard to our cats' paws.


I think that'd be pretty cool, anyway.


Properly setting up a crontab entry using 'date' to generate timestamps

I have a custom backup script that I want to run every night. I'd also like to have all the output (standard and error) redirected to a timestamped log file for subsequent review. My first stab at defining a cron job to handle this was something like the following:

00 0 * * * ~/bin/backup.sh > backup_$(date + %Y-%m-%d).log 2&>1

The only problem is it doesn't work!

Cron sends you messages pertaining to failed jobs in the system mail queue, which on Mac OS X you can access using the 'mailx' program, which comes with the system. Doing so, I saw this message:

/bin/sh: -c: line 1: unexpected EOF while looking for matching `)'

A little broswing the cron man pages turned up that "%" (as well as "#") is interpreted as a comment character. Looks like my timestamp generation was causing the job to fail. Grrr.

The solution? Escape each "%" with a backslash. The functional cron job definition is

00 0 * * * ~/bin/backup.sh > backup_$(date + \%Y-\%m-\%d).log 2&>1

Sanity restored.

Tuesday, July 10, 2007

Liskov's Substitution Principle, equals, and Hibernate Proxies

Hibernate's CGLIB dynamic proxy classes reared their ugly head today. I've been using the Eclipse-generated hashcode() and equals() methods in my domain objects for quite a while, with no problems. Then today I write a test that does a simple equality check and everything blows up!


The problem seems to be due to the way my equals() methods were written, and how Hibernate's default dynamic proxy strategy interacts with that. I was doing this:



if (getClass() != obj.getClass())

return false;
However, in my exploding test, one of my objects being compared was a regular old domain object, while the other was a proxied object. Since the proxied object that Hibernate creates is actually technically a subclass of my class (with additional Hibernate-specific methods and such), my getClass()-based equality test was choking badly; after all, com.foo.MyClass is most definitely not com.foo.MyClass$$EnhancerByCGLIB$$beb95050.

It turns out that this is due to something called the Liskov Substitution Principle, which basically formalizes the intuition behind the inheritance portion of the object-oriented programming model; if S is a subtype of T, then you can use an instance of S wherever an instance of T is called for and nothing breaks. The corollary would be that if something does break, then some of your assumptions might need re-examining.

In this case, specifying the equals() method in terms of getClass() is too restrictive; Hibernate proxies should be able to be used anywhere one of my domain objects is used (that's the whole point!). A way to solve this problem is offered by Josh Bloch in his Effective Java book: use an instanceof-based test instead:



if (!(obj instanceof MyClass))

return false;
Here, the proxied object, as a subclass of MyClass, is also an instanceof MyClass. Since the CGLIB proxy doesn't override equals(), the proxy inherits the same implementation of equals() as the base class, thus maintaining the symmetric property any valid equals() implementation must have. If the subclass did override equals(), then things would be different, but then you'd have a violation of the Liskov Substitution Principle. Also, as Bloch states in Effective Java, page 30:


It turns out that this is a fundamental problem of equivalence relations in object-oriented languages. There is simply no way to extend an instantiable class and add an aspect while preserving the equals contract. (emphasis in original)
If you're paranoid, you can declare your implementation of equals() to be final, so you can be sure that it is never overridden. Since the CGLIB proxy doesn't try to override it, you're safe.

The "downside" of this approach, if it can be called that, is that each "terminal" domain object needs its own implementation of equals() (note that it's (obj instanceof MyClass), not (obj instanceof getClass())); in other words, you can't define a general equals() in a superclass and let it do all the heavy lifting for inheriting classes. However, in the grand scheme of things, I don't really see that as much of a downside. Yeah, it's a bit of a pain to write equals() methods, but it has to be done anyway (it's your job as a designer), and it is arguably a more accurate approach to take. As a designer, you need to be aware of the implication of what you code. If the getClass() method works for you, fine; just be aware of what that implies. Ditto for the instanceof method. I'm convinced that in my particular case, the instanceof approach is the semantically correct one.


Update: I just checked my copy of Java Persistence with Hibernate and, sure enough, they use instanceof in their equals() implementations. Clearly, the interaction with the proxies is a driving reason to use this formulation. Apart from that, though, I still think that using instanceof is more semantically correct.

Helpful Links