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

File WikiDescriptorCache.java

 

Coverage histogram

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

Code metrics

0
18
10
1
167
76
11
0.61
1.8
10
1.1

Classes

Class Line # Actions
WikiDescriptorCache 44 18 0% 11 1
0.9642857396.4%
 

Contributing tests

This file is covered by 4 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.wiki.internal.manager;
21   
22    import java.util.Collection;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.cache.Cache;
28    import org.xwiki.cache.CacheException;
29    import org.xwiki.cache.CacheManager;
30    import org.xwiki.cache.config.CacheConfiguration;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.phase.Initializable;
33    import org.xwiki.component.phase.InitializationException;
34    import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;
35   
36    /**
37    * Component that handle caching for wiki descriptors.
38    *
39    * @version $Id: 90fafedd21af7a1fb9e210c1e51654c92479b909 $
40    * @since 5.3M2
41    */
42    @Component(roles = WikiDescriptorCache.class)
43    @Singleton
 
44    public class WikiDescriptorCache implements Initializable
45    {
46    @Inject
47    private CacheManager cacheManager;
48   
49    private Cache<DefaultWikiDescriptor> wikiAliasCache;
50   
51    private Cache<DefaultWikiDescriptor> wikiIdCache;
52   
53    private Collection<String> wikiIds;
54   
 
55  69 toggle @Override
56    public void initialize() throws InitializationException
57    {
58  69 this.wikiAliasCache = createCache("wiki.descriptor.cache.wikiAlias");
59  69 this.wikiIdCache = createCache("wiki.descriptor.cache.wikiId");
60    }
61   
 
62  138 toggle private Cache<DefaultWikiDescriptor> createCache(String cacheId) throws InitializationException
63    {
64  138 CacheConfiguration configuration = new CacheConfiguration(cacheId);
65   
66  138 try {
67  138 return this.cacheManager.createNewCache(configuration);
68    } catch (CacheException e) {
69  0 throw new InitializationException(String.format("Failed to initialize wiki descriptor caches [%s]",
70    configuration.getConfigurationId()), e);
71    }
72    }
73   
74    /**
75    * Add a descriptor to the cache.
76    *
77    * @param descriptor descriptor to add
78    */
 
79  106 toggle public void add(DefaultWikiDescriptor descriptor)
80    {
81    // Update the wiki name cache
82  106 addFromId(descriptor.getId(), descriptor);
83   
84    // Update the wiki alias cache
85  106 for (String alias : descriptor.getAliases()) {
86  107 addFromAlias(alias, descriptor);
87    }
88    }
89   
90    /**
91    * Add a descriptor to the cache.
92    *
93    * @param wikiAlias Alias of the wiki to get
94    * @param descriptor descriptor to add
95    */
 
96  107 toggle public void addFromAlias(String wikiAlias, DefaultWikiDescriptor descriptor)
97    {
98  107 this.wikiAliasCache.set(wikiAlias, descriptor);
99    }
100   
101    /**
102    * Add a descriptor to the cache.
103    *
104    * @param wikiId Id of the wiki to get
105    * @param descriptor descriptor to add
106    */
 
107  106 toggle public void addFromId(String wikiId, DefaultWikiDescriptor descriptor)
108    {
109  106 this.wikiIdCache.set(wikiId, descriptor);
110    }
111   
112    /**
113    * Remove a descriptor from the cache.
114    *
115    * @param descriptor descriptor to remove
116    */
 
117  45 toggle public void remove(DefaultWikiDescriptor descriptor)
118    {
119    // Remove from the wiki name cache
120  45 this.wikiIdCache.remove(descriptor.getId());
121   
122    // Remove from the wiki alias cache
123  45 for (String alias : descriptor.getAliases()) {
124  46 this.wikiAliasCache.remove(alias);
125    }
126    }
127   
128    /**
129    * Get a descriptor from the cache.
130    *
131    * @param wikiId Id of the wiki to get
132    * @return the descriptor related to the id or null if there is no corresponding descriptor in the cache
133    */
 
134  8201 toggle public DefaultWikiDescriptor getFromId(String wikiId)
135    {
136  8202 return wikiIdCache.get(wikiId);
137    }
138   
139    /**
140    * Get a descriptor from the cache.
141    *
142    * @param wikiAlias Alias of the wiki to get
143    * @return the descriptor related to the alias or null if there is no corresponding descriptor in the cache
144    */
 
145  11586 toggle public DefaultWikiDescriptor getFromAlias(String wikiAlias)
146    {
147  11586 return wikiAliasCache.get(wikiAlias);
148    }
149   
150    /**
151    * @param wikiIds the full list of wikis identifiers
152    * @since 6.2M1
153    */
 
154  199 toggle public void setWikiIds(Collection<String> wikiIds)
155    {
156  199 this.wikiIds = wikiIds;
157    }
158   
159    /**
160    * @return the full list of wikis identifiers
161    * @since 6.2M1
162    */
 
163  415 toggle public Collection<String> getWikiIds()
164    {
165  415 return this.wikiIds;
166    }
167    }