iBrasten

My methods of calculating time are far superior to yours, in every way.

 

This is the blog of Brasten Sager, a freelance software developer, Mariners fan, guitarist, haphazard philosopher.

Jactive! -- ActiveResource for Java

September 15, 2006 @ 07:09 AM

I plan on giving more detail about this later, but I put together a little library for using ActiveResource objects in Java. Actually, I guess ActiveResource is the client API, so it’s more like a Java port of ActiveResource compatible with the Rails REST services.

For the examples below, I’ve hacked out things like the imports and getters/setters. So these obviously won’t compile as-is.

Order.java (Model):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import com.nagilum.jactive.*;
import com.nagilum.annotation.*;

@Resource(resourceName="orders")
public class Order implements ActiveResource {
    
    private @ResourceConnector static Connector connector;
    
    private @Attribute int id;
    private @Attribute String status;
    private @Attribute double totalAmount;
    
    public Order save() throws Exception {
        return (Order) connector.save(this);
    }
    
    public Order delete() throws Exception {
        return (Order) connector.delete(this);
    }
    
    public static Order find(int id) throws Exception {
        return (Order) connector.find(Order.class, id);
    }

    (accessor methods for attributes)
}
Example of Jactive in use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Connector connector = new Connector("http://localhost:3000/");
connector.registerResource(Order.class);

// Finding/Updating works
Order order = Order.find(1);
order.setStatus("PENDING");
order.save();

// Creating new resources works
Order newOrder = new Order();
newOrder.setStatus("NEW");
newOrder.setTotalAmount(150.00);
newOrder = newOrder.save();

// Update new order
newOrder.setStatus("APPROVED");
newOrder.save();

// We're done here.
newOrder.delete();
order.delete();

Coming soon…

To answer a few anticipated questions:

  • Jactive is open-source. I will release the source shortly.
  • All of that actually works right now.
  • I will be adding findAll() soon.
  • You don’t HAVE to implement the ActiveResource interface if you don’t want to (say, if you have existing value objects). If you don’t, the Connector will act as a DAO. Example: connector.save(order);
  • Most importantly, Jactive is NOT attempting to be compatible with some or all hypothetical REST implementations. The purpose of Jactive is to be compatible with Rails’ REST implementation. Period. Hence, Jactive. Not, JEST.

1 Responses to “Jactive! -- ActiveResource for Java”

  1. Hi, do you have any more information about Jactive? I’m looking into connecting Rails apps with Java apps and Jactive looks just like something i need.

    Thanks,

    Jeroen Zwartepoorte

    Jeroen