log4p

Peter Maas’s Weblog

Archive for December, 2007

Closures and the return of the return

I attended Joshua Blochs' presentation on closures at JavaPolis last week (watch the video here). This slides about return not return from what you'd expect kept me wondering: how do other languages solve this 'problem'.

The example from Bloch, taken from Slide 38 of Blochs' closures controversy presentation:

  1. static <E> Boolean contains(Iterable<E> seq, {E => Boolean} p) {
  2.     for (E e : seq)
  3.         if (p.invoke(e))
  4.             return true;
  5.     return false;
  6. }
  7.  
  8. static Boolean test() {
  9.     List<Character> list = Arrays.asList(
  10.             'h', 'e', 'l', 'l', 'o');
  11.     return contains(list, { Character c => return c> 'j'; } );
  12. }

The point Josh tried to make was that due to this 'copy and paste' error the contains method would return on the first invocation of the passed block. Auch....

It asked around at Morning coffee but nobody really knew what this would actually do in Ruby.

So, let's just try it. In Ruby this might look a bit like this:

  1. def contains(enumerable, &test)
  2.   enumerable.each do |e|
  3.     return true if test.call(e)
  4.   end
  5.   false
  6. end
  7.  
  8. puts contains(['h','e','l','l','o']){|v| return v == "z"}

In the Ruby a return statement in a block returns from the enclosing function as well. However, if the return statement is part of a closure passed to a function returning is not allowed:

closure_return.rb:9: unexpected return (LocalJumpError)
from closure_return.rb:4:in `call'
from closure_return.rb:4:in `contains'
from closure_return.rb:3:in `each'
from closure_return.rb:3:in `contains'
from closure_return.rb:9

Throwing an error might be considered a better solution. The problem in this case is the fact that the error is thrown at the first invocation of the block; code before the invocation of the block will be called... but code after the invocation won't. Of coarse this is 'just' a matter of taking this into account:

  1. def contains(enumerable, &test)
  2.   begin
  3.     puts "a"
  4.     enumerable.each do |e|
  5.       return true if test.call(e)
  6.     end
  7.   rescue
  8.     puts "Error in passed test-block: #{$!}"
  9.   ensure   
  10.     puts "b"
  11.   end 
  12.    
  13.   false
  14. end
  15.  
  16. puts contains(['h','e','l','l','o']){|v| return v == "z"}

In this case the ensure statement will always be called. I'm not really in favor of adding closures to Java; just go to Ruby/Groovy/Scala if you want them). I do however think that the 'return' argument Josh made isn't to strong; a solution similar to the way it works in Ruby could probably be found for Java.

13 comments

How Elvis showed me a neat way of using operators in Ruby

Recently the Groovy team introduced a new operator to the Groovy language. It is called the Elvis operator. There is one thing I particularly like about this operator. It's name.

To bad the Elvis operator is only a shortening of Java's ternary operator, written like ?:. One use-case for the operator is returning a 'sensible default' value if an expression resolves to false or null. A simple example might look like this:

  1. groovy:000> name = null
  2. groovy:000> displayName = name ?: "anonymous"
  3. ===> anonymous

I had a good laugh about it with some people at JavaPolis, and forgot about it. Until I noticed something while reading the expressions chapter in the PickAxe (I'm trying to boost my Ruby knowledge).

In Ruby anything which isn't nil is true in an expression. Due to this behavior the result of a shortcut expression doesn't have to be a boolean but is actually the value which evaluated to true (in Groovy the result is a boolean). This means Ruby implicitly has a 'Elvis' operator.

  1. irb(main):069:0> name = nil
  2. irb(main):070:0> displayName = name || "anonymous"
  3. => "anonymous"

Even better, Ruby allows you to use the shortcut OR in combination with the assignment operator:

  1. irb(main):072:0> name = nil
  2. irb(main):073:0> name ||= "anonymous"
  3. => "anonymous"
  4. irb(main):074:0> name = "peter"
  5. irb(main):075:0> name ||= "anonymous"
  6. => "peter"

Funny to see how a slightly different behavior of something like to or operator makes it even more useful!

5 comments

Javapolis 2007 – Day 3

Wicket 2 (Martijn Dashorst)

I knew that a talk labeled ‘Wicket 2’ was a bit suspicious. There is no such thing (anymore) as Wicket 2, the latest release is 1.3 and upcoming is 1.4. I have played around with Wicket a bit in the past and knew a bit about it at forehand. Martijn didn’t really expand my knowledge. He did however do some live coding, which in concept is a very good idea. In practice it wasn’t such a great one. Martijn managed to make typing mistakes in almost every line he wrote which really slowed down the presentation.

Still, I think Wicket is a great framework; I like the error screens (which we saw to many during the presentation) in particular which quite accurately manage to tell you what is wrong.

Terracotta (Geert Bevin)

I’ve seen presentations on Terracotta before and I’ve seen Geert Bevin presenting (Rife) before. Both where great. Somehow the combination didn’t really add extra value. The examples where impressive again. When will we see the first application servers with Terracotta build-in?

OSGI (Peter Kriens)

Peter Kriens managed to make terrible jokes and give a far to complex presentation about the excellent OSGi framework in the same time. I really like the concepts in OSGi and think it will be everywhere in the future.

Test driven development in practice (Lasse Koskela)

Lasse gave a nice and solid presentation on TDD. He started with the TDD mantra:

TDD Mantra

After the introductio he talked about one of my favorite concepts: behavior driven design. He actually didn’t understand how it differed from TDD. And conceptually I t

hink he is right. As long as you test behavior using unittests there aren’t much/any differences. Personally I find it easier to think in terms of behavior when using a DSL to do so.

He also talked about test doubles, and what type of test doubles should be used in which situation. The types he talked about where:

  1. Stubs (simplest possible thing that could possible work)
  2. Fakes (alternative/lightweight implementation)
  3. Mocks (interaction based)

His rule of thumb would be to start of with a stub, use fakes if stubs are not sufficient anymore and start using mocks if the other two approaches are to limiting.

Now, finally, during the last session they actually started programming on stage! It was a bit slow… but it was a very nice way to end the final session of the conference.

Special thanks

Special thanks go to Marcel Maatkamp for never leaving home unprepared. I probably wouldn’t have found my car-key after dropping it if he hadn’t been carrying a Maglite around!

1 comment

Javapolis 2007 – Day 2

Keynote

The opening keynotes seem to involve quite a bit of Adobes’ Flex this year. I must say I’m getting less skeptical, the upcoming version of parleys.com (presented by Stephan Janssen) which is build in Flex and Air (for being able to look at presentations offline) looks extremely cool. Some screenshots of Parleys V2 can be seen here.

After the bombardment of nifty Flex stuff Sun did a presentation on JavaFX. Even-though it is getting better and better, it’s not there yet. I went out of the room when they started talking about mobile again; I’ll read up when I actually get to develop something for a mobile device.

Scala (Martin Odersky)

I had a couple of short looks at Scala during the last months, but never had enough time to have a good look at it. Martin managed to tick all the boxes: Scala rocks! It’s like a more Scientific version of Groovy; which seems to result in clearer synthax. Like Groovy Scala is full interoperable with Java.

The key features are:

  • Uniform object model
  • Pattern matching and higher-order functions
  • Novel ways to abstract and compose programs
  • Scala programs compile to JVM bytecode

The hypothesis of martin odersky

The code examples looked really nice. Scala currently sits waiting in my download folder… I’ll let you know!

The closure controversy (Joshua Bloch)

Joshua Bloch presented his ideas about adding closures to Java. Joshua more or less prefers the CICE/ARM point of view over the BGGA one. Although I thought I wanted to have closures in Java I must say I’ve changed my mind. I do still like the ARM (Automatic Resource Management) solution though, which would result in something like this:

try(BufferedReader r = new BufferedReader(new FileReader(path)){
    String s = r.readLine();
}

And if you want to have closures just use a language like Scala, Groovy or JRuby… which are all fully supported on the JVM.

Apache ServiceMix

From cool languages and language related stuff to Enterprise dullness. I almost fell asleep during the 50th or so introduction to SOA… But after the introduction I got interested more and more. ServiceMix looks really great! The configuration of the bus is done using plain old Spring files; ServiceMix comes with it’s own namespace containing components for most of the integration patterns from the EIP book. New elements are added to the bus using Maven Archetypes. The upcoming graphical route builder looked great as wel. I’ll probably have a look at it quite soon.

Webbeans

The Webbeans JSR (299) was presented by Bob Lee (Author of Guice), he is the expert groud lead. If you know Seam lot’s of it might feel remarkably familiar. I do however get the feeling that people are really going to fare with annotations. Bob showed example classes with about 8 annotations on them; in real life one will probably need twice as much. And yes, I did get the part about how one can put annotations on annotations to have some sort of grouping; but I think this doesn’t make it look any better; especially since a annotations can be overridden.

Java puzzlers (Bloch & Gafter)

A really nice interactive presentation containing code, humor and educational value! Amongst many obscure ‘bugs’ in the language I learned not the use java.lang.URL, and staying with primitives as much as possible. I tried to buy the Java Puzzlers book afterwards… but they where all sold out.

Evening

During the evening we had a nice meal and visited the christmas market (turns out hot Apple Jenever is far stickier then expected).

A father christmas shaped van

No comments

Javapolis 2007 – Day 1

This year I visited Javapolis for the second time. I won’t even try to write an accurate journal of everything I’ve seen/heard; it’s just to much. I did however make some notes and pictures to give you an impression of the conference.

Opening Keynotes

Javapolis 2007 is sold out with over 3200 visitors from around the globe and over 100 speakers.

Bruce Eckel

Introduction to Open Spaces. Why travel if you can watch from home? Hallway conversations. Conferences with only hallway conversations are called unconferences. The law of two feet => just leave if you’re not really interested! I’ll certainly be trying to have as much hallway conversations as possible.

Gossling

Gossling, the father of Java. With a kind of boring (netbeans!! netbeans!! netbeans!!) presentation. Some of the highlights where:

  • Ruby is present on two of Gosslings’ slide!
  • realtime stuff…
  • Java wordt steeds sneller, nu sneller dan c/c++
    * Java is getting faster and faster, many benchmarks show it’s faster thenk C/C++ now
  • Gossling wants closures!!

James Gossling on Javapolis 2007

The rest of the presentation was actually quite horrible:

  • JavaFX => Technologies and tools, forthcoming open source programs, consumer facing… but not there yet
    * Jaspersoft presentation: genererate a report from netbeans… snore
    * JavaFX demo door het Sun evangelism Team… the interface is just ugly bad antialiasing etc.

Talks

Groovy by: Guillaume Laforge and Dierk König

A resenable presentation of groovy. Nothing to execiting. Some highlights of the recent 1.5 release. Most of the changes are related to adding Java5 features (Generics, Annotations, Static import) to Groovy. Mostly to allow hooking groovy code into frameworks. Guillaume also noted the recent enhancements to the Expando metaclass.

GWT

Dick Wall and Didier Girard managed to give a horrible presentation about a great product: GWT. I’m not to sure if Dick Wall had actually seen the presentation before presenting it. Didier clicked through a couple of GWT examples and started creating a simple open social application.
Everybody (well, at least I was) was waiting to see some GWT code, but Didiers’ presentation lacked a bit op depth.

That said, I saw some really interesting features of GWT and will certainly be giving it a go when I have some spare time.

JBoss ESB (Mark Little)

Next to Mule and ServiceMix (more on ServiceMix in one of the followup posts) JBoss ESB still doesn’t seem to be there yet. I really hoped this presentation would change this view; but the presentation was far to superficial and was mainly focussed on high level ESB/SOA concepts.

EJB 3.1 (Kenneth Saks)

Kenneth talked about making EJB development simpler. Important changes include:

  • Removal of the requirement for a separate local business interface.
  • Support for direct use of EJBs in the servlet container, including simplified packaging options.
  • Singleton beans.
  • Support for asynchronous session bean invocation.
  • Support for stateful web services via stateful session bean web service endpoints.
  • Specification of concurrency options for stateful session beans.
  • Application-level callback notifications, including for container initialization and shutdown.
  • EJB Timer Service enhancements to support cron-like scheduling, deployment-time timer creation, and stateful session bean timed objects.
  • An ejb-jar level component environment to simplify the specification of shared dependencies among components.

No real surprises here, all proposed changes sound great to me: remove the clutter!

The Future of Computing (Gossling, Gafter, Bloch, Odersky)

I think I expected far to much from this talk. I know this wasn’t supposed to be about Java;

Unconferencing

Through the day

I talked to some interesting people in the hallways; The whiteboards containing statements and polls attracted people with various opinions and I had some nice discussions about new language features.

A poll on new language features

Through the evening

We went back to the great place where we had some beers last year called “het 11de gebod” (the eleventh commandment). JTeam, VX Company, Profict, VPRO and of course Finalist where presented. Nice!

Having a good beer, and discussion!

No comments

SOA and ESB stencil for Omnigraffle released

Over the weekend I've put some time in creating a basic SOA and ESB stencil for Omnigraffle. I'm pleased with the results:

ESB and SOA Omnigraffle Stencil

I've decided the give the stencil its' own page containing more details, the and installation guidelines and will try to provide future updates to it.

3 comments

The virtues of HPricot: scraping DZone

I couple of days ago I was looking at XML parsing solutions available in Ruby. I played a bit with REXML, a conformant but kind of dull XML processor, it works great. When working with less conformant XML-like (like websites) data it might just not be what you're looking for. Luckily ruby demi-god and respected colleague Remco pointed me to HPricot.

Hpricot is a very flexible HTML parser, based on Tanaka Akira's HTree and John Resig's JQuery, but with the scanner recoded in C.

Like most Ruby frameworks HPricot is not hard to install ('gem install hpricot' or 'gem install hpricot --source http://code.whytheluckystiff.net' for the current development version). After this to use HPricot just require 'hpricot'

You can parse files, strings etc. by passing it to HPricot:

  1. doc = Hpricot("<p>A simple <b>test</b> string.</p>")
  2. doc = open("index.html") { |f| Hpricot(f) }
  3. require 'open-uri'
  4. doc = Hpricot(open("http://qwantz.com/"))

If you get hold of your HPricot document you can use XPath or CSS selectors (!!!!!) to traverse it.

Now say I want to scrape the details of the links I posted to DZone recently, which are displayed in a rich HTML page.

The divs containing information about each link can be identified by their CSS class 'linkblock'. If I want to obtain this part of the page you can simply do this:

  1. require 'rubygems'
  2. require 'open-uri'
  3. require 'hpricot'
  4.  
  5. doc = Hpricot(open("http://www.dzone.com/links/users/links/193149.html"))
  6. puts (doc/'.linkblock').first.inner_html

This will print the HTML content of the first node matching the CSS selector. If you don't like the slash '(doc/'.linkblock')' synthax, it's identical to calling 'doc.search('.linkblock')'. Now, let's start scraping some information:

  1. require 'rubygems'
  2. require 'open-uri'
  3. require 'hpricot'
  4.  
  5. doc = Hpricot(open("http://www.dzone.com/links/users/links/193149.html"))
  6.  
  7. (doc/'.linkblock').each do |lb|
  8.     title = (lb/'//a[@rel="bookmark"]').first.inner_html # xpath as well! Get the title of the bookmark
  9.     rating = (lb/'a.upcount').first.inner_html           # the first 'a' element with styleclass 'upcount'
  10.     details = (lb/'p.fineprint')[1].inner_html           # the second 'p' element with styleclass 'fineprint'
  11.     puts "#{title} => rating: #{rating}, details: #{details}"
  12. end

Which will give a list of the links which I recently posted on DZone and some details like ratings:

Using Rails without a database => rating: 4, details: Submitted: Dec 05 / 07:26. Views: 63, Clicks: 24
Using propertyMissing to enhance Date (in Groovy) => rating: 3, details: Submitted: Dec 02 / 16:37. Views: 56, Clicks: 9
Extending LDAP in Java with JLDAP => rating: 2, details: Submitted: Nov 16 / 17:11. Views: 99, Clicks: 30
CPD with maven2 and PMD => rating: 1, details: Submitted: Nov 14 / 15:12. Views: 66, Clicks: 9
multiproject maven2: getting the site to work => rating: 2, details: Submitted: Nov 14 / 14:47. Views: 86, Clicks: 22
Getting started with multiproject maven2 => rating: 12, details: Published: Nov 14 / 19:32. Views: 614, Clicks: 302
Getting started with JBoss Seam (using seam-gen) => rating: 6, details: Published: Oct 20 / 12:23. Views: 961, Clicks: 408
Starting with JBoss Seam (using seam-gen) => rating: 1, details: Submitted: Oct 18 / 14:35. Views: 184, Clicks: 40
Howto: package level annotations => rating: 2, details: Submitted: Oct 13 / 15:28. Views: 155, Clicks: 29
How to start doing behaviour driven development using RSpec => rating: 6, details: Published: Oct 12 / 07:39. Views: 747, Clicks: 393
Testing a Grails taglib using XmlSlurper => rating: 6, details: Submitted: Oct 02 / 15:11. Views: 135, Clicks: 17
LazyList implementation for Googles' Collection Library => rating: 12, details: Published: Oct 04 / 07:48. Views: 639, Clicks: 303
Quick look at the Google Collection Library => rating: 13, details: Published: Oct 01 / 01:00. Views: 1752, Clicks: 1088
Getting started with TDD in Grails => rating: 12, details: Published: Sep 28 / 03:48. Views: 514, Clicks: 125
ActiveForm plugin released => rating: 3, details: Submitted: Sep 25 / 02:33. Views: 97, Clicks: 22
Quick take on Wicket Web Beans => rating: 9, details: Published: Sep 15 / 06:28. Views: 604, Clicks: 249
Unittesting e-mail sending in Spring => rating: 3, details: Submitted: Aug 29 / 03:37. Views: 86, Clicks: 20
Exposing services to SOAP in Grails => rating: 8, details: Published: Aug 31 / 12:12. Views: 559, Clicks: 143

Wow, with only 6 lines of actual code! Great! For more tips and tricks I really recommend the HPricot website, the documentation is superb.

No comments

Using Rails without a database

Today I was fiddling with Rails to create a simple web UI on top of some REST services. Therefor I didn't really need (or is it really didn't need) a database server. Just writing and running Rails code without a database isn't a problem. Running unittests (and rspec tests) however is a problem. By default the unittest helpers used by the generated tests try to initialize a database connection.

Rails Recipes to the rescue. Recipe 14 ('Rails without a database') explains how to modify the helpers and environment configuration to get everything to work without a database and ActiveRecord. Kudos for pointing this out to Levi!!.

Getting the tests to work
open /test/test_helper.rb and replace it's content with

  1. ENV["RAILS_ENV"] = "test"
  2. require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
  3. require 'application'
  4. require 'test/unit'
  5. require 'action_controller/test_process'
  6. require 'action_web_service/test_invoke'
  7. require 'breakpoint'

Getting the tests to work with Rake
In this case the solution proposed in Rails Recipes actually didn't work; it referenced a non-existing 'lookup' method on the Rake::Task class. That wasn't to hard to solve.
In /lib/tasks/ create a file called 'clear_database_prerequisites.rake' and insert the following code:

  1. [:test_units, :test_functional, :recent].each do |name|
  2.   Rake::Task[name].prerequisites.clear # I replaced 'Task.lookup(name)' by Task[name]
  3. end

According to Rails Recipes this single line looks up each Task using Rake’s API and clears the Task’s dependencies. Now according to Rails Recipes running 'rake test' should work as well; in my case it didn't I had to disable loading ActiveRecord.

Disabling ActiveRecord altogether
It is possible to disable loading ActiveRecord by making a simple modification to '/config/environment.rb':

  1. # Skip frameworks you ' re not going to use
  2. config.frameworks -= [ :active_record ]

This was a bit complexer then I'd hoped, but I'm glad to be able to write AND run tests again!

Removing database.yml
After doing all of the above I found the following ticket in Rails' issuetracker: http://dev.rubyonrails.org/ticket/7868 which claims that the convention is to remove your database.yml after which Active Record assumes it's inactive. I tried it, but it doesn't seem to work like that:

  1. bash>rake test               
  2. (in /Users/peter/Development/rails/tttt)
  3. /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb" 
  4. /opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb" 
  5. No such file or directory - /Users/peter/Development/rails/tttt/config/../config/database.yml
  6. ...

but this might be related to my setup. I'll stick with the Rails Recipes solutions for now.

2 comments

Using propertyMissing to enhance Date (in Groovy)

In my previous post I had a go at dates and ranges in Groovy. I wasn't to enthusiastic about the fact that it felt a bit verbose and Java-ish. So I took the opportunity to have a go at the new meta-programming stuff in Groovy >= 1.1beta3.

Say I want to be able to do something like this (the properties being Calendar fields):

  1. d = new Date()
  2.  
  3. println d.dayOfWeek
  4. println d.hourOfDay
  5. println "$d.hourOfDay:$d.minute"
  6.  
  7. etc...

For me, this seemed like a good case to implement using methodMissing and/or propertyMissing. I came up with the following:

  1. Date.metaClass.propertyMissing = { String name ->
  2.     wrd = name[0].toUpperCase() + name[1..<name.size()]  // dayOfWeek ==> DayOfWeek
  3.     split = wrd =~ /([A-Z][a-z]+)// collect words
  4.     par = split.collect{it}?.join('_').toUpperCase()  // ["Day","Of","Week"] ==> DAY_OF_WEEK
  5.  
  6.     try{
  7.         con = Calendar."$par"  // see if the field exist in calendar, throws MissingPropertyException if not
  8.         Date.metaClass."get$wrd" <<{  // register the 'get' method ==> getDayOfWeek()
  9.             cal = Calendar.instance  // obtain a Calendar instance                           
  10.             cal.time = delegate  // set the time to the current instance
  11.             return  cal.get(con)  // get the field
  12.         }
  13.     }
  14.     catch(MissingPropertyException mpe){
  15.         throw new MissingPropertyException(name, delegate.class) // wrap the exception
  16.     }
  17.  
  18.     delegate."get$wrd"()
  19. }

Performance-wise (tips, anyone?) this might not be the best solution (the documentation is actually a bit scarce in this point) but now I can remove the dreaded calendar object from my previous code:

  1. sdf = new java.text.SimpleDateFormat("dd-MM-yyyy")
  2. bd_daan = sdf.parse("23-01-1980")   
  3. bd_peter = sdf.parse("18-01-1979")
  4.  
  5. (bd_peter..bd_daan).each{ d ->
  6.     if(d.dayOfWeek == Calendar.SUNDAY)
  7.         println sdf.format(d)
  8. }

Feels better, doesn't it? Now... what about adding a 'parse' and 'format' method to Date... next time ;)

No comments

Ranges with dates (in Groovy)

During my previous ramblings with Groovy I didn't touch anything fancy in the Date/Calendar API; no need for it. But after reading a lengthy blogpost on the topic I just needed to have a look at it.

Consider the following code:

  1. sdf = new java.text.SimpleDateFormat("dd-MM-yyyy")
  2.  
  3. bd_daan = sdf.parse("23-01-1980")   
  4. bd_peter = sdf.parse("18-01-1979")
  5.  
  6. t = Calendar.instance 
  7. all_sundays = (bd_peter..bd_daan).findAll{
  8.     t.time = it;
  9.     t.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
  10. }
  11.  
  12. all_sundays.each{
  13.     println sdf.format(it)
  14. }

It prints a list of all Sundays between my and my wifes' birthday. Most of the 'magic' is done by the range operator (..) on which one can call all collection operations.

I somehow expected a more groovy like interface to the Calendar object; t.get( xxx ) just doesn't feel Groovy. Might be a nice little project to use method missing to make calls like t.dayOfWeek to work... maybe tomorrow ;)

9 comments