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

File AbstractCachedTranslationBundle.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
24
6
1
129
64
13
0.54
4
6
2.17

Classes

Class Line # Actions
AbstractCachedTranslationBundle 35 24 0% 13 9
0.7857142778.6%
 

Contributing tests

This file is covered by 19 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.internal;
21   
22    import java.util.Locale;
23    import java.util.Map;
24    import java.util.concurrent.ConcurrentHashMap;
25   
26    import org.xwiki.localization.LocaleUtils;
27    import org.xwiki.localization.Translation;
28   
29    /**
30    * Extends {@link AbstractTranslationBundle} and add {@link Locale} based cache management.
31    *
32    * @version $Id: cfaf1e962de0bb3e0ffa9e3dde63051bdf40d493 $
33    * @since 4.3M2
34    */
 
35    public abstract class AbstractCachedTranslationBundle extends AbstractTranslationBundle
36    {
37    /**
38    * The bundle cache.
39    */
40    protected Map<Locale, LocalizedTranslationBundle> bundleCache =
41    new ConcurrentHashMap<Locale, LocalizedTranslationBundle>();
42   
43    /**
44    * Default constructor.
45    */
 
46  216 toggle protected AbstractCachedTranslationBundle()
47    {
48    }
49   
50    /**
51    * @param id the identifier of the bundle
52    */
 
53  0 toggle public AbstractCachedTranslationBundle(String id)
54    {
55  0 super(id);
56    }
57   
58    /**
59    * @param id the identifier of the bundle
60    * @param priority the priority of the bundle
61    */
 
62  0 toggle public AbstractCachedTranslationBundle(String id, int priority)
63    {
64  0 super(id, priority);
65    }
66   
67    /**
68    * @param locale the Locale
69    * @return the bundle containing translation for the passed Locale
70    */
 
71  1384037 toggle private LocalizedTranslationBundle getLocalizedBundle(Locale locale)
72    {
73  1384038 LocalizedTranslationBundle bundle = this.bundleCache.get(locale);
74  1384034 if (bundle == null) {
75  440 bundle = getSynchLocalizedBundle(locale);
76    }
77   
78  1384037 return bundle;
79    }
80   
81    /**
82    * @param locale the Locale
83    * @return the bundle containing translation for the passed Locale
84    */
 
85  440 toggle private synchronized LocalizedTranslationBundle getSynchLocalizedBundle(Locale locale)
86    {
87  440 LocalizedTranslationBundle bundle = this.bundleCache.get(locale);
88   
89  440 if (bundle == null) {
90  440 try {
91  440 bundle = createBundle(locale);
92  440 if (bundle != null) {
93  440 this.bundleCache.put(locale, bundle);
94    }
95    } catch (Exception e) {
96  0 this.logger.error("Failed to get localization bundle", e);
97    }
98    }
99   
100  440 return bundle;
101    }
102   
 
103  1384035 toggle @Override
104    public Translation getTranslation(String key, Locale locale)
105    {
106  1384036 Translation translation;
107   
108  1384037 LocalizedTranslationBundle bundle = getLocalizedBundle(locale);
109  1384033 if (bundle != null) {
110  1384035 translation = bundle.getTranslation(key);
111  1384037 if (translation == null) {
112  1228519 Locale parentLocale = LocaleUtils.getParentLocale(locale);
113  1228524 if (parentLocale != null) {
114  462920 translation = getTranslation(key, parentLocale);
115    }
116    }
117    } else {
118  0 translation = null;
119    }
120   
121  1384041 return translation;
122    }
123   
124    /**
125    * @param locale the locale
126    * @return the bundle containing translation for the passed Locale
127    */
128    protected abstract LocalizedTranslationBundle createBundle(Locale locale) throws Exception;
129    }