Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache

File ContentCachingConfiguration.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
59% of files have more coverage

Code metrics

6
18
7
1
181
103
10
0.56
2.57
7
1.43
46.6% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ContentCachingConfiguration 60 18 46.6% 10 13
0.5806451458.1%
 

Contributing tests

This file is covered by 29 tests. .

Source view

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.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 = new info.magnolia.module.cache.cachepolicy.Never();
70    private FlushPolicy flushPolicy;
71    private BrowserCachePolicy browserCachePolicy = new info.magnolia.module.cache.browsercachepolicy.Never();
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  0 toggle @Deprecated
80    public ContentCachingConfiguration() {
81  0 this(Components.getComponent(CacheModule.class), Components.getComponent(RepositoryManager.class));
82    }
83   
 
84  72 toggle @Inject
85    public ContentCachingConfiguration(CacheModule cacheModule, RepositoryManager repositoryManager) {
86  72 this.cacheModule = cacheModule;
87  72 this.repositoryManager = repositoryManager;
88  72 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  0 toggle ContentCachingConfiguration(String name, CachePolicy cachePolicy, FlushPolicy flushPolicy) {
95  0 this.name = name;
96  0 this.cachePolicy = cachePolicy;
97  0 this.flushPolicy = flushPolicy;
98    }
99   
 
100    toggle public String getName() {
101    return name;
102    }
103   
 
104    toggle public void setName(String name) {
105    this.name = name;
106    }
107   
 
108  26 toggle public String getCacheName() {
109  26 return cacheName != null ? cacheName : name;
110    }
111   
 
112    toggle public void setCacheName(String cacheName) {
113    this.cacheName = cacheName;
114    }
115   
 
116    toggle public CachePolicy getCachePolicy() {
117    return cachePolicy;
118    }
119   
 
120    toggle public void setCachePolicy(CachePolicy cachePolicy) {
121    this.cachePolicy = cachePolicy;
122    }
123   
 
124    toggle public FlushPolicy getFlushPolicy() {
125    return flushPolicy;
126    }
127   
 
128    toggle public void setFlushPolicy(FlushPolicy flushPolicy) {
129    this.flushPolicy = flushPolicy;
130    }
131   
 
132  20 toggle public CachePolicyExecutor getExecutor(CachePolicyResult.CachePolicyBehaviour behaviour) {
133  20 if (executors.containsKey(behaviour.getName())) {
134  19 return executors.get(behaviour.getName());
135    }
136    // someone probably deleted executors node - fallback to bypass cache policy
137  1 log.error("Check your cache configuration! Executor " + behaviour.getName() + " is probably not set! Bypassing cache.");
138  1 return new Bypass();
139    }
140   
 
141    toggle public BrowserCachePolicy getBrowserCachePolicy() {
142    return browserCachePolicy;
143    }
144   
 
145    toggle public void setBrowserCachePolicy(BrowserCachePolicy browserCachePolicy) {
146    this.browserCachePolicy = browserCachePolicy;
147    }
148   
 
149    toggle public Map<String, CachePolicyExecutor> getExecutors() {
150    return executors;
151    }
152   
 
153    toggle public void setExecutors(Map<String, CachePolicyExecutor> executors) {
154    for (Entry<String, CachePolicyExecutor> executor : executors.entrySet()) {
155    this.addExecutor(executor.getKey(), executor.getValue());
156    }
157    }
158   
 
159  74 toggle public void addExecutor(String name, CachePolicyExecutor executor) {
160  74 executor.setContentCachingConfiguration(this);
161  74 this.executors.put(name, executor);
162    }
163   
 
164    toggle public List<AbstractCacheListener> getListeners() {
165    return listeners;
166    }
167   
 
168    toggle public void setListeners(List<AbstractCacheListener> listeners) {
169    this.listeners = listeners;
170    }
171   
 
172  0 toggle public AbstractCacheListener getListener(String name) {
173  0 for (AbstractCacheListener listener : getListeners()) {
174  0 if (name.equals(listener.getName())) {
175  0 return listener;
176    }
177    }
178  0 return null;
179    }
180   
181    }