Java

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

Upcoming talks

I’ve got a few talks coming up in the next couple months:

March:

Domain Specific Languages - I’ll be talking to the Omaha Dynamic Language Users Group on March 6th.    I will be focusing on the Groovy language, but may slip in some Ruby and Lisp.

Basic Spring -  This talk will be part of the Omaha Java Users Group March 20th meeting.  It will be a “nothing but Spring” meeting.   Nick Larson will be talking about Spring MVC, whil I will focus on the fundamentals.

April:

This year I will be presenting at Infotec, the local Omaha conference for Information Technologies, put together by the local AITP association.  I have one session and one tutorial:

Agile Java Web Frameworks - sort of an omnibus of the latest frameworks, and of course what a “agile web framework” is.

Introduction to Ruby on Rails - this will be a four, yes FOUR, hour tutorial  using Ruby on Rails.  I am so excited to be doing this!  We will be building an application from scratch, covering most of the features in Rails.
Should be a fun couple of months!

AJAX/Web 2.0
Agile
Java
OJUG
Rails
Ruby
Self
Software Development
Speaking
Thought

Comments (0)

Permalink

Groovy Quiz coming to a website near you!

I’ve been working on my first Grails app off and on since I posted my Groovy peg game code last month.   It is a site dedicated to learning Groovy, using the quiz format in the spirit of Ruby Quiz.

The premise is pretty simple:  every week a new quiz  will  be posted on the website (groovyquiz.com), and interested developers will be able to discuss the quiz (without giving solutions ;)) for 48 hours, and then they can post their solutions on the website.

Personally, I find the quiz format to be a wonderful way to learn a new language.  It gives me focus on a specific task, and how to solve it, versus meandering through the language’s documentation.  I still do a little bit of the latter, as it has always been a good way of learning things about the language that I may not have know before.  Once I figured out a way to make dynamic calls in COBOL using that learning technique, but thats a story for another time.

You can help!

I can use your help coming up with Quizzes, and if someone wants to help moderate the site, I would really appreciate it.

I am thinking that groovyquiz.com should be operational in the next week or two… at which point I will be posting again here and in the Groovy User/Dev mailing lists.

General
Groovy
Java
Software Development

Comments (0)

Permalink