Trampoline Systems

* Trampoline Description Here

Trampoline Systems

* Trampoline Description Here


Content

Machines

Ideas, thoughts and observations from Trampoline's technical brains

Archive for the ‘Ruby’ Category

mccraig

removing global fixtures for ruby tests

By craig mcmillan on July 2nd, 2008

global fixtures are evil, but we’ve got a bunch of unit tests depending on them, so we still need them around

here’s a neat [and generally fast, though a degenerate O(#tables^2) case is possible] way of deleting all fixtures without invoking db dependent ways of ignoring foreign-key constraints, and without loading all the objects into memory :

classes = ActiveRecord::Base.connection.tables.map {|t|
  t.singularize.camelize.constantize rescue nil
}.compact.reject{|cls| !cls.ancestors.include?(ActiveRecord::Base)}

while classes.size > 0
  classes = classes.select{|c|
    begin
      c.delete_all
      false
    rescue
      true
    end
  }.reverse!
end
jan

Springy 0.3 released

By Jan Berkel on August 2nd, 2007

No big changes this time, mainly compatibility fixes for JRuby 1.0. It is now also possible to build the project using Maven, for those too afraid to use rake. Documentation and code for springy are available here.

I’m also happy to announce that Craig Walls, the author of “Spring in Action”, is going to talk about Springy as part of his “Spring Cleaning: Tips for Managing XML Clutter” talk at this year’s No Fluff Just Stuff series of events as well as the Spring Experience 2007 in Florida.

jan

Java and Rails integration with GoldSpike

By Jan Berkel on July 13th, 2007

While trying to create a unified testing framework (shared between Rails and our Java backend code) I came across ActiveRecordJDBC which is an adapter to use JDBC drivers with JRuby on Rails. It works fine, although it can be a bit complicated to get a DRY database.yml configuration. The goal is to get rid of our dbunit/manually crafted database tests on the Java side by using ActiveRecord fixtures. After some research I found out about the Rails integration project (now called GoldSpike), which tries to make it easy to deploy a Rails app on a Java servlet container such as jetty. As far as I know Thoughtworks uses this approach to deploy their new product, Mingle. GoldSpike is under constant development but it is already usable, although a few patches were required. After everything was set up, a simple


$ rake war:standalone:create
Reading user configuration
Assembling web application
Adding Java library commons-pool-1.3
Adding Java library rails-integration-1.1.1
Adding Java library activation-1.1
Adding Java library mysql-connector-java-5.0.5
Adding Java library bcprov-jdk14-124
Adding Java library jruby-complete-1.0
Adding web application
Adding Ruby gem ActiveRecord-JDBC version 0.4
Creating web archive

creates a .war file which can be directly deployed to a container. I’ve never been a big fan of war files, but in the context of JRuby+Rails it makes perfect sense, because Ruby and all the required gems can be packaged up in a single file. No need to worry about missing gems, C bindings or wrong versions of the installed software, all you need to deploy is Java and jetty (which is pretty lightweight).

Not quite sure what the implications of this are, but it seems like a good alternative to mongrel/CRuby deployments. Performance-wise it looks good, too, though we haven’t done any benchmarking. GoldSpike doesn’t have a project homepage yet, but you can find it on the jruby-extras project page. Let’s wait and see what this will mean for the adoption of Rails in corporate environments (Oracle is currently looking for Rails developers to join the Enterprise 2.0 team, for example).

jan

growl-lastfm

By Jan Berkel on July 10th, 2007

We use last.fm a lot in the office - but one thing I always found annoying was that there’s no easy way to find out what’s currently playing (you need to go to the web page and hit refresh, very distracting) so I knocked up a little ruby script which uses growl to display the currently playing song.


#!/usr/bin/env ruby
require ‘rubygems’
require ‘ruby-growl’
require ‘hpricot’
require ‘cgi’
require ‘open-uri’

raise “#{$0} <user>” if ARGV.empty?
user = ARGV[0]
already_notified = []

while true do
now_listening = Hpricot(open(“http://www.last.fm/user/#{user}”))/“.nowListening td.subject”
unless now_listening.empty?
currently_played = now_listening.last.inner_text.strip
unless already_notified.include?(currently_played)
already_notified << currently_played
g = Growl.new(“localhost”, “ruby-growl”, [“ruby-growl Notification”])
g.notify(“ruby-growl Notification”, “#{user} is listening to”,
CGI::unescapeHTML(currently_played))
end
end
sleep 60
end

Make sure that you have hpricot and ruby-growl installed (gem install hpricot ruby-growl -y), then copy and paste the script (or download it) and start it with a last.fm username as parameter. You also need to have “Listen for incoming notifications” and “Allow remote registration” checked in growl’s preferences (System Preferences | Growl | Network).

mccraig

installing Oracle 10g on 64 bit centos 5

By craig mcmillan on June 28th, 2007

We’ve just got ourselves a couple of new servers for running SONAR on, for internal use and demonstration. They are quite beefy, with 8GB RAM, 15k disks, and 8 x 3.2GHz threads. We decided to go with centos 5 as the operating system, since we have had pain installing Oracle on our debian platforms. Here is the install procedure I uncovered, which gets the server ready for SONAR on either MySQL or oracle. It covers installing Ruby, RubyGems, lots of useful Gems, Java, and Oracle 10g

yum install -y ruby
yum install -y ruby-devel ruby-docs ruby-ri ruby-irb ruby-devel ruby-rdoc
yum install -y w3m # for text based browsing and downloading, where curl won't work

# download rubygems from rubygems.org with w3m then...
gunzip rubygems-0.9.4.tgz
tar xf rubygems-0.9.4.tar
cd rubygems-0.9.4
ruby setup.rb

# get jdk rpm from java.sun.com and install

# add an /etc/profile.d/java.sh file, containing
##
JAVA_HOME=/usr/java/default
export JAVA_HOME
##
chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh

yum install -y ImageMagick-devel
yum install -y perl-DBI
yum remove -y sendmail
yum install -y postfix
chkconfig postfix on
yum install -y ntp
chkconfig ntpd on
yum install -y mysql-server mysql mysql-devel

chkconfig mysqld on
/usr/bin/mysqladmin -u root password '<password>'
/usr/bin/mysqladmin -u root -h <hostname> password '<password>'

yum install -y gcc
gem install builder
gem install camping
gem install capistrano
gem install cgi_multipart_eof_fix
gem install cheat
gem install chronic
gem install ci_reporter
gem install daemons
gem install fastthread
gem install gem_plugin
gem install hpricot
gem install image_science
gem install jerbil --source http://code.trampolinesystems.com
gem install json
gem install markaby
gem install metaid
gem install mongrel
gem install needle
gem install net-sftp
gem install net-ssh
gem install rake
gem install rmagick  # [ ignore errors about fonts... they are just for the docs ]
yum install -y libxml2-devel
yum install -y libxslt-devel
gem install ruby-xslt
gem install rubyforge
gem install RubyInline
gem install mysql -- --build-flags --with-mysql-config
yum install -y httpd
yum install -y subversion

# install rpmforge for centos 5 as detailed here :
# http://wiki.centos.org/Repositories/RPMForge#head-d766e7aacdee6150c6705d9369aa36c9bcabc139

yum install -y firefox
yum install -y xorg-x11-xauth
yum install -y vnc-server
yum install -y emacs
yum install -y libXtst
yum install -y xorg-x11-utils
yum install -y xorg-x11-resutils
yum install -y xorg-x11-server-utils

yum install -y compat-libgcc-296 compat-libstdc++-296 compat-libstdc++-33 openmotif22
yum groupinstall -y "Development Libraries"
yum groupinstall -y "Development Tools"
yum groupinstall -y "Legacy Software Development"
yum groupinstall -y "Server Configuration Tools"
yum groupinstall -y "X Software Development"
yum groupinstall -y "Administration Tools"

# fire up a vncserver on the remote machine :
vncserver
# and enable connections to the X server from other accounts :
xhost +

# connect an ssh session from your local machine, forwarding the vnc port [ 5900 + screen # ]
# connect your local vnc client to the server
# run firefox on the server
# download oracle 10g from www.oracle.com, thus avoiding having to upload 750M at 512Kb/s
# install as shown here : http://www.dizwell.com/prod/node/681?page=0%2C3
# and add an init.d startup script as described here : http://www.dizwell.com/prod/node/50?page=0%2C4

# done ! reboot to check that your init.d scripts are functioning correctly
jan

Springy 0.2 released

By Jan Berkel on March 22nd, 2007

I’ve just released a new version of springy, the JRuby-based alternative to Spring’s XML configuration format. It now has a “serialise-to-xml”-feature which allows you to write your whole config to a nicely formatted Spring XML file. Why would you want to do that ? In our case we’ve created a Ruby-based configuration framework modeled after Rails environments which pulls config files from different locations, deals with overrides and so on. The only downside is that this is pretty slow (especially when unit testing), hence the idea of serialising the whole context to XML.

The serialised XML config file is about twice the size of the springy context (~1600 vs. ~800 lines), which shows how expressive Ruby can be. A small example from our configuration:


DAOS = [ :ZoneDAO, :EmailDomainDAO, :DayDAO, :PreferenceDAO,
 :WhatEverDao… ]
DAOS.each do |dao|
bean(dao, “daos.hibernate.#{dao}Hibernate”)
{|b| b.new(“sonarSession”)}
end

This snippet uses metaprogramming to create 20 odd DAOs in 8 lines of code.

Springy has matured since the first release when it was still in the proof-of-concept-stage. We’re using it successfully in Sonar, and there is no way we’ll ever go back to using XML for configuration. Anyway, XML is for girls, according to Hani.

My new favourite feature in JRuby is JavaUtilities.extend_proxy. Basically it allows you to extend an existing Java class with Ruby code. All instances of this class will then have additional Ruby methods on them. This can be handy to fix completely useless APIs like org.w3c.dom.Document. If you’ve ever used Ruby’s xml builder you will know what I mean.

JRuby allows us to do this:


JavaUtilities.
extend_proxy(‘com.sun…internal.dom.DocumentImpl’) {
def tag(sym, *args, &block)
#ruby code omitted
end
}

doc = DocumentImpl.new
doc.tag(“beans”) do
doc.tag(“bean”, “id”=>“mybean”) doend
end
doc.validate!
doc.serialize(System.err)

DOM is finally usable! See serialization.rb for the full code.

mccraig

We want fresh engineer blood

By craig mcmillan on March 20th, 2007

SONAR brings statistical natural language processing and social network analysis to the problem of browsing and filtering large archives. We permit users to see who is talking to who, how much and about what. There are applications in areas including expertise discovery, compliance, forensics, search and alerting.

We are a small company, with large ambition. We number currently 10, and have just secured £3M of venture funding to help us grow our R&D efforts and establish a North American presence.

The SONAR platform consists of a very Ajaxy web front-end in Ruby on Rails, with a Java back-end doing the asynchronous natural language processing and social network analysis.

We are looking for software developers with experience in the following technologies :

Java, Hibernate, Spring framework, JMS, Ruby, Ruby on Rails, Transactions, Transactional messaging, SQL, MySQL, Oracle, Linux, Windows

Experience in all of the above is not a necessity. We are looking for smart people who can learn quickly, love solving difficult problems and can grow with the company. These attributes are far more important than any tick list.

If you are interested, please contact :

Craig McMillan, craig AT trampolinesystems DOT com

Please note : if you are an agency and are not already talking to us, then do not contact us. Really.

alistair

Java vs. Ruby with StatSVN

By Alistair Davidson on February 23rd, 2007

I’m a big fan of SVN, and I know it’s in very common use throughout the development community, so I thought I’d give a shout out to an interesting project that extracts some fascinating SVN stats and draws lots of pretty pictures - StatSVN

We’ve now added a StatSVN task to our build scripts, and the figures it kicks out are just great - once you start looking, you can lose yourself in there for ages….

To give some random samples:

  • As of three days ago, there were 75130 lines of code in the Sonar trunk, of which Craig has contributed 33.6%, myself 32.1% and Jan 22.2%. As Jan has mostly been working on the Ruby On Rails interface, and Craig and I have mostly been writing the Java backend, what does this say about the relative expressive efficiency and productivity of each language?
  • Craig took “Developer Of The Month” for February, with 5286 lines, taking over from me in January with 7546 lines.
  • Over 80% of my changes are additions, with under 20% being updates. Craig is more or less the same, with a few percent more modifications - probably due to fixing my occasional “EVIL” quick hacks…
  • By far and away the most commits overall get done between 4pm and 5pm, but personally, I do three times as many commits between 2pm and 3pm than any other hour. Probably because I keep saying to myself got to finish this before I go to lunch…
  • Jan has not done a single commit before 12 noon :-) However, he’s the only person to have committed between 1am and 2am!
  • I do a huge number of commits on a Monday (400+), followed by successively fewer every day until Friday (~60). Does this mean I’m fresher and more enthusiastic after the weekend, or is it that when I start the week, I go for the easiest tasks first, and ramp up towards the more difficult tasks as I go on? On the other hand, overall the most commits are done on a Friday, followed by Tuesday

You can also measure some metrics around files, rather than developers:

  • Overall, across all file types, we have an average of around 40 lines of code per file
  • 58.7% of the files in the repository are .java, but they contribute 73.3% of the total lines of code
  • On average, each .rhtml template is just 26.6 lines of code
  • The most amended file in the whole repository is the Rakefile!

I could go on for hours….but it’s nearly 2pm and I need to commit some code, dammit! So install StatSVN on your repository and have a play yourself. It’s fun.

jan

Wiring up Spring with JRuby

By Jan Berkel on November 21st, 2006

The term “XML sit-ups” is often used by Rails developers to make fun of the XML-loving Java crowd. When I first heard it, I was slightly annoyed - there’re many valid use cases for XML. However, using it as a scripting language is clearly not one of them. The build system Ant fell into this trap, and so did the Spring Framework: the configuration is just too verbose and awkward.

<bean id="bean3" class="springy.beans.Bean3">
<constructor-arg value="${vic}"/>
<constructor-arg ref="bean2"/>
<property name="aProperty">
<list>
<ref bean="bean1"/>
<value>A String</value>
</list>
</property>
</bean>

This slightly contrived example creates a new instance of the class springy.beans.Bean3 (passing two values to the constructor) and eventually sets a property (in this case of type java.util.List) on the instance. The first constructor argument, ${vic}, is a placeholder which will get resolved at runtime (requiring more plumbing, I’ll spare you the details).

Fortunately, the bean/object management bits of Spring are completely unaware of the XML horrors happening around them, in best Java tradition (“if we need to change the implementation some day”). I decided that the day had arrived, inspired by a blog post talking about a successful Groovy-based Spring configuration (Programmatic Configuration in Spring).

Instead of Groovy I opted for Ruby (in its Java-based incarnation JRuby), a language known for its DSL-friendly syntax. Trying to make the DSL similar to Rake’s the above XML turned into:


bean :bean3, “springy.beans.Bean3″ do |b|
b.new(vic, :bean2)
b.aProperty = [ :bean1, “A String” ]
end

A fair bit of Ruby’s magic is used to make this possible: the notorious method_missing to set properties and :symbols to reference existing beans. The nice thing about using a DSL based on real programming language instead of pure XML is that you can use all features the host language has to offer. For example, constructs like <import resource=”x”/> can be replaced with Ruby’s require() or load() mechanisms. This is not only restricted to Ruby code, as JRuby allows you to use normal Java objects as well.

Extending the syntax is just a matter of adding methods, as opposed to having to deal with XML schema and coding definition parsers. The Extensible XML authoring example from the Spring documentation translates into the following Ruby code:


def dateformat(id, format, lenient=false, args = {})
register_bean(BeanDef.new(id, “java.text.SimpleDateFormat”, args)
.new(format).lenient(lenient))
end

This is three lines of code compared to writing the XML schema (20 lines), parsing handler (30 lines), namespace handler (10 lines) plus registering the handler and schema (two configuration files to edit).

In case that XML really is required, you can put additional XML markup inline:

inline_xml do <<XML
<beans>
<bean id="bean1-2" class="springy.beans.Bean1"/>
</beans>
XML
end

The implementation of this DSL requires ca. 200 lines of JRuby and 20 lines of Java. However, the obvious advantage is not the DSL itself but the access to both Ruby and Java from within the ‘configuration’ files, giving way to far more interesting possibilities like Ruby-based AOP (currently not implemented), which could look like this:


around_advice “expression(* foo(..))” do |joinpoint|
joinpoint.this.do_something
joinpoint.proceed
end

In case you want to give it a spin you can find the code here. However, bear in mind that it is far from being complete as many features are currently not implemented. It serves more as a proof of concept and starting point for a full implementation. Also, using an interpreted language there’s an obvious performance overhead but it’s not that noticeable (ca. 1 sec slower on my machine) and the increased flexibility makes more than up for it. Things can only get better: the JRuby guys are putting a lot of effort into performance improvements at the moment.

Edit 08/12/06: Simplified example

Edit 18/12/06: Got some interesting feedback from the InfoQ community. The readability of the DSL was a main concern of most of the posts. I must admit that it is probably difficult to read without some basic understanding of Ruby concepts and data types. It is probably more interesting to developers already familiar with Ruby. It’s a typical problem with internal DSLs.