Java vs Ruby

Posted by Ben Poweski Fri, 22 Feb 2008 17:04:00 GMT

Forgive me, as this will most likely spark deep seeded hatred from those forced to navigate the deep dark world of Java development. I have managed to break free from it for sometime now, enjoying my Ruby bliss of block passing and metaclasses. Just when I thought I was out, they pull me back in. They, being work. Why can’t we all develop with a language so pointed and terse.

The Java Version

StringBuffer subType = new StringBuffer();
for (String token : tokens) {
  // pretty print the tag
  char[] c = token.replaceAll("flag", "").toCharArray();

  // make first char upper case
  if (c.length > 0)
    c[0] = Character.toUpperCase(c[0]);
  subType.append(c);
}

The Prettier, More Compact, Ruby Version

tokens.collect {|t| t.sub(/flag/, '').capitalize }.join

I can hear the snide remarks right now…save them. There are plenty more examples where that came from.

Deploying Java Applications with Capistrano

Posted by Ben Poweski Fri, 22 Feb 2008 17:01:00 GMT

Capistrano 2, the fantastic sequel to the already superb Rails deployment framework, is an excellent solution to the otherwise mundane task of deploying Java applications.

Network security restrictions prohibit me from using the typically SCM -> Production server configuration. Next, I ran into a few problems uploading Jar files using the put command. Luckily Alex Gorbatchev, posted an example of how to use SFTP within a Capistrano deployment recipe.

I used his idea and adapted my rails recipe using SFTP deployment.

namespace :deploy do                
  task :update_code do
    on_rollback { run "rm -rf #{release_path}" }
    run "mkdir #{release_path}" 
    files = Dir.glob('lib/*.jar') + Dir.glob('dist/*.jar')
    execute_on_servers(options) do |servers|
      servers.each do |server|
        files.each do |path|
          logger.info "uploading #{File.basename(path)} to #{server}"  
          sftp = sessions[server].sftp  
          sftp.connect unless sftp.state == :open  
          sftp.put_file path, File.join(current_path, File.basename(path))  
          logger.debug "done uploading #{File.basename(path)} to #{server}"  
        end
      end
    end    
    finalize_update
  end
end