I have different Axis2 services which expose some methods and would like to wrap those services and all their methods in something like a try catch to be able to filter all occurring exceptions before Axis2 sends them to the client. I already read about phases and flows and handlers and OutFaultFlow and such, but what I would like to have is something on the service level which can be configured by the service itself on startup time. My service should be self contained as much as possible. I already have some things per service which needs to get initialized on startup time and therefore am already implementing the interface ServiceLifeCycle Axis2 provides.
But I can't seem to be able to get from the provided ConfigurationContext or AxisService to something "more higher" around the service and register some new handler for any flow or such in which I could intercept exceptions and decide how to proceed with those.
Is there any way to register flow handlers or such form a service level on startup? Do you have any other idea besides adding try catch in all of my exposed methods?
Thanks!
It's not that hard at all: Just use ConfigurationContext.getAxisConfiguration and AxisConfiguration.getOutFaultFlowPhases to iterate over the registered phases in the startUp method. I currently search for a always present phase like "MessageOut" and simply add a handler with my own logic at the end of it. The logic to register the handler just needs to take into account that another service before could already have registered the handler, therefore first compare the names of all registered handlers with the one you would provide yourself.
That approach guarantees that the handler is only registered on service startup and doesn't introduce any overhead during method invocation at all. But it's global of course.
Another approach I didn't test is to use the MessageContext to get an OutTransport and register a handler on that. That may or may not be message local, but maybe worth a try for somebody.
synchronized static void register(ConfigurationContext context)
{
Phase hookPhase = null;
for (Phase phase: context.getAxisConfiguration().getOutFaultFlowPhases())
{
for (Handler handler : phase.getHandlers())
{
if (WsExceptionFilter.isMe(handler))
{
return;
}
}
if (phase.getPhaseName().equals("MessageOut"))
{
hookPhase = phase;
}
}
if (hookPhase == null)
{
throw new UnsupportedOperationException("Missing hook phase.");
}
hookPhase.addHandler(new WsExceptionFilter());
}