1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization.legacy.internal.xwikipreferences

File XWikiPreferencesTranslationBundle.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
33
9
1
237
121
18
0.55
3.67
9
2

Classes

Class Line # Actions
XWikiPreferencesTranslationBundle 57 33 0% 18 7
0.8653846486.5%
 

Contributing tests

This file is covered by 10 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.localization.legacy.internal.xwikipreferences;
21   
22    import java.util.Locale;
23    import java.util.Map;
24    import java.util.concurrent.ConcurrentHashMap;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.cache.Cache;
32    import org.xwiki.cache.CacheException;
33    import org.xwiki.cache.CacheManager;
34    import org.xwiki.cache.config.CacheConfiguration;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.component.manager.ComponentLookupException;
37    import org.xwiki.component.manager.ComponentManager;
38    import org.xwiki.component.phase.Initializable;
39    import org.xwiki.component.phase.InitializationException;
40    import org.xwiki.localization.Translation;
41    import org.xwiki.localization.internal.AbstractTranslationBundle;
42    import org.xwiki.localization.message.TranslationMessageParser;
43    import org.xwiki.model.reference.DocumentReference;
44    import org.xwiki.model.reference.EntityReferenceSerializer;
45   
46    import com.xpn.xwiki.XWikiContext;
47   
48    /**
49    * Bundle corresponding to global (at the wiki level) localization documents.
50    *
51    * @version $Id: 6e2a104023fbff0bcba2e0e628e3f060f055bba7 $
52    * @since 4.3M2
53    */
54    @Component
55    @Named(XWikiPreferencesTranslationBundle.ID)
56    @Singleton
 
57    public class XWikiPreferencesTranslationBundle extends AbstractTranslationBundle implements Initializable
58    {
59    /**
60    * The identifier of the bundle.
61    */
62    protected static final String ID = "XWikiPreferences";
63   
64    /**
65    * The prefix to use when generating documents bundles unique ids.
66    */
67    protected static final String IDPREFIX = "XWikiPreferences:";
68   
69    /**
70    * Used to create a cache.
71    */
72    @Inject
73    private CacheManager cacheManager;
74   
75    /**
76    * Passed to {@link XWikiPreferencesWikiTranslationBundle}.
77    */
78    @Inject
79    private ComponentManager componentManager;
80   
81    /**
82    * Used to parse translation messages.
83    */
84    @Inject
85    @Named("messagetool/1.0")
86    private TranslationMessageParser translationMessageParser;
87   
88    /**
89    * Used to generate a document unique String identifier.
90    */
91    @Inject
92    @Named("uid")
93    private EntityReferenceSerializer<String> uidSerializer;
94   
95    @Inject
96    private Provider<XWikiContext> contextProvider;
97   
98    /**
99    * The cache of bundles by wiki.
100    */
101    private Map<String, XWikiPreferencesWikiTranslationBundle> wikiBundlesCache =
102    new ConcurrentHashMap<String, XWikiPreferencesWikiTranslationBundle>();
103   
104    /**
105    * The cache of bundles by document id.
106    */
107    private Cache<XWikiPreferencesDocumentTranslationBundle> documentBundlesCache;
108   
109    /**
110    * Default constructor.
111    */
 
112  70 toggle public XWikiPreferencesTranslationBundle()
113    {
114  70 super(ID, 300);
115    }
116   
 
117  70 toggle @Override
118    public void initialize() throws InitializationException
119    {
120    // Setup cache
121  70 CacheConfiguration cacheConfiguration = new CacheConfiguration("localization." + getId());
122   
123  70 try {
124  70 this.documentBundlesCache = this.cacheManager.createNewCache(cacheConfiguration);
125    } catch (CacheException e) {
126  0 this.logger.error("Failed to create cache [{}]", cacheConfiguration.getConfigurationId(), e);
127    }
128    }
129   
 
130  165943 toggle @Override
131    public Translation getTranslation(String key, Locale locale)
132    {
133  165943 XWikiContext xcontext = this.contextProvider.get();
134   
135    // Don't do anything when XWiki is not ready
136  165944 return xcontext != null && xcontext.getWiki() != null ? getBundle().getTranslation(key, locale) : null;
137    }
138   
139    /**
140    * @return the {@link XWikiPreferencesTranslationBundle} for the current wiki
141    */
 
142  165943 toggle private XWikiPreferencesWikiTranslationBundle getBundle()
143    {
144  165945 String currentWiki = this.contextProvider.get().getWikiId();
145   
146  165945 return getBundle(currentWiki);
147    }
148   
149    /**
150    * @param wiki the wiki
151    * @return the {@link XWikiPreferencesWikiTranslationBundle} for the provided wiki
152    */
 
153  165942 toggle private XWikiPreferencesWikiTranslationBundle getBundle(String wiki)
154    {
155  165943 XWikiPreferencesWikiTranslationBundle bundle = this.wikiBundlesCache.get(wiki);
156  165940 if (bundle == null) {
157  73 bundle = getBundleSynchronized(wiki);
158    }
159   
160  165942 return bundle;
161    }
162   
163    /**
164    * Synchronized version of {@link #getBundle()} so that we synchronize only when necessary.
165    *
166    * @param wiki the wiki
167    * @return the {@link XWikiPreferencesWikiTranslationBundle} for the provided wiki
168    */
 
169  73 toggle private synchronized XWikiPreferencesWikiTranslationBundle getBundleSynchronized(String wiki)
170    {
171  73 XWikiPreferencesWikiTranslationBundle bundle = this.wikiBundlesCache.get(wiki);
172  73 if (bundle == null) {
173  73 try {
174  73 bundle = createWikiBundle(wiki);
175  73 this.wikiBundlesCache.put(wiki, bundle);
176    } catch (ComponentLookupException e) {
177  0 this.logger.error("Failed to create preferences bundle for wiki [{}]", wiki, e);
178    }
179    }
180   
181  73 return bundle;
182    }
183   
184    /**
185    * @param wiki the wiki
186    * @return the XWikiPreferencesWikiBundle for the provided wiki
187    * @throws ComponentLookupException failed to create the bundle
188    */
 
189  73 toggle private XWikiPreferencesWikiTranslationBundle createWikiBundle(String wiki) throws ComponentLookupException
190    {
191  73 return new XWikiPreferencesWikiTranslationBundle(wiki, this, this.componentManager);
192    }
193   
194    /**
195    * Get document bundle from passed reference.
196    *
197    * @param document the document reference
198    * @return the document bundle
199    */
 
200  9 toggle protected XWikiPreferencesDocumentTranslationBundle getDocumentTranslationBundle(DocumentReference document)
201    {
202  9 String uid = this.uidSerializer.serialize(document);
203   
204  9 XWikiPreferencesDocumentTranslationBundle documentBundle = this.documentBundlesCache.get(uid);
205  9 if (documentBundle == null) {
206  9 documentBundle = getDocumentTranslationBundleSynchronized(uid, document);
207    }
208   
209  9 return documentBundle;
210    }
211   
212    /**
213    * Get document bundle from passed reference.
214    *
215    * @param uid the bundle uid
216    * @param document the document reference
217    * @return the document bundle
218    */
 
219  9 toggle private synchronized XWikiPreferencesDocumentTranslationBundle getDocumentTranslationBundleSynchronized(String uid,
220    DocumentReference document)
221    {
222  9 XWikiPreferencesDocumentTranslationBundle documentBundle = this.documentBundlesCache.get(uid);
223  9 if (documentBundle == null) {
224  9 try {
225  9 documentBundle =
226    new XWikiPreferencesDocumentTranslationBundle(IDPREFIX, document, this.componentManager,
227    this.translationMessageParser);
228  9 this.documentBundlesCache.set(uid, documentBundle);
229    } catch (ComponentLookupException e) {
230    // Should never happen
231  0 this.logger.error("Failed to create document bundle for document [{}]", document, e);
232    }
233    }
234   
235  9 return documentBundle;
236    }
237    }