Dash and Java
February 16 2009, 3:07pm
One of the fundamental concepts in Dash is multi-platform support. Dash is not a Ruby library; it is an open specification with implementations in several languages.
The fact remains though that a programming language has a huge influence on the API design and supported functionality. Ruby’s powerful metaprogramming makes it quite simple for the dash-ruby implementation to wrap a method at runtime, just by declaring it:
recipe.counter :requests, "Requests", :method => 'ActionController::Base#perform_action'
This code tells Dash to look up the ActionController::Base class, find the perform_action method on it and dynamically wrap it in code which counts the number of invocations.
The Java API is more verbose:
public static class Requests extends BaseMetric { public String getName() { return "Requests"; } public IMetricCallback getCallback() { return new IMetricCallback() { public NamespaceValue[] getCurrentValue() { return basicValue(getRequestsAndReset()); } }; } }
A metric definition is about 10 lines of code and that doesn’t even count the implementation of the getRequestsAndReset method. Essentially we are forcing the developer to instrument their code and the callback just gets the result of that instrumentation.
We’d like to simplify this API as much as possible by doing the instrumentation automatically. Since this API needs to work with all Java applications, we like to stick with the facilities in Java 5+ and not rely on Spring-AOP or similar. Can Java wrap arbitrary code at runtime with a minimum of modification to the target application? The closest thing we’ve found so far is Groovy’s dynamic MetaClass capabilities or AspectJ’s runtime weaving. Java pros, any other suggestions or ideas?
- Tags:
- Open Source

