Coverage Report - org.mule.processor.builder.InterceptingChainCompositeMessageProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
InterceptingChainCompositeMessageProcessor
0%
0/50
0%
0/34
0
 
 1  
 /*
 2  
  * $Id: InterceptingChainCompositeMessageProcessor.java 19207 2010-08-26 05:02:51Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.processor.builder;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleEvent;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.construct.FlowConstruct;
 17  
 import org.mule.api.construct.FlowConstructAware;
 18  
 import org.mule.api.context.MuleContextAware;
 19  
 import org.mule.api.lifecycle.Disposable;
 20  
 import org.mule.api.lifecycle.Initialisable;
 21  
 import org.mule.api.lifecycle.InitialisationException;
 22  
 import org.mule.api.lifecycle.Lifecycle;
 23  
 import org.mule.api.lifecycle.Startable;
 24  
 import org.mule.api.lifecycle.Stoppable;
 25  
 import org.mule.api.processor.InterceptingMessageProcessor;
 26  
 import org.mule.api.processor.MessageProcessor;
 27  
 import org.mule.util.StringUtils;
 28  
 
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 import org.apache.commons.logging.Log;
 33  
 import org.apache.commons.logging.LogFactory;
 34  
 
 35  
 /**
 36  
  * Builder needs to return a composite rather than the first MessageProcessor in
 37  
  * the chain. This is so that if this chain is nested in another chain the next
 38  
  * MessageProcessor in the parent chain is not injected into the first in the
 39  
  * nested chain.
 40  
  */
 41  
 public class InterceptingChainCompositeMessageProcessor implements MessageProcessor, Lifecycle, FlowConstructAware, MuleContextAware
 42  
 {
 43  
     private Log log;
 44  
     private String name;
 45  
     private MessageProcessor firstInChain;
 46  
     private List<MessageProcessor> allProcessors;
 47  
 
 48  
     public InterceptingChainCompositeMessageProcessor(InterceptingMessageProcessor firstInChain,
 49  
                                                       List<MessageProcessor> allProcessors,
 50  
                                                       String name)
 51  0
     {
 52  0
         this.name = name;
 53  0
         this.firstInChain = firstInChain;
 54  0
         this.allProcessors = allProcessors;
 55  
         // TODO You a custom categories?
 56  0
         log = LogFactory.getLog(InterceptingChainCompositeMessageProcessor.class);
 57  0
     }
 58  
 
 59  
     public MuleEvent process(MuleEvent event) throws MuleException
 60  
     {
 61  0
         if (log.isDebugEnabled())
 62  
         {
 63  0
             log.debug("Invoking " + this + " with event " + event);
 64  
         }
 65  0
         return firstInChain.process(event);
 66  
     }
 67  
 
 68  
     public void initialise() throws InitialisationException
 69  
     {
 70  0
         for (MessageProcessor processor : allProcessors)
 71  
         {
 72  
             //MULE-5002 TODO review MP Lifecycle
 73  0
             if (processor instanceof Initialisable /*&& !(processor instanceof Transformer)*/)
 74  
             {
 75  0
                 ((Initialisable) processor).initialise();
 76  
             }
 77  
         }
 78  0
     }
 79  
 
 80  
     public void start() throws MuleException
 81  
     {
 82  0
         for (MessageProcessor processor : allProcessors)
 83  
         {
 84  0
             if (processor instanceof Startable)
 85  
             {
 86  0
                 ((Startable) processor).start();
 87  
             }
 88  
         }
 89  0
     }
 90  
 
 91  
     public void stop() throws MuleException
 92  
     {
 93  0
         for (MessageProcessor processor : allProcessors)
 94  
         {
 95  0
             if (processor instanceof Stoppable)
 96  
             {
 97  0
                 ((Stoppable) processor).stop();
 98  
             }
 99  
         }
 100  0
     }
 101  
 
 102  
     public void dispose()
 103  
     {
 104  0
         for (MessageProcessor processor : allProcessors)
 105  
         {
 106  0
             if (processor instanceof Disposable)
 107  
             {
 108  0
                 ((Disposable) processor).dispose();
 109  
             }
 110  
         }
 111  0
         firstInChain = null;
 112  0
         allProcessors.clear();
 113  0
     }
 114  
 
 115  
     public void setFlowConstruct(FlowConstruct flowConstruct)
 116  
     {
 117  0
         for (MessageProcessor processor : allProcessors)
 118  
         {
 119  0
             if (processor instanceof FlowConstructAware)
 120  
             {
 121  0
                 ((FlowConstructAware) processor).setFlowConstruct(flowConstruct);
 122  
             }
 123  
         }
 124  0
     }
 125  
 
 126  
     public void setMuleContext(MuleContext context)
 127  
     {
 128  0
         for (MessageProcessor processor : allProcessors)
 129  
         {
 130  0
             if (processor instanceof MuleContextAware)
 131  
             {
 132  0
                 ((MuleContextAware) processor).setMuleContext(context);
 133  
             }
 134  
         }
 135  0
     }
 136  
 
 137  
     @Override
 138  
     public String toString()
 139  
     {
 140  0
         StringBuffer string = new StringBuffer();
 141  0
         string.append("InterceptingChainCompositeMessageProcessor ");
 142  0
         if (name != null)
 143  
         {
 144  0
             string.append(" '" + name + "' ");
 145  
         }
 146  
 
 147  0
         Iterator<MessageProcessor> mpIterator = allProcessors.iterator();
 148  0
         if (mpIterator.hasNext())
 149  
         {
 150  0
             string.append("\n[ ");
 151  0
             while (mpIterator.hasNext())
 152  
             {
 153  0
                 MessageProcessor mp = mpIterator.next();
 154  0
                 string.append("\n  " + StringUtils.replace(mp.toString(), "\n", "\n  "));
 155  0
                 if (mpIterator.hasNext())
 156  
                 {
 157  0
                     string.append(", ");
 158  
                 }
 159  0
             }
 160  0
             string.append("\n]");
 161  
         }
 162  
 
 163  0
         return string.toString();
 164  
     }
 165  
 }