1. Project Clover database Sat Feb 2 2019 06:45:20 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
25
5
1
169
91
12
0.48
5
5
2.4

Classes

Class Line # Actions
InfinispanCacheFactory 56 25 0% 12 1
0.97597.5%
 

Contributing tests

This file is covered by 102 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: 28f8df478af6edb179ad7ab7eef87129ca81a6a4 $
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  240 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  240 try {
93  240 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  240 InputStream configurationStream = getConfigurationFileAsStream();
99   
100  240 if (configurationStream != null) {
101    // CacheManager initialization
102  39 try {
103  39 this.cacheManager = new DefaultCacheManager(configurationStream);
104    } catch (IOException e) {
105  0 throw new InitializationException("Failed to create Infinispan cache manager", e);
106    } finally {
107  39 IOUtils.closeQuietly(configurationStream);
108    }
109    } else {
110  201 this.cacheManager = new DefaultCacheManager();
111    }
112    }
113   
 
114  235 toggle @Override
115    public void dispose() throws ComponentLifecycleException
116    {
117  235 getCacheManager().stop();
118    }
119   
120    /**
121    * @return the Infinispan cache manager
122    */
 
123  235 toggle public EmbeddedCacheManager getCacheManager()
124    {
125  235 return this.cacheManager;
126    }
127   
128    /**
129    * @return the default Infinispan configuration file in the container as stream
130    */
 
131  240 toggle private InputStream getConfigurationFileAsStream()
132    {
133  240 InputStream is = null;
134   
135  240 if (this.environment != null) {
136  227 is = this.environment.getResourceAsStream(DEFAULT_CONFIGURATION_FILE);
137    }
138   
139  240 return is;
140    }
141   
 
142  1873 toggle @Override
143    public <T> org.xwiki.cache.Cache<T> newCache(CacheConfiguration configuration) throws CacheException
144    {
145  1873 InfinispanConfigurationLoader loader = new InfinispanConfigurationLoader(configuration, this.environment);
146   
147  1873 String cacheName = configuration.getConfigurationId();
148   
149    // Set custom configuration
150   
151  1873 Configuration modifiedConfiguration =
152    loader.customize(this.cacheManager.getDefaultCacheConfiguration(),
153  1873 cacheName != null ? this.cacheManager.getCacheConfiguration(cacheName) : null);
154   
155  1873 if (cacheName == null) {
156    // Infinispan require a name for the cache
157  10 cacheName = UUID.randomUUID().toString();
158  10 loader.getCacheConfiguration().setConfigurationId(cacheName);
159    }
160   
161  1873 if (modifiedConfiguration != null) {
162  772 this.cacheManager.defineConfiguration(cacheName, modifiedConfiguration);
163    }
164   
165    // create cache
166   
167  1873 return new InfinispanCache<T>(this.cacheManager, loader.getCacheConfiguration());
168    }
169    }