Author: jonathanb

  • Spark (for Java)

    I almost missed this goodie on Technology Radar — not only is it shadowed by the popular Apache Spark name, it’s reference was hidden in a Spring Boot summary… not my favorite family of XML-bloated tools. Spark is a lightweight web framework for Java 8. It has modest run-time dependencies — Jetty and slf4j and four-line hello-world example — including imports, but not close curly braces.

    Let’s go through a somewhat more complex conversation with Spark than “Hello, World” and set up a simple key-value store.

    Project Setup

    Create a Maven project. Spark has instructions for Intellij and Eclipse. You don’t need an archetype; just make sure to select Java SDK 1.8.

    Salutations, Terrene

    We’ll implement a simple REST dictionary so that we can show off our vocabulary, or our thesaurus skills, and because we’re snooty, we’ll “protect” our dictionary with a password.

    package org.bredin;
    
    import spark.*
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.*
    
    public class Main {
        private static Map<String,String> keyStore = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    
        public static void main(String[] args) {
            Spark.before((request, response) -> {
                        if (!"blam".equalsIgnoreCase(request.queryParams("secret"))) {
                            Spark.halt(401, "invalid secret");
                        }
                    });
            Spark.get("/get/:key", (request, response) -> readEntry(request, response));
            Spark.get("/put/:key/:value", (request, response) -> writeEntry(request, response));
        }
    
        public static Object readEntry(Request request, Response response) {
            String key = request.params(":key");
            String value = keyStore.get(key);
            if (value == null) {
                response.status(404);
                return "unknown key " + key;
            } else {
                return value;
            }
        }
    
        public static Object writeEntry(Request request, Response response) throws UnsupportedEncodingException {
            String key = request.params(":key");
            String value = URLDecoder.decode(request.params(":value"), "UTF-8");
            String oldValue = keyStore.put(key, value);
            if (oldValue == null) return "";
            else return oldValue;
        }
    }
    
    
    
    
    

    OK, it’s not as terse as Ruby or Node.js, but it’s readable (similar to Express), statically-typed, and integrates with the rest of your JVM. The real beauty of Spark is in the route definitions and filters — try approaching that level of conciseness with Spring… or even Jetty and annotations.

    Spark provides before() and after() filters, presumably for authentication, logging, forwarding…. executed in the order applied in your code. Above, there’s only an unsophisticated password check. I’ve not dug in to discover whether or not Spark exposes enough bells and whistles for Kerberos.

    The Spark.get() methods provide conduits for REST into your application. Spark checks to see that the request parameters are present, returning 404 otherwise, and dispatches your registered handlers.

    You can run and test drive the example

    $ curl 'localhost:4567/put/foo/What%20precedes%20bar?secret=BLAM'
    
    $ curl localhost:4567/get/foo?secret=BLAM
    What precedes bar
    

    Neat! I’ve always been uneasy that Jetty’s annotations aren’t thoroughly checked by the compiler. DropWizard has loads of dependencies with versioning issues that have tripped me up.

  • President’s Day Out

    President’s Day Out

    It’s hardly travel until I consider how much time I’ve spent out and about in Arkansas since we moved here. Monday I had the day off and Mattie and I went out to Devil’s Den State Park. I’d read that the park could be crowded, but we hardly saw another soul. We trotted around the 1.7-mile main loop and poked our nose and camera into the limestone formations.

  • Mandolin Cairns

    Mandolin Cairns

    I picked up playing music late in life. If I have any innate musical ability, it’s buried deep down waiting to be found. In my thirties I started playing electric and string bass. It was fun. I picked up some basic jazz and blues tricks playing with friends and through lessons, but some fundamental facts involving gravity and bass-clef rhythm instruments made practice less than fulfilling.

    Walking by Tejon Street Music on my way to a bass lesson, I noticed a mandolin in the window. How shallow can you get, wanting to play an instrument because of appearance, but I was tired of lugging amps or a 7/8 string bass and I was drawn to the arcs comprising mandolin profiles…. I quickly learned that the mandolin is tuned the same as a bass…. except upside down, and player contribute to both melody and rhythm parts. Practice could be fun!

    Many hours of practice, lessons, and hacking later, I still haven’t found my talent, but I’ve had fun along the way. Here are some cairns I’ve left to trace my steps for my own reflection and hopefully to speed others’ journey to uncover their talents.

    Models and teachers: It helps to hear different voices and different techniques from musician’s at all levels. The masters like Chris Thile and Jethro Burns still wreck my brain trying to figure out what’s going on. Having help breaking down what David Grisman and Bill Monroe makes many other licks and songs approachable at higher speeds. Hearing mortals play at the farmer’s markets, teachers, lessons, and instructional videos ease acoustic digestion.

    Play with others: Metronomes make dull partners, but finding a patient group of peers will help you play to someone else’s time, expose your ear to the other voices of the same song…. The Mandolin Orchestra of Northwest Arkansas are wonderful colleagues and teachers. iReal Pro is another tool to help keep time during practice.

    Projects: I’ll be posting some simple projects here that have served as launch points. Sweet Georgia Brown, Black Orpheus, and Hotel California are some landmarks I’d like to explore in future posts. Picking up a fake book is an easy way to explore new tunes and get started.

    Experiment with strings and picks: Mandolins are expensive! String and pick choice have a lot more bang for the buck than laying out a mortgage payment for a new mandolin. After five years of experimenting with a clunky Breedlove KO (and waiting for Oregon production to end), I think I’ve finally dialed it in with Thomastik strings and a casein pick.

    Learn to read: I wish I could hear better, but reading music gives me a jump start. Seeing lots of notes laid out in front of me used to be intimidating. Read Bach! The cello suites transcribed to the treble clef are a good start. Handel and Telemann wrote lots of music appropriate for mandolin (flute and violin). Seeing the music also makes theory more approachable, which helps improvisation, memorization, and accompaniment. Reading isn’t a panacea, so make sure to trust your ears.Mandolin Cairns