Friday, November 27, 2009

Groovy 1.7-RC1 Released / Groovy 1.7 Before Christmas

Groovy 1.7 RC1 has just been released and the final Groovy 1.7 is to be released by Christmas. By far my favourite new feature is the ability to add annotations on import statements. This allows you to put your @Grape() calls at the import statement that actually loads the packages grabbed which is where it should be IMHO.

While I'm still wrestling with the correct model for packaging 'grapes' in a production environment, as a veteran Perl coder I think grape will take Groovy to another level; It finally makes the Java ecosystem accessible and leveraging other's work easy. Now we just need a repository and IDE's that are grape aware :)

Labels:

Friday, November 20, 2009

SQLite vs Derby / Derby SQLite Comparison

I stumbled upon this wonderful comparison of Derby and SQLite. It even contains some details on SQLite's concurrency model that I had been looking for.

Tuesday, November 17, 2009

Groovy JSON / Using json-lib with Groovy / Using json_simple with Groovy

After a bit of research I've concluded there are two clear options for using JSON with groovy.

1) Using json-lib . Note the annotation is Groovy 1.7 syntax and it rocks.

import net.sf.json.groovy.*
@Grab(group='net.sf.json-lib', module='json-lib', version='2.3', classifier='jdk15')

def books = builder.books {
book(title: "Groovy in Action", author: "Dierk Koenig")
}

assert books.toString() == '''{"books":{
"book":{"title":"Groovy in Action","author":"Dierk Koenig"}}}'''

2) Using the json_simple library .

import org.json.simple.JSONValue;
def response = [ : ];

response.error = [ message : "Not authorized" ];
def jsonString = JSONValue.toJSONString( response );
def deserializedResponse = JSONValue.parse( jsonString );
I prefer 2) but to be honest I haven't looked into Builders much. There's a simple analogy between Groovy datastructures and JSON. Why complicate it any further?

Thursday, November 5, 2009

The Groovy Cookbook

I just stumbled upon an incredible resource that I wish I knew about sooner. PLEAC, short for Progamming Language Example Alike Cookbook provides the solutions to the problems contained in the respected Perl Cookbook using OTHER langues, including Groovy. This is a terrific idea and if my favourite two languages weren't already complete I'd be eager to contribute.

Here's the Groovy table of Cotents. Learn how to deal with:
1. Strings (100.0%)
2. Numbers (100.0%)
3. Dates and Times (100.0%)
4. Arrays (100.0%)
5. Hashes (100.0%)
6. Pattern Matching (100.0%)
7. File Access (100.0%)
8. File Contents (100.0%)
9. Directories (100.0%)
10. Subroutines (100.0%)
11. References and Records (100.0%)
12. Packages, Libraries, and Modules (100.0%)
13. Classes, Objects, and Ties (100.0%)
14. Database Access (100.0%)
15. User Interfaces (100.0%)
16. Process Management and Communication (100.0%)
17. Sockets (100.0%)
18. Internet Services (100.0%)
19. CGI Programming (100.0%)
20. Web Automation (100.0%)

Tuesday, November 3, 2009

JAX-RS, Jersey and Groovy

Groovy makes writing multi-threaded daemon a snap. While searching for a way to control such daemon simply, a co-worker suggested I use JAX-RS. More specifically, he recommended Jersey, sun's reference implementation of JAX-RS or JSR 311.

Jersey made it extremely easy to implement a RESTful web service inside my Groovy based daemon in order to control it while running. Groovy's dynamic class loading nature did case some issues with the resource file. In the end it was easy to overcome though.To get up an running quickly follow the getting started guide. You can create a groovy class which uses the Resource Class annotations they show. Then inside your main application you can spawn the webservice like so:


def resourceConfig = new DefaultResourceConfig( JSONRPCService.class )
HttpServer myServer = HttpServerFactory.create("http://localhost:9090/", resourceConfig );
myServer.start();


Notice that we have to create resource config object and pass it to the HttpServerFactory. Thats the key, since under groovy the server factory can not seem to find the resource on its own.

That's it. Next post I will how how to create an JSON-RPC interface using Jersey and JSON-lib.