Querying java.util.Collections for fun and profit with JoSQL

I’m working on a little project that implements an event queue. I could have happily implemented it in a List, but I’ve wanted the power to query it in all sorts of unusual ways so I ended up just writing it to a database. But this week I’ve come across JoSQL - a very cool little library for querying java collections using SQL style semantics.

Now this little library isn’t a database tool, it’s a query tool for java.util.Collections in memory (the name is a little unfortunate - but makes sense once you’ve used it). Once you wrap your head around it, things get very funky. To query my event queue (an ArrayList) for events of a particular value, I can do stuff like:

Query q = new Query();
q.parse("SELECT * from event.queue.Event where key = '" + eventCode + "'");
QueryResults qr = q.execute(eventQueue);
if (qr.getResults() != null)
    return (Event[]) qr.getResults().toArray(new MonitorEvent[0]);

Of course this is something you could write by hand by iterating your collections and performing your custom checks on each iteration, but using JoSQL is just way more productive and intuitive - and you can express what you’re after very concisely.

You can event do ORDER BY and all sorts of SQL goodness. Very cool stuff. And how performant is it? Well, compared with the database I was persisting this stuff to… let’s just say I’m a long way ahead…

Big props to the guys at JoSQL. Very funky and productive library!

[Read More]

opencsv 1.3 ships - with heaps better Excel CSV parsing

Thanks to some fantastic bug reports (especially Rogan Creswick who submitted two patches, three unit tests and supporting test files… Wow! Now that’s living the open source dream. Gold star effort) - opencsv 1.3 is now available on

Sourceforge for your comma-separated pleasure.

This release fixes numerous problems with handling embedded double-quote characters using Excel-style escaping (where “” represents an escaped embedded quote). Turns out the rules for this get pretty tricky when you have strings like “””””” which represents an embedded empty quoted string (sheesh!).

Many thanks to the punters who keep posting patches and unit tests. You guys are awesome.

[Read More]

Have you ever run a training course?

I’ve often been interested in getting more involved in running training courses. I’ve run a couple in my time, and really enjoyed the experience. I’ve never done anything “professionally” though.

I live in Canberra, the capital city of Australia, and the training options are numerous in stock standard areas (Intro to Java, Java Web Programming, Intro to J2EE, etc), but it’s all very mainstream. I’m wondering where there might be business opportunities to offer training Australia-wide in more niche (but growing/exciting) fields of Java technology (eg Spring, Hibernate, Eclipse RCP) that developers are likely to really benefit from.

I guess the real killers would be:

Training in Australia seems pretty expensive (circa $3-4k/week/student), so the maths seems to add up pretty quickly if you can put together a class of 6-8 students. Even if you were only “working” one week a month and prepping the rest the numbers could probably work.

I’d love to hear from anyone who has run any commercial developer training. Did you develop your own materials? How did you go about marketing yourself? And based on the amount of work you could generate, did the maths add up for you?

[Read More]

Browsing and Editing Jar Files from within Windows Explorer

After Florin mentioned he could browse and edit jar files from within Total Commander, I figured there had to be a Windows Shell extension that did a similar thing. Turns out it’s built into XP! I showed one of my mates, and he said, “Sure, I do that all the time”… so I guess it’s old news to some, but not to me!

All you need to do is associate jar/ear/war files with the Compressed (zipped) Folder extension from within Explorer and it’s all go. Just fire up Explorer, head into /Folder/Options.. and from the File Types tab, create a new association for JAR files to point to “Compressed (Zipped) Folders”. There is a groovy little how-to available covering all this.

The true coolness, though, is that when you are browsing inside the jar, the folders are writable via standard Explorer cut and paste / drag and drop, which is great for hacking jar files in place and patching a given class (not that you would ever do such a thing… but there you go).

Alright, enough of the win32-ery for a while… We return you to your standard cross-platform bloggery…

[Read More]

Two Very Cool Developer Utils for Windows

While some of my development is done on Mac, and some on Linux, most of my time is spent on Windows. Not really fussed about operating systems these days, so happy to work on whatever. Anyways, I work with this guy Shayne who knows more about Windows utilities than anyone, and he keeps showing me cool stuff.

The first one is a clipboard extender called Yankee Clipper. This beast keeps a running history of your clipboard over time, and gives you a cute dropdown menu to pick from when you use Ctrl-Alt-V. Shayne has a more complete review.

The second cool utility that I just can’t live without any more is Dirkey. This guy allows you to bring up a picklist of Windows Explorer shortcuts when you are in File Dialogs (or Explorer itself) simply by right clicking on the Close Window button. This turns out to be phenominally useful when navigating the filesystem.

Both utilities are freeware…. And too cool for school.

[Read More]

Reducing Pebble comment spam with maths captchas

My blog seems to have received heaps of comment spam over the last couple of weeks and it seems I’m not alone. So I did the open source thing and took matters into my own hands!

I’ve hacked the source for pebble.jar and the comments html to create a “Answer a simple maths question” captcha for comments (check out the Comments form to see it in action). Other blog systems seem to be using this kind of approach with some success so I thought I’d give it a go.

So far it’s working a treat! And it will be a pain for them to write a screen scaper to pull out the maths question, solve it, then submit it. For those interested in the mods, you can get a zip of the diff, along with a prebuild jar from here.

Enjoy your spam-free comments… At least for now…

[Read More]

Now this is a decent bug report..

I’ve just uploaded opencsv 1.2 to Sourceforge which fixes a bunch of annoying bugs. But one bug report has really stood out for me as a sensational open source bug report. Gabriel not just told me about the defect (not handling nulls correctly), but he posted a test case, and the fix required to make it green.

Test cases are just so useful for reporting and fixing bugs… and it just adds tons of value when people take the time to post a useful test. The test is actually even more valuable to me than the fix, since a test is a piece of code that just keeps on giving for future iterations and regression testing.

Cheers Gabriel! Made my day.

[Read More]

djUnit is one cool plugin

I’ve been looking for a decent code coverage tool that integrates nicely with Eclipse for some time. I’ve used clover and cobertura on the server side as part of automated builds and they are both very cool tools, but I’ve been looking for an open source tool to run on single tests in the IDE just to see how much things are improving.

Enter djUnit. This tool has exactly the format and integration that I was after. Instead of doing a /Run As…/JUnit Test, you simply change that to /Run As…/djUnit test. And voila! Check out the sample report it generates.

The other thing that is really simple is removing all of its coverage markers. Open up the djUnit view (I use it as a fastview), click “remove all markers”, you’re done. Easy, non-intrusive, and extremely useful.

The speed the plugin does its on-the-fly instrumenting has also improved enormously over the last few months, to the point where it’s slower, but not painfully slower, than running a normal jUnit test - and I’m happy to wait a little for the incredible convenience.

Huge kudos to the the djUnit team. Awesome stuff.

[Read More]

Sudoku + Java + SpringMVC =

Well… an online sudoku solver web app using SpringMVC. Taking our local JUG’s Sudoku Coding Challenge to the next level seemed like a pretty good waste of time to me… so there you go. My solver uses very basic heuristics and has no brute force logic yet. Even so, it’s manage to solve all but the most evil computer generated Sudokus that I’ve thrown at it.

As an aside, if you’re in the market for some web templates for your app, I can really recommend oswd - open source web design as an awesome site to browse. Being the king of the FrankenWebApp I’ve found it invaluable.

[Read More]

Back from 3 Days at IBM Labs Australia

Just came back from 3 days at IBM’s Tivoli Labs on the Gold Coast - and had a great time - even though it was unbelievably hot up there. It was great to meet the guys up there who have just shipped Tivoli Access Management (TAM) 6. But what I was really excited to see was the very cool culture that they have build up there - very casual but with tons of young smart people having fun and doing great work.

I was up there getting a briefing on what IBM have been up to in the Federated Identity area with their various FIM and TAM products. Had a good chance to write some code to integrate WebSEAL with a client’s internal Java identity management software - and it was awesome to be able to talk to they guys that are actually working on the source base. It’s pretty cool to have access to vendor people who can say, “I’ll just check it in the source and see how that bit’s implemented”. Great stuff.

I was really inspired for my own dreams in the ISV space. It is so doable - and people up the road from me have done it! (the guys in the Labs were acquired from a small startup of 20-sometings about 7 years ago). Very pumped.

Anyways, looks like I’ll have a chance to play with a whole bunch of IBM Java identity and access management stuff in ‘06 - and it’s great to have the contacts to be able to follow up with the guys actually building the products.

[Read More]

Java Sudoku Solving...

At the moment I’m tinkering with a little Java puzzle from our local CJUG that centres around Sudoku Solving. If you check out that link you can download Matt’s harness to try your skills. Subclass the base solver and implement the abstract solve() method and you’re in business. The harness will fire a bunch of sample Sudokus of varying difficulty at your class and tell you what you’re missing.

Crushingly after two hours I can only manage to solve two of the easy and two of the mild problems. Maybe I should submit it as a TabloidSolver?

Try your hand if you’re feeling game… And you have too much time on your hands…

[Read More]

opencsv 1.1 - now with csv writing!

This is the version that I was never going to write, but I’ve decided to add csv export to my little PasswordSafeSWT project and, since people have asked about csv writing capability, I now had a reason for opencsv 1.1.

opencsv is an ultra-lightweight minimalist cvs parsing and writing library for Java under an Apache 2.0 license. The API is extremely tiny with only two methods and a couple of constructors. It handles embedded carriage returns, custom delimiters, and the common cases for most csv files. Might save you an hour if you’re in a hurry.

[Read More]

You really don't grok Spring IoC when...

Working on my first major SpringMVC app at the moment and having a ball. But this whole ApplicationContext thing was really bugging me. I was looking up beans all over the place, and in order to get a handle to the appContext to do the lookup I was making heaps of stuff ApplicationContextAware. How do people feel comfortable doing this in their apps?

But the penny dropped when looking through some of the Lingo spring examples…

Then in your Java code, just lookup the client in Spring (or preferably have it dependency injected into your code by Spring)

Of course! Why am I looking this stuff up via an appContext?!?. Geez. It’s an IoC container - it’s designed to inject stuff.

Couple of hours later and I’d pretty much removed all my ApplicationContextAware beans and almost all my getBean() calls and injected the real objects straight in them. I now have a much cleaner codebase that isn’t even tied to Spring as an IoC container.

So I think I just discovered a very basic Spring Antipattern: If you have tons of code doing getBean() lookups, and a dozen ApplicationContextAware there’s a code smell going on. It’s time to invert…

[Read More]

Joshua Bloch Podcast (... and I'm loving the Java Posse Podcast!)

I’m right into this podcasting deal of late, and I’ve gotta point you to The Java Posse for a great Java-centric podcast with excellent interview content.

This last week they’ve interviewed Joshua Bloch from Google (of JDK collections fame - and writer of that excellent book) and it was a very interesting listen. In particular it’s interesting to see that Google run on C++ for the core search stuff, and Java a lot of everywhere else, with some Python in there for the glue. Joshua is one very smart dude, and it’s interesting to hear some of his ideas on the future of Java (also makes me want to play with Python just so I can get a feel for what it’s like for scripty goodness).

If you haven’t got into the podcasting thing yet, grab an aggregator and make it happen! I can also recommend DirectCut as a great tool for splitting podcasts into manageable segments (I split mine into 5 minute segments which makes it easier to jump around in my player).

I’m loving listening to interviews and news on the way to work! Kudos to those Java Posse dudes!

[Read More]

Handling Window Close Events in JFace

Just so I remember how to do this next time… JFace apps work a little different to normal SWT applications. In an SWT app, you’d normally register an event listener and check for SWT.Close. If you do that, you might not get the behaviour you expect (I found that my current shell was gone by the time I got the event!).

JFace actually make all this a whole lot easier. You just need to override handleShellCloseEvent() and do your own magic. The default implementation just closes everything down. In PasswordSafeSWT, I wanted to prompt for the user to save their modified safe before exiting, so my implementation looks like this:

    /**
     *
     * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
     */
    protected void handleShellCloseEvent() {
        boolean cancelled = saveAppIfDirty();
        if (cancelled) {
            setReturnCode(OK);
        } else {
            tidyUpOnExit();
            super.handleShellCloseEvent();
        }
    }

And you’re in business. If the user cancels the exit, you just set a ReturnCode of OK and life continues as normal. If you’re happy that things are ok to exit, just invoke the superclass and you’re done.

This took ages to work out since I was doing all sorts of evil in my SWT.Close event listener to try and work around it. In future I’ll just read the Javadoc.

[Read More]

Launching an external browser url from SWT

In developing PasswordSafeSWT, I’ve never really been able to get URL launching working successfully (“Go to Password Safe Website” type stuff) on all platforms. I’ve toyed with BrowserLauncher (which seems to work great on Win32 and Linux but causes my OSX box all sorts of grief).

Well, after checking out some of the SWT newsgroups, the answer is much simpler than I thought. You can launch arbitrary programs using the Program class, and they launch with whatever app is associated with them (ie whatever is the default browser on the platform). So I just dropped the following line into my Action, and I was in business:

org.eclipse.swt.program.Program.launch("http://passwordsafe.sourceforge.net/");

I’m a raving fan-boy. I just love SWT….

[Read More]

PasswordSafeSWT 0.4 is out

PasswordSafeSWT is an Java/SWT port of the popular Win32 blowfish-based password manager Password Safe.

This new version (0.4) fixes a number of annoying bugs from the 0.3 release. In particular:

  • Prompt for saving unsaved entries on close (thanks Neil)
  • Tree views now display correctly
  • Remembering your active view between invocations
  • Proper exiting from the opening dialogs
  • Version information now available in about dialog
  • Edit dialog now defaults Ok button
  • Preference changes are now applied immediately (rather than on restart)
  • Tray icons now displaying correctly on Linux

Win32 Screenshot of PasswordSafeSWT 0.4

You can download the latest release for Mac, Linux and Windows from here .

Enjoy!

[Read More]

opencsv 1.0 ships

Ending my obsession with all things comma-delimited, I’ve deployed 1.0 final of opencsv to Sourceforge. This version adds the capability to supply custom separator and quote chars, and pretty much rounds out everything I want to do with CSV.

Read the quickstart, then grab the files from the download page.

Happy delimiting!

[Read More]

opencsv 0.2 released to Sourceforge...

Continuing my quest to change the world, one comma at a time, opencsv 0.2 is now available from Sourceforge.

This version includes a quickstart guide, the ability to handle quoted entries that span lines, a simple example and generally better packaging.

Subject to bug fixes, this version will become 1.0 down the track. Enjoy.

[Read More]

opencsv 0.1 - Yet another Java CSV parser (but under an Apache 2.0 license)

Yeah. I know. Why is anyone even considering building yet another CSV parser for Java? You got me, but I am. Go figure. The reason is that I haven’t been able to find a tiny CSV parsing class that’s available under a commercial friendly license.

For some reason, all of the ones I’ve come across are a strict “GPL or buy a commercial license” type deal. Well, it’s a CSV parser - how hard could it be? (always a fatal question, since my effort doesn’t handle a lot of the commercial features, but at least it handles the common cases). So here’s my afternoon’s effort to at least get you started.

The quick case:

   CSVReader myReader = new CSVReader(yourFileReaderOrWhatever);
   List entries = myReader.readAll(); // a List of String[]
   // .... iterate and enjoy

Or, if you’d prefer a pull model to conserve memory…. use the readNext()</code > method to get a <code>String[] for the next line, and you’re all done when you hit a null.

This version is very bare bones. Supports double-quoted elements, and that’s about it. No comment support yet. Probably heaps of edge cases aren’t handled. No need to whinge. It is, at least, an Apache 2.0 licensed CSV parser for Java. Modify to suit.

Enjoy. Download it here.

[Read More]

Processing 2Gb XML Files with StAX

As part of a recent CJUG XML Smackdown we played with processing a very large (2Gb) XML file using SAX &#038; StAX. I was doing the StAX bit (using Woodstox), and I learnt some very interesting lessons on processing large XML files along the way.

The file we used to demo was a large rdf file from the dmoz directory. Its compressed (gzip) size is 299Mb, but it unzips to just under 2Gb. The test was to iterate all elements and pull the urls out of the “about” attribute.

I tried the demo on three machines. A Mac Mini (290secs), An old Thinkpad (370secs), and a newish AMD 2.4 Linux box (80secs). The results were very interesting - I/O was definitely the killer on the slower boxes. To experiment further, I changed the code to read from the original 300Mb file using a GZipInputStream - and the Thinkpad came in at 270secs - I wiped off nearly a third of the time! No such luck on the AMD box (which has a SATA hard disk) - where throughput actually slowed when using the Gzip method.

Interestingly the Sax performance was nearly identical in all tests (mostly slightly faster than Stax, but sometimes slightly slower - but either way not really significant given the amount of processing).

So the learning experience for me:

It’s sure no empirical test - really just a back-of-a-napkin kinda thing. But it was fun to demo, and gave me my first ever chance to use a GZipInputStream!

[Read More]

Pro Spring in one very cool book...

For a new web project I’ve been learning the Spring Framework. Based on a few recommendations I’d read around the blogsphere I ordered a copy of Pro Spring and I’ve gotta say I’ve been hugely impressed.

I’ve been reading/implementing for around two weeks. When it arrived I was sure it was a lemon (how could it cover MVC in a measly 45 pages?). What I didn’t count on was how good the examples are. Sure, you don’t get the total coverage of a dedicated MVC book, but you get excellent coverage of more areas of the framework than you could possibly imagine.

The database-driven web app I’m building is using Velocity, iBatis, Postgres and Spring (what I’m calling VIPs ;-), and, even though I’ve used iBatis before, I’ve learned heaps of tricks while reading the Spring/iBatis integration chapter (like using inheritance in resultmaps). I’ve got parent/child selects and inserts going on, I’ve got my auditing going to the database (using a Spring HandlerInterceptor to check page access), I’ve got connection pooling and all sorts of cool stuff. And I’ve been having a ball all the way through.

Even the mail integration chapter is awesome! Gives great examples of generating rich html emails using Velocity with embedded images (exactly the thing I’ve always wanted to do with mail!).

The appendix on unit testing is also full of gems! What a great idea to have a transactional unit test that you can roll back when your test is done without touching your test db!

What has really surprised me about this book is the rich quality of the examples. There have really picked stuff that you want to do in the real world. In fact, the more I get into this book, the more battle tested it feels. It definitely has the feel of “we’ve built a dozen production apps in this framework, and here’s the dirt on what we’ve learned along the way”. I love that in a book!

I’ve had a scan through Spring in Action and at first glance it doesn’t seem to have either the breadth or depth of Pro Spring. Matt Raible’s Spring Live looks very cool, though (particularly since he has stuff in there about Spring WebFlow). Be good if SourceBeat provided a detailed table of contents, though, where you could see the details of what’s covered before buying a copy (rather than just see the title of each chapter).

Anyways, Pro Spring gets a huge tick from me! Great job, Rob &#038; Jan! And great stuff apress! Highly recommended!

[Read More]

Eclipse + JettyLaucher = Loving Web Development

I’m working on my first serious web app for ages and taking the opportunity to learn SpringMVC in the process. I’ve been busy doing lots of client-side SWT programming and plenty of backend EJB stuff, but amazingly haven’t managed to do much in the web space for quite some time.

Anyways, I’ve been a longterm fan of Jetty - it starts in a flash, is easy to configure, has a tiny footprint and it’s Australian - what more could you ask for? Anyways, I was keen to see if to see if I could easily deploy my web app to Jetty straight from Eclipse.

Enter the JettyLauncher Plugin for Eclipse. Jetty was always designed to be embeddable, with a simple API for configuring your deployments programmatically. This plug in takes advantage of that embeddable API and embeds Jetty straight into Eclipse!

Once you’ve got it installed you can now go into your Run… menu in Eclipse and choose the “Jetty Web” option. From there you simply tell it the location of your project’s war directory, what context you want to expose it under in Jetty, and what port you want Jetty to run on. It’s a snack! Suddenly you have Jetty output in your Console window!

Because Jetty starts in a couple of seconds, I have found it’s a very snappy way to test our your webapp - especially when you’re learning a new framework at the time. I can hack my Spring context and restart in a flash, or just edit my Velocity templates straight in Eclipse and keep interacting with Jetty without any need to restart!

If you’re looking for a productive toolset for developing your web apps from within Eclipse, it’s definitely worth a look. Great stuff!

[Read More]

Driving Skype from Java with JNIWrapper

As a developer, if you’ve every used Skype, you gotta be thinking… “wonder if there’s an API for this thing…”. Well the answer is “yes”, but not in a format that’s like anything you’ve used before!

The good news is that pretty much anything that you can do with Skype can be automated from the public api (and in a documented way). The bad news is that the API it exposes is consumed in a pretty unique fashion. You can grab the api docs from the Skype Developer page.

On Win32, the API works but using SendMessage calls to the Skype window (ie putting stuff directly on its message queue). There are also GTK equivalents that use freedesktop DBus to do similar. So it’s pretty darn ugly stuff - you end up copying API commands onto the queue (as strings… like the way you used to drive an AT modem!), and off they go. Skype has to be started and visible (part of the API license is that it has to remain visible), so no clean options there for a silent skype in the background.

So how do you call it from Java? Couplea options. There are a few libraries out there now, but mostly in a pretty average state. I found the quickest way to get going was generating my own using JNIWrapper. I installed the (very cool) ActiveSkype COM library from KhaosLabs that defines all the constants and strings you need. Then fed that COM library to JNIWrapper, and I was in business - current Skype Java lib read to roll! Then I could do stuff like:

    IAccess myAccess = Access.create(ClsCtx.SERVER);
    IUserCollection mates = myAccess.getFriendList();
    Int32 mateCount = mates.getCount();
    System.out.println("You have: " + mateCount + " mate(s)");
    for (int i = 0; i < mateCount.getValue(); i++) {
        IUser nextMate = mates.getItem(new Variant(i+1));
        System.out.println("Next Mate is: " + nextMate.getHandle());
    }

You can get access to their online status, place a call to them, or pretty much whatever the client can do. The API is pretty complete, including event notifiers for new calls, but it does have an unusual approach to exposing its services.

I thought about playing around with an “answering machine” style app… but couldn’t find hooks for sending WAV files down the line. Audio pretty much has to come down the mic channel - and whilst there are ways around it (like installing a “Virtual Audio Cable” VAC driver on windows then remapping it to a media player)… well you get the picture.. it’s a horror story.

It’s fun to be able to script Skype, though. JNIWrapper is so the business. If you have to do any COM work it’s definitely worth the dollars. It’s one of the “Just Works” products that I just gotta rave about.

[Read More]