views:

47

answers:

2

We're looking at technologies for an up and coming project here and I really want to use Guice as our dependency injection framework, I also want to use Hessian for client/server comms, but it doesn't seem to be compatible with Guice.

public class WebMobule extends ServletModule {

@Override
protected void configureServlets() {

    serve("/fileupload").with(FileUploadServlet.class);

    // this doesn't work! AuthenticationServlet extends HessianServlet
    // HessianServlet extends GenericServlet - Guice wants something that extends
    // HttpServlet
    serve("/authentication").with(AuthenticationServlet.class); 

}

Has anyone managed to solve this problem - if so how did you do it?

cheers

Phil

+2  A: 

I would write a custom HessianHttpServlet which extends HttpServlet and delegates method calls to a encapsulated HessianServlet. In this way the Guice serve call will be satiated and you will be using HessianServlet behavior.

Syntax
A: 

It needs some work, but fundamentally I solved the problem with this (thanks Syntax!):

@Singleton
public class AuthenticationWrapperServlet extends HttpServlet {

    private static final Logger LOG = Logger.getLogger(HessianDelegateServlet.class);

    // this is the HessianServlet
    private AuthenticationServlet authenticationServlet;

    @Override
    public void init(ServletConfig config) throws ServletException {
        LOG.trace("init() in");
        try {
            if (authenticationServlet == null) {
                authenticationServlet = new AuthenticationServlet();
            }
            authenticationServlet.init(config);
        } catch (Throwable t) {
            LOG.error("Error initialising hessian servlet", t);
            throw new ServletException(t);
        }
        LOG.trace("init() out");
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

        try {
            authenticationServlet.service(request, response);
        } catch (Throwable t) {
            LOG.error("Error calling service()", t);
            throw new ServletException(t);
        }

    }
}
philc2000