1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named(XWikiPreferencesTranslationBundle.ID) |
56 |
|
@Singleton |
|
|
| 86.5% |
Uncovered Elements: 7 (52) |
Complexity: 18 |
Complexity Density: 0.55 |
|
57 |
|
public class XWikiPreferencesTranslationBundle extends AbstractTranslationBundle implements Initializable |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
protected static final String ID = "XWikiPreferences"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
protected static final String IDPREFIX = "XWikiPreferences:"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private CacheManager cacheManager; |
74 |
|
|
75 |
|
|
76 |
|
@link |
77 |
|
|
78 |
|
@Inject |
79 |
|
private ComponentManager componentManager; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
@Named("messagetool/1.0") |
86 |
|
private TranslationMessageParser translationMessageParser; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@Inject |
92 |
|
@Named("uid") |
93 |
|
private EntityReferenceSerializer<String> uidSerializer; |
94 |
|
|
95 |
|
@Inject |
96 |
|
private Provider<XWikiContext> contextProvider; |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
private Map<String, XWikiPreferencesWikiTranslationBundle> wikiBundlesCache = |
102 |
|
new ConcurrentHashMap<String, XWikiPreferencesWikiTranslationBundle>(); |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
private Cache<XWikiPreferencesDocumentTranslationBundle> documentBundlesCache; |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
70 |
public XWikiPreferencesTranslationBundle()... |
113 |
|
{ |
114 |
70 |
super(ID, 300); |
115 |
|
} |
116 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
117 |
70 |
@Override... |
118 |
|
public void initialize() throws InitializationException |
119 |
|
{ |
120 |
|
|
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 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
130 |
165943 |
@Override... |
131 |
|
public Translation getTranslation(String key, Locale locale) |
132 |
|
{ |
133 |
165943 |
XWikiContext xcontext = this.contextProvider.get(); |
134 |
|
|
135 |
|
|
136 |
165944 |
return xcontext != null && xcontext.getWiki() != null ? getBundle().getTranslation(key, locale) : null; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
@return@link |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
142 |
165943 |
private XWikiPreferencesWikiTranslationBundle getBundle()... |
143 |
|
{ |
144 |
165945 |
String currentWiki = this.contextProvider.get().getWikiId(); |
145 |
|
|
146 |
165945 |
return getBundle(currentWiki); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
@param |
151 |
|
@return@link |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
153 |
165942 |
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 |
|
@link |
165 |
|
|
166 |
|
@param |
167 |
|
@return@link |
168 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
169 |
73 |
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 |
186 |
|
@return |
187 |
|
@throws |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
189 |
73 |
private XWikiPreferencesWikiTranslationBundle createWikiBundle(String wiki) throws ComponentLookupException... |
190 |
|
{ |
191 |
73 |
return new XWikiPreferencesWikiTranslationBundle(wiki, this, this.componentManager); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@return |
199 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
200 |
9 |
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 |
|
|
214 |
|
|
215 |
|
@param |
216 |
|
@param |
217 |
|
@return |
218 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
219 |
9 |
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 |
|
|
231 |
0 |
this.logger.error("Failed to create document bundle for document [{}]", document, e); |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
9 |
return documentBundle; |
236 |
|
} |
237 |
|
} |