package org.jboss.webservice.handler;
import org.jboss.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.handler.Handler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.soap.SOAPFaultException;
public class HandlerWrapper implements Handler
{
private static Logger log = Logger.getLogger(HandlerWrapper.class);
public final static int DOES_NOT_EXIST = 0;
public final static int METHOD_READY = 1;
private static String[] stateNames = new String[]{"DOES_NOT_EXIST", "METHOD_READY"};
private Handler delegate;
private int state;
public HandlerWrapper(Handler handler)
{
delegate = handler;
state = DOES_NOT_EXIST; }
public int getState()
{
return state;
}
public String getStateAsString()
{
return stateNames[state];
}
public QName[] getHeaders()
{
return delegate.getHeaders();
}
public void init(HandlerInfo config) throws JAXRPCException
{
log.debug("init: " + delegate);
delegate.init(config);
state = METHOD_READY;
}
public void destroy() throws JAXRPCException
{
log.debug("destroy: " + delegate);
state = DOES_NOT_EXIST;
delegate.destroy();
}
public boolean handleRequest(MessageContext msgContext) throws JAXRPCException, SOAPFaultException
{
if (state == DOES_NOT_EXIST)
{
log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleRequest for: " + delegate);
return true;
}
try
{
return delegate.handleRequest(msgContext);
}
catch (RuntimeException e)
{
return handleRuntimeException(e);
}
}
public boolean handleResponse(MessageContext msgContext)
{
if (state == DOES_NOT_EXIST)
{
log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleResponse for: " + delegate);
return true;
}
try
{
return delegate.handleResponse(msgContext);
}
catch (RuntimeException e)
{
return handleRuntimeException(e);
}
}
public boolean handleFault(MessageContext msgContext)
{
if (state == DOES_NOT_EXIST)
{
log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleFault for: " + delegate);
return true;
}
try
{
return delegate.handleFault(msgContext);
}
catch (RuntimeException e)
{
return handleRuntimeException(e);
}
}
private boolean handleRuntimeException(RuntimeException e)
{
if ((e instanceof SOAPFaultException) == false)
{
log.warn("RuntimeException in handler method, transition to DOES_NOT_EXIST");
destroy();
}
throw e;
}
public int hashCode()
{
return delegate.hashCode();
}
public String toString()
{
return "[state=" + getStateAsString() + ",handler=" + delegate + "]";
}
}