users@jersey.java.net

Re: Does jersey provide a startup hook?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 10 Aug 2007 13:36:40 +0200

Hi Aditya,

Aditya Gore wrote:
> Hi,
>
> I am trying to write a simple AddressBook app that is modeled on the
> SimpleStorageService example. While browsing through the
> SimpleStorageService I noticed that the MemoryStore used for holding
> the created containers and objects holds a static reference to itself.

Right, it is a bit of a hack to show a RESTful interface similar to the
Amazons S3 service.


> I am wondering if there's a better way to do this in my AddressBook
> app. To be specific, I want a place where I can create an
> AddressBookStore object once during startup and inject it into my
> resource classes. Is there some kind of startup hook that jersey
> provides for doing this?
>

Unfortunately we do not have any mechanism for application-specific
injection. But, Marc is working on a pluggable mechanism for resource
life-cycle and injection and we may be able to consider that as a use-case.

One way you could do this today is use the same pattern as that of
Hudson. Part of JAX-RS and Jersey is modeled on Stapler the foundation
that Hudson uses.

You need a singleton root resource that is responsible for controlling
the life-cycle of the sub-resources and so on down the hierarchy.

@UriTemplate("/")
class Root {

    AddressBooks r;

    Root() {
       r = ...
    }

    @UriTemplate("books")
    AddressBooks getBooks() {
       return r;
    }
}

class AddressBooks {
    AddressBook[] r;

    AddressBook() {
       r = ...
    }

    @UriTemplate("{book}")
    AddressBook getBook(@UriParam("book") int book) {
       return r[i];
    }
}

So when the URI path '/books/1' is processed an instance of AddressBook
will be returned to the Jersey runtime and an HTTP request will be
dispatched to that instance.

Thus it is possible to say get information from a file system or
database and cache the resources in memory. Of course one needs to
manage states changes correctly...

Another way is for the Root resource to instantiate the address book
info object and pass this as a construction parameter to the sub-resources.

Hope this helps,
Paul.

> regards,
> :aditya
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109