Skip to content
Nov 16 / David Rice

Developers, abandon your headphones when in the office

ThoughtWorks Studios in San Francisco is a typical agile dev shop. We all sit out in a common space, mostly working at pairing stations. We like music. We use Squeezebox Server to server up the tunes. The web interface provides a nice collaborative jukebox feel. We enjoy queueing up playlists for each other and ribbing our colleagues a bit about an off selection every now and then. Nothing too unusual there. I’m sure many similar shops do the same.

What I want to call out is what I witnessed when I returned from leave this Monday. The music server was broken. (Is it me or is all the Squeezebox stuff less and less stable since the Logitech acquisition?) Everyone was wearing headphones. Nobody was talking. That’s not good. Nobody intentionally went into “loner” mode, but it happened. Anyway, Barrow fixed the server. Everyone’s headphones came off and we are all talking again. If you work at a shop where everyone is mostly wearing headphones all day (you know the scene i’m talking about here… we all to it more than we should), try setting up some sort of public jukebox. Communication will increase. Just watch. And don’t worry, you can still break out the headphones when you need 30 minutes to yourself or simply cannot stand Sherry’s death metal.

Mar 10 / David Rice

Using H2 Database with JRuby and Rails

Often when starting a new Rails project, or knocking out a quick proof of concept, we use an embedded H2 database. (Ideally, the customer never asks for a “real” database and we go live with H2.) Anyway, we always hit the same problem: db:test:prepare does not work out of the box with the combination of H2 + JDBC + JRuby + Rails. We’re going to take a look to see if there are any patches that even make sense for a “proper” fix, but in the meantime… so that I never have to figure this out again… I’m writing it down here.

Given a db config that looks like this:

development:
  adapter: jdbch2
  database: db/development/database

test:
  adapter: jdbch2
  database: db/test/database

production:
  adapter: jdbch2
  database: db/production/database

Add the code below to your rake tasks and you’ll be good to go!

module Rake

  module TaskManager

    def redefine_task(task_class, *args, &block)
      task_name, arg_names, deps = resolve_args(args)
      task_name = task_class.scope_name(@scope, task_name)
      deps = [deps] unless deps.respond_to?(:to_ary)
      deps = deps.collect {|d| d.to_s }
      task = @tasks[task_name.to_s] = task_class.new(task_name, self)
      task.set_arg_names(arg_names) unless arg_names.empty?
      task.add_description(@last_description)
      @last_description = nil
      task.enhance(deps, &block)
      task
    end

  end

  class Task
    class << self
      def redefine_task(*args, &block)
        Rake.application.redefine_task(self, *args, &block)
      end
    end
  end

end

def redefine_task(*args, &block)
  Rake::Task.redefine_task(*args, &block)
end

# hack to make db:test:prepare work for h2-jdbc adapter
namespace :db do
  namespace :test do
    redefine_task :prepare => [:environment] do
      abc = ActiveRecord::Base.configurations["test"]      
      if abc["adapter"] == 'jdbch2'
        rm_rf Dir.glob("#{abc['database']}*")
        ActiveRecord::Base.establish_connection(abc)
        ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")        
      else
        if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
          Rake::Task[{ :sql  => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
        end
      end
    end
  end
end
Feb 27 / David Rice

Working on a Clojure project in Intellij IDEA

I’ve previously posted on how to get started with Emacs and Clojure. In the interim I’ve not been able to devote the necessary time to learning either Emacs or Clojure. Thus, when I do feel the urge to dip into Clojure, I prefer to just get on with it rather than spend the majority of my cycles restoring my rather rudimentary Emacs muscle memory.

Here’s what I’ve settled on for using Clojure in Intellij IDEA:

Install the La Clojure Plugin for syntax support.

Install the Leiningen Plugin if you are using leiningen for builds and wish to invoke tasks from within IDEA. Mostly useful for running all your tests.

Define a custom run configuration (Java Application) to launch a REPL with your code loaded and in your namespace:

Aug 13 / David Rice

A Newbies Guide to Getting Started with Clojure and Emacs

THIS ARTICLE IS VERY OLD AND YOU SHOULD NOT READ IT!! THIS INFORMATION IS ALL MASSIVELY OUT OF DATE AND OBSOLETE!!

Like many of you I’ve spent some time learning Clojure over the last 1-2 years. When talking to fellow Clojure noobs, the almost always first topic to arise is ‘What editor are you using?’ Outside of those already well versed in Emacs, SLIME, and LISP, the answers more often than not express dissatisfaction and dismay. And at some point that dissatisfaction will lead someone learning Clojure to wanting to try their hand at working with Clojure in Emacs. While there are indeed well-maintained, documented projects that enable working with Clojure in Emacs, I never found one place where I could find step-by-step instructions for getting started. I eventually cobbled together the knowledge I needed to get started and I figured I’d share. Here I present step-by-step instructions for working with a Leiningen-based Clojure project in Emacs.

These instructions assume you are working on OS X. Also, this post is not even remotely a recommendation of this setup when working with Clojure. I’m a complete noob when it comes to Emacs and next-to-complete noob when it comes to Clojure. Use this information in that context.

Part 1: Install Leiningen, write function, run tests, run REPL

  • Download the lein script and put it on your PATH, making it executable
  • Install leiningen (into ~/.m2/repository/leiningen): >> lein self-install
  • Create a project: >> lein new my_first_clojure_project
  • Add your first function to my_first_clojure_project/src/my_first_clojure_project/core.clj:
      (ns my_first_clojure_project.core)

      (defn my-add [a b]
        (+ a b))
  • Add a test for that function to my_first_clojure_project/test/my_first_clojure_project/core.clj:
      (ns my_first_clojure_project.test.core
        (:use [my_first_clojure_project.core] :reload-all)
        (:use [clojure.test]))

      (deftest test-my-add
        (is 5 (my-add 2 3))) 
  • Run the tests: my_first_clojure_project>> lein test
  • For REPL to know in what namespace to start, update my_first_clojure_project/project.clj to specify :main:
      (defproject my_first_clojure_project "1.0.0-SNAPSHOT" 
        :description "Learning about leiningen" 
        :dependencies [[org.clojure/clojure "1.2.0-beta1"]
                       [org.clojure/clojure-contrib "1.2.0-beta1"]]
        :main my_first_clojure_project.core)
  • Start the REPL: >> lein repl
  • Call your function: my_first_clojure_project.core=> (my-add 2 3)
  • Get a result: 5
  • Now, let’s jar it up for standard Java execution. To start, edit my_first_clojure_project/src/my_first_clojure_project/core.clj to add a “-main” function and a :gen-class directive:
      (ns my_first_clojure_project.core
        (:gen-class))

      (defn my-add [a b]
        (+ a b))

      (defn -main []
        (println "Hello! The result of 7 + 3 via the my-add function is: " (my-add 7 3)))
  • Next, build the standalone jar: >> lein uberjar
  • Now run the main function via the java executable: >>java -jar my_first_clojure_project-1.0.0-SNAPSHOT-standalone.jar
  • Get a result: Hello! The result of 7 + 3 via the my-add function is: 10

Part 2: Working with a lein project in Emacs

  • Before you get started, I highly recommend that you spend 9 bucks and watch the peepcode Meet Emacs screencast. If you want to just jump in, perhaps my instructions that follow are good enough :)
  • Emacs commands are always a series of keystrokes, usually shortcuts, but sometimes function names. When you see a capital C, that means ‘Control’ and when you see capital M, that means ‘Meta,’ which on OS X is the ‘Apple’ or ‘Command’ or ‘Splat’ key that is next to the space bar. M-x b would be ‘x with the command key, b on its own.’ You will quickly get the hang of it. Here’s a good summary of Emacs Commands
  • As the screencast suggests, give your emacs setup a jumpstart by using Phil Hagelberg’s Emacs Starter Kit. If you watched the video, you’ve likely already installed the starter kit, but if not, here are two examples of how to do the install:
  • To do a simple install into your home folder:
      git clone http://github.com/technomancy/emacs-starter-kit.git ~/.emacs.d
  • To share across all your machines and accounts using Dropbox:
      git clone http://github.com/technomancy/emacs-starter-kit.git ~/Dropbox/emacs-starter-kit
      ln -s ~/Dropbox/emacs-starter-kit ~/.emacs.d  
  • Download, install, and start Carbon Emacs. When you first start Emacs you will see a bunch of compilation and associated errors flying by your eyes. I’m told this is normal (like I said, I know next to nothing about emacs beyond watching the movie and playing around with Clojure.) Once this activity stops you will see a split frame, the top window showing the scratch buffer, the bottom frame showing the compile log.
  • Before we get started, a couple of Emacs tips if you’re totally new to this:
    • Use the mouse if you must! Don’t be afraid to click into a window and place the cursor where you want it. Eventually, you’ll get over this, but don’t worry about it.
    • That said, do play with windows early and often:
      • C-x 1 will make the current window the only window
      • C-x 2 split the current window vertically
      • C-x 0 close the current window
      • C-x o place cursor in other window
      • C-x 3 split the current window horizontally
      • C-x b to switch which buffer is shown in the current window. This is MASSIVELY useful when you get stack traces and need to switch a window back to the REPL or your source. Once you’ve started the REPL and opened files, this command is your friend!!
      • Shift with arrow keys will also navigate windows
  • Now it’s time to install some packages. Type M-x package-list-packages to show the currently installed and available packages. Note that the package list shows up in whatever window had the cursor when you typed the command.
  • First, install the clojure-mode package, by using the arrow keys to scroll to the clojure-mode package. With the cursor aligned to that package, type i to select it, and x to install. Again you’ll see some compile errors, but as with the initial emacs startup this is normal.
  • Now install the following packages in order, using the method described in the above point, one at a time: slime, slime-repl, swank-clojure
  • It’s likely possible that the packages can be installed in a bulk manner, or perhaps installing swank-clojure would install some of its dependencies, but I could not find any install procedure, other than ordered one-by-one that worked reliably.
  • Now, go back to your lein project file and add the swank-clojure dependency:
      (defproject my_first_clojure_project "1.0.0-SNAPSHOT" 
        :description "Learning about leiningen" 
        :dependencies [[org.clojure/clojure "1.2.0-beta1"]
                       [org.clojure/clojure-contrib "1.2.0-beta1"]]
        :dev-dependencies [[swank-clojure "1.2.1"]]
        :main my_first_clojure_project.core)    
  • In the console opened to your lein project directory (not in emacs), update dependencies:>> lein deps
  • In the console opened to your lein project directory (not in emacs), start a swank server:>> lein swank
  • Now, back in emacs, type M-x slime-connect to connect to the swank server you just started in your lein project console. Accept the default host and port by pressing ENTER. You will likely see an error about a version mismatch of slime and swank. Type y to ignore this warning. (Digging around Google it looks like it’s possible to configure emacs to not warn you about the version issue. I’ll leave that as an exercise to the reader.)
  • At this point you likely have a frame with a couple of windows, if you’ve had luck up to this point, one of those windows should be showing a Clojure REPL in the ‘user’ namespace. At this point you still cannot use the code you wrote earlier as it has not been loaded into the REPL.
  • Before getting your code loaded into the REPL, let’s first get your windows sorted (btw, in case your curious, what you’d normally call a ‘window’ on your desktop, emacs calls a ‘frame,’ and within the frame are any number of tiled windows.) To start you’ll likely just want 2 windows, 1 with the REPL and one with your core.clj source file. In an earlier bullet I listed useful window commands.
  • OK, so now you have at least one window with the REPL open. Now, move the cursor to any window that is not the REPL window. In that window, type C-x C-f to load the src/my_first_clojure_project/core.clj file from disk. You can use the keyboard, ENTER key and DELETE key to navigate to the file in the mini-buffer. I’ll spare the long explanation, as it’s fairly intuitive to figure out the navigation. If at any point you feel you’ve screwed up and want to restart the command type C-g to reset.
  • Once core.clj is open in a window, load it into the REPL by typing C-c C-k while the cursor is in the core.clj window.
  • Now, still in the core.clj windows, type C-c M-p to switch the REPL to the namespace of the file open in the code window. This will make it easier to test drive your code.
  • Switch to the REPL window, and give your my-add function a whirl:>> (my-add 4 6)
  • To run your tests:
    • In the code window, open the test/my_first_clojure_project/core.clj file and load into the buffer following the exact same instructions as with the src/my_first_clojure_project/core.clj file.
    • Run the tests in the REPL:(clojure.test/run-tests 'my_first_clojure_project.test.core)

A final note

One of the more frustrating pieces of Emacs is feeling not fully in control of what’s showing in a particular window. Given that, I’ll relist the windows commands I earlier listed. I’m still not fully in command of avoiding a window inappropriate switching to another buffer, but these commands can help you recover:

  • C-x 1 will make the current window the only window
  • C-x 2 split the current window vertically
  • C-x 0 close the current window
  • C-x o place cursor in other window
  • C-x 3 split the current window horizontally
  • C-x b to switch which buffer is shown in the current window. This is MASSIVELY useful when you get stack traces and need to switch a window back to the REPL or your source. Once you’ve started the REPL and opened files, this command is your friend!!

Resources

Jul 29 / David Rice

OAuth Explained

Something we’re busy with right now at ThoughtWorks Studios is introducing a gadget style of integration to our products. The plan is to support OpenSocial gadgets (focus on gadgets, not social for now), allowing our applications to show each others’ data in its native form. As we do not have a notion of shared or common users across the suite, one of the hurdles is how to handle authentication when, say, Mingle displays a Build Pipeline status gadget from Go. The Mingle user should only see what she’s allowed to see in Go, and Go should know that it’s showing something to that specific user. We are going to solve this issue by implementing OAuth 2.0. OAuth is a standard protocol that allows a user to share his private data between two sites without having to hand out his password to the site “borrowing” the data.

I’m proud to say that we’ve given something back and published not only an OAuth 2.0 Provider plugin for Rails but also a a series of Introductory videos that might help you understand OAuth without having to spend a couple days attempting to read an IETF specification, continually wiping the drool from your mouth. I should also note that the plugin’s README contains a lot of relevant information and references and might be worth checking out even if didn’t want to use it.

OAuth provides a nice, dare I say comforting, user experience. That’s the nice part of OAuth. The ease of use plus the growing adoption made it a no-brainer for us to use OAuth in our products. However, implementation wise, it still feels like a lot of moving parts as you’ll see in part 4 of the videos. I like my security (well all my software, but particularly security) software to have as little complexity as possible. OAuth is getting there, but I would not call it simple.

As to how OAuth 2.0 compares to 1.0, the Web Server flow in OAuth 2.0 is mostly similar to OAuth 1.0, less the nearly impossible to code correctly request signing. OAuth 2.0 simply states “Use SSL!” rather than ask client developers to code potentially buggy implementations of the request signature. That’s a good thing.

Here is the set of videos, where I exlain how the fictitious parsley.com personal financial portal uses OAuth to lookup user account information at Acme Bank: