1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.cache.infinispan.internal

File InfinispanCacheFactory.java

 

Coverage histogram

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

Code metrics

10
26
5
1
172
92
12
0.46
5.2
5
2.4

Classes

Class Line # Actions
InfinispanCacheFactory 56 26 0% 12 1
0.975609897.6%
 

Contributing tests

This file is covered by 87 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 java.io.IOException;
23    import java.io.InputStream;
24    import java.util.UUID;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.io.IOUtils;
31    import org.infinispan.configuration.cache.Configuration;
32    import org.infinispan.manager.DefaultCacheManager;
33    import org.infinispan.manager.EmbeddedCacheManager;
34    import org.slf4j.Logger;
35    import org.xwiki.cache.CacheException;
36    import org.xwiki.cache.CacheFactory;
37    import org.xwiki.cache.config.CacheConfiguration;
38    import org.xwiki.component.annotation.Component;
39    import org.xwiki.component.manager.ComponentLifecycleException;
40    import org.xwiki.component.manager.ComponentLookupException;
41    import org.xwiki.component.manager.ComponentManager;
42    import org.xwiki.component.phase.Disposable;
43    import org.xwiki.component.phase.Initializable;
44    import org.xwiki.component.phase.InitializationException;
45    import org.xwiki.environment.Environment;
46   
47    /**
48    * Implements {@link org.xwiki.cache.CacheFactory} based on Infinispan.
49    *
50    * @version $Id: 9f92cf9548852bb31df6874f53bfd56d500cd772 $
51    * @since 3.3M1
52    */
53    @Component
54    @Named("infinispan")
55    @Singleton
 
56    public class InfinispanCacheFactory implements CacheFactory, Initializable, Disposable
57    {
58    /**
59    * The folder containing Infinispan properties files.
60    */
61    private static final String DEFAULT_CONFIGURATION_FILE = "/WEB-INF/cache/infinispan/config.xml";
62   
63    /**
64    * The logger to log.
65    */
66    @Inject
67    private Logger logger;
68   
69    /**
70    * Used to lookup the container.
71    */
72    @Inject
73    private ComponentManager componentManager;
74   
75    /**
76    * Optional Environment used to access configuration files.
77    */
78    private Environment environment;
79   
80    /**
81    * Used to create Infinispan caches.
82    */
83    private EmbeddedCacheManager cacheManager;
84   
 
85  207 toggle @Override
86    public void initialize() throws InitializationException
87    {
88    // Container
89    // Note that the reason we lazy load the container is because we want to be able to use the Cache in
90    // environments where there's no container.
91   
92  207 try {
93  207 this.environment = this.componentManager.getInstance(Environment.class);
94    } catch (ComponentLookupException e) {
95  13 this.logger.debug("Can't find any Environment", e);
96    }
97   
98  207 InputStream configurationStream = getConfigurationFileAsStream();
99   
100  207 if (configurationStream != null) {
101    // CacheManager initialization
102   
103  32 configurationStream = getConfigurationFileAsStream();
104   
105  32 try {
106  32 this.cacheManager = new DefaultCacheManager(configurationStream);
107    } catch (IOException e) {
108  0 throw new InitializationException("Failed to create Infinispan cache manager", e);
109    } finally {
110  32 IOUtils.closeQuietly(configurationStream);
111    }
112    } else {
113  175 this.cacheManager = new DefaultCacheManager();
114    }
115    }
116   
 
117  207 toggle @Override
118    public void dispose() throws ComponentLifecycleException
119    {
120  207 getCacheManager().stop();
121    }
122   
123    /**
124    * @return the Infinispan cache manager
125    */
 
126  207 toggle public EmbeddedCacheManager getCacheManager()
127    {
128  207 return this.cacheManager;
129    }
130   
131    /**
132    * @return the default Infinispan configuration file in the container as stream
133    */
 
134  239 toggle private InputStream getConfigurationFileAsStream()
135    {
136  239 InputStream is = null;
137   
138  239 if (this.environment != null) {
139  226 is = this.environment.getResourceAsStream(DEFAULT_CONFIGURATION_FILE);
140    }
141   
142  239 return is;
143    }
144   
 
145  1339 toggle @Override
146    public <T> org.xwiki.cache.Cache<T> newCache(CacheConfiguration configuration) throws CacheException
147    {
148  1339 InfinispanConfigurationLoader loader = new InfinispanConfigurationLoader(configuration, this.environment);
149   
150  1339 String cacheName = configuration.getConfigurationId();
151   
152    // Set custom configuration
153   
154  1339 Configuration modifiedConfiguration =
155    loader.customize(this.cacheManager.getDefaultCacheConfiguration(),
156  1339 cacheName != null ? this.cacheManager.getCacheConfiguration(cacheName) : null);
157   
158  1339 if (cacheName == null) {
159    // Infinispan require a name for the cache
160  11 cacheName = UUID.randomUUID().toString();
161  11 loader.getCacheConfiguration().setConfigurationId(cacheName);
162    }
163   
164  1339 if (modifiedConfiguration != null) {
165  430 this.cacheManager.defineConfiguration(cacheName, modifiedConfiguration);
166    }
167   
168    // create cache
169   
170  1339 return new InfinispanCache<T>(this.cacheManager, loader.getCacheConfiguration());
171    }
172    }