Groovy

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

Using Textile with Grails

During the process of writing my first Grails application, Groovyquiz.com (coming soon), I needed a clean way to write the quiz text, and was generally not interested in writing everything as HTML.

I have always liked Textile, and quickly found a couple of options for using that format via two Java libraries: JTextile and textile4j. Now, I don’t know about you, but when I need a Java library of any nature, I tend to go with one that has most and preferably all of the following:

  • Jar file
  • JavaDocs
  • Example code
  • README file
  • Tests

When I don’t see those, I generally disregard the project and try to find something else. That was the case with JTextile… the download included 3 files: one java source file (JTextile.java), one text file for testing, and another java source file for running the test (Test.java). Where as textile4j had all of the features I listed above.

So, after deciding that I would use textile4j to Textilize my app, I followed these steps to get everything set up.

1. Copy textile4j jar to my

<grails-root>/lib

folder.

2. Created a

TextileCodec.groovy

file in

<grails-root>/grails-app/utils

folder with the following contents:

  1. import net.sf.textile4j.Textile
  2. class TextileCodec {
  3.   static encode = { str ->
  4.         return new Textile().process(str)
  5.   }
  6. }

3. Used it in my code! I needed it for a view:

  1. ${quiz.problem.encodeAsTextile()}

Simple as that!

General
Grails
Groovy
Software Development

Comments (3)

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