A Better Movie?

The Princess Bride is a great book and a great movie.   I recently read Cary Elwes’ book As You Wish: Inconceivable Tales from the Making of The Princess Bride and learned a lot about the making of the movie.  Normally the book is better than the movie, but in the case of The Princess Bride I think the movie is just as good as the book and arguably better because it has had a wider influence.  Reading Elwes’ book caused me to think a lot about why The Princess Bride movie is so great.

I like a good story, but even a good story needs to be told properly.  When verbalizing a story, I use different words and sentence structures than when I am writing the same story.  There are many ways and mediums to tell the same story and many of those have been used to tell the story of The Hitchhiker’ Guide to the Galaxy.  Some stories, or at least some narratives, tend to work better for certain mediums and I think often when a good book is adapted for screen that the movies isn’t as good because the medium isn’t as right for the story.  But this is not the case for The Princess Bride.

William Goldman, a gifted author, wrote The Princess Bride book in 1973.  But Goldman is also a gifted screenwriter, and I think in the years between writing the book and then crafting the screenplay he thought a lot about how to tell the same story in a different way.  This means that the movie,  while not 100% faithful to the book, is still faithful to the story.

Both the book and the movie tell the story of a six-fingered man, iocane powder, rodents of unusual size, true love and more.  The book can provide a depth of details that couldn’t effectively be conveyed in movie format, but the movie is incredibly quotable.  I wouldn’t go so far as to claim the movie is better than the book, but I think it is just as great because it is the same story told by a creator skilled in telling a story in two different mediums.

 

My Accidental Introduction to Yoshikawa Eiji

I really enjoy the writings of Yoshikawa Eiji.  I’ve already written far too much about Yoshikawa and his writings in my thesis Yoshikawa Eiji’s Three kingdoms as Japanese literature, but in that sort of academic paper there isn’t much opportunity to mention my personal experiences with his writings.

I encountered Yoshikawa by accident.  I was at a Barnes and Noble bookstore looking for a Yoshimoto Banana book (a very different author) when I saw a paperback of Musashi (actually, as I later learned, only the first part of the story) and bought it based only on the information on the cover.  I often find myself reading multiple books during the same stretch of time, and I remember while reading Musashi I was also reading Foucault’s Pendulum so the two books will always be strangely linked in my brain despite having little in common.  I was sad when I reached the end of my incomplete Musashi and so I soon invested in a volume containing the complete text.  Since then, I’ve spent many hours reading various writings by and about Yoshikawa Eiji in both English and Japanese.

When I read the literary works of Yoshikawa Eiji, I enter a vivid world with rich characters.  Unlike many books where the protagonist is a generic “everyman” or “everywoman” (although typically with a bit of spunk) which take place in the “here and now” or thereabouts, Yoshikawa’s books take place during specific times and places in history centered around actual historical people.  Of course there is a lot of fiction filling in the details.  In the past I have said that if a person is going to read only one book of Japanese literature, it should be Endo’s Silence.  However, typically when I am recommending a book to introduce someone to Japanese literature, I will suggest Yoshikawa’s Musashi.

 

 

 

 

My Sheveled Desk

It’s no secret that my desk is neither neat nor tidy.  Apparently the second Monday in January (today) is “National Clean Off Your Desk Day”, so I decided to clean off my desk.  Here are some items I encountered:

  • Three paper crowns (one from Christmas dinner, one from dinner of New Year’s Eve, and I’m not sure about the third–perhaps Christmas 2016)
  • Screws for the upstairs tub faucet (now reinstalled)
  • Two tape measures (not counting the one on the shelf across from the desk)
  • Tickets for the Lantern Tour of Jewel Cave from 21 June 2016
  • The case for my Leatherman multitool (on the opposite end of the desk from the actual tool)
  • The case for my HP 48G calculator (on the opposite end of the desk from the actual calculator)
  • Eight tins originally containing Altoids, Trader Joe’s Gingermints, etc. (now mostly empty, but one containing ear buds and two contained dryer lint and matches)
  • Two pair sunglasses (I never wear sunglasses)
  • Three D20
  • Random dusty picture of me and my wife (taken by an elephant statue in Dallas, Texas in 2000)
  • Container of BB’s (last used in early 2016 to add mass to a Pinewood Derby car)
  • Battery cover for a remote (no sign of the remote)
  • Several batteries of unknown charge (mostly size AA and AAA )
  • Dozen CDs and DVDs (including Ubuntu 15.10, DBAN, and “A Sesame Street Celebration”)
  • Numerous checks that have been deposited via smart phone

Basically I removed pretty much everything from the desk that wasn’t connected by wires (and some things that were).  Cleaned and sanitized the keyboards and mice and phones.  I decided the second phone doesn’t need a spot since I only use it every three months or so.  I replaced items that I frequently use (such as glasses, calculator, box cutter) into the usual locations.  The improvement is significant.

Now I’ll wait for “National Clean Up the Rest of Your Office Where You Dumped All the Stuff When Cleaning Off Your Desk Day”.

A Knife in the Dark

I have happy memories associated with Little House on the Prairie and the other books by Laura Ingalls Wilder. Some people seem to consider the books to be “girly” or fluff, but I think them to be a great read both for what they are and for the memories experiencing the books with family.

I was about five or six when my mother read the series aloud to me and my older sister.  The things I remember most from the story are 1) the kids using the pig’s bladder as a ball and 2) the snow fort and subsequent snowball fight.  But what I remember best was that while my mother was reading about the struggles of the Ingalls’ family, my father would quietly appear in the doorway behind her and mime the story–somehow even the most somber of stories was hilarious when depicted in that manner.  When my sister and I would burst out laughing, my father would disappear behind the corner before my mother could turn around.

A few years ago, while visiting my parents, I stole those same books (which technically belong to my sister) to share with my children.  We spent a few months of after dinner reading time working our way through the series.  The kids found the stories interesting and entertaining.

In order to carry on the tradition of silliness, and also to make sure everyone was paying attention, I would occasionally slip in a little bit of extra content.  Typical favorites would be to add “A Ghost in” at the beginning of a chapter name and/or append “of Doom” to the end.  So while reading These Happy Golden Years and not long after advertising “Sleigh Bells of Doom”, I read out the chapter title “A Knife in the Dark”.  Nobody believed me until I displayed the actual text.  So now that chapter (or at least the title) is probably the family favorite and best remembered both for the ominous tone and for the memory of disbelief.

Some may say that the “Little House” books are for girls, but to me they represent fatherhood.  For the stories about about the fathers in the books.  For the stories acted out by my father.  And for being a father with children dubious about a chapter entitled “A Knife in the Dark”.

Watching the Eclipse

Apparently if I were to drive about 21 miles southwest from where I live, I could experience eclipse “totality”. Instead, I’m going to stay home and be satisfied with 99.3% coverage of the sun.

Like most people, the family procured eclipse glasses.  We also pointed the telescope at the sun and directed the view piece onto paper.  Viewing it on the paper made it easier to see the actual movement.

 

 

 

 

 

 

Another interesting thing we observed was that the sun shining through the trees created slivers of light shaped the same as the mostly obscured sun.

Back in 1984, when I was about the age of the girl child, I remember seeing an eclipse in Montgomery, Alabama.  I hope the kids have happy memories of today.

Dynamically downloading a JAR and adding to classpath

I recently had a situation where I had a JAR that had dependencies on another JAR, but wouldn’t always be packaged with the other JAR.  I’m not completely happy with what I ended up doing, but since I succeeded in doing what I wasn’t certain was possible, I decided to document it.  Basically (as the title suggests), I tweaked my code so that at runtime it will download the JAR and add it to the classpath.

Downloading from a URL

Normally I’d use something like the Apache Commons IO library to help with downloading the JAR, but since that’s one of the pieces in the JAR to be downloaded, I’m in a catch-22 situation.  Instead, I used vanilla Java for the implementation.  I ran into some minor complications because the server hosting the JAR did not have a signed certificate, so I had to force Java to ignore certificate errors.  Fortunately, no authentication was required–otherwise things would have been a bit more complicated.  Here is my class:


package com.nathanbak.gomi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class UrlDownloader {

    /**
     * Downloads the specified URL to the specified file location. Maximum size
     * allowed is <code>Long.MAX_VALUE</code> bytes.
     *
     * @param url
     *            location to read
     * @param file
     *            location to write
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @throws IOException
     */

    public void download(URL url, File file) throws NoSuchAlgorithmException, KeyManagementException, IOException {
        TrustManager [] trustManagers = new TrustManager [] { new NvbTrustManager() };
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, trustManagers, null);
       
        // Set connections to use lenient TrustManager and HostnameVerifier
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new NvbHostnameVerifier());
       
        InputStream is = url.openStream();
        ReadableByteChannel rbc = Channels.newChannel(is);
        FileOutputStream fos = null;
       
        try {
            fos = new FileOutputStream(file);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } finally {
            if (fos != null) {
                fos.close();
            }
            is.close();
        }
    }

    /**
     * Simple <code>TrustManager</code> that allows unsigned certificates.
     */

    private static final class NvbTrustManager implements TrustManager, X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
   
    /**
     * Simple <code>HostnameVerifier</code> that allows any hostname and session.
     */

    private static final class NvbHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }
}

Adding JAR to classpath

This was my first foray into dynamically changing the classpath at runtime.  I found many examples of how to load a specific class (when you know the full class name) from a JAR file, but there wasn’t as much information about stuffing a JAR of whatever into the current, running classpath.  After much trial and error, this is what I finally produced:


package com.nathanbak.gomi;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;

public class JarAdder {
    public void addJarToClasspath(File jar) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MalformedURLException {
        // Get the ClassLoader class
        ClassLoader cl = ClassLoader.getSystemClassLoader();
        Class<?> clazz = cl.getClass();
       
        // Get the protected addURL method from the parent URLClassLoader class
        Method method = clazz.getSuperclass().getDeclaredMethod("addURL", new Class[] {URL.class});
       
        // Run projected addURL method to add JAR to classpath
        method.setAccessible(true);
        method.invoke(cl, new Object[] {jar.toURI().toURL()});
    }
}

The addJarToClasspath() method wasn’t necessary on my Windows system.  My main JAR had a classpath to the dependency JAR specified in the MANIFEST.MF and as long as the JAR was downloaded there, it would be found.  However, on Linux it didn’t work and so the method is necessary (and it doesn’t seem to hurt anything on Windows).

Other thoughts

  • It is important to do the download and classpath changes before calling any code that depends on the stuff in the JAR.  Even imports in the same class can cause problems.
  • The downloadUrl() method is pretty generic and could be reused in a lot of situations provided the content being downloaded doesn’t get to big.
  • Different versions of Java seem to behave differently–I’ve only tested two Java’s so far (one on Windows and one on Linux), but have seen very different behaviour.
  • Since the certificate checking is disabled and code is loaded and at runtime, it seems like it would be an easy setup to attack or hack.
  • This method could potentially be used for applications to self-update without needing to restart.

Conclusion

I’m not sure this is a permanent solution for my problem, but it does work for the time being.  Also, I think the parts I learned while going through the process have potential to be used in future situations.

Escaping to Discworld

A couple years ago I had a lovely six months reading through Robert Jordan’s The Wheel of Time series.  This summer I’m binging on Sir Terry Pratchett’s Discworld novels.  I’ve read most of them before, but I’m really enjoying consuming the books in mass quantities.  There are several reasons I think they make great summer reads.

It’s nice not to have to worry about what to read next–you just grab another book in the series.  However, the Discworld series adds an extra dimension because I can easily jump around between storylines thanks to Jakub Oleksów’s reading order guide (there are newer versions than the one in the link, but they don’t always seem to mention the artist and I think he should get credit for his creation).  If I should need a break (I didn’t) from the misadventures of Rincewind (a “Wizzard” who cannot do magic), I can easily switch to another storyline such as the city watch or industrialization.  So in the Discworld series there is something readily available for any mood (or at least any of my recent moods).

The Discworld novels, for the most part, are written such that they can be appreciated on various levels.  The entire family engaged in much laughter as we read aloud Tiffany Aching’s encounter with the Nac Mac Feegle in The Wee Free Men.  Boy #1 and Boy #3 can read through the same book and enjoy it for entirely different reasons with Boy #1 commenting that he “got” so many more of the jokes on his second read now that he’s older.  Boy #3 decided to build The Mended Drum out of Popsicle sticks.

It’s easy to pickup where you left off (or even where you’ve never been) in Discworld.  The novels are great to take with on vacation because it’s not necessary to reserve a few hours in a special reading spot in order to appreciate the text.  I just tossed a couple of books in the suitcase when packing for each trip in July, and had something to enjoy as time permitted.

Some people categorize Pratchett’s Discworld novels as silly (they are) or state that it’s not “real” fantasy (just another way of saying silly), but that is the intent.  I’ve heard some people say that if you’ve read one Discworld book, you’ve read them all, but that is like saying if you’ve eaten one ice cream cone, you don’t need to eat another one since each book provides its own unique satisfaction  So this summer, even though I’ve been keeping busy, when I do find a few minutes to escape, I love heading over to Discworld.

Unit testing Java main() methods

Most of the code I write does not go into Java main() methods and I’m quite accustomed to testing code in “normal” methods.  Recently, however, I came across a situation where I needed to confirm a defect, verify a fix, and perform additional testing of a handful of edge cases related to the functionality of a main() method.  In this post I describe the solution used.

First, some disclaimers to help define the scope and also recognize that I’m doing a couple things that I would normally not consider best practices:

  • Since the main() method is usually the entry to the entire application, running it normally involves a lot of code.  In this case I want to only test the functionality of the code in the main() method itself and not the functionality of stuff that it calls–these are real unit tests.
  • There are already many unit tests in place using JUnit so I want to continue using JUnit and be able to run everything together.
  • Generally I think it’s poor form to to modify code to make it testable–code that is well-written should already be easy to test.  However, I occasionally make exceptions such as making a method protected instead of private in order to expose it to testing (I think that’s a lot cleaner than using reflection to run a private method).

So, here’s a very simplified example of the class with which I started (note that the non-main methods are not implemented):

package com.nathanbak.test.main;

public class Application {

    protected Application() { }

    public static void main(String[] args) {
        String string = args[0];
        Application application = new Application();
        try {
            application.run(string);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }
        System.exit(0);
    }
    
    protected void run(String string) throws Exception { }
    
    protected void logException() { }
}

I try to avoid putting much code in main() methods, but typically end up having things like:

  • Parsing/validating arguments (something the above example does poorly)
  • Object instantiation
  • Running of object methods (often with arguments)
  • Providing some sort of top level exception handling and user output
  • Provide a relevant exit code (typically 0 for success and 1 for failure)

When I tried creating JUnits for the main() method, here are some problems I encountered:

  1. If a test called the main() method, the test run would halt because of the System.exit() call.
  2. Verifying output to System.out is difficult.
  3. Running the main() method means running the run() method which can take a long time and changes the scope of the test.
  4. It’s hard to test edge cases (such as what if the run() method throws an exception that doesn’t have any message).

I was worried that #1 was going to be a deal breaker, but fortunately found an easy solution in System Rules which bills itself as “A collection of JUnit rules for testing code that uses java.lang.System.”  I added the system-rules-1.16.0.jar to my classpath, followed the examples on the main page, and had a great solution to #1 and #2 above (as well as solutions to various other problems I hadn’t considered but will keep in mind for the future).

To cover #3 and #4 above was where I had to get a bit more unorthodox.  I created a private static method that creates and returns the new object and replaced the constructor in the main() method with a call to that new method.  I also added a protected static property that I could use to store an instance and had my new static method use that instance if it was not null.  This is now my class turned out:

package com.nathanbak.test.main;

public class Application {

    protected Application() { }

    public static void main(String[] args) {
        String string = args[0];
        Application application = newApplication();
        try {
            application.run(string);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }
        System.exit(0);
    }
    
    protected void run(String string) throws Exception { }
    
    protected void logException() { }

    protected static Application instance = null;
    
    private static Application newApplication() {
        return instance == null ? new Application() : instance;
    }
}

With the ability to control the object used to run methods, I was able to create tests like:

package com.nathanbak.test.main;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

public class ApplicationTest extends Application {

    @After
    public void tearDown() throws Exception {
        instance = null;
    }
    
    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();

    @Test
    public void testMain() {
        instance = new CrashAndBurn();
        String [] args = { "one", "two", "three" };
        exit.expectSystemExitWithStatus(1);
        main(args);
    }
    
    private static class CrashAndBurn extends Application {
        @Override
        protected void run(String string) throws Exception { 
            throw new Exception();
        }
    }
}

A few things to note about the test class:

  • The test class needs to extend the class under test in order to have access to the instance property.
  • The testMain() test currently calls main with some args and then the CrashAndBurn class throws an exception when the run() method is called and the test confirms that the main() method returns an exit code of 1 (the test would fail if any other exit code was returned).
  • The tearDown() method sets the instance back to null after every test to ensure that specific instances are only used when desired for specific tests.

Obviously my real tests are more numerous and comprehensive, but the above example shows how I was able to overcome the difficulties encountered when trying to create unit tests for a Java main() method.

Let there be light (again)

There was a broken light switch in the upstairs hall.  Since I knew I was going to go through the trouble of replacing the switch, I decided to get another fancy smart switch.  This time I went with the TP-Link Smart Wi-Fi Light Switch.

The TP-Link switch is a lot like the WeMo switch (see part 2).  In fact, it was an accident that I got that switch–I intended to get another WeMo, but wasn’t paying attention and bought the TP-Link instead.  It took awhile for me to notice that things were different.

The wiring of the switch was simple and pretty much identical to what I did for the WeMo.  I did notice that the switch packaging was different, and the switch was slightly different, but the differences are so minor that I initially assumed I had a slightly different model of the same thing.

It wasn’t until I started trying to connect the TP-Link switch to the Echo that I ran into problems.  Most of the problems stemmed from the fact that I was going through the same steps I did for the WeMo.  Once I realized that the switch was different, it wasn’t immediately obvious where to find instructions (there was nothing helpful in the box).  After searching online, I discovered that the setup is a bit more complicated and required installing the TP-Link Kasa app, creating a Kasa account, installing a special Alexa skill, and linking the accounts.

Now that everything is set up and working, there’s no different from the WeMo in terms of functionality.  The switch works like a switch and the lights can also be voice activated via Alexa.  The thing that I like least about the switch was that the setup was comparably more complicated, but some of that was due to incorrect assumptions on my part.

Configuring a Raspberry Pi from Another Computer

Introduction

One thing I like about the Raspberry Pi is that it’s a small gadget that, once configured, only needs power in order to sit somewhere and do something.  For example, BakBoard runs on a Raspberry Pi that only plugs into a TV (for both power and display).  Unfortunately, in order to get everything running, I typically have ended up connecting various extra wires (network, keyboard, mouse, display) and work directly on the Pi before I can stick it in some random location to do what I want it to do.  Below are the steps I figured out so that I can do everything from my main computer and the only wire I need to my Pi is for power.

Prereqs

  • My main computer is currently running Ubuntu 16.04 although I think it would be easy to adapt the steps for most operating systems.
  • I have an SD card (32 GB in my case, but it probably only needs to be about 4GB).
  • I have an SD card adapter so I can read/write the SD card from my computer.

OS Image

To get the base image, I went to the Raspberry Pi Downloads Page and grabbed the latest Raspbian image (specifically 2017-01-11-raspbian-jessie-lite).  Once I had downloaded the zip, I opened it and then doubled clicked the image file (2017-01-11-raspbian-jessie-lite.img).  This brought up the Ubuntu image tool and it was easy to “restore” the image to the SD Card.

Wireless Networking

Since I don’t want to mess with a network cable, I want my Pi to be able to access my wireless network.  In order to do so, I modified the interfaces file.  It is in the image at etc/network/images.  Basically I changed the bit:

iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

To be:

iface wlan0 inet dhcp
    wpa-ssid "NATHANS_NETWORK"
    wpa-psk "NATHANS_PASSWORD"

Obviously I plugged in the correct network name and password.

Enable SSH

The Raspbian OS used to have SSH enabled by default, but last year that changed as a security precaution.  The explanation for the change (and where I learned to do what is described below) is described on the Raspberry Pi Blog.

Basically, to enable ssh, I just create a file called “ssh” in the boot directory.  The contents of boot/ssh don’t matter–apparently the OS will see that file, enable ssh, and then delete the file.  The tricky part was that there were two “boot” directories.  There was one at the root of the volume, but there was actually a separate volume as well that is named boot–that’s the one where the ssh file must be created.

Authentication

The default password to  the Raspberry Pi is well-known which is nice because I don’t have to remember yet another password, but also a security risk since everyone knows it.  Instead, I like to use key based authentication and disable password authentication for ssh access.  Here’s how I did that:

First, I generated my public and private key (that was done a long time ago, and there are plenty of sources on the Internet how to do that).  My public key is id_rsa.pub (in the .ssh folder in my home directory) and the private key is id_rsa.  That creation was on my main computer.  Then, on the Raspberry Pi volume, I created the directory home/pi/.ssh.  I then copied the public key file (id_rsa.pub) into the home/pi/.ssh folder and also copied the file and named the copy “authorized_keys”.

Then, to disable password authentication via SSH I opened up the file etc/ssh/sshd_config in a text editor and changed:

#PasswordAuthentication yes

UsePAM yes

To be:

PasswordAuthentication no

UsePAM no

Conclusion

Once the above has been completed, I can stick the SD card into the Raspberry Pi and then plug in the Pi (giving it power).  It automatically connects to the wifi and I’m able to SSH into it without a password.  There’s nothing new here that can’t be found in various places online, but I’ve gathered the pieces together for my own reference at least.  Here are a few “gotchas” I encountered along the way:

  • All of the paths mentioned above are relative paths–the volume might be mounted in various places–in my place it was something like /media/nathan/90asd8f60s9g69789sd6gjherlkuyds8 for the main volume and /media/nathan/boot for the boot volume.
  • As mentioned before, there are two “boot” directories–make sure that the “ssh” file is created in the boot volume.
  • In order to create/modify some of the files, I had to use sudo (or change to root).
  • I the past, I used to have to run raspi-config to expand the volume to use all available space on the SD card, but that no longer seems necessary–it now seems to happen automagically.
  • Even though password authentication is disabled for SSH access, whenever logging in there is still a warning.  I usually do change the password and just write it down somewhere since I never use it.