FileDocCategorySizeDatePackage
SimpleChain.javaAPI DocApache Axis 1.47945Sat Apr 22 18:57:28 BST 2006org.apache.axis

SimpleChain

public class SimpleChain extends org.apache.axis.handlers.BasicHandler implements Chain
A Simple Chain is a 'composite' Handler in that it aggregates a collection of Handlers and also acts as a Handler which delegates its operations to the collection.

A Simple Chain initially has no Handlers. Handlers may be added until the chain is invoke()d after which Handlers may not be added (and any attempt to do so will throw an exception).

author
Doug Davis (dug@us.ibm.com)
author
Glyn Normington (norm@uk.ibm.com)

Fields Summary
private static Log
log
protected Vector
handlers
protected boolean
invoked
private String
CAUGHTFAULT_PROPERTY
private static final HandlerIterationStrategy
iVisitor
private static final HandlerIterationStrategy
wsdlVisitor
Constructors Summary
Methods Summary
public voidaddHandler(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 booleancanHandleBlock(javax.xml.namespace.QName qname)

        for ( int i = 0 ; i < handlers.size() ; i++ )
            if ( ((Handler) handlers.elementAt( i )).canHandleBlock(qname) )
                return( true );
        return( false );
    
public voidcleanup()

        for ( int i = 0 ; i < handlers.size() ; i++ )
            ((Handler) handlers.elementAt( i )).cleanup();
    
public booleancontains(Handler handler)

        return( handlers.contains( handler ));
    
private voiddoVisiting(MessageContext msgContext, HandlerIterationStrategy visitor)

        int i = 0 ;
        try {
            Enumeration enumeration = handlers.elements();
            while (enumeration.hasMoreElements()) {
                Handler h = (Handler)enumeration.nextElement();
                visitor.visit(h, msgContext);
                i++;
            }
        } catch( AxisFault f ) {
            // Something went wrong.  If we haven't already put this fault
            // into the MessageContext's response message, do so and make sure
            // we only do it once.  This allows onFault() methods to safely
            // set headers and such in the response message without them
            // getting stomped.
            if (!msgContext.isPropertyTrue(CAUGHTFAULT_PROPERTY)) {
                // Attach the fault to the response message; enabling access to the
                // fault details while inside the handler onFault methods.
                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 voidgenerateWSDL(MessageContext msgContext)
Iterate over the chain letting each handler have a crack at contributing to a WSDL description.

param
msgContext the MessageContext to write the WSDL out to
throws
AxisFault if there was a problem writing the WSDL

        if (log.isDebugEnabled()) {
            log.debug("Enter: SimpleChain::generateWSDL");
        }

        invoked = true;
        doVisiting(msgContext, wsdlVisitor);

        if (log.isDebugEnabled()) {
            log.debug("Exit: SimpleChain::generateWSDL");
        }
    
public org.w3c.dom.ElementgetDeploymentData(org.w3c.dom.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 );
    
public Handler[]getHandlers()

        if (handlers.size() == 0)
            return null;

        Handler [] ret = new Handler[handlers.size()];
        return( (Handler[]) handlers.toArray(ret) );
    
public voidinit()


       
        for ( int i = 0 ; i < handlers.size() ; i++ )
            ((Handler) handlers.elementAt( i )).init();
    
public voidinvoke(MessageContext msgContext)
Iterate over the chain invoking each handler. If there's a fault then call 'onFault' for each completed handler in reverse order, then rethrow the exception.

throws
AxisFault if there was a fault with any of the handlers


                                               
          
        if (log.isDebugEnabled()) {
            log.debug("Enter: SimpleChain::invoke");
        }

       invoked = true;
        doVisiting(msgContext, iVisitor);

        if (log.isDebugEnabled()) {
            log.debug("Exit: SimpleChain::invoke");
        }
   
public voidonFault(MessageContext msgContext)
Notify the handlers in this chain because some handler later on has faulted - in reverse order. If any handlers have been added since we visited the chain, they will get notified too!

param
msgContext the context to process

        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");
        }