Lexington Green

When summoned by the horseman’s cry
from breaking fast and tending barns
The Yeoman farmers trained response
was to secure their ready arms

The nineteenth dawn that April bore
revealed a revolutionary scene
Captain Parker’s men had met
to learn the news on Lexington Green

But red clad soldiers marched all night
From landing ships on Cambridge shore
And through Lexington they’d pass
En route to capture Concord’s Store

Lieutenant William Sutherland
Called to the men across the sward
Commanding them “Disperse Ye Rebels
Ye Villains, most unruly mob.”

Outnumbered, Captain Parker’s men
were ordered to disperse once more
But chaos and uncertainty
lay beneath the fog of war

Who fired first? the tales suggest
Perhaps a sniper off the scene
but British shot and bayonets
Killed nine men on Lexington Green

Lieutenant Colonel Francis Smith
Arriving with three companies
Ordered drumbeats for the march
To Concord’s Bridge and history

In Concord town the Minutemen
Had learned of Lexington’s Melee
On Punkatasset over-watched
As Redcoats made their first foray

Searching houses, barns and fields
for weapons cached and powder kegs
but long since moved and all they found
were milk and barely, ham and eggs

When searching soldiers caught the site
Of mounds fresh piled in the fields
And ready spades we turned to dig
And three great cannon were revealed

But as they searched, the Minutemen
From Acton, Bedford, and Westford
Joined the Lincoln men above
The Concord rivers northern shore

A Regiment of militiamen
descended  Punkatasset Ridge
assaulted Captain Parson’s Force
assigned to guard the Northern Bridge

The Regulars formed to volley fire
As if for warfare in the town
A warning shot rang out and then
the musket balls were raining down

They fled their post and headed south
to form with Regiments complete
and leaving off their fruitless search
the British Colonel called retreat

The local men who took the bridge
Had learned to shoot when they were Boys
Some had marched to Montreal
To fight the French and Iroquois

At Meriam’s Corner, and Brooks Hill
The local muskets took their toll
and passing through the ambuscade
at Bloody Angle Thirty  fell

At last they came back to the field
where shot had shattered dawn serene
And Parker had then his revenge
Upon the sward of Lexington Green

Guice gets it

I’m currently working on Candlepin, a Java web service application that uses Google Guice for dependency injection. I’ve long suspected that I would like  Guice. I’m not a big fan of annotations, but I’ll admit that there are something that you can’t do in Java using other approaches.   Guice makes appropriate use of annotations to make dependency injection work in a type safe an intelligent manner.

I had to break a dependency in order to make a class testable.  Here’s the old interface:

@Inject
public JavascriptEnforcer(
     DateSource dateSource,
     RulesCurator rulesCurator,
     ProductServiceAdapter prodAdapter );

The problem with this is that RulesCurator is a database access class, which means I need a DB for my unit test.   What the JavascriptEnforcer specifically needs is the product of the database query.

Here’s a piece of code from the body of the constructor hat I also want to remove.

    
    ScriptEngineManager mgr = new ScriptEngineManager();
    jsEngine = mgr.getEngineByName("JavaScript");

This links us to one creation strategy.  I want to use a different one in  my Unit test.

My first step was to create a new constructor, and have all of the code I want to extract in the old constructor.  This would probably have been sufficient for the unit tests, although it might risk a class not found for the DB stuff.  Here’s the new constructor interface:

public JavascriptEnforcer(
    DateSource dateSource,
    Reader rulesReader,
    ProductServiceAdapter prodAdapter,
    ScriptEngine jsEngine) ;

Here’s where Guice shined.  I need two custom compnents here, one which fulfills the rulesReader dependency, the other which fulfils the jEngine.  The jsEngine one is easier, and I’ll show that.  First, create a custom Provider.  A Provider is a factory.  For the jsEngine, that factory just looks like this:

package org.fedoraproject.candlepin.guice;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import com.google.inject.Provider;
public class ScriptEngineProvider implements Provider<ScriptEngine> {
    public ScriptEngine get() {
        return new ScriptEngineManager()
            .getEngineByName("JavaScript");
    }
}

Which we then register with our custom module:

class DefaultConfig extends AbstractModule {
     @Override
     public void configure() {
     ...
        bind(ScriptEngine.class)
            .toProvider(ScriptEngineProvider.class);
    }
}

Now comes the interesting one.  We want a Reader.  But a Reader is a very common class, and we don’t want to force everything that needs a Reader to use the same creation strategy.  Here is where Guice uses Annoations.

public class RulesReaderProvider implements Provider<Reader> {
    private RulesCurator rulesCurator;
    @Inject
    public RulesReaderProvider(RulesCurator rulesCurator) {
        super();
        this.rulesCurator = rulesCurator;
    }
    public Reader get() {
        return new StringReader(rulesCurator.getRules().getRules());
    }
}

Note how the Provider itself is a component.  This allows us to use another component, the RulesCurator, without creating a direct dependency between the two classes.  Still, this does not distinguish one reader from another. That happens with another bind call.

public void configure() {
    ...
    bind(ScriptEngine.class)
        .toProvider(ScriptEngineProvider.class);
    bind(Reader.class)
        .annotatedWith(Names.named("RulesReader"))
        .toProvider(RulesReaderProvider.class);
}

Then, the inject Annotation for our Enforcer looks like this:

    
    @Inject
    public JavascriptEnforcer(
        DateSource dateSource, 
        @Named("RulesReader") Reader rulesReader, 
        [...] 
       ScriptEngine jsEngine)

The key here is that the @Named matches between the two components.

Could Maven use a single directory for archives.

Maven is too important a part of too many projects for most Java developers to ignore. However, some of the decisions made in building with Maven are suspect, mostly the blind download of binary files from a remote repository. While Maven gets more and more Open Source clean, there are still issues, and the biggest is building Maven itself. Both Debian and Fedora have fairly old versions of Maven, in the range of 2.0.7 as of this writing. Considering that the GA is 2.2.0 and There is work on 3.0, we risk a pretty serious divide in the Open Source Java world of we don’t keep up with Maven, and get a clean way to build it.

Continue reading

My “Two Main Problems With Java” Rant

This is not an Anti-Java rant  Per Se.  It is a rant about the two main things missing from the language that force people into code heavy work-arounds.

Java has two flaws that hurt programmers using the language.  The first is that the reflection API does not provide the parameter names for a function.  The second is that Java allows null pointers.  This article explains why these two flaws are the impetus for many of the workarounds that require a lot of coding to do simple things.  This added complexity in turn leads to code that is harder to maintain and less performant.

Continue reading

The overhead of Java

A programming language is a tool. When choosing the right tool for the job, you want to have good information about it. I’ve worked with both C and Java, and have dealt with a lot of misconceptions about both. I’m going to try and generate some data to use in helping guide discussions about the different languages. Consider this, then as the next instalment of my comparison of programming languages that I started in my IPv6 days.

Continue reading

Map Reduce is kinda like “Normalize on the Fly”

One undervalued aspect of Data modeling is that you actually get time to consider the form of the data before you get the data. In a Map reduce job, you kow that your map phase is going to get the data, and that it is not going to be normalized . I could have said, not likely to be normalized, but the reality is that if you are using Map-Reduced, you are not going to get structured data.

Continue reading