1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.cache.infinispan.internal

File InfinispanConfigurationLoader.java

 

Coverage histogram

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

Code metrics

36
73
10
1
282
159
35
0.48
7.3
10
3.5

Classes

Class Line # Actions
InfinispanConfigurationLoader 46 73 0% 35 9
0.9243697592.4%
 

Contributing tests

This file is covered by 100 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 org.xwiki.cache.infinispan.internal;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.infinispan.commons.configuration.Builder;
24    import org.infinispan.commons.configuration.ConfigurationUtils;
25    import org.infinispan.configuration.cache.Configuration;
26    import org.infinispan.configuration.cache.ConfigurationBuilder;
27    import org.infinispan.configuration.cache.PersistenceConfiguration;
28    import org.infinispan.configuration.cache.PersistenceConfigurationBuilder;
29    import org.infinispan.configuration.cache.SingleFileStoreConfiguration;
30    import org.infinispan.configuration.cache.SingleFileStoreConfigurationBuilder;
31    import org.infinispan.configuration.cache.StoreConfiguration;
32    import org.infinispan.configuration.cache.StoreConfigurationBuilder;
33    import org.infinispan.eviction.EvictionStrategy;
34    import org.infinispan.eviction.EvictionType;
35    import org.xwiki.cache.config.CacheConfiguration;
36    import org.xwiki.cache.eviction.EntryEvictionConfiguration;
37    import org.xwiki.cache.eviction.LRUEvictionConfiguration;
38    import org.xwiki.cache.util.AbstractCacheConfigurationLoader;
39    import org.xwiki.environment.Environment;
40   
41    /**
42    * Customize Infinispan configuration based on XWiki Cache configuration.
43    *
44    * @version $Id: 05ca239a7e7f09d5f1aec5c5dd09db24a95adcc3 $
45    */
 
46    public class InfinispanConfigurationLoader extends AbstractCacheConfigurationLoader
47    {
48    /**
49    * The name of the field containing the wakeup interval used for expiration to set in the {@link EvictionConfig}.
50    */
51    public static final String CONFX_EXPIRATION_WAKEUPINTERVAL = "infinispan.expiration.wakeupinterval";
52   
53    /**
54    * The default location of a filesystem based cache loader when not provided in the xml configuration file.
55    */
56    private static final String DEFAULT_SINGLEFILESTORE_LOCATION = "Infinispan-SingleFileStore";
57   
58    /**
59    * @param configuration the XWiki cache configuration
60    * @param environment teh environment, can be null
61    */
 
62  1873 toggle public InfinispanConfigurationLoader(CacheConfiguration configuration, Environment environment)
63    {
64  1873 super(configuration, environment, null);
65    }
66   
67    /**
68    * @param builder the current builder
69    * @param isconfiguration the configuration to customize
70    * @return the new builder based on provided configuration if the current one is null
71    */
 
72  1045 toggle private ConfigurationBuilder builder(ConfigurationBuilder builder, Configuration isconfiguration)
73    {
74  1045 if (builder != null) {
75  273 return builder;
76    }
77   
78  772 ConfigurationBuilder newBuilder = new ConfigurationBuilder();
79   
80  772 if (isconfiguration != null) {
81  39 newBuilder.read(isconfiguration);
82    }
83   
84  772 return newBuilder;
85    }
86   
87    /**
88    * @param isconfiguration the configuration to check
89    * @return true if one of the loader is an incomplete {@link FileCacheStoreConfiguration}
90    */
 
91  137 toggle private boolean containsIncompleteFileLoader(Configuration isconfiguration)
92    {
93  137 PersistenceConfiguration persistenceConfiguration = isconfiguration.persistence();
94   
95  137 for (StoreConfiguration storeConfiguration : persistenceConfiguration.stores()) {
96  39 if (storeConfiguration instanceof SingleFileStoreConfiguration) {
97  39 SingleFileStoreConfiguration singleFileStoreConfiguration =
98    (SingleFileStoreConfiguration) storeConfiguration;
99   
100  39 String location = singleFileStoreConfiguration.location();
101   
102    // "Infinispan-SingleFileStore" is the default location...
103  39 if (StringUtils.isBlank(location) || location.equals(DEFAULT_SINGLEFILESTORE_LOCATION)) {
104  39 return true;
105    }
106    }
107    }
108   
109  98 return false;
110    }
111   
112    /**
113    * Customize the eviction configuration.
114    *
115    * @param currentBuilder the configuration builder
116    * @param configuration the configuration
117    * @return the configuration builder
118    */
 
119  1736 toggle private ConfigurationBuilder customizeEviction(ConfigurationBuilder currentBuilder, Configuration configuration)
120    {
121  1736 ConfigurationBuilder builder = currentBuilder;
122   
123  1736 EntryEvictionConfiguration eec =
124    (EntryEvictionConfiguration) getCacheConfiguration().get(EntryEvictionConfiguration.CONFIGURATIONID);
125   
126  1736 if (eec != null && eec.getAlgorithm() == EntryEvictionConfiguration.Algorithm.LRU) {
127    ////////////////////
128    // Eviction
129    // Max entries
130  817 builder = customizeEvictionMaxEntries(builder, configuration, eec);
131   
132    ////////////////////
133    // Expiration
134    // Wakeup interval
135  817 builder = customizeExpirationWakeUpInterval(builder, configuration, eec);
136   
137    // Max idle
138  817 builder = customizeExpirationMaxIdle(builder, configuration, eec);
139   
140    // Lifespan
141  817 builder = customizeExpirationLifespan(builder, configuration, eec);
142    }
143   
144  1736 return builder;
145    }
146   
 
147  817 toggle private ConfigurationBuilder customizeEvictionMaxEntries(ConfigurationBuilder currentBuilder,
148    Configuration configuration, EntryEvictionConfiguration eec)
149    {
150  817 ConfigurationBuilder builder = currentBuilder;
151   
152  817 if (eec.containsKey(LRUEvictionConfiguration.MAXENTRIES_ID)) {
153  811 int maxEntries = ((Number) eec.get(LRUEvictionConfiguration.MAXENTRIES_ID)).intValue();
154  811 if (configuration.eviction() == null || configuration.eviction().strategy() != EvictionStrategy.LRU
155    || configuration.eviction().maxEntries() != maxEntries) {
156  692 builder = builder(builder, null);
157  692 builder.eviction().strategy(EvictionStrategy.LRU);
158  692 builder.eviction().type(EvictionType.COUNT).size(maxEntries);
159    }
160    }
161   
162  817 return builder;
163    }
164   
 
165  817 toggle private ConfigurationBuilder customizeExpirationWakeUpInterval(ConfigurationBuilder currentBuilder,
166    Configuration configuration, EntryEvictionConfiguration eec)
167    {
168  817 ConfigurationBuilder builder = currentBuilder;
169   
170  817 if (eec.get(CONFX_EXPIRATION_WAKEUPINTERVAL) instanceof Number) {
171  4 builder = builder(builder, null);
172  4 builder.expiration().wakeUpInterval(((Number) eec.get(CONFX_EXPIRATION_WAKEUPINTERVAL)).longValue());
173    }
174   
175  817 return builder;
176    }
177   
 
178  817 toggle private ConfigurationBuilder customizeExpirationMaxIdle(ConfigurationBuilder currentBuilder,
179    Configuration configuration, EntryEvictionConfiguration eec)
180    {
181  817 ConfigurationBuilder builder = currentBuilder;
182   
183  817 if (eec.getTimeToLive() > 0) {
184  302 long maxIdle = eec.getTimeToLive() * 1000L;
185  302 if (configuration.expiration() == null || configuration.expiration().maxIdle() != maxIdle) {
186  302 builder = builder(builder, null);
187  302 builder.expiration().maxIdle(eec.getTimeToLive() * 1000L);
188    }
189    }
190   
191  817 return builder;
192    }
193   
 
194  817 toggle private ConfigurationBuilder customizeExpirationLifespan(ConfigurationBuilder currentBuilder,
195    Configuration configuration, EntryEvictionConfiguration eec)
196    {
197  817 ConfigurationBuilder builder = currentBuilder;
198   
199  817 if (eec.containsKey(LRUEvictionConfiguration.LIFESPAN_ID)) {
200  8 long lifespan = (Integer) eec.get(LRUEvictionConfiguration.LIFESPAN_ID) * 1000L;
201  8 if (configuration.expiration() == null || configuration.expiration().lifespan() != lifespan) {
202  8 builder = builder(builder, null);
203  8 builder.expiration().lifespan(lifespan);
204    }
205    }
206   
207  817 return builder;
208    }
209   
210    /**
211    * Add missing location for filesystem based cache.
212    *
213    * @param currentBuilder the configuration builder
214    * @param configuration the configuration
215    * @return the configuration builder
216    */
 
217  137 toggle private ConfigurationBuilder completeFilesystem(ConfigurationBuilder currentBuilder, Configuration configuration)
218    {
219  137 ConfigurationBuilder builder = currentBuilder;
220   
221  137 if (containsIncompleteFileLoader(configuration)) {
222  39 builder = builder(builder, configuration);
223   
224  39 PersistenceConfigurationBuilder persistence = builder.persistence();
225   
226  39 persistence.clearStores();
227   
228  39 for (StoreConfiguration storeConfiguration : configuration.persistence().stores()) {
229  39 if (storeConfiguration instanceof SingleFileStoreConfiguration) {
230  39 SingleFileStoreConfiguration singleFileStoreConfiguration =
231    (SingleFileStoreConfiguration) storeConfiguration;
232   
233  39 String location = singleFileStoreConfiguration.location();
234   
235    // "Infinispan-SingleFileStore" is the default location...
236  39 if (StringUtils.isBlank(location) || location.equals(DEFAULT_SINGLEFILESTORE_LOCATION)) {
237  39 SingleFileStoreConfigurationBuilder singleFileStoreConfigurationBuilder =
238    persistence.addSingleFileStore();
239  39 singleFileStoreConfigurationBuilder.read(singleFileStoreConfiguration);
240  39 singleFileStoreConfigurationBuilder.location(createTempDir());
241    }
242    } else {
243    // Copy the loader as it is
244  0 Class<? extends StoreConfigurationBuilder<?, ?>> storeBuilderClass =
245    (Class<? extends StoreConfigurationBuilder<?, ?>>) ConfigurationUtils
246    .<StoreConfiguration>builderFor(storeConfiguration);
247  0 Builder<StoreConfiguration> storeBuilder =
248    (Builder<StoreConfiguration>) persistence.addStore(storeBuilderClass);
249  0 storeBuilder.read(storeConfiguration);
250    }
251    }
252    }
253   
254  137 return builder;
255    }
256   
257    /**
258    * Customize provided configuration based on XWiki cache configuration.
259    *
260    * @param defaultConfiguration the default Infinispan configuration
261    * @param namedConfiguration the named default Infinispan configuration
262    * @return the new configuration or the passed one if nothing changed
263    */
 
264  1873 toggle public Configuration customize(Configuration defaultConfiguration, Configuration namedConfiguration)
265    {
266    // Set custom configuration
267   
268  1873 ConfigurationBuilder builder = null;
269   
270  1873 if (namedConfiguration == null) {
271  1736 builder = customizeEviction(builder, defaultConfiguration);
272    }
273   
274    // Make sure filesystem based caches have a proper location
275   
276  1873 if (namedConfiguration != null) {
277  137 builder = completeFilesystem(builder, namedConfiguration);
278    }
279   
280  1873 return builder != null ? builder.build() : null;
281    }
282    }