package org.jboss.axis;
import org.jboss.axis.handlers.BasicHandler;
import org.jboss.axis.strategies.InvocationStrategy;
import org.jboss.axis.strategies.WSDLGenStrategy;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.namespace.QName;
import java.util.Enumeration;
import java.util.Vector;
public class SimpleChain extends BasicHandler implements Chain
{
private static Logger log = Logger.getLogger(SimpleChain.class.getName());
protected Vector handlers = new Vector();
protected boolean invoked = false;
private String CAUGHTFAULT_PROPERTY =
"org.jboss.axis.SimpleChain.caughtFaultInResponse";
public void init()
{
for (int i = 0; i < handlers.size(); i++)
((Handler)handlers.elementAt(i)).init();
}
public void cleanup()
{
for (int i = 0; i < handlers.size(); i++)
((Handler)handlers.elementAt(i)).cleanup();
}
private static final HandlerIterationStrategy iVisitor =
new InvocationStrategy();
private static final HandlerIterationStrategy wsdlVisitor =
new WSDLGenStrategy();
public void invoke(MessageContext msgContext) throws AxisFault
{
if (log.isDebugEnabled())
{
log.debug("Enter: SimpleChain::invoke");
}
invoked = true;
doVisiting(msgContext, iVisitor);
if (log.isDebugEnabled())
{
log.debug("Exit: SimpleChain::invoke");
}
}
public void generateWSDL(MessageContext msgContext) throws AxisFault
{
if (log.isDebugEnabled())
{
log.debug("Enter: SimpleChain::generateWSDL");
}
invoked = true;
doVisiting(msgContext, wsdlVisitor);
if (log.isDebugEnabled())
{
log.debug("Exit: SimpleChain::generateWSDL");
}
}
private void doVisiting(MessageContext msgContext,
HandlerIterationStrategy visitor) throws AxisFault
{
int i = 0;
try
{
Enumeration en = handlers.elements();
while (en.hasMoreElements())
{
visitor.visit((Handler)en.nextElement(), msgContext);
i++;
}
}
catch (AxisFault f)
{
if (!msgContext.isPropertyTrue(CAUGHTFAULT_PROPERTY))
{
Message respMsg = new Message(f);
msgContext.setResponseMessage(respMsg);
msgContext.setProperty(CAUGHTFAULT_PROPERTY, Boolean.TRUE);
}
while (--i >= 0)
((Handler)handlers.elementAt(i)).onFault(msgContext);
throw f;
}
}
public void onFault(MessageContext msgContext)
{
if (log.isDebugEnabled())
{
log.debug("Enter: SimpleChain::onFault");
}
for (int i = handlers.size() - 1; i >= 0; i--)
((Handler)handlers.elementAt(i)).onFault(msgContext);
if (log.isDebugEnabled())
{
log.debug("Exit: SimpleChain::onFault");
}
}
public boolean canHandleBlock(QName qname)
{
for (int i = 0; i < handlers.size(); i++)
if (((Handler)handlers.elementAt(i)).canHandleBlock(qname))
return (true);
return (false);
}
public void addHandler(Handler handler)
{
if (handler == null)
throw new InternalException(Messages.getMessage("nullHandler00",
"SimpleChain::addHandler"));
if (invoked)
throw new InternalException(Messages.getMessage("addAfterInvoke00",
"SimpleChain::addHandler"));
handlers.add(handler);
}
public boolean contains(Handler handler)
{
return (handlers.contains(handler));
}
public Handler[] getHandlers()
{
if (handlers.size() == 0)
return null;
Handler[] ret = new Handler[handlers.size()];
return ((Handler[])handlers.toArray(ret));
}
public Element getDeploymentData(Document doc)
{
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("enter00",
"SimpleChain::getDeploymentData"));
}
Element root = doc.createElementNS("", "chain");
StringBuffer str = new StringBuffer();
int i = 0;
while (i < handlers.size())
{
if (i != 0) str.append(",");
Handler h = (Handler)handlers.elementAt(i);
str.append(h.getName());
i++;
}
if (i > 0)
{
root.setAttribute("flow", str.toString());
}
if (options != null)
{
Enumeration e = options.keys();
while (e.hasMoreElements())
{
String k = (String)e.nextElement();
Object v = options.get(k);
Element e1 = doc.createElementNS("", "option");
e1.setAttribute("name", k);
e1.setAttribute("value", v.toString());
root.appendChild(e1);
}
}
if (log.isDebugEnabled())
{
log.debug("Exit: SimpleChain::getDeploymentData");
}
return (root);
}
}