Coverage Report - org.mule.cache.CachingMessageProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
CachingMessageProcessor
0%
0/29
0%
0/8
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.cache;
 8  
 
 9  
 import org.mule.api.MuleEvent;
 10  
 import org.mule.api.MuleException;
 11  
 import org.mule.api.lifecycle.Initialisable;
 12  
 import org.mule.api.lifecycle.InitialisationException;
 13  
 import org.mule.api.processor.InterceptingMessageProcessor;
 14  
 import org.mule.api.processor.MessageProcessor;
 15  
 import org.mule.api.routing.filter.Filter;
 16  
 import org.mule.processor.AbstractMessageProcessorOwner;
 17  
 import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
 18  
 import org.mule.routing.filters.AcceptAllFilter;
 19  
 
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 /**
 26  
  * Processes a {@link MuleEvent} using a {@link CachingStrategy}.
 27  
  * <p/>
 28  
  * Provides a configurable filter to check whether or not a given request has
 29  
  * to go through the cache or not. All requests are processed using the caching
 30  
  * strategy by default.
 31  
  */
 32  0
 public class CachingMessageProcessor extends AbstractMessageProcessorOwner
 33  
         implements Initialisable, InterceptingMessageProcessor
 34  
 {
 35  
 
 36  0
     protected Log logger = LogFactory.getLog(getClass());
 37  
 
 38  
     private List<MessageProcessor> messageProcessors;
 39  
 
 40  
     private MessageProcessor cachedMessageProcessor;
 41  
 
 42  
     private MessageProcessor next;
 43  
 
 44  
     private CachingStrategy cachingStrategy;
 45  
 
 46  
     private Filter filter;
 47  
 
 48  
     @Override
 49  
     public void initialise() throws InitialisationException
 50  
     {
 51  0
         super.initialise();
 52  
 
 53  0
         if (cachingStrategy == null)
 54  
         {
 55  0
             cachingStrategy = createDefaultCachingStrategy();
 56  
         }
 57  
 
 58  0
         if (filter == null)
 59  
         {
 60  0
             filter = createDefaultCacheFilter();
 61  
         }
 62  0
     }
 63  
 
 64  
     protected AcceptAllFilter createDefaultCacheFilter()
 65  
     {
 66  0
         return new AcceptAllFilter();
 67  
     }
 68  
 
 69  
     protected CachingStrategy createDefaultCachingStrategy()
 70  
     {
 71  0
         return new ObjectStoreCachingStrategy();
 72  
     }
 73  
 
 74  
     public MuleEvent process(MuleEvent event) throws MuleException
 75  
     {
 76  
         MuleEvent responseFromCachedMessageProcessor;
 77  
 
 78  0
         if (filter.accept(event.getMessage()))
 79  
         {
 80  0
             responseFromCachedMessageProcessor = cachingStrategy.process(event, cachedMessageProcessor);
 81  
         }
 82  
         else
 83  
         {
 84  0
             responseFromCachedMessageProcessor = cachedMessageProcessor.process(event);
 85  
         }
 86  
 
 87  0
         return processNext(responseFromCachedMessageProcessor);
 88  
     }
 89  
 
 90  
     protected MuleEvent processNext(MuleEvent event) throws MuleException
 91  
     {
 92  0
         if (next == null)
 93  
         {
 94  0
             return event;
 95  
         }
 96  
         else
 97  
         {
 98  0
             return next.process(event);
 99  
         }
 100  
     }
 101  
 
 102  
     public void setMessageProcessors(List<MessageProcessor> messageProcessors) throws MuleException
 103  
     {
 104  0
         this.messageProcessors = messageProcessors;
 105  0
         this.cachedMessageProcessor = new DefaultMessageProcessorChainBuilder().chain(messageProcessors).build();
 106  0
     }
 107  
 
 108  
     @Override
 109  
     protected List<MessageProcessor> getOwnedMessageProcessors()
 110  
     {
 111  0
         return messageProcessors;
 112  
     }
 113  
 
 114  
     public void setListener(MessageProcessor listener)
 115  
     {
 116  0
         next = listener;
 117  0
     }
 118  
 
 119  
     public CachingStrategy getCachingStrategy()
 120  
     {
 121  0
         return cachingStrategy;
 122  
     }
 123  
 
 124  
     public void setCachingStrategy(CachingStrategy cachingStrategy)
 125  
     {
 126  0
         this.cachingStrategy = cachingStrategy;
 127  0
     }
 128  
 
 129  
     public Filter getFilter()
 130  
     {
 131  0
         return filter;
 132  
     }
 133  
 
 134  
     public void setFilter(Filter filter)
 135  
     {
 136  0
         this.filter = filter;
 137  0
     }
 138  
 }