views:

108

answers:

2

I'm trying to use the restlet.org library to build a RESTful web interface, and I've noticed that unlike it's servlet counterpart, it has no complement to GenericServlet.init().

Short of building another servlet to do my startup logic, is there any built-in way to avoid building my database factories and the like on the first request (and all the commensurate locking I'll have to do to avoid multiple initializations)?

+3  A: 

Are you wanting to run it in a servlet container? If not, the documentation shows you how to run it stand-alone:

public static void main(String[] args) throws Exception {  
    // Create a new Component.  
    Component component = new Component();  

    // Add a new HTTP server listening on port 8182.  
    component.getServers().add(Protocol.HTTP, 8182);  

    // Attach the sample application.  
    component.getDefaultHost().attach("/firstSteps",  
            new FirstStepsApplication());  

    // Start the component.  
    component.start();
}  

You could certainly do the initialization there.

If you want to use the servlet approach, try writing a new servlet and extending theirs. Implement your init method and call the one for the super class.

Brad Gardner
Yes, I want to run this from Tomcat or Jboss or whatever. It *looks* like I could subclass org.restlet.ext.servlet.ServerServlet and implement ServerServlet.init()... A might bit annoying IMHO. :-/
Chris Kaminski
A: 

If you really want to do this in a servlet environment, you could potentially have two servlets within your webapp: one for the Restlet application/component and one for your initialization, using load-on-startup (which you wouldn't necessarily map to any URL, as far as I'm aware you don't have to). This way, you wouldn't have to subclass org.restlet.ext.servlet.ServerServlet. I think this is probably easier, since that init servlet would just contain init(), but this would only work for things that do not depend on the Restlet app/component to be initialized first.

<context-param>
    <param-name>org.restlet.clients</param-name>
    <param-value>HTTP HTTPS CLAP FILE</param-value>
</context-param>

<servlet>
    <servlet-name>ExampleInit</servlet-name>
    <servlet-class>example.TestInitServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>Example</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.application</param-name>
        <param-value>example.TestApplication</param-value>
    </init-param>
    <init-param>
        <param-name>org.restlet.autoWire</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>Example</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Alternatively (or perhaps in complement to this), I tend to use JNDI for initializing the database connections and a few other configuration settings. This also allows me to keep the same configuration and loading mechanisms, whether I use a Restlet standalone server or Restlet within a webapp.

For example, for deployment within a servlet container (e.g. Jetty or Tomcat), I use the container's JNDI configuration, but for local tests (with a standalone Restlet app), I use the Jetty JNDI context factory (which you can get as a separate jar from the rest of Jetty).

Bruno