Archives for May 2010
Griffon at Chicago Groovy Users Group
Next month, on June 8th, I'll be presenting at the Chicago Groovy Users Group on Griffon. I'll be covering the basics of Griffon development and give some tips on how to port legacy applications from Java. Come one, come all.
You can sign up to attend here.
Motej with Groovy
In my presentation at GeeCon 2010, I showed how you could use Groovy to interact with the Nintendo Wiimote, here is the code and a little explanation from one of my demos.
- Install OS specific native Bluetooth libraries
- Grab the jars for the Java JSR 82 implementation. Either BlueCove or Aventa. Note that Aventa only has a 14day trial and BlueCove requires linking to an extra gpl jar if you use it on Linux. Mac users on Snow Leopard will have to compile Bluecove from source.
- Grab slf4j-api-1.5.8.jar and slf4j-simple-1.5.8.jar.
Open a Groovy console and add all the jars (bluecove-2.1.0.jar, bluecove-gpl-2.1.0.jar [if on Linux] and the slf4j jars) to the classpath using Script -> Add Jar to classpath.
Copy the following code into the console.
import motej.*
import motej.event.*
def listener = [moteFound:{Mote mote ->
println("Found mote: " + mote.getBluetoothAddress())
mote.setPlayerLeds([false, true, false, true] as boolean[])
mote.rumble(2000l)
Thread.sleep(10000l)
mote.disconnect()
}
] as MoteFinderListener
MoteFinder finder = MoteFinder.getMoteFinder()
finder.addMoteFinderListener(listener)
finder.startDiscovery()
Thread.sleep(30000l)
finder.stopDiscovery()
The above code starts discovery for controllers, registers a listener that rumbles the wiimote and changes the LEDs, and disconnects and stops discovery after a delay. You can activate your Wiimote for discovery by pressing the 1 & 2 buttons at the same time.
You can also download the source and libs in a ready-made project here.
MongoSF videos
If you weren't able to attend MongoSF, you can now view the videos and slides from some of the sessions here. All the videos haven't been uploaded yet so check that link and often over the next week or so to see what's new.
Below is the video from my afternoon session. The morning session went without a hitch so karma fated this one to have technical difficulties (oh well). Enjoy!
Using MongoDB with Morphia and Groovy
As I mentioned in this post, I live coded a blog site using Morphia and MongoDB in my workshop session at MongoSF. I used Grails as the platform to quickly scaffold the application. One might wonder why I didn't use the Grails plugin. I used Morphia because it aligns with traditional Java paradigms and didn't want to have too many new items by using a full Groovy stack.
Setup
After installing MongoDB, you will need a version of the Java driver and the Morphia library. Make sure to use the latest snapshot.
Creating the mapped objects
Morphia uses Java annotations to describe how to persist objects in the database. The following annotations are available:
- @Id - marks the MongoDB id field to be autogenerated
- @Entity - marks the class to be persisted as an object and optionally the collection
- @Embedded - tells Morphia to embed this object in another
- @Reference - analogous to MongoDB DBRef
- @Indexed - prepare an index for this property
- @Serialized - store as BSON
- @Property - indicates that the property name in MongoDB will be different than in the object
- @Transient - doesn't persist the property
As you may already know, Groovy adds a few properties to objects. You will get an error that it won't persist the unmapped fields but it doesn't inhibit execution. Our BlogEntry class contains a title field, date it was created, content, a MongoDB document id and an embedded list of comments.
import com.google.code.morphia.annotations.*
@Entity
public class BlogEntry implements Comparable{
@Id private String id
String title
Date dateCreated = new Date()
String content
@Embedded
List<comment> comments = [ ]
}
Similarly, our Comment object contains properties for the name, dateCreated, and content. Please note that both places that refer to Comment need the @Embedded annotation.
import com.google.code.morphia.annotations.*
@Embedded
public class Comment {
String name
Date dateCreated = new Date()
String content
}
While you can marshal/unmarshal objects from POJO/POGOs to BasicDBObjects and persist them to a collection by hand, it is much simpler to use a data access object(DAO) which provides all the CRUD functions for you.
import com.mongodb.Mongo
import com.google.code.morphia.*
public class EntryDAO extends DAO {
public EntryDAO(Morphia morphia, Mongo mongo) {
super(mongo,morphia, "entries")
}
}
In this snippet, I initialize a DAO object, create BlogEntry and Comment objects, persist them to the DAO, and read them back out to prove they were stored. Please excuse the Java-ness of the code but I didn't want to blow people's minds with too much Groovy.
import com.mongodb.*
import com.google.code.morphia.Morphia
mongo = new Mongo()
morphia = new Morphia()
dao = new EntryDAO(morphia, mongo)
entry = new BlogEntry()
entry.setTitle("Test Entry")
entry.setDateCreated(new Date())
entry.setContent("This is a test entry.")
comment = new Comment()
comment.setDateCreated(new Date())
comment.setContent("Test comment")
entry.comments = []
entry.comments.add(comment)
dao.save(entry)
entries = dao.find()
entries.each {
println it.properties
}
With a running instance of MongoDB, you could run all these bits in a Groovy console and see it in action. In the next post, I'll cover how to integrate these classes into a Grails application.
Java at MongoSF
As has come to be expected the 10gen-ers have put on a great conference. It's almost like a MongoDB world domination tour with the NoSQL Live in Boston this March, MongoSF and MongoNYC this month and MongoEU this summer. There seemed to be bigger numbers of Ruby and Python devs but that wasn't so surprising given that they tend be early adopters compared to Java devs. Because of that I believe the path to reach Java developers is to show them that they don't have to throw away all their code to use MongoDB. My morning session capitalized on that and used Grails to show how you could quickly wire up persistence using DAOs with Morphia and MongoDB.The afternoon session that was a bit more high level primer for what was covered in the morning.