View Javadoc
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  public class CachingMessageProcessor extends AbstractMessageProcessorOwner
33          implements Initialisable, InterceptingMessageProcessor
34  {
35  
36      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          super.initialise();
52  
53          if (cachingStrategy == null)
54          {
55              cachingStrategy = createDefaultCachingStrategy();
56          }
57  
58          if (filter == null)
59          {
60              filter = createDefaultCacheFilter();
61          }
62      }
63  
64      protected AcceptAllFilter createDefaultCacheFilter()
65      {
66          return new AcceptAllFilter();
67      }
68  
69      protected CachingStrategy createDefaultCachingStrategy()
70      {
71          return new ObjectStoreCachingStrategy();
72      }
73  
74      public MuleEvent process(MuleEvent event) throws MuleException
75      {
76          MuleEvent responseFromCachedMessageProcessor;
77  
78          if (filter.accept(event.getMessage()))
79          {
80              responseFromCachedMessageProcessor = cachingStrategy.process(event, cachedMessageProcessor);
81          }
82          else
83          {
84              responseFromCachedMessageProcessor = cachedMessageProcessor.process(event);
85          }
86  
87          return processNext(responseFromCachedMessageProcessor);
88      }
89  
90      protected MuleEvent processNext(MuleEvent event) throws MuleException
91      {
92          if (next == null)
93          {
94              return event;
95          }
96          else
97          {
98              return next.process(event);
99          }
100     }
101 
102     public void setMessageProcessors(List<MessageProcessor> messageProcessors) throws MuleException
103     {
104         this.messageProcessors = messageProcessors;
105         this.cachedMessageProcessor = new DefaultMessageProcessorChainBuilder().chain(messageProcessors).build();
106     }
107 
108     @Override
109     protected List<MessageProcessor> getOwnedMessageProcessors()
110     {
111         return messageProcessors;
112     }
113 
114     public void setListener(MessageProcessor listener)
115     {
116         next = listener;
117     }
118 
119     public CachingStrategy getCachingStrategy()
120     {
121         return cachingStrategy;
122     }
123 
124     public void setCachingStrategy(CachingStrategy cachingStrategy)
125     {
126         this.cachingStrategy = cachingStrategy;
127     }
128 
129     public Filter getFilter()
130     {
131         return filter;
132     }
133 
134     public void setFilter(Filter filter)
135     {
136         this.filter = filter;
137     }
138 }