Search code examples
javagoogle-app-engineservletsguice

Guice Singleton Servlet Binding work-around for third-party servelets


I'm trying to figure out how to Singleton bind a servlet for my code:

public class GuiceServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("org.restlet.application", "com.mycomp.server.RestletApplication");
        serve("/rest/*").with(org.restlet.ext.servlet.ServerServlet.class, params);
        serve("/remote_api").with(com.google.apphosting.utils.remoteapi.RemoteApiServlet.class);
    }
}

The problem here is that both servelets the app needs to serve is a third-party library (Restlet and GAE).

The exception thrown is:

[INFO] javax.servlet.ServletException: Servlets must be bound as singletons. Key[type=org.restlet.ext.servlet.ServerServlet, annotation=[none]] was not bound in singleton scope.

How can I deal with this when the servlets is a third-party library that can't be modified at least at this point. Is there a work-around to make this work?


Solution

  • The solution is to add:

        bind(RemoteApiServlet.class).in(Scopes.SINGLETON);
        bind(ServerServlet.class).in(Scopes.SINGLETON);