1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.cache.rendering

File DefaultRenderingCacheConfiguration.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
35
11
1
256
123
23
0.66
3.18
11
2.09

Classes

Class Line # Actions
DefaultRenderingCacheConfiguration 44 35 0% 23 1
0.98387198.4%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.internal.cache.rendering;
21   
22    import java.util.List;
23    import java.util.regex.Pattern;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.configuration.ConfigurationSource;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.ModelContext;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35   
36    /**
37    * Default implementation of {@link RenderingCacheConfiguration}.
38    *
39    * @version $Id: 59f59e52cd010ded793dfa10489994fbdf28eec3 $
40    * @since 2.4M1
41    */
42    @Component
43    @Singleton
 
44    public class DefaultRenderingCacheConfiguration implements RenderingCacheConfiguration
45    {
46    /**
47    * Configuration key prefix.
48    */
49    private static final String PREFIX = "core.renderingcache.";
50   
51    /**
52    * Name of the property indication if the cache is enabled or not.
53    */
54    private static final String PROPNAME_ENABLED = PREFIX + "enabled";
55   
56    /**
57    * Name of the property listing the references of the documents to cache.
58    */
59    private static final String PROPNAME_DOCUMENTS = PREFIX + "documents";
60   
61    /**
62    * Name of the property indication the time to live of the elements in the cache.
63    */
64    private static final String PROPNAME_DURATION = PREFIX + "duration";
65   
66    /**
67    * The default time to live of the elements in the cache.
68    */
69    private static final int PROPVALUE_DURATION = 300;
70   
71    /**
72    * Name of the property indication the size of the cache.
73    */
74    private static final String PROPNAME_SIZE = PREFIX + "size";
75   
76    /**
77    * The default size of the cache.
78    */
79    private static final int PROPVALUE_SIZE = 100;
80   
81    /**
82    * xwiki.properties file configurations.
83    */
84    @Inject
85    @Named("xwikiproperties")
86    private ConfigurationSource farmConfiguration;
87   
88    /**
89    * Wiki configuration.
90    */
91    @Inject
92    @Named("wiki")
93    private ConfigurationSource wikiConfiguration;
94   
95    /**
96    * Used to serialize a document reference into a String.
97    */
98    @Inject
99    private EntityReferenceSerializer<String> serializer;
100   
101    /**
102    * Used to serialize a document reference into a String.
103    */
104    @Inject
105    @Named("compactwiki")
106    private EntityReferenceSerializer<String> wikiSerializer;
107   
108    /**
109    * Used to get the current wiki.
110    */
111    @Inject
112    private ModelContext modelContext;
113   
114    /**
115    * The cached pattern coming from xwiki.properties file.
116    */
117    private Pattern farmPattern;
118   
 
119  47 toggle @Override
120    public boolean isEnabled()
121    {
122  47 return isFarmEnabled();
123    }
124   
125    /**
126    * @return true if the rendering cache system is enabled in general
127    */
 
128  12758 toggle public boolean isFarmEnabled()
129    {
130  12758 return this.farmConfiguration.getProperty(PROPNAME_ENABLED, false);
131    }
132   
133    /**
134    * @return true if the rendering cache system is enabled in general
135    */
 
136  4 toggle public boolean isWikiEnabled()
137    {
138  4 return this.wikiConfiguration.getProperty(PROPNAME_ENABLED, true);
139    }
140   
 
141  1 toggle @Override
142    public int getDuration()
143    {
144  1 return this.farmConfiguration.getProperty(PROPNAME_DURATION, PROPVALUE_DURATION);
145    }
146   
 
147  1 toggle @Override
148    public int getSize()
149    {
150  1 return this.farmConfiguration.getProperty(PROPNAME_SIZE, PROPVALUE_SIZE);
151    }
152   
 
153  12713 toggle @Override
154    public boolean isCached(DocumentReference documentReference)
155    {
156  12711 if (documentReference != null && isFarmEnabled()) {
157  13 if (isCachedInFarm(documentReference)) {
158  9 return true;
159    }
160   
161  4 return isCachedInWiki(documentReference);
162    }
163   
164  12699 return false;
165    }
166   
167    /**
168    * Indicate if the provided document's rendering result should be cached according to farm configuration.
169    *
170    * @param documentReference the reference of the document
171    * @return true if the document should be cached, false otherwise
172    */
 
173  13 toggle private boolean isCachedInFarm(DocumentReference documentReference)
174    {
175  13 Pattern pattern = getFarmPattern();
176   
177  13 if (pattern != null) {
178  10 String documentReferenceString = this.serializer.serialize(documentReference);
179   
180  10 return pattern.matcher(documentReferenceString).matches();
181    }
182   
183  3 return false;
184    }
185   
186    /**
187    * Indicate if the provided document's rendering result should be cached according to wiki configuration.
188    *
189    * @param documentReference the reference of the document
190    * @return true if the document should be cached, false otherwise
191    */
 
192  4 toggle public boolean isCachedInWiki(DocumentReference documentReference)
193    {
194  4 if (isWikiEnabled()
195    && this.modelContext.getCurrentEntityReference() != null
196    && documentReference.getWikiReference().getName()
197    .equals(this.modelContext.getCurrentEntityReference().extractReference(EntityType.WIKI).getName())) {
198  4 Pattern pattern = getWikiPattern();
199   
200  4 if (pattern != null) {
201  2 return pattern.matcher(this.serializer.serialize(documentReference)).matches()
202    || pattern.matcher(this.wikiSerializer.serialize(documentReference)).matches();
203    }
204    }
205   
206  2 return false;
207    }
208   
209    /**
210    * @return the pattern to match documents to cache according to farm configuration.
211    */
 
212  13 toggle private Pattern getFarmPattern()
213    {
214  13 if (this.farmPattern == null) {
215  9 this.farmPattern = getPattern(this.farmConfiguration.getProperty(PROPNAME_DOCUMENTS, List.class));
216    }
217   
218  13 return this.farmPattern;
219    }
220   
221    /**
222    * @return the pattern to match documents to cache according to wiki configuration.
223    */
 
224  4 toggle private Pattern getWikiPattern()
225    {
226  4 return getPattern(this.wikiConfiguration.getProperty(PROPNAME_DOCUMENTS, List.class));
227    }
228   
229    /**
230    * Convert a list of String patterns into one {@link Pattern} object.
231    *
232    * @param configuration the {@link String} to convert to one {@link Pattern}
233    * @return {@link Pattern} version of the provided list of {@link String}.
234    */
 
235  13 toggle private Pattern getPattern(List<String> configuration)
236    {
237  13 Pattern pattern = null;
238   
239  13 if (configuration != null && !configuration.isEmpty()) {
240  8 StringBuffer patternBuffer = new StringBuffer();
241   
242  8 for (String patternString : configuration) {
243  12 if (patternBuffer.length() > 0) {
244  4 patternBuffer.append('|');
245    }
246  12 patternBuffer.append('(');
247  12 patternBuffer.append(patternString);
248  12 patternBuffer.append(')');
249    }
250   
251  8 pattern = Pattern.compile(patternBuffer.toString());
252    }
253   
254  13 return pattern;
255    }
256    }