The Kaptain on … stuff

16 Dec, 2009

Jira Grails Plugin

Posted by: TheKaptain In: Development

I set out to explore some of the Atlassian API’s this week, along with a test drive of Jira 4, and ended up developing a simple XML-RPC plugin for Grails. Utilizing little more than the Groovy XMLRPC library I was able to implement a client service for the Jira XmlRpcService in minutes.

Nothing terribly complicated going on for this plugin. Aside from the XMLRPC library and one more jar it depends upon there’s a service which implements calls for all of the methods on the XmlRpcService interface and a ‘helper’ class that makes using it a little easier(removes the need to pass the host name into each method). And any controller injected with the service gets a withJira(hostname, Closure) method which wraps the ‘helper’ class. This might not be the optimal way to do things, but I was looking for some better experience with MOP’ing in Groovy along with this little project so it worked out perfectly for me.

The method added to Controller classes just creates a ‘helper’ and sets it as the delegate for the closure.

[groovy language=”true”]
cClass.metaClass.withJira = {String hostname, Closure closure ->
def helper = delegate.jiraXmlRpcService.createHelper(hostname)
closure.setDelegate(helper)
closure.setResolveStrategy(Closure.DELEGATE_FIRST)
closure()
}
[/groovy]

Calling the withJira method looks like this, along with minimal exception handling:

[groovy language=”true”]
try
{
def projects, priorities, serverInfo
withJira(jiraInstance.baseUrl) {
def token = login(jiraInstance.userName, jiraInstance.password)
serverInfo = getServerInfo(token)
priorities = getPriorities(token)
projects = getProjectsNoSchemes(token)
logout(token)
}
[jiraInstance: jiraInstance, projects: projects, priorities: priorities, serverInfo: serverInfo]
}
catch (e)
{
flash.message = "Failed to load data from ${jiraInstance.baseUrl}"
redirect(action: "list")
}
[/groovy]

The ‘helper’ class itself represents an experiment with the Groovy ‘methodMissing’ behaviour. The idea is to store the ‘hostname’ for a series of calls against the stateless service and add that as the first parameter for all wrapped methods. It was simple and quick, but right off the bat you lose the ability to use IDE syntax completion support on this object. I thought another possibility here would be to use the @Delegate annotation on the service and/or Closure currying to add the additional ‘hostname’ parameter to methods.

[groovy language=”true”]
class JiraXmlRpcServiceHelper
{
def hostname
def JiraXmlRpcService service

def methodMissing(String name, args)
{
assert hostname && service
def method = service.metaClass.methods.find {it.name == name}
if(!method)
{
throw new NotImplementedException("The $name method is not implemented for ${this.getClass()}")
}

def newArgs = [hostname]
args.each{newArgs << it}
return method.invoke(service, newArgs as Object[])
}
}
[/groovy]

This plugin works with Grails 1.2.0.RC1 and represents about 8 hours of work all told – and a lot of fun to make! Jira 4 is not too shappy either, especially with the GreenHopper plugin. But that’s a post for another day.

A special thank you to Grails in Action and this article by Ted Naleid – both were extremely helpful putting this together. If you care to download it and take a spin here it is. Feedback is always welcome!

[download id=”1″]

Reblog this post [with Zemanta]

Lately I’ve been thinking about all the different ways to bring Groovy into a pure Java or command line environment, and ended up diving into some code to explore the various options. Turns out there’s definitely a good variety of options for running Groovy dynamically inside and out of a Java application. I started out on this page from the Groovy site.
In particular for the environments I’ve been working in lately it’s been important to be able to run the same code both from within a Java application and from the command line. It’s also been a ‘nice to have’ to be able to package a jar with a bunch of the same scripts compiled together. Using maven as a harness also has the benefit of allowing for testing compiled scripts directly through instantiation even though the intended usage is from within a Java app using one of these methods. Source code is available here on github.

Groovy on the command line

The quickest and simplest way to run a Groovy Script or Class, command line arguments are automatically marshalled into an ‘args’ String array. Please note that due to a problem I’m having with my syntax highlighter plugin the process execution is shown here in single quotes; the actual code requires a GString(double quoted) in order to do the replacement for the inline variable. " THAT WordPress!
[groovy]
//the script
myArgs = args
result = args.join(‘ ‘)
println result
println myArgs

//…and the test
void testGroovyCall()
{
def proc = ‘groovy $groovyScriptOne Hello World’.execute()
proc.waitFor()
def result = proc.text.split()
assert result[0] == ‘Hello’
assert result[1] == ‘World’
}

[/groovy]

GroovyShell

This is the basis of Groovy script execution. The GroovyShell allows for executing scripts, passing in a particular Binding context that allows for bi-directional communication between the script and the calling code. Parameters can be passed into the executing script in the Binding and results can be stored there to be returned to the calling context. GroovyShell also allows for running a class from the ‘main’ method, passing in String arguments. It will also execute implementers of Runnable and test files for JUnit or TestNG. Script text can also be declared inline and executed in the same way as files on disk. All in all, pretty bloody handy. Here’s a straightforward example of running a dirt simple Groovy script and inspecting the results. Note that this isn’t executable as shown, but I’ll provide the full source code on github for anyone who wants a closer look. Note that I’m also passing in an ‘out’ variable in the Binding, which effectively redirect System.out to a specified Writer implementation – a nice touch for inspecting output.
[groovy language=”true”]
//the script
myArgs = args
result = args.join(‘ ‘)
println result
println myArgs

//…and the test
void testGroovyShell()
{
Binding binding = helper.createBinding()
def shell = new GroovyShell(binding)
shell.evaluate(new File(groovyScriptOne))
helper.assertBinding(binding)
}

//…and the Binding creation/assertion
def static args = [‘Hello’, ‘World’].asImmutable()
/**
* Create a Binding with a single parameter to be passed to scripts and an ‘out’ Writer to redirect console output.
*/
private Binding createBinding()
{
Binding binding = new Binding()
def sWriter = new StringWriter()
def pWriter = new PrintWriter(sWriter)
binding.setVariable (‘args’, new ArrayList(args))
binding.setVariable (‘out’, pWriter)
return binding
}

/**
* Assert that the expected ‘common’ actions are done with the Binding by each of the use cases.
* The original ‘args’ should be as expected.
* A copy of ‘args’ should have been placed in the Binding during execution.
* The ‘result’ should be the concatentation of ‘args’ separated by spaces.
*/
private def assertBinding(Binding binding)
{
assert binding.variables.size() == 4
assert binding.variables.args.value[0].toString() == args[0]
assert binding.variables.args.value[1].toString() == args[1]
assert binding.variables.result.value.toString() == args.join(‘ ‘)
assert binding.variables.myArgs.value[0].toString() == args[0]
assert binding.variables.myArgs.value[1].toString() == args[1]
}
[/groovy]

GroovyScriptEngine

The GroovyScriptEngine enables dynamically running Groovy sources located in a fixed set of content roots, complete with reloading modified scripts in between executions. Running a Groovy script this way is essentially the same as using GroovyShell.
[groovy language=”true”]
void testGroovyScriptEngine()
{
Binding binding = helper.createBinding()
def gse = new GroovyScriptEngine(new File(‘.’).toURL())
gse.run(groovyScriptOne, binding)
helper.assertBinding(binding)
}
[/groovy]

GroovyClassLoader

An extension to URLClassLoader that enables parsing Groovy sources into Class representations. Once a Class object is created, instances of the class can be created easily and either cast to a known type or manipulated through convention by use of the standard Groovy ‘invokeMethod’. This works equally well on Groovy and Java btw. Here’s an example of running a Java class using GroovyClassLoader. In this case the Java file has a field called ‘binding’ and implements a ‘run’ method.
[groovy language=”true”]
/**
* Dynamically compile, instantiate, inspect and call methods on a POJO.
*/
void testGroovyClassLoaderOnJava()
{
GroovyClassLoader loader = new GroovyClassLoader();
Class javaClass = loader.parseClass(new File(javaFileOne));

def groovyObject = javaClass.newInstance();
def binding = helper.createBinding()
groovyObject.binding = binding
if(groovyObject.metaClass.respondsTo(groovyObject, ‘run’))
{
groovyObject.invokeMethod(‘run’, null);
helper.assertBinding(binding)
}
if(groovyObject.metaClass.respondsTo(groovyObject, ‘main’))
{
groovyObject.invokeMethod(‘main’, new ArrayList(helper.args) as String[]);
}
}
[/groovy]

(Groovy)Console

The Console can be embedded in Java or Groovy code to provide a dynamic interactive Swing environment. This is the same UI spawned from the command line invocation of ‘groovyConsole’. Internally it uses GroovyShell for actual execution, and so can do everything that GroovyShell can do – plus a couple of additions. For one, you can add jars and/or directories to the classpath used when executing your scripts.
Groovy Console

The Best of Both Worlds – at Least for my use case

In actual practice these patterns can be used a lot more successfully by observing standard Java practices, like casting classes parsed using GroovyClassLoader to a known interface before interacting with them, or by using Classes to organize business logic inside of a Script that essentially functions as a ‘main’ method. This example defines two dependent internal classes, marshals parameters to them and then returns the results attached to the originally passed in Binding.
[groovy language=”true”]
/**
* Classes inside of a Script.
*/
class TestableClass
{
Binding binding

def run()
{
binding.with
{
setVariable(‘myArgs’, getVariable(‘args’))
setVariable(‘result’, getVariable(‘args’)?.join(‘ ‘))
}
return binding
}
}

class TestableClass2
{
Binding binding

public TestableClass2(Binding binding)
{
this.binding = binding;
}

def run()
{
return new TestableClass(binding: binding).run()
}
}

if (args)
{
def internalBinding = new Binding()
internalBinding.setVariable(‘args’, new ArrayList(args))
internalBinding = new TestableClass2(internalBinding).run()
args = internalBinding.args
myArgs = internalBinding.myArgs
result = internalBinding.result //return value from script
}
else
{
println ‘no args!!’
}
[/groovy]

[table id=1]

Reblog this post [with Zemanta]

25 Oct, 2009

Grails-UI DataTable using XML for a model

Posted by: TheKaptain In: Development

I had a great chance to play with some new stuff this weekend, namely Grails 1.2-M3, the new Grails REST plugin and Groovy in general. Awhile back I wrote some Groovy code to explore the various Jira, Fisheye and Crucible REST api’s using Apache HttpClient directly, and I wanted to see what it would like abstracted behind HTTPBuilder. With the help of the Grails-UI and Google-charts plugins, I got some pretty nice views of the data as well.

First off, dealing with a RESTful call was about as simple as I’ve ever seen. This is the entire code that deals with creating the connection, authenticating, executing a GET, and parsing the returned XML into a ‘data’ property on the response.
[groovy language=”language=”]
private withFisheye(String url, Map args) {
withRest(uri: url) {
auth.basic userName, password
return get(query: args)
}
}
[/groovy]
Getting XML using Andres’ plugin is extremely simple and we can achieve some pretty nice visualizations of the resulting data very easily as well. In particular I wanted to share how to use an XML response as a backing model for DataTable. The excellent examples provided by Matthew Taylor on his blog seemed to concentrate on using GORM to load and sort domain objects to utilize as data models. When using XML as an alternative way to represent the data, and using RESTful calls to populate that data, the available sorting options on a DataTable weren’t able to function properly without a little extra help.

Consider an list of XML elements like this, which represents a single review in Crucible.
[xml language=”true”]
<reviewdata>
<allowreviewerstojoin>false</allowreviewerstojoin>
<author>
<displayname>Kelly Robinson</displayname>
<username>TheKaptain</username>
</author>
<closedate>20010101T00::00.0000000</closedate>
<createdate>20010101T00::00.0000000</createdate>
<creator>
<displayname>Kelly Robinson</displayname>
<username>TheKaptain</username>
</creator>
<description>
Test review
</description>
<moderator>
<displayname>Kelly Robinson</displayname>
<username>TheKaptain</username>
</moderator>
<name>
Test review
</name>
<permaid>
<id>CR1</id>
</permaid>
<projectkey>CR</projectkey>
<state>Closed</state>
<summary>
Test review
</summary>
</reviewdata>
[/xml]
You can leverage this list as a DataTable model by extracting specific elements from the XML, and providing a sorting mechanism based on dynamic map value lookups.
[groovy language=”true”]
private def createReviewDataJson(HttpServletResponse response, Map params, reviews) {
def list = []
response.setHeader("Cache-Control", "no-store")
//build a List of Maps, one for each row in the DataTable
reviews.reviewData.each {
list << [
id: it.permaId.id.text(),
author: it.author.displayName.text(),
creator: it.creator.displayName.text(),
moderator: it.moderator.displayName.text(),
description: it.description.text(),
state: it.state.text()
]
}
//sort by the ‘order’ and ‘sort’ parameters passed in by DataTable
switch (params.order) {
case ‘asc’:
list.sort { it."${params.sort}" } //sort by value where key == params.sort
break
case ‘desc’:
list.sort {a, b -> b."${params.sort}" <=> a."${params.sort}" } //reverse sort by value
break
}
//the entire model
def data = [totalRecords: list.size(), results: list]
render data as JSON
}
[/groovy]

This model can then be rendered using the tag. Note that this is pretty much a direct copy of a provided example, nothing fancy here.
[xml language=”language=”]
<gui:datatable id="statDataTable" draggablecolumns="true" columndefs="[
[id:’id’, sortable:true, resizeable:true],
[author:’author’, sortable:true, resizeable:true],
[creator:’creator’, sortable:true, resizeable:true],
[moderator:’moderator’, sortable:true, resizeable:true],
[description:’description’, sortable:true, resizeable:true, formatter:’textarea’],
[state:’state’, sortable:true, resizeable:true]
]" paginatorconfig="[
template:'{PreviousPageLink} {PageLinks} {NextPageLink} {CurrentPageReport}’,
pageReportTemplate:'{totalRecords} total records’
]" sortedby="id" controller="fisheye" action="${action}" resultslist="results" rowsperpage="10">
[/xml]

So for very little work you get a sortable, paginated DataTable, complete with column resizing and dragging. The DataTable is also being rendered using a template, allowing it to be used arbitrarily by controller methods that populate the model with different reviews. Pretty cool if you ask me. I also used the same backing XML model to drive some PieCharts, but that’s perhaps a topic for another post.

πŸ™‚

24 Oct, 2009

Groovy reverse map sort done easy

Posted by: TheKaptain In: Development

Sorting a Map by value with Groovy is very simple, due to the added ‘sort’ method on the Map interface.
[groovy language=”language="true”]
def map = [a:3, b:2, c:1]
map = map.sort {it.value}
assert map == [c:1, b:2, a:3]
[/groovy]

Turns out doing a reverse sort on a Map by values is almost as easy.
[groovy language=”language="true”]
def map = [a:1, b:2, c:3]
map = map.sort {a, b -> b.value <=> a.value}
assert map == [c:3, b:2, a:1]
[/groovy]

Using the spaceship operator (‘< =>‘) you can define a very terse syntax for accessing ‘compareTo’, as long as the values in the map implement Comparable that is.
Nice!

11 Oct, 2009

Twitter Weekly Updates for 2009-10-12

Posted by: TheKaptain In: TweetTweet

Powered by Twitter Tools

04 Oct, 2009

Twitter Weekly Updates for 2009-10-05

Posted by: TheKaptain In: TweetTweet

  • RT @alphasimple http://alphasimple.com is now available! Please complete survey. Interested in alpha testing?Just drop us a line.RT@abstratt #
  • Is it an omen if your screensaver shows words from a Dictionary and you come back to your desk to find "anomaly" splashed across the screen? #
  • For all the times I'm told to build(another) JTable visualization. RT @kirillcool: And yes, itÒ€ℒs really all Java Swing – http://bit.ly/uRFPE #
  • RT @Rod_Phillips: #yyj 1st 50 replies or RT's gets choice of 1 of the 3 items. Pick up after 6:30pm today. #
  • @Rod_Phillips Royal Oak please πŸ™‚ #
  • @Rod_Phillips Red at Royal Oak tyvm, sir #
  • @Rod_Phillips quality of live FTW! and if it's not too late – Hermans is my fave beer, so if I could get that instead of red? in reply to Rod_Phillips #
  • RT Happy 1st birthday @GroovyMag! (via @hartsock) #

Powered by Twitter Tools