Category: tech-experiments

  • Chromebooks for Developers?

    Earlier this year, my mother-in-law-J’s hand-me-down MacBook kicked the bucket. To reduce complexity and “support” phone time, my wife convinced me to buy her a Chromebook. I still worry about not being able to help out (and I’m a sucker for gadgets), so I blew the net savings from the Chromebook gift in comparison to the potential MacBook purchase on a second Chromebook.

    There are two stories here, one short and one developing…. The short story is that since for most people a Chromebook only runs a browser, there’s really no support beyond figuring out web services. We’ve yet to receive a cry for help from J. To increase n to 2, we gave mother-in-law-L a Chromebook for Christmas 5ish years ago…. and I wouldn’t know she used it ever, except that she says that she does and I occasionally see it on the kitchen island.

    The longer story is what I should do with the extra Chromebook….

    The Hardware

    I dragged my feet on buying Chromebooks until BestBuy had a sale. Google had a sales rep at BestBuy, and from his enthusiasm, I think I might have been his only customer ever. I don’t think he believed that I was going to buy a pair, even, as he took my photo and walked me to the cashier. In the end, I bought two Samsung Chromebook Pros.

    The display is beautiful, generating guilt never to use the touch screen; there’s a built in stylus that I’ve only used to demo Adobe Sketch; the keyboard has all the programming bracket keys, but oddly spaced; the case is a nice compromise between cheap, sturdy, and thin; the battery life lets me forget about the machine for a few days.

    Messing Around

    Google says that you can run Android apps on Chrome OS, but that might be stretching the truth a little. Chrome OS emulates Android for each app. Apps appear on a phone-sized rectangle, with a buggy option to resize to the entire screen. The file access seems to be limited to files under the app’s install directory, so text-editor output might not be useful and there’s no access to the SD-card from Android, presenting a hurdle for playing music stored locally.

    I tried without success to get several Android music players to work. To make matters more confusing, VLC has two versions that appear identical in the application tray — one for Android and one for Chrome OS. The former is buggy and freezes. The latter is a Chrome app…. which Google won’t support outside Chrome OS, so I worry VLC’s future. Google Play Music no longer lets you play music from an SD card. In the end, I found that Remo works reliably, but exposes a track ordering and song selection identical to the filesystem.

    I’m not so in love with my favorite Android apps, kWS and Jota, that I’ve unlocked Chrome OS beyond installing local Chrome Extensions.

    Accessing a Server

    The easiest way to get my code fix via Chrome OS is just to install an ssh Chrome extension and connect to my server. There are several similar extensions. I just chose the one recommended from the crosh shell. Programming via an ssh connection reminds me of VT-100 and modem wails in college, but out of the box I got the same syntax highlighting from vim, YouCompleteMe, and npx. mdv piped into less -R pretty prints markdown well enough.

    Untethered Development

    For proof of concept that one could write something using only a ChromeBook, I used Text to write a simple hello-world Chrome Extension from Google’s tutorial. Presumably one could work in the emulated Android environment with Termux, suggested on Medium….

  • 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.