Hi there 👋

I’m Tom, I make software

Using Jackson Deduction to Simplify Deserialisation

I find the documentation for Jackson is on the terse side, and dare I say obstructively self-referential. If you aren’t moved fully to tears by the Javadoc entries for JsonTypeInfo.As, you deserve an ACM award. In short: I need a helpful and up-to-date guide, so I’m writing one. What are we trying to do? We often want to vary the content of our JSON. It’s integral to the Envelope pattern for instance....

May 30, 2024 · 3 min · 465 words
A child playing in sand with a digger

Quick, Repeatable, Maintainable Maven Builds

There are few things more frustrating than running into issues with your build tool. One moment your build runs smoothly, and the next it fails inexplicably. Or maybe you’ve been typing mvn clean package for years without really knowing why. In this post, I’ll share some best practices for using Maven more effectively, and attempt to demystify some of its features. Understand the Clean Plugin Running the Clean plugin during every build will spend unnecessary time on recompilation and packaging....

April 25, 2023 · 4 min · 667 words

Making Pull Requests

Excited much? Yes I am. I have wanted to contribute to an open source project for a really, really long time. I am unreasonably psyched about the whole experience right now. Trigger warning: I am going to be unironically enthusiastic. Sorry. Here is how I did the do It was unbelievably easy. I noticed in the github news stream that one of my friends had starred a project, and it looked kind of cool....

May 24, 2014 · 4 min · 772 words

Loading a DTD from a JAR

Bundling resources into a jar file is a common way to deploy your Java application, useful because it makes the jar’s resources available to retrieve via the classloader at runtime. When loading XML, you can require DTD veirification, which is a means of validating the XML is gramatical. Hopefully everything up till now is old news. I struggled trying to load XML from the classpath to also have the DTD load correctly....

December 2, 2013 · 2 min · 238 words

Writing Libraries in Bash

If you’re a Python programmer, chances are you’re familiar with the idiom: if __name__ == '__main__': main() This lets you put behaviour in a file that will only be executed if it’s run from the command line, not when it’s included in a program. So it’s possible to achieve the same thing in bash, simply using: [[ $0 != -${SHELL##*/} ]] && main Could that be clearer? Probably. Here’s what it means from left to right:...

November 19, 2013 · 2 min · 399 words

Getting OCMock to play nicely with XCode 5

I’ve started writing a dashboard Cocoa app, and I’ve found the process of setting up OCMock really unreasonably painful. The Setup I got the OCMock framework from ocmock.org. In my case I want the OSX directory for my Cocoa app, so I drag and drop OCMock.framework from the dmg onto the Frameworks group in the project navigator. This triggers a dialog to configure it. The important things to note are:...

November 17, 2013 · 2 min · 251 words

Switching Text Editors

After 7 years, I’m finally doing the dirty dead: jumping off the Vim ship, into the [adjective], [adjective] something [noun] that is emacs. There follows a brief description of the process so far. ![Gnu by Dean Gugler at flickr CC BY-NC] (https://farm7.staticflickr.com/6173/6169267550_753ebd220b.jpg) Install a recent version of emacs My version of OS X has emacs 22.2 installed, which is way out of date. To install the most recent version, I used homebrew....

September 8, 2013 · 3 min · 446 words

Using Mockito as a Crutch

“omne trium perfectum”, as the Latins used to say: a set of three is perfect. Sadly I don’t have three facts I know about mockito, but I do have two facts. That’s not enough to impress anyone, which means almost by definition, it’s enough for a blog. Returning different values from a mock on successive calls This is so basic, I expect I’m the last person to hear about it. I stumbled on it while trying to mock an Enumeration....

August 28, 2013 · 2 min · 400 words

Hanging Out with Linked Hash Maps

LinkedHashMap is a surprising useful data structure, and missing from many standard libraries. In my case I have class that represents a message and contains a set of headers. Imagining an HTTP message might be useful. Historically these sets have been mapped using a configuration file that dictates the order of field keys and values (primarily it’s done this way to support ordered value-only fields): So, to support messages for which the index has no special significance, we’re basically implementing a dictionary inside a list....

August 18, 2013 · 1 min · 209 words

Chopping Down Tall Classes

I’ve been having bags of fun today trying to get to grips with an enormously busy class. The carbuncle of code is basically a data structure that contains a tree of values loaded from a plain-text configuration file. To keep it from being too simple and boring, somebody decided you should be able to pass it an instance of our application, and have it call lots of configuration methods on it for you....

August 12, 2013 · 2 min · 331 words

Pressing Buttons in Bash

Shortcuts are a source of constant amusement as far as I’m concerned, and nothing on Earth makes me more ecstatically happy than accidentally discovering a new one. Today’s mis-fingering was ^t in bash, which roughly translated means transpose the character under the cursor and the previous character. Two transposed characters is by far my commonest typo (vim: xp), so I’m not going to lie, I’m pretty happy. To celebrate I’m going to try and use bash this week by only touching letter keys, Ctrl, Alt and Shift....

May 12, 2013 · 1 min · 162 words

Writing a Simple Gradle Build

‘Build as code’ is one of those dreadful smells you start to recognise after a while, so gradle, which is a highly free-form system, is not my first choice for complex builds, but it is a beautiful way to build a simple set of modules. I’ve written something along the lines of the following build enough times for me to make a note of it for future copy-pasting, so here it is....

May 5, 2013 · 2 min · 217 words

Deleting Files from Git Object Store

Tonight I accidentally did something stupid and committed binaries to my git repository, despite knowing better in the first place. After the commit I tried to push. I hadn’t even noticed my mistake until I came back to my terminal after 10 minutes and saw Writing objects: 42% (16/38), 10.02 MiB | 21 KiB/s Unfortunately simply git rm-ing the files is no good, because they’re in the object store at this point, which is essentially git’s historical record— its database of versions....

April 17, 2013 · 1 min · 166 words

Defusing XML

XML: the most terrifying thing on the planet Rule 1 of the Internet— is never, ever trust the Internet. Reading from a socket is the equivalent of putting your hand into an unknown box that’s full of snakes, but you don’t know it’s full of snakes and it ends up pretty touch-and-go as to whether you get out in one piece. Getting XML over the wire is like that, but more terrifying and more dangerous....

April 15, 2013 · 2 min · 320 words

Blogging with Pelican

All bloggers tend to pass through three phases: enthusiasm, occasional updates, and blogging about blogging… I got a little bit tired of the magic behind Octopress and decided to move to the ‘better for Me(tm)’ platform Pelican. Pelican has been lovely and although Octopress was a great way to get started, I’m not blessed with a great deal of web-dev-ishness, I was never going to take the time to understand its many bundled frameworks....

April 13, 2013 · 1 min · 156 words

Bringing Eclipse Shortcuts to XCode

When I first arrived in the real world, I was introduced to new things; amongst these were ‘collaboration’ and ‘gigantic code bases’. I grudgingly accepted the need for an IDE, and started to use Eclipse. There are actions I use in Eclipse regularly that I find really useful, like searching and manipulating text. Recently doing some Cocoa development and getting started with XCode, I’ve been slowed down by having to learn these same actions again, using different, and sometimes hand-contorting, Emacsian keys....

January 12, 2013 · 1 min · 194 words

Disable Cache in Chrome and Other Goodies

Through some lucky keyboard-mashing I stumbled across a couple of useful developer features in Google Chrome, including an option to disable the cache, and settings for device testing. Here’s how to: Bring up the inspector: ⌥ ⌘ i Hit fn F1 Voilà! The general tab with cache options: The overrides tab with device options: I’ve popped some easter eggs in this post to amuse future-me.

January 6, 2013 · 1 min · 65 words

Code Shame

Naturally when I write C I’m reasonably careful about a number of things. Because at runtime it’s a free-for-all, I take care to notice buffer overruns, uninitialized memory, and in particular the possible values assigned to a pointer. I’ll back up a second, because unfortunately I recently managed to make a bit of a bollocks of the last point, which I only noticed when I came back to change the code....

January 3, 2013 · 2 min · 289 words

Disabling the Next Button in an Eclipse Wizard

I’ve been writing an Eclipse wizard that will be used to create new socket applications. The options selected in early in the wizard determine whether it’s appropriate for the user to configure connection properties before creating the project. If you have written a wizard, chances are you’re familiar with the setPageComplete(complete:boolean) method that is used to enable the ‘Finish’ button when enough user input has been gathered. It’s more complicated to disable the ‘Next’ button....

December 9, 2012 · 1 min · 209 words

Lazy Instantiation in Objective C

Lazy instantiation is a relatively cheap way of allocating memory to component objects as it’s required instead of up-front in a constructor. {% highlight objectivec %} @interface MyStack() @property (nonatomic, strong) NSMutableArray *stack; @end @implementation MyStack @synthesize stack = _stack; (NSMutableArray*)stack { if (_stack == nil) { _stack = [[NSMutableArray alloc] init]; } return _stack; } (void)push:(double)operand { [self.stack addObject: [NSNumber numberWithDouble:operand]]; } @end {% endhighlight %} This implementation overrides the synthesized accessor and adds an alloc-init call if the storage hasn’t yet been initialized....

December 9, 2012 · 1 min · 113 words

Unit Testing in C with Check

###Pushy I’ve been developing a cheerfully indolent little HTTP server for some time now, as one of those ’learn-more-c’, ’learn-TDD’ projects that I imagine nearly everyone has lying around, half neglected. You can see the code for pushy on github. I’ve also been familiarizing myself with the HTTP RFCs as I go, so I was planning to add a 414 response^[1] to pushy’s repertoire, which I reckoned would take me about 30 minutes, but I have the TDD policy on this project, so naturally I can’t write a line of code until there’s a failing test....

December 9, 2012 · 5 min · 994 words

Deploying an XML File with Maven

Something clever we’ve started doing at work is publishing common build files to our local maven repository, so builds can depend on known versions of scripts, rather than just the latest from source control. Setting this up for the simplest case, involves uploading a single file, for instance an ant script, to the repository. The best way I’ve seen this done uses the build-helper plugin, which — amongst many other things — can be used to deploy any file as the primary build artifact....

November 6, 2012 · 1 min · 141 words

Fixing a Permissions Error Opening a Semaphore

Today I spent a couple of hours getting semaphores to work. I was opening a semaphore like this: {% highlight c %} if (SEM_FAILED == (CACHE_LOCK = sem_open("/cache_lock", O_CREAT, 0660, 1))) { printf(“error opening semphore: %i\n”, errno); return -1; /* error creating semaphore */ } {% endhighlight %} This runs fine on my Mac, but fails on an Ubuntu machine. On failure it sets errno to EACCESS (13). Whatever you might expect, this fact alone did not allow me to figure out the cause of the problem....

October 6, 2012 · 1 min · 155 words

Reading from Sockets

This is a story about how I learned to read streams from sockets, or to put it another way: {% highlight diff %} strncpy(msg_buf, “HTTP/1.0 404 Not Found\r\n”, HTTP_RESPONSE_LEN); strncpy(msg_buf, “HTTP/1.1 404 Not Found\r\n”, HTTP_RESPONSE_LEN); {% endhighlight %} How I learned to stop worrying and begin to implement HTTP/1.1. That’s pushy. All it knows is it doesn’t know. HTTP/1.0 For most of the practical work I’ve done with sockets until now I’ve used datagrams, so I decided that all socket reads, including streaming connections, work in the same way as HTTP/1....

September 22, 2012 · 3 min · 534 words

Saving the Workspace from an Eclipse Plugin

Since I’m going to the trouble of making a plugin for Eclipse, I thought I may as well try and make if look and feel a bit Eclipsy, so when it came to writing an export feature to bundle the user’s project, I decided it would be a jolly good thing if the user got a dialog to prompt them to save their changes, much as when you run an application in Eclipse....

September 17, 2012 · 2 min · 382 words

Fixing Git Upload Pack Not Found

I’ve only just realised that blogging about problems is a great way of delaying doing the thing I intended to get done in the first place. I’ve just changed the location of git repositories on my server so that instead of living in a directory belonging to the user ’tom’, they now live in the home directory belonging to ‘git’. Basically, it means when I create a new repository on my server I don’t have to remember:...

September 8, 2012 · 2 min · 270 words

Monitor a Long Running Process with Control T

One of the nice things about being in Linux exile is learning about new things that I hadn’t noticed were missing from my life, like being able to tell if a process has hung, or it’s just taking a really long time. tom@binky:~:$ cp Downloads/FreeBSD-9.0-RELEASE-i386-disc1.iso . ... ^t ... load: 0.17 cmd: cp 20253 running 0.00u 0.16s Downloads/FreeBSD-9.0-RELEASE-i386-disc1.iso -> ./FreeBSD-9.0-RELEASE-i386-disc1.iso 35% Hitting Control-t while waiting for a process to complete outputs, and ‘scuse me while I quote from the manpage:...

September 7, 2012 · 1 min · 144 words

Building an Eclipse Plugin with Tycho

I spent a couple of days this week converting the build of an Eclipse plugin project from Ant with Ivy to Maven using the Tycho plugins. I’ll demonstrate the build with an example project. First off I already have the following pre-requisites: Eclipse 4.2 Eclipse PDE Maven 3 Create a plugin project in Eclipse I created the following: a plugin, with some simple functionality a feature project (features are just groupings of related plugins) an update site, which acts as a distributable package Each project is a necessary part of the build: the site contains a feature, and the feature contains one or more plugins....

September 2, 2012 · 3 min · 607 words

Unit Testing in C with Check - Setting Up a Project

In a previous post I went through the steps to run the Check tutorial, had a bit of a whinge about how difficult is had all been, and mumbled a bit about coming back when I’d figured out how to make everything work on a real project. In fact, that was much simpler than I expected. At the end of this, I’ll have a simple C program with a corresponding build that uses the Check testing framework....

August 27, 2012 · 5 min · 913 words

To Sudo or not to Sudo

This is a pretty neat trick I’ve just seen in a tutorial from the chaps at opscode. I’ve got a command whose output needs to be processed using sudo (a realistic example is curling down a file to install); I’ve got at least three principals that apply in this situation: only use sudo if privileges need to be escalated waste not a minute one line all of the things So:...

August 25, 2012 · 1 min · 122 words

Unpacking Maven Dependencies

Unpacking a kit for inclusion in a Java library The first time I came to do this I was new to maven and I didn’t know about the maven-dependency-plugin, so I had a hard time keeping a handle on the downloaded kit. Well, I’m back, and this time I mean business. I’ve listed the plugins I used in something like sequential order: maven-dependency-plugin The maven-dependency-plugin is used to download a kit which contains a bunch of templates used in my artifact (which happens to be a Java library)....

August 7, 2012 · 2 min · 272 words

Using JVM Shutdown Hooks to Kill a Child Process

Today I spent some time writing a runner for a command-line Java socket application. I’d assumed that it would be easy enough to quit the process by hitting ^C. I’m here to report that this is not the case. Hitting ^C shuts down the JVM, so if you’ve forked a separate Java process it’ll become orphaned. To get past this you have to implement a shutdown hook, which will run during the JVM shutdown sequence, in effect allowing your application to handle the signal....

August 6, 2012 · 1 min · 155 words

Modular Projects with Bash

I try to use bash scripts wherever I can over ruby or python. Partly that’s down to stubbornness, but it’s mostly because the problems I’m trying to solve can be tackled with the tools already available in UNIX. One of my past sprawling bash projects was a tool that I used for simple, repetitive tasks, like creating new source files from templates, creating working tree structures for projects, running unit tests, and so on....

July 19, 2012 · 3 min · 549 words