GWT: Using source-path to create your own JRE emulation classes

August 15, 2008

I recently ran into an issue where a class I share between GWT and server-side java code would really, really benefit from being able to accept or return a Calendar object.  However, GWT doesn’t come with a Calendar class, so the appeared to be impossible.  Or, is it?

After some digging around in the documentation I discovered that GWT provides a directive in their module XML called “super-path”.  By adding something like:

<super-path path=”gwtonly”/>

To my *.gwt.xml file, I can tell GWT to load my own emulation classes when compiling javascript; when running in hosted mode, it will use the Java implementation of these classes.

Here’s an example source tree structure for this:

  • myapp/
  • myapp/MyApp.gwt.xml
  • myapp/client/*.java
  • myapp/gwtonly/java/lang/
  • myapp/gwtonly/java/lang/

In my Calendar and GregorianCalendar I just define exactly the methods and constants that I need for the code to *compile*.  Note that I’m not planning to actually use these classes;  I could, but the implementation is so minimal it would cause confusion for future programmers who wouldn’t understand why the calendar class behaved so weirdly in client-side code.

However, now I discovered that I have errors in my code when I compile.  Huh?  In eclipse it’s happy but in GWT it says something like “package declaration should be java.lang, but it should be”.  Oh, I see, it’s really using that package as a source folder, not as a package.  So, I fix it by changing the “package” declaration at the top to “java.lang”.  Guess who is unhappy now?  eclipse!  The two compilers can’t see eye-to-eye any more.

There is a solution – eclipse has a feature called “exclusion filters”.  I open the “Java Build Path” panel for the project, find the source folder that has all of this in it, and add an “exclude” filter for myapp/gwtonly/.  Now eclipse just ignores those files; I’ve lost a lot of eclipse java features as a result, but at least it works.

All this is part of my work on simple online accounting software, if you’re a consultant, entrepreneur, virtual assistant, bookkeeper, or freelancer go check it out.

Getting an SSL private key into glassfish

July 2, 2008

After much frustration I finally figured out how to get my existing private key and certificate into glassfish’ keystore so that it worked.  You see, I did something that the makers of java keytool never thought of – I didn’t use keytool to generate my private key!  Unfortunately, keytool doesn’t allow you to import an existing private key, you can only import the certificates (e.g. the public keys).  It took me a few hours to figure out this bit of idiocy, and kudos to this blog post for enlightening me:

Import private key and certificate into Java Key Store (JKS)

I followed his steps to convert the keys into DER format and generate a keystore file from that.  Then I used keytool’s -importkeystore command to merge that new keystore into glassfish’ keystore, and used keytool to change the key’s password to match the keystore’s password.  Fixed!