Mark Derricutt's Disturbing Thoughts

My Top Tags

                                       

My Jaiku

The Taste of the Day

Thursday, 24 July 2008 10:28 A GMT+12

War of Ages – The Deception of Strongholds

Wednesday, 23 July 2008 5:41 P GMT+12

"L" is not a code smell

Wednesday, 23 July 2008 8:54 A GMT+12

Meshuggah – Sum

Tuesday, 22 July 2008 4:37 P GMT+12

Dates: Relative or Absolute?

Tuesday, 22 July 2008 8:19 A GMT+12

HTTP errors - a set on Flickr

Monday, 21 July 2008 5:37 P GMT+12

Daylight Dies – Last Alone

Monday, 21 July 2008 3:59 P GMT+12

How I Got Started in Software Development

Sunday, 20 July 2008 6:06 P GMT+12

Soilent Green – In The Same Breath

Sunday, 20 July 2008 3:40 P GMT+12

Search Box

 

Spring + Groovy + Services

posted Tuesday, 26 April 2005
So I've just been playing around with integrating my spring based application, with groovy scripting, nothing emensely fancy, but it's starting to open some interesting ideas.

A simple "Shell" class reads a spring configuration file, and binds all the beans into a Groovy context, then executes the script.

XmlBeanFactory factory = new XmlBeanFactory(new FileInputStream(new File(springFile)));

Binding binding = new Binding();
String[] beans = factory.getBeanDefinitionNames();
for (int i = 0; i < beans.length; i++) {
  String beanName = beans[i];
  binding.setVariable(beanName, factory.getBean(beanName));
}

GroovyShell shell = new GroovyShell(binding);
shell.evaluate(script.toString());

Now, I have a spring XML file lying around for an EJB based application which includes two beans (amongst others): userSearch and sendSMS, these two session beans simply expose the services of the application, given the following wee script:

println "There are ${userSearch.totalUserCount} users in the current system."
println "Sending an SMS message to all admin users..."

userSearch.userAccountSearch(1, 100, "").each {
  if (it.userRole == 1) {
    println "${it.userName} has a phone number of ${it.mobileNo}"
    sendSMS.sendMessage(it.userId,
      "${it.mobileNumber}",
      "Hello ${it.firstName} ${it.lastName} :)",
      "0" )
  }
}

It's a simple script, taking advantage of Groovy's dynamic nature and blocks, Spring's injection of fully configured beans, and the services provided by those beans.

All this is run from a small shell script, which sets up the required classpath, and kicks in the Shell class.

The next step is to get the shell loading the script from STDIN so I can then start writing scripts like:

#!/bin/bmshell

Several benefits I can already see with this is that it highlights areas of the exposed API that somewhat suck and could be improved, and also allows you to easily run adhoc code against a managed system. It's no where near the same as opening a new workspace in Smalltalk and running adhoc code against the live system, but it's getting pretty close.

links: digg this    del.icio.us    technorati    reddit