View Javadoc
1   /**
2    * This file Copyright (c) 2008-2014 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.objectfactory.Components;
39  import info.magnolia.repository.RepositoryManager;
40  
41  import java.util.ArrayList;
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Map;
45  import java.util.Map.Entry;
46  
47  import javax.inject.Inject;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Each ContentCachingConfiguration holds a CachePolicy, a FlushPolicy and a
54   * BrowserCachePolicy. Based on the outcome of the cachePolicy the defined
55   * executors will be called.
56   *
57   * @see CachePolicy
58   * @see FlushPolicy
59   */
60  public class ContentCachingConfiguration {
61  
62      private static final Logger log = LoggerFactory.getLogger(ContentCachingConfiguration.class);
63  
64      private CacheModule cacheModule;
65      private RepositoryManager repositoryManager;
66  
67      private String name;
68      private String cacheName;
69      private CachePolicy cachePolicy;
70      private FlushPolicy flushPolicy;
71      private BrowserCachePolicy browserCachePolicy;
72  
73      private Map<String, CachePolicyExecutor> executors = new HashMap<String, CachePolicyExecutor>();
74      private List<AbstractCacheListener> listeners = new ArrayList<AbstractCacheListener>();
75  
76      /**
77       * @deprecated since 5.4. Use {@link #ContentCachingConfiguration(CacheModule, info.magnolia.repository.RepositoryManager)} instead.
78       */
79      @Deprecated
80      public ContentCachingConfiguration() {
81          this(Components.getComponent(CacheModule.class), Components.getComponent(RepositoryManager.class));
82      }
83  
84      @Inject
85      public ContentCachingConfiguration(CacheModule cacheModule, RepositoryManager repositoryManager) {
86          this.cacheModule = cacheModule;
87          this.repositoryManager = repositoryManager;
88          flushPolicy = new FlushAllListeningPolicy(cacheModule, repositoryManager);
89      }
90  
91      /**
92       * This constructor is intended for tests only. For regular use, instantiate this class from its repository configuration using content2bean.
93       */
94      ContentCachingConfiguration(String name, CachePolicy cachePolicy, FlushPolicy flushPolicy) {
95          this.name = name;
96          this.cachePolicy = cachePolicy;
97          this.flushPolicy = flushPolicy;
98      }
99  
100     public String getName() {
101         return name;
102     }
103 
104     public void setName(String name) {
105         this.name = name;
106     }
107 
108     public String getCacheName() {
109         return cacheName != null ? cacheName : name;
110     }
111 
112     public void setCacheName(String cacheName) {
113         this.cacheName = cacheName;
114     }
115 
116     public CachePolicy getCachePolicy() {
117         if (cachePolicy != null) {
118             return cachePolicy;
119         }
120         // someone probably deleted cachePolicy node - fallback to no cache policy
121         log.error("Check your cache configuration! CachePolicy is probably not set! Falling back Never cache policy.");
122         return new info.magnolia.module.cache.cachepolicy.Never();
123     }
124 
125     public void setCachePolicy(CachePolicy cachePolicy) {
126         this.cachePolicy = cachePolicy;
127     }
128 
129     public FlushPolicy getFlushPolicy() {
130         return flushPolicy;
131     }
132 
133     public void setFlushPolicy(FlushPolicy flushPolicy) {
134         this.flushPolicy = flushPolicy;
135     }
136 
137     public CachePolicyExecutor getExecutor(CachePolicyResult.CachePolicyBehaviour behaviour) {
138         if (executors.containsKey(behaviour.getName())) {
139             return executors.get(behaviour.getName());
140         }
141         // someone probably deleted executors node - fallback to bypass cache policy
142         log.error("Check your cache configuration! Executor " + behaviour.getName() + " is probably not set! Bypassing cache.");
143         return new Bypass();
144     }
145 
146     public BrowserCachePolicy getBrowserCachePolicy() {
147         if (browserCachePolicy != null) {
148             return browserCachePolicy;
149         }
150         // someone probably deleted cachePolicy node - fallback to no cache policy
151         log.error("Check your cache configuration! BrowserCachePolicy is probably not set! Falling back to Never cache policy.");
152         return new info.magnolia.module.cache.browsercachepolicy.Never();
153     }
154 
155     public void setBrowserCachePolicy(BrowserCachePolicy browserCachePolicy) {
156         this.browserCachePolicy = browserCachePolicy;
157     }
158 
159     public Map<String, CachePolicyExecutor> getExecutors() {
160         return executors;
161     }
162 
163     public void setExecutors(Map<String, CachePolicyExecutor> executors) {
164         for (Entry<String, CachePolicyExecutor> executor : executors.entrySet()) {
165             this.addExecutor(executor.getKey(), executor.getValue());
166         }
167     }
168 
169     public void addExecutor(String name, CachePolicyExecutor executor) {
170         executor.setContentCachingConfiguration(this);
171         this.executors.put(name, executor);
172     }
173 
174     public List<AbstractCacheListener> getListeners() {
175         return listeners;
176     }
177 
178     public void setListeners(List<AbstractCacheListener> listeners) {
179         this.listeners = listeners;
180     }
181 
182     public AbstractCacheListener getListener(String name) {
183         for (AbstractCacheListener listener : getListeners()) {
184             if (name.equals(listener.getName())) {
185                 return listener;
186             }
187         }
188         return null;
189     }
190 
191 }