Category Archives: Java

Steps to Setup Datomic on a Cassandra Cluster on MacOS Mavericks using Virtualbox

Just a quick note – I’m in the process of publishing a book, Clojure Recipes. You can see it for presale on Amazon here.


This post shows you how to setup Datomic running on Cassandra using Virtualbox VMs from scratch. This is non-trivial and if everything goes well, will probably take a couple of hours.

Note that this are my notes from going through this process, so there will definitely be room for improvement, either a simpler way to do things or a better way to explain what is going on.


  • This has been written for a Macbook Pro Retina running MacOS X 10.9.3 (Mavericks) with 16GB of RAM. You’ll need to make a judgment as to whether your setup is similar enough for these steps to apply to your system.
  • VirtualBox uses the language ‘guest’ to describe the client virtual machine running. This is not to be confused with the ‘guest login’ (which we won’t use anyway)


  •  you need at least 9G free on your hard drive
  •  you need at least 2.5G free RAM (running three virtual machines)
  •  to actually do this – you need a datomic pro evaluation key – you can get this by registering on the site – and then under Account click Send licence key and under Downloads – and also get the latest version of Datomic Pro.


This has the following steps:

Note that steps A-J are here.
K. Setting Up Datomic
L. Running Datomic


Process Steps

Note that steps 1-53 are here:

K. Setting Up Datomic:

54. Expand the datomic download

unzip -d datomic-pro-0.9.4384
cd datomic-pro-0.9.4384

55. Provision a keyspace and table for datomic from the datomic directory:

/home/cassandra/apache-cassandra-2.0.3/bin/cqlsh -f bin/cql/cassandra-keyspace.cql -u cassandra -p cassandra

56. Copy the cassandra transactor sample into the config directory

cd config/samples
cp ..
cd ..

57. Modify the cassandra template to setup the properties of our local instance.

cd ..

58. Copy the licence key you downloaded (at step 16) into the file under the property entry


59. Set the following property entries


60. Start the datomic transactor with  the new cassandra template properties we just created:

bin/transactor config/

Ensure you get a result like:

System started datomic:case://localhost:9042/datomic.datomic/<DB-NAME>

Note this down – this will be the URI to connect.

L. Running Datomic

61. In a new command prompt in the datomic-pro directory run:


Ensure you get the prompt

Datomic Java Shell

62. Run through the following commands from the datomic tutorial:

String uri = "datomic:cass://localhost:9042/datomic.datomic/seattle";
conn = Peer.connect(uri);

63. Load up the schema

schema_rdr = new FileReader("samples/seattle/seattle-schema.dtm");
schema_tx = Util.readAll(schema_rdr).get(0);
txResult = conn.transact(schema_tx).get();

64. Add some seed data

data_rdr = new FileReader("samples/seattle/seattle-data0.dtm");
data_tx = Util.readAll(data_rdr).get(0);
txResult = conn.transact(data_tx).get();

65. Query the database

db = conn.db();
for (Object result : results) {
entity = db.entity(result.get(0));

Voila – you have a Datomic database running on Cassandra across three nodes!


Running Checkstyle at Full Throttle on a Legacy Codebase


So in summary:

You can turn on all checks for new checkins on a horrible old legacy code base in checkstyle by using checkstyle to generate a supressions file to ignore all the old mistakes.

Get new version of Checkstyle

Download the patched version here.

See the patch in Sourceforge here.

To run simply add the the following new task in your build file to generate the suppressions file from the build.xml here:

<target name="generate.suppressions"

Then you’ll need to add the following to your existing checkstyle task.

<property key="checkstyle.suppressions.file" 

The Story

I think checkstyle is fantastic, because I work in a large team of developers on a large codebase, and I want to know we’re all on the same page with our coding styles.

“But it shouldn’t matter,” you might argue. “All it does is slow you down. That’s not providing benefits or reducing risks.” Fair enough. Suppose you find the following in your codebase.

if (inputString == "Expected String") {

Should the developer responsible have known? Should the code reviewer have picked it up? Is everybody on your team on the same page that This Is Not A Good Idea?

The team discussion

“But”, they argue [and here comes the broken window argument.] “This pattern is already all over the code base. The developer was just copying and pasting.”

[At this point you might groan internally and consider changing jobs. Alternatively you can acknowledge that there is real money in maintaining legacy codebases for profitable businesses, and try and find the opportunity in the murkiness.]

“That’s not a good enough reason.” You will respond. “There has to be a way for our tools to support this scenario.”

Enter Checkstyle

So checkstyle can run some checks over your code base. It comes with lots of fancy checks and it feels like it would address this situation.

So you run it with the default set of checks. You get back several thousand errors. So you alter the checks file to just the checks you’re interested in: StringLiteralEqualityCheck. Still your codebase comes back with 50 errors.

The Legacy Push-back

“We can do this,” you hope internally. We only have to touch 50 classes. Now imagine that this system has 20 000 users a day, which collectively transact about $1B a week through your system. The tolerance the organisation has for risk and defects is pretty close to zero, and changes requiring a regression test to system components not on the project will be looked on unfavourably by your project manager and user acceptance test manager. This Is Not The Way Forward.

“Well we can just use the supressions file!” you exuberantly exclaim. For these 50 errors, it should only take An Hour Of My Time to Manually Type In This XML. Because manually typing 150 lines of XML is the reason you took that Computer Science degree – right?

A New Way Forward

“Hang on a minute,” you think. “This doesn’t feel right. What I wanted was a way of working with my team where we can agree on a way of not writing crappy code. Here am I using an hour of my time to make one measly check work. What about the other checks?”

“Surely someone has done this before?” A quick search of Google and StackOverflow says no.

“What I want is for checkstyle to generate the supressions file for me, for all checks if I want. Surely generating a supressions file is about the same complexity as generating an errors file – and it already does that.”

Cracking open the checkstyle codebase one evening, it was surprisingly easy to do just that.

The Benefits

What you want is instant feedback for your developers. As soon as they save a file and run a checkstyle task in eclipse, or check it in and get an email from the Build Server – they should get some indication of whether their new code matches up with the existing codebase.

What you want is checkstyle running at full-throttle. You want all the checks possible. You don’t want to be held back by having to change legacy code to get there. You want the new code to be in line. This helps you get there.

How do I get my netbeans drag and drop widget to know whether it is rendering inside the netbeans design view window or the running application?

Question: Why would you want this?

Answer: In case you are doing custom animation that will go outside the bounds of the component.


Here is an example of it in use:

package test;

import java.awt.Graphics;
import java.beans.Beans;

import javax.swing.JLabel;

public class TestLabel extends JLabel {
    private static final long serialVersionUID = -2438507032083091628L;

    public TestLabel() {

    public void paint(Graphics g) {

        if (Beans.isDesignTime()) 
                this.setText("Design Time");
                this.setText("Production runtime");

This is the prior method I had. It’s vastly inferior.

public void paintComponent(Graphics g) {
    if (super.getRootPane().getParent() instanceof JFrame) {
        Component c = javax.swing.SwingUtilities.getRoot(this);
        String className = c.getClass().getCanonicalName();
        if (!"".equalsIgnoreCase(className)) {
            JFrame parentFrame = (JFrame) super.getRootPane().getParent();
        }else {
            // Inside Netbeans demo window

10 steps to beginning to parsing with antlr

“I’d love to get started with antlr – but I need some help to get started.” Does this sound familiar?

What is antlr?

Antlr stands for “ANother Tool for Language Recognition”. In short it enables you to take some source code written in a particular language and build a parse tree for it.

That sounds hard. Why should I bother?

Good question. Steve Yegge answers it in his blog here. Very simply:

  1. In case you have a tricky coding style issue that eclipse can’t handle
  2. You want to write a javadoc style code documentation generator that parses source
  3. You have a situation that requires a unique and complex refactoring
  4. You display source code and you want to add syntax colouring
  5. You need to send bizarre syntax to a router as part of your project
  6. You need to do a redesign of your complex code base
  7. You need in a source code base to transfer one coding idiom to another

The essence of his argument is that it is ‘rich programmer food’ – ie something more intellectually engaging to make you grow as a programmer.

Are there any good books on it?

I can recommend this one – written by the author of antlr.

So how do I get started?

Load up eclipse – and do the following:

1. Download antlr.

2. Start a new Eclipse java project

3. Create the following source to be parsed:

package parseable;

public class Class1 {
    * @param args
    public static void main(String[] args) {
        System.out.println("Hello world!");

4. Add in the antlr jars


5. Get the language grammar file for Java


6. Change the following line of java.g from this

options {k=2; backtrack=true; memoize=true;}

To this

options {k=2; backtrack=true; memoize=true;
output=AST;ASTLabelType=CommonTree; }

7. Generate your java lexer and parser

java -classpath antlrworks-1.1.7.jar org.antlr.Tool Java.g

8. Drop them into your eclipse project

9. Add the following parser code

package astprinter;

import generated.JavaLexer;
import generated.JavaParser;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.TreeAdaptor;

public class ASTPrinter {

    public static void main(String[] args) throws Exception {
        new ASTPrinter().init();

    public void init() throws Exception {
        // Read the source

        CharStream c = new ANTLRFileStream(
            "C:/Documents and Settings/User/workspace/"+
            "antlr beginners guide/src/parseable/");

        // create the lexer attached to stdin
        Lexer lexer = new JavaLexer(c);

        // create the buffer of tokens between the lexer and parser
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        // create the parser attached to the token buffer
        // and tell it which debug event listener to use
        JavaParser parser = new JavaParser(tokens);

        // launch the parser using the treeadaptor

        // Get the compilation unit item
        JavaParser.compilationUnit_return ret = parser.compilationUnit();

        // Get the associated tree
        CommonTree tree = (CommonTree) ret.getTree();

        // Print the tree
        printTree(tree, 1);

    static final TreeAdaptor adaptor = new CommonTreeAdaptor() {
        public Object create(Token payload) {
            return new CommonTree(payload);

    public void printTree(CommonTree t, int indent) {
        printTreeHelper(t, indent);

    private void printTreeHelper(CommonTree t, int indent) {
        if (t != null) {
            StringBuffer sb = new StringBuffer(indent);
            for (int i = 0; i &lt; indent; i++)
                sb = sb.append("   ");
            for (int i = 0; i &lt; t.getChildCount(); i++) {
                //if (t.getChild(i).getType()==4)
                System.out.println(sb.toString() + t.getChild(i).toString()
                + " [" + JavaParser.tokenNames[t.getChild(i).getType()]
                + "]");
                printTreeHelper((CommonTree) t.getChild(i), indent + 1);

10. Run it – and voila – basic parse tree of your syntax

package ['package']
parseable [Identifier]
; [';']
public ['public']
class ['class']
Class1 [Identifier]
{ ['{']
public ['public']
static ['static']
void ['void']
main [Identifier]
( ['(']
String [Identifier]
[ ['[']
] [']']
args [Identifier]
) [')']
{ ['{']
System [Identifier]
. ['.']
out [Identifier]
. ['.']
println [Identifier]
( ['(']
"Hello world!" [StringLiteral]
) [')']
; [';']
} ['}']
} ['}']

Any thoughts – let me know.