ActiveResource goodness in Java.
ActiveResource is tailored for consuming services provided by Rails applications. jactiveresource helps you easily consume these same services in your Java based applications.
Ryan Daigle says: “ActiveResource provides a large piece of the REST puzzle by basically implementing the client side of a RESTful system – the parts of a decoupled system that consume RESTful services. At its essence, ActiveResource provides a way to utilize model objects as REST-based client proxies to remote services.”
There's tons of stuff out there to help you create RESTful services in java. But that's only half the battle. I'd rather be using ActiveResource to consume my RESTful services, but when I'm stuck in java, jactiveresource is the next best thang.
Say you built a killer rails application to keep track of people's birthdays. Ok, maybe it's not a killer app, but it's convenient for this example. Now you want to be able to get at those people from a java application you are working on. jactiveresource does all the grunt work for you (except for the getters and setters, those are James Gosling's fault). jactiveresource emphasizes Convention over Configuration and is about as DRY as a staticly typed language like Java can be.
Start by creating a Person class that either subclasses ActiveResource, or implements Resource. Here we choose to implement the interface, which lets us use POJO's for our resource classes:
public class Person implements Resource {
private String id;
private String name;
private Date birthdate;
private Date createdAt;
private Date updatedAt;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
This is one sloppy wet part (very unDRY) of jactiveresource: because you can't create java methods at runtime based on the data you see from the service (this is how ActiveResource does it), you have to define them all up front. These getter/setter names should smell like Rails. In fact, this object matches up perfectly with a person service created with the scaffold generator in rails.
$ script/generate scaffold person name:string birthdate:datetime
In Ruby you can add static methods to your ActiveResource classes at runtime, and Rails makes heavy use of this convenience. In Java, we create a ResourceFactory and a ResourceConnection instead. Here's a ResourceFactory, which magically handles all the dirty work:
ResourceConnection c = new ResourceConnection("http://localhost:3000");
ResourceFactory pf = new ResourceFactory<Person>(c,Person.class);
You're done. Behind the scenes jactiveresource has singularized, dasherized and serialized the rails service so now you can use it. The ResourceFactory has the methods to create and find records, and instances of our Person class have methods to save and delete, like so:
# make the monk
Person p = new Person();
p.setName("Medieval Monk");
p.setBirthdate(new Date(new Long("-99999999999999"));
pf.save(p);
# and get rid of him
pf.delete(p);
There's more examples and stuff inside, and you can also look at the tests in the source code for all the gritty details.