View Javadoc
1   /**
2    * This file Copyright (c) 2008-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache;
35  
36  import info.magnolia.module.cache.executor.Bypass;
37  import info.magnolia.module.cache.listeners.AbstractCacheListener;
38  import info.magnolia.repository.RepositoryManager;
39  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  
46  import javax.inject.Inject;
47  
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  /**
52   * Each ContentCachingConfiguration holds a CachePolicy, a FlushPolicy and a
53   * BrowserCachePolicy. Based on the outcome of the cachePolicy the defined
54   * executors will be called.
55   *
56   * @see CachePolicy
57   * @see FlushPolicy
58   */
59  public class ContentCachingConfiguration {
60  
61      private static final Logger log = LoggerFactory.getLogger(ContentCachingConfiguration.class);
62  
63      private CacheModule cacheModule;
64      private RepositoryManager repositoryManager;
65  
66      private String name;
67      private String cacheName;
68      private CachePolicy cachePolicy = new info.magnolia.module.cache.cachepolicy.Never();
69      private FlushPolicy flushPolicy;
70      private BrowserCachePolicy browserCachePolicy = new info.magnolia.module.cache.browsercachepolicy.Never();
71  
72      private Map<String, CachePolicyExecutor> executors = new HashMap<String, CachePolicyExecutor>();
73      private List<AbstractCacheListener> listeners = new ArrayList<AbstractCacheListener>();
74  
75      @Inject
76      public ContentCachingConfiguration(CacheModule cacheModule, RepositoryManager repositoryManager) {
77          this.cacheModule = cacheModule;
78          this.repositoryManager = repositoryManager;
79          flushPolicy = new FlushAllListeningPolicy(cacheModule, repositoryManager);
80      }
81  
82      public String getName() {
83          return name;
84      }
85  
86      public void setName(String name) {
87          this.name = name;
88      }
89  
90      public String getCacheName() {
91          return cacheName != null ? cacheName : name;
92      }
93  
94      public void setCacheName(String cacheName) {
95          this.cacheName = cacheName;
96      }
97  
98      public CachePolicy getCachePolicy() {
99          return cachePolicy;
100     }
101 
102     public void setCachePolicy(CachePolicy cachePolicy) {
103         this.cachePolicy = cachePolicy;
104     }
105 
106     public FlushPolicy getFlushPolicy() {
107         return flushPolicy;
108     }
109 
110     public void setFlushPolicy(FlushPolicy flushPolicy) {
111         this.flushPolicy = flushPolicy;
112     }
113 
114     public CachePolicyExecutor getExecutor(CachePolicyResult.CachePolicyBehaviour behaviour) {
115         if (executors.containsKey(behaviour.getName())) {
116             return executors.get(behaviour.getName());
117         }
118         // someone probably deleted executors node - fallback to bypass cache policy
119         log.error("Check your cache configuration! Executor " + behaviour.getName() + " is probably not set! Bypassing cache.");
120         return new Bypass();
121     }
122 
123     public BrowserCachePolicy getBrowserCachePolicy() {
124         return browserCachePolicy;
125     }
126 
127     public void setBrowserCachePolicy(BrowserCachePolicy browserCachePolicy) {
128         this.browserCachePolicy = browserCachePolicy;
129     }
130 
131     public Map<String, CachePolicyExecutor> getExecutors() {
132         return executors;
133     }
134 
135     public void setExecutors(Map<String, CachePolicyExecutor> executors) {
136         for (Entry<String, CachePolicyExecutor> executor : executors.entrySet()) {
137             this.addExecutor(executor.getKey(), executor.getValue());
138         }
139     }
140 
141     public void addExecutor(String name, CachePolicyExecutor executor) {
142         executor.setContentCachingConfiguration(this);
143         this.executors.put(name, executor);
144     }
145 
146     public List<AbstractCacheListener> getListeners() {
147         return listeners;
148     }
149 
150     public void setListeners(List<AbstractCacheListener> listeners) {
151         this.listeners = listeners;
152     }
153 
154     public AbstractCacheListener getListener(String name) {
155         for (AbstractCacheListener listener : getListeners()) {
156             if (name.equals(listener.getName())) {
157                 return listener;
158             }
159         }
160         return null;
161     }
162 
163 }