package org.codehaus.xfire.transport;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.exchange.InMessage;
import org.codehaus.xfire.exchange.MessageExchange;
import org.codehaus.xfire.fault.XFireFault;
import org.codehaus.xfire.handler.DefaultFaultHandler;
import org.codehaus.xfire.handler.HandlerPipeline;

/**
 * A <code>ChannelEndpoint</code> which executes the in pipeline
 * on the service and starts a <code>MessageExchange</code>.
 * 
 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
 */
public class DefaultEndpoint
    implements ChannelEndpoint
{
    private static final Log log = LogFactory.getLog(DefaultEndpoint.class);
    public static final String SERVICE_HANDLERS_REGISTERED = "service.handlers.registered";

    public DefaultEndpoint()
    {
    }
    
    public void onReceive(MessageContext context, InMessage msg)
    {
        if (log.isDebugEnabled()) log.debug("Received message to " + msg.getUri());
        
        if (context.getExchange() == null)
        {
            MessageExchange exchange = new MessageExchange(context);
            exchange.setInMessage(msg);
            context.setCurrentMessage(msg);
        }
        
        // Create the handlerpipeline and invoke it
        HandlerPipeline pipeline = new HandlerPipeline(context.getXFire().getInPhases());
        pipeline.addHandlers(context.getXFire().getInHandlers());
        pipeline.addHandlers(msg.getChannel().getTransport().getInHandlers());

        if (context.getService() != null)
        {
            pipeline.addHandlers(context.getService().getInHandlers());
            context.setProperty(SERVICE_HANDLERS_REGISTERED, Boolean.TRUE);
        }
        
        context.setInPipeline(pipeline);
        
        if (context.getFaultHandler() == null)
            context.setFaultHandler(new DefaultFaultHandler());
        
        try
        {
            pipeline.invoke(context);
            
            // finishReadingMessage(msg, context);
        }
        catch (Exception e)
        {
            context.setProperty(DefaultFaultHandler.EXCEPTION, e);

            try
            {
                context.getFaultHandler().invoke(context);
            }
            catch (Exception e1)
            {
                log.warn("Error invoking fault handler.", e1);
            }
        }
    }

    public void finishReadingMessage(InMessage message, MessageContext context)
        throws XFireFault
    {
        XMLStreamReader reader = message.getXMLStreamReader();

        try
        {
            int event = reader.getEventType();
            while (event != XMLStreamReader.END_DOCUMENT && reader.hasNext()) 
                event = reader.next();
        }
        catch (XMLStreamException e)
        {
            log.warn("Couldn't parse to end of message.", e);
        }
    }
}

@@ -442,16 +442,59 @@
 andler;%0A
+import org.codehaus.xfire.handler.Handler;%0A
 import o
@@ -2055,19 +2055,14 @@
 ler(
-new Default
+create
 Faul
@@ -2567,16 +2567,114 @@
 %0A    %7D%0A%0A
+    protected Handler createFaultHandler()%0A    %7B%0A        return new DefaultFaultHandler();%0A    %7D%0A%0A
     publ

Make the endpoint a bit more flexible for mbean stuff