View Javadoc

1   /*
2    * $Id: CachingMessageProcessor.java 22607 2011-08-08 02:13:54Z pablo.kraan $
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.cache;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.processor.InterceptingMessageProcessor;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.routing.filter.Filter;
20  import org.mule.processor.AbstractMessageProcessorOwner;
21  import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
22  import org.mule.routing.filters.AcceptAllFilter;
23  
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * Processes a {@link MuleEvent} using a {@link CachingStrategy}.
31   * <p/>
32   * Provides a configurable filter to check whether or not a given request has
33   * to go through the cache or not. All requests are processed using the caching
34   * strategy by default.
35   */
36  public class CachingMessageProcessor extends AbstractMessageProcessorOwner
37          implements Initialisable, InterceptingMessageProcessor
38  {
39  
40      protected Log logger = LogFactory.getLog(getClass());
41  
42      private List<MessageProcessor> messageProcessors;
43  
44      private MessageProcessor cachedMessageProcessor;
45  
46      private MessageProcessor next;
47  
48      private CachingStrategy cachingStrategy;
49  
50      private Filter filter;
51  
52      @Override
53      public void initialise() throws InitialisationException
54      {
55          super.initialise();
56  
57          if (cachingStrategy == null)
58          {
59              cachingStrategy = createDefaultCachingStrategy();
60          }
61  
62          if (filter == null)
63          {
64              filter = createDefaultCacheFilter();
65          }
66      }
67  
68      protected AcceptAllFilter createDefaultCacheFilter()
69      {
70          return new AcceptAllFilter();
71      }
72  
73      protected CachingStrategy createDefaultCachingStrategy()
74      {
75          return new ObjectStoreCachingStrategy();
76      }
77  
78      public MuleEvent process(MuleEvent event) throws MuleException
79      {
80          MuleEvent responseFromCachedMessageProcessor;
81  
82          if (filter.accept(event.getMessage()))
83          {
84              responseFromCachedMessageProcessor = cachingStrategy.process(event, cachedMessageProcessor);
85          }
86          else
87          {
88              responseFromCachedMessageProcessor = cachedMessageProcessor.process(event);
89          }
90  
91          return processNext(responseFromCachedMessageProcessor);
92      }
93  
94      protected MuleEvent processNext(MuleEvent event) throws MuleException
95      {
96          if (next == null)
97          {
98              return event;
99          }
100         else
101         {
102             return next.process(event);
103         }
104     }
105 
106     public void setMessageProcessors(List<MessageProcessor> messageProcessors) throws MuleException
107     {
108         this.messageProcessors = messageProcessors;
109         this.cachedMessageProcessor = new DefaultMessageProcessorChainBuilder().chain(messageProcessors).build();
110     }
111 
112     @Override
113     protected List<MessageProcessor> getOwnedMessageProcessors()
114     {
115         return messageProcessors;
116     }
117 
118     public void setListener(MessageProcessor listener)
119     {
120         next = listener;
121     }
122 
123     public CachingStrategy getCachingStrategy()
124     {
125         return cachingStrategy;
126     }
127 
128     public void setCachingStrategy(CachingStrategy cachingStrategy)
129     {
130         this.cachingStrategy = cachingStrategy;
131     }
132 
133     public Filter getFilter()
134     {
135         return filter;
136     }
137 
138     public void setFilter(Filter filter)
139     {
140         this.filter = filter;
141     }
142 }