1. Project Clover database Tue Dec 20 2016 21:24:09 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 85 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  1339 toggle public InfinispanConfigurationLoader(CacheConfiguration configuration, Environment environment)
63    {
64  1339 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  541 toggle private ConfigurationBuilder builder(ConfigurationBuilder builder, Configuration isconfiguration)
73    {
74  541 if (builder != null) {
75  111 return builder;
76    }
77   
78  430 ConfigurationBuilder newBuilder = new ConfigurationBuilder();
79   
80  430 if (isconfiguration != null) {
81  32 newBuilder.read(isconfiguration);
82    }
83   
84  430 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  192 toggle private boolean containsIncompleteFileLoader(Configuration isconfiguration)
92    {
93  192 PersistenceConfiguration persistenceConfiguration = isconfiguration.persistence();
94   
95  192 for (StoreConfiguration storeConfiguration : persistenceConfiguration.stores()) {
96  32 if (storeConfiguration instanceof SingleFileStoreConfiguration) {
97  32 SingleFileStoreConfiguration singleFileStoreConfiguration =
98    (SingleFileStoreConfiguration) storeConfiguration;
99   
100  32 String location = singleFileStoreConfiguration.location();
101   
102    // "Infinispan-SingleFileStore" is the default location...
103  32 if (StringUtils.isBlank(location) || location.equals(DEFAULT_SINGLEFILESTORE_LOCATION)) {
104  32 return true;
105    }
106    }
107    }
108   
109  160 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  1147 toggle private ConfigurationBuilder customizeEviction(ConfigurationBuilder currentBuilder, Configuration configuration)
120    {
121  1147 ConfigurationBuilder builder = currentBuilder;
122   
123  1147 EntryEvictionConfiguration eec =
124    (EntryEvictionConfiguration) getCacheConfiguration().get(EntryEvictionConfiguration.CONFIGURATIONID);
125   
126  1147 if (eec != null && eec.getAlgorithm() == EntryEvictionConfiguration.Algorithm.LRU) {
127    ////////////////////
128    // Eviction
129    // Max entries
130  468 builder = customizeEvictionMaxEntries(builder, configuration, eec);
131   
132    ////////////////////
133    // Expiration
134    // Wakeup interval
135  468 builder = customizeExpirationWakeUpInterval(builder, configuration, eec);
136   
137    // Max idle
138  468 builder = customizeExpirationMaxIdle(builder, configuration, eec);
139   
140    // Lifespan
141  468 builder = customizeExpirationLifespan(builder, configuration, eec);
142    }
143   
144  1147 return builder;
145    }
146   
 
147  468 toggle private ConfigurationBuilder customizeEvictionMaxEntries(ConfigurationBuilder currentBuilder,
148    Configuration configuration, EntryEvictionConfiguration eec)
149    {
150  468 ConfigurationBuilder builder = currentBuilder;
151   
152  468 if (eec.containsKey(LRUEvictionConfiguration.MAXENTRIES_ID)) {
153  461 int maxEntries = ((Number) eec.get(LRUEvictionConfiguration.MAXENTRIES_ID)).intValue();
154  461 if (configuration.eviction() == null || configuration.eviction().strategy() != EvictionStrategy.LRU
155    || configuration.eviction().maxEntries() != maxEntries) {
156  395 builder = builder(builder, null);
157  395 builder.eviction().strategy(EvictionStrategy.LRU);
158  395 builder.eviction().type(EvictionType.COUNT).size(maxEntries);
159    }
160    }
161   
162  468 return builder;
163    }
164   
 
165  468 toggle private ConfigurationBuilder customizeExpirationWakeUpInterval(ConfigurationBuilder currentBuilder,
166    Configuration configuration, EntryEvictionConfiguration eec)
167    {
168  468 ConfigurationBuilder builder = currentBuilder;
169   
170  468 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  468 return builder;
176    }
177   
 
178  468 toggle private ConfigurationBuilder customizeExpirationMaxIdle(ConfigurationBuilder currentBuilder,
179    Configuration configuration, EntryEvictionConfiguration eec)
180    {
181  468 ConfigurationBuilder builder = currentBuilder;
182   
183  468 if (eec.getTimeToLive() > 0) {
184  102 long maxIdle = eec.getTimeToLive() * 1000L;
185  102 if (configuration.expiration() == null || configuration.expiration().maxIdle() != maxIdle) {
186  102 builder = builder(builder, null);
187  102 builder.expiration().maxIdle(eec.getTimeToLive() * 1000L);
188    }
189    }
190   
191  468 return builder;
192    }
193   
 
194  468 toggle private ConfigurationBuilder customizeExpirationLifespan(ConfigurationBuilder currentBuilder,
195    Configuration configuration, EntryEvictionConfiguration eec)
196    {
197  468 ConfigurationBuilder builder = currentBuilder;
198   
199  468 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  468 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  192 toggle private ConfigurationBuilder completeFilesystem(ConfigurationBuilder currentBuilder, Configuration configuration)
218    {
219  192 ConfigurationBuilder builder = currentBuilder;
220   
221  192 if (containsIncompleteFileLoader(configuration)) {
222  32 builder = builder(builder, configuration);
223   
224  32 PersistenceConfigurationBuilder persistence = builder.persistence();
225   
226  32 persistence.clearStores();
227   
228  32 for (StoreConfiguration storeConfiguration : configuration.persistence().stores()) {
229  32 if (storeConfiguration instanceof SingleFileStoreConfiguration) {
230  32 SingleFileStoreConfiguration singleFileStoreConfiguration =
231    (SingleFileStoreConfiguration) storeConfiguration;
232   
233  32 String location = singleFileStoreConfiguration.location();
234   
235    // "Infinispan-SingleFileStore" is the default location...
236  32 if (StringUtils.isBlank(location) || location.equals(DEFAULT_SINGLEFILESTORE_LOCATION)) {
237  32 SingleFileStoreConfigurationBuilder singleFileStoreConfigurationBuilder =
238    persistence.addSingleFileStore();
239  32 singleFileStoreConfigurationBuilder.read(singleFileStoreConfiguration);
240  32 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  192 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  1339 toggle public Configuration customize(Configuration defaultConfiguration, Configuration namedConfiguration)
265    {
266    // Set custom configuration
267   
268  1339 ConfigurationBuilder builder = null;
269   
270  1339 if (namedConfiguration == null) {
271  1147 builder = customizeEviction(builder, defaultConfiguration);
272    }
273   
274    // Make sure filesystem based caches have a proper location
275   
276  1339 if (namedConfiguration != null) {
277  192 builder = completeFilesystem(builder, namedConfiguration);
278    }
279   
280  1339 return builder != null ? builder.build() : null;
281    }
282    }