views:

97

answers:

3

I'm embedding Jetty in a similar manner as described here. When the RequestLogHandler can't open the specified logfile, it throws an exception which is unfortunately caught by org.eclipse.jetty.server.Server and swallowed (but logged first, at least). This means that there's no obvious way for me to tell if the log handler was started correctly.

Is there a way that I'm missing to detect when a handler couldn't start?

A: 

If you get no answer here, you can try write to the users list: [email protected]

From : http://xircles.codehaus.org/projects/jetty/lists

Tom Brito
A: 

How about modifying the jetty code? You could add some simple println statements in strategic places in the RequestLogHandler which would indicate to you whether or not the handler was started.

prometheus
Log statements are generated, so I (a person) am able to tell when a handler can't start. The question is whether this can be done in code.
scompt.com
+2  A: 

This idea is based on the implementation of WebAppContext where you can use WebAppContext.getUnavailableException() to determine whether the context was initialized successfully.

Simply replace the default implementation of Server and Context with your own:

public static class MyContext extends Context {

    private Exception _exception;

    @Override
    protected void doStart() throws Exception {
        try {
            super.doStart();
        } catch (final Exception e) {
            _exception = e;
        }
    }

    @Override
    protected void doStop() throws Exception {
        try {
            super.doStop();
        } finally {
            _exception = null;
        }
    }

    public Exception getException() {
        return _exception;
    }

}

public static class MyServer extends Server implements InitializingBean {

    public void afterPropertiesSet() throws Exception {
        start();

        for (final Handler h : getHandlers()) {
            if (h instanceof MyContext) {
                final MyContext c = (MyContext) h;
                if (c.getException() != null) {
                    throw new RuntimeException("failed to init context " + c.getDisplayName(),
                            c.getException());
                }
            }
        }
    }
}

In your beans.xml, simply replace org.mortbay.jetty.Server (and remove init-method="start") and org.mortbay.jetty.servlet.Context with your own implementations.

This code is for Jetty 6 though (as is the example you linked to), as that's what I have around. I didn't test it though, but it's pretty much the same as we are successfully using in conjunction with WebAppContext. In order to extend this to RequestLogHandler, you could either do the same for just any handler you are using or create a decorator to wrap any handler. You may want to look at org.mortbay.jetty.handler.HandlerWrapper for this purpose.

sfussenegger
I haven't gotten this to work yet, but it seems like the only solution at the moment. It's unfortunately a hack round the (as I see it) bug that exceptions are swallowed when a handler is started.
scompt.com