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

File DefaultTranslationBundleContext.java

 

Coverage histogram

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

Code metrics

10
32
6
1
169
98
12
0.38
5.33
6
2

Classes

Class Line # Actions
DefaultTranslationBundleContext 54 32 0% 12 2
0.958333395.8%
 

Contributing tests

This file is covered by 33 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.Collection;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.SortedSet;
27    import java.util.TreeSet;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Provider;
32    import javax.inject.Singleton;
33   
34    import org.slf4j.Logger;
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.context.Execution;
39    import org.xwiki.context.ExecutionContext;
40    import org.xwiki.localization.TranslationBundle;
41    import org.xwiki.localization.TranslationBundleContext;
42    import org.xwiki.model.EntityType;
43    import org.xwiki.model.ModelContext;
44    import org.xwiki.model.reference.EntityReference;
45   
46    /**
47    * Default implementation of {@link TranslationBundleContext}.
48    *
49    * @version $Id: c032b0f3cfc21ca71fad00bd72668683700b3167 $
50    * @since 4.3M2
51    */
52    @Component
53    @Singleton
 
54    public class DefaultTranslationBundleContext implements TranslationBundleContext
55    {
56    /**
57    * The key associated to the list of bundles in the {@link ExecutionContext}.
58    */
59    public static final String CKEY_BUNDLES = "localization.bundles";
60   
61    /**
62    * Used to access the current context.
63    */
64    @Inject
65    private Execution execution;
66   
67    /**
68    * Used to access Bundles registered as components.
69    */
70    @Inject
71    @Named("context")
72    private Provider<ComponentManager> componentManagerProvider;
73   
74    /**
75    * The logger.
76    */
77    @Inject
78    private Logger logger;
79   
80    @Inject
81    private ModelContext modelContext;
82   
 
83  2911 toggle private SortedSet<TranslationBundle> initializeCurrentBundles()
84    {
85  2909 SortedSet<TranslationBundle> currentBundles = new TreeSet<>();
86   
87  2911 try {
88  2909 ComponentManager componentManager = this.componentManagerProvider.get();
89  2910 List<TranslationBundle> availableBundles =
90    componentManager.<TranslationBundle>getInstanceList(TranslationBundle.class);
91  2911 currentBundles.addAll(availableBundles);
92    } catch (ComponentLookupException e) {
93  0 this.logger.error("Failed to lookup Bundle components", e);
94    }
95   
96  2910 return currentBundles;
97    }
98   
99    /**
100    * @return the current bundles
101    */
 
102  242319 toggle private Map<String, SortedSet<TranslationBundle>> getBundlesInternal()
103    {
104  242319 Map<String, SortedSet<TranslationBundle>> bundles;
105   
106  242320 ExecutionContext context = this.execution.getContext();
107  242320 if (context != null) {
108  242300 bundles = (Map<String, SortedSet<TranslationBundle>>) context.getProperty(CKEY_BUNDLES);
109   
110  242298 if (bundles == null) {
111    // Register the Execution Context property with an empty map that will be populated for each wiki.
112  2888 bundles = new HashMap<>();
113  2885 context.newProperty(CKEY_BUNDLES).inherited().cloneValue().initial(bundles).declare();
114    }
115    } else {
116  20 bundles = new HashMap<>();
117    }
118   
119  242320 return bundles;
120    }
121   
 
122  242308 toggle private SortedSet<TranslationBundle> getCurrentBundlesInternal()
123    {
124  242308 String currentWiki = getCurrentWiki();
125  242318 Map<String, SortedSet<TranslationBundle>> bundlesMap = getBundlesInternal();
126  242320 SortedSet<TranslationBundle> currentBundles = bundlesMap.get(currentWiki);
127   
128  242321 if (currentBundles == null) {
129    // The context wiki has changed, initialize the bundles for the new current wiki.
130  2910 currentBundles = initializeCurrentBundles();
131  2910 bundlesMap.put(currentWiki, currentBundles);
132    }
133   
134  242320 return currentBundles;
135    }
136   
 
137  242306 toggle @Override
138    public Collection<TranslationBundle> getBundles()
139    {
140  242307 return getCurrentBundlesInternal();
141    }
142   
 
143  2 toggle @Override
144    public void addBundle(TranslationBundle bundle)
145    {
146    // Add the bundle to the current wiki's bundles. This makes sure that onDemand bundles are visible/isolated to
147    // the wiki they were demanded from (i.e. displaying a document from another wiki that includes an onDemand
148    // bundle will not affect the bundles of the wiki of the calling document, when the display finishes, so they
149    // will be properly isolated. This is valid the other way around as well.)
150  2 getCurrentBundlesInternal().add(bundle);
151    }
152   
 
153  242307 toggle private String getCurrentWiki()
154    {
155    // If, for some reason the current wiki is not set, avoid a NPE by using an empty string as key in the bundles
156    // map.
157  242308 String currentWiki = "";
158   
159  242307 EntityReference currentReference = modelContext.getCurrentEntityReference();
160  242320 if (currentReference != null) {
161  242300 EntityReference wikiReference = currentReference.extractReference(EntityType.WIKI);
162  242299 if (wikiReference != null) {
163  242300 currentWiki = wikiReference.getName();
164    }
165    }
166   
167  242317 return currentWiki;
168    }
169    }