March 2007

GroovyQuiz.com is finally here!

Earlier today I put GroovyQuiz.com online, along with its first quiz (Anyone up for a game of Life?)

It amazing to see the response its getting already… there are a number of sites linking to it already (Thank you aboutGroovy.com and groovyblogs.org… you both rock!). And I’ve got a number of solutions (one in under 3 hours!) to post as soon as I finish up that section of the site.

General
Grails
Groovy
Software Development

Comments (3)

Permalink

Creating an RSS feed in Grails

I am in love with Groovy’s Builders… they make working with heirarchical structures a breeze. For instance, I needed to create an RSS feed for GroovyQuiz.com, and the builders make that so easy:

  1. // … in a Controller somewhere far far away …
  2.         def rss = {
  3.                 // render some XML markup to the response
  4.                 def url = “http://groovyquiz.com/groovyquiz/main/quiz”
  5.                 render(contentType:“text/xml”) {
  6.                         rss(version: “2.0″) {
  7.                                 channel {
  8.                                         title “GroovyQuiz.com - Latest Quizzes”
  9.                                         link “http://groovyquiz.com”
  10.                                         description “Weekly Groovy language challenges to help you learn”
  11.  
  12.                                         for(quiz in Quiz.latestActive())
  13.                                                 item {
  14.                                                         title   quiz.title
  15.                                                         link    “${url}/${quiz.number}”
  16.                                                         description     quiz.description
  17.                                                         pubDate quiz.publishedDate()
  18.                                                         guid    “${url}/${quiz.number}”
  19.                                                 }
  20.  
  21.                                 }
  22.                         }                     
  23.                 }            
  24.         }

General
Grails
Groovy
Java
Software Development

Comments (1)

Permalink

Using Groovy to monitor CruiseControl builds

So, I am starting up a new project, and wanted to have a more visible indicator of what my Continuous Integration builds were doing. I am sticking with CruiseControl for this project, even though I recently looked at Continuum, and must say that it is really nice. I probably will start using that more and more as time goes on. But there is also a Ruby version of CruiseControl in the works, so one never knows…

Anyway, for this project it is good ol’ CruiseControl. And, I wanted to monitor it. There are plenty of monitors out there, but I was looking for something in particular. I wanted a big visible sign that something went wrong. The CC server, a desktop PC that will also server as first-stage test server and DB, is sitting in the same area as my partner and I. So, we will be able to see the screen when something goes wrong. And it will provide a cool green glow when everything is alright.

I decided to write my own monitor, after seeing that CruiseControl has a Socket interface for publishing statuses.

While digging around in Groovy in Action (a fabulous book, BTW), I noticed that Groovy scripts can run in interactive mode, so one could write something as simple as this:

groovy -l port -e “System.out.println line”

And one has a simple command line monitor that will just dump the status to the console. Now, this is really no better than doing a:

tail -f cruisecontrol .log

Which it is rather boring… so I decided to use SwingBuilder.

My first experience with SwingBuilder was for my Groovy DSL talk a few weeks ago… I implemented a pseudo-LOGO interpreter on top of Groovy, using SwingBuilder to build my environment. It was the first time in a LONG time that I enjoyed working with Swing.

Here is the code for the monitor:

  1. import java.net.*
  2. import groovy.swing.SwingBuilder
  3. import java.awt.Color
  4. import javax.swing.WindowConstants as WC
  5.  
  6. class Listener {
  7.         def GRAY = new Color(123,123,123,123)
  8.        
  9.         def colorMap = [“Success” : Color.GREEN,
  10.                                “Failure” : Color.RED,
  11.                                “Fixed” : Color.YELLOW]
  12.        
  13.         def swing = new SwingBuilder()
  14.         def frame = swing.frame(title:‘Build Status’,
  15.                                            location:[0,0],
  16.                                            resizable:false,
  17.                                            defaultCloseOperation:WC.EXIT_ON_CLOSE,
  18.                                            background: GRAY) {
  19.                 boxLayout()
  20.                 vbox {
  21.                         panel(  id:‘canvas’, size:[300,600], background: GRAY ) {
  22.                                 vstrut(height:300)
  23.                                 hstrut(width:600)
  24.                         }
  25.             }
  26.         }
  27.                
  28.         def process(socket) {
  29.                 try {
  30.                         socket.inputStream.eachLine { line ->
  31.                                 println “received ‘${line}’ at “ < < new Date().toString()
  32.                                 swing.canvas.background = colorMap[(line =~ /^\w+/)[0]]
  33.                         }
  34.                 }
  35.                 catch(Exception e) {
  36.                         e.printStackTrace()
  37.                         swing.canvas.background = colorMap[“Failure”]
  38.                 }
  39.                 frame.toFront()
  40.         }
  41.        
  42.         def listen(port) {
  43.                 frame.pack()
  44.                 frame.show()
  45.                
  46.                 def server = new ServerSocket(port)
  47.                 while(true) {
  48.                         def socket = server.accept()
  49.                         Thread.startDaemon { process(socket) }
  50.                 }
  51.         }
  52. }
  53.  
  54. new Listener().listen(7600)

The code is fairly straightfoward… open up a ServerSocket listening on a port, waiting for a socket to connect…. when one does, the process method is called and the window’s color is changed to the appropriate color for the status.

Simple, and quick, it didn’t take too long to throw that together, and now I have a tool that can be reused for any other time I use CruiseControl.

Here are the results:

Success!
Screenshot

Failure :(
Failure

Fixed!
Fixed!

Agile
General
Groovy
Java
Software Development
Thought

Comments (0)

Permalink

Groovy DSL presentation now online

Last week I did a presentation for the Omaha Dynamic Language Users Group, entitled DSLs, Groovy, and YOU!. It was a lot of fun, and was well received. When you get down to it, everything we do as programmers is revolves around DSLs. There is definitely a blog post or three in that last sentence.

So, the files are online for the presentation, the handout, and for the Groovy partial-implementation of LOGO that I did for the talk.

General
Groovy
Java
Self
Software Development
Speaking

Comments (2)

Permalink