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

File JARTranslationBundleFactoryListener.java

 

Coverage histogram

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

Code metrics

12
41
9
1
243
140
17
0.41
4.56
9
1.89

Classes

Class Line # Actions
JARTranslationBundleFactoryListener 62 41 0% 17 6
0.903225890.3%
 

Contributing tests

This file is covered by 5 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.jar.internal;
21   
22    import java.io.File;
23    import java.net.MalformedURLException;
24    import java.net.URL;
25    import java.util.Arrays;
26    import java.util.Collection;
27    import java.util.List;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.descriptor.ComponentDescriptor;
36    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
37    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
38    import org.xwiki.component.internal.multi.ComponentManagerManager;
39    import org.xwiki.component.manager.ComponentManager;
40    import org.xwiki.component.phase.Initializable;
41    import org.xwiki.component.phase.InitializationException;
42    import org.xwiki.extension.InstalledExtension;
43    import org.xwiki.extension.event.ExtensionEvent;
44    import org.xwiki.extension.event.ExtensionInstalledEvent;
45    import org.xwiki.extension.event.ExtensionUninstalledEvent;
46    import org.xwiki.extension.event.ExtensionUpgradedEvent;
47    import org.xwiki.extension.repository.InstalledExtensionRepository;
48    import org.xwiki.localization.TranslationBundle;
49    import org.xwiki.localization.message.TranslationMessageParser;
50    import org.xwiki.observation.EventListener;
51    import org.xwiki.observation.event.Event;
52   
53    /**
54    * Generate and manage resource based translations bundles.
55    *
56    * @version $Id: 272749cd428ded25bd04060520b90b659738bc38 $
57    * @since 4.5M1
58    */
59    @Component
60    @Named(JARTranslationBundleFactoryListener.NAME)
61    @Singleton
 
62    public class JARTranslationBundleFactoryListener implements EventListener, Initializable
63    {
64    /**
65    * The name of the event listener.
66    */
67    protected static final String NAME = "localization.bundle.JARTranslationBundleFactoryListener";
68   
69    /**
70    * The type of extension supported by this translation bundle.
71    */
72    private static final String EXTENSION_TYPE = "jar";
73   
74    /**
75    * The events to listen.
76    */
77    private static final List<Event> EVENTS = Arrays.<Event> asList(new ExtensionInstalledEvent(),
78    new ExtensionUninstalledEvent(), new ExtensionUpgradedEvent());
79   
80    /**
81    * Used to create or access right component manager depending of a namespace.
82    */
83    @Inject
84    private ComponentManagerManager componentManagerManager;
85   
86    /**
87    * The root component manager to fallback on.
88    */
89    @Inject
90    private ComponentManager rootComponentManager;
91   
92    /**
93    * Used to parse translation messages.
94    */
95    @Inject
96    @Named("messagetool/1.0")
97    private TranslationMessageParser translationParser;
98   
99    /**
100    * The virtual repository containing installed extensions.
101    */
102    @Inject
103    private InstalledExtensionRepository installedRepository;
104   
105    /**
106    * USed to log.
107    */
108    @Inject
109    private Logger logger;
110   
 
111  37 toggle @Override
112    public void onEvent(Event event, Object source, Object data)
113    {
114  37 ExtensionEvent extensionEvent = (ExtensionEvent) event;
115  37 InstalledExtension extension = (InstalledExtension) source;
116   
117  37 if (extension.getType().equals(EXTENSION_TYPE)) {
118  9 if (event instanceof ExtensionInstalledEvent) {
119  6 extensionAdded(extension, extensionEvent.getNamespace());
120  3 } else if (event instanceof ExtensionUninstalledEvent) {
121  2 extensionDeleted(extension, extensionEvent.getNamespace());
122    } else {
123  1 extensionUpgraded(extension, (Collection<InstalledExtension>) data, extensionEvent.getNamespace());
124    }
125    }
126    }
127   
 
128  377 toggle @Override
129    public String getName()
130    {
131  377 return "localization.bundle.JARTranslationBundleFactory";
132    }
133   
 
134  74 toggle @Override
135    public List<Event> getEvents()
136    {
137  74 return EVENTS;
138    }
139   
 
140  75 toggle @Override
141    public void initialize() throws InitializationException
142    {
143    // Load installed extensions
144  75 for (InstalledExtension extension : this.installedRepository.getInstalledExtensions()) {
145  1 if (extension.getType().equals(EXTENSION_TYPE)) {
146  1 if (extension.isInstalled(null)) {
147  1 extensionAdded(extension, null);
148    } else {
149  0 for (String namespace : extension.getNamespaces()) {
150  0 extensionAdded(extension, namespace);
151    }
152    }
153    }
154    }
155    }
156   
157    /**
158    * @param jarURL the jar URL
159    * @return the component descriptor to use to register/unregister the translation bundle
160    */
 
161  11 toggle private ComponentDescriptor<TranslationBundle> createComponentDescriptor(URL jarURL)
162    {
163  11 DefaultComponentDescriptor<TranslationBundle> descriptor = new DefaultComponentDescriptor<TranslationBundle>();
164   
165  11 descriptor.setImplementation(JARFileTranslationBundle.class);
166  11 descriptor.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
167  11 descriptor.setRoleHint(JARTranslationBundleFactory.ID + ':' + jarURL);
168  11 descriptor.setRoleType(TranslationBundle.class);
169   
170  11 return descriptor;
171    }
172   
173    /**
174    * @param extension the jar extension
175    * @return the component descriptor to use to register/unregister the translation bundle
176    * @throws MalformedURLException failed to create URL for the extension file
177    */
 
178  3 toggle private ComponentDescriptor<TranslationBundle> createComponentDescriptor(InstalledExtension extension)
179    throws MalformedURLException
180    {
181  3 File jarFile = new File(extension.getFile().getAbsolutePath());
182   
183  3 return createComponentDescriptor(jarFile.toURI().toURL());
184    }
185   
186    /**
187    * @param newExtension the installed extension
188    * @param previousExtensions the previous version of the extensions
189    * @param namespace the namespace where this upgrade took place
190    */
 
191  1 toggle private void extensionUpgraded(InstalledExtension newExtension, Collection<InstalledExtension> previousExtensions,
192    String namespace)
193    {
194  1 for (InstalledExtension previousExtension : previousExtensions) {
195  1 extensionDeleted(previousExtension, namespace);
196    }
197  1 extensionAdded(newExtension, namespace);
198    }
199   
200    /**
201    * @param extension the installed extension
202    * @param namespace the namespace where the extension has been installed
203    */
 
204  3 toggle private void extensionDeleted(InstalledExtension extension, String namespace)
205   
206    {
207  3 try {
208  3 ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(extension);
209   
210  3 ComponentManager componentManager = this.componentManagerManager.getComponentManager(namespace, false);
211   
212  3 componentManager.unregisterComponent(descriptor);
213    } catch (Exception e) {
214  0 this.logger.error("Failed to create TranslationBundle descriptor for extension [{}]", extension, e);
215    }
216    }
217   
218    /**
219    * @param extension the uninstalled extension
220    * @param namespace the namespace from where the extension has been uninstalled
221    */
 
222  8 toggle private void extensionAdded(InstalledExtension extension, String namespace)
223    {
224  8 try {
225  8 File jarFile = new File(extension.getFile().getAbsolutePath());
226   
227  8 ComponentManager componentManager = this.componentManagerManager.getComponentManager(namespace, false);
228   
229  8 if (componentManager == null) {
230  1 componentManager = this.rootComponentManager;
231    }
232   
233  8 JARFileTranslationBundle bundle =
234    new JARFileTranslationBundle(jarFile, componentManager, this.translationParser);
235   
236  8 ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(jarFile.toURI().toURL());
237  8 componentManager.registerComponent(descriptor, bundle);
238    } catch (Exception e) {
239  0 this.logger.error("Failed to register a TranslationBundle component for extension [{}] on namespace [{}]",
240    extension, namespace, e);
241    }
242    }
243    }