Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 732
quarks Avatar asked Feb 11 '15 07:02

quarks


1 Answers

The solution is to add:

    bind(RemoteApiServlet.class).in(Scopes.SINGLETON);
    bind(ServerServlet.class).in(Scopes.SINGLETON);
like image 169
quarks Avatar answered Nov 06 '22 20:11

quarks