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

File DefaultFlavorManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
25
5
1
161
97
10
0.4
5
5
2

Classes

Class Line # Actions
DefaultFlavorManager 55 25 0% 10 36
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.platform.flavor.internal;
21   
22    import java.util.Collection;
23    import java.util.Collections;
24    import java.util.LinkedHashSet;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.extension.CoreExtension;
33    import org.xwiki.extension.Extension;
34    import org.xwiki.extension.ExtensionId;
35    import org.xwiki.extension.InstalledExtension;
36    import org.xwiki.extension.repository.CoreExtensionRepository;
37    import org.xwiki.extension.repository.ExtensionRepositoryManager;
38    import org.xwiki.extension.repository.InstalledExtensionRepository;
39    import org.xwiki.extension.repository.LocalExtensionRepository;
40    import org.xwiki.extension.repository.internal.RepositoryUtils;
41    import org.xwiki.extension.repository.result.IterableResult;
42    import org.xwiki.extension.repository.search.SearchException;
43    import org.xwiki.platform.flavor.FlavorManager;
44    import org.xwiki.platform.flavor.FlavorQuery;
45    import org.xwiki.properties.ConverterManager;
46   
47    /**
48    * Default implementation of {@link org.xwiki.platform.flavor.FlavorManager}.
49    *
50    * @version $Id: c5421b8cabf1341338ee75cbddd18497f5e3637b $
51    * @since 7.1M2
52    */
53    @Component
54    @Singleton
 
55    public class DefaultFlavorManager implements FlavorManager
56    {
57    @Inject
58    private ExtensionRepositoryManager extensionRepositoryManager;
59   
60    @Inject
61    private LocalExtensionRepository localRepository;
62   
63    @Inject
64    private InstalledExtensionRepository installedRepository;
65   
66    @Inject
67    private CoreExtensionRepository coreRepository;
68   
69    @Inject
70    private ConverterManager converter;
71   
72    @Inject
73    private ConfigurationSource configurationSource;
74   
75    @Inject
76    private Logger logger;
77   
 
78  0 toggle @Override
79    @Deprecated
80    public IterableResult<Extension> getFlavors(FlavorQuery query) throws SearchException
81    {
82  0 return searchFlavors(query);
83    }
84   
 
85  0 toggle @Override
86    public IterableResult<Extension> searchFlavors(FlavorQuery query) throws SearchException
87    {
88  0 IterableResult<Extension> result = null;
89   
90    // Search local flavors
91  0 try {
92  0 result = RepositoryUtils.appendSearchResults(result, this.localRepository.search(query));
93    } catch (SearchException e) {
94  0 this.logger.error("Failed to search in local repository", e);
95    }
96   
97    // Search remote flavors
98  0 result = RepositoryUtils.appendSearchResults(result, this.extensionRepositoryManager.search(query));
99   
100  0 return result;
101    }
102   
 
103  0 toggle @Override
104    public Collection<ExtensionId> getKnownFlavors()
105    {
106  0 Collection<ExtensionId> flavors = new LinkedHashSet<>();
107   
108    // Get flavors from environment extension
109  0 CoreExtension extension = this.coreRepository.getEnvironmentExtension();
110  0 if (extension != null) {
111  0 String flavorsString = extension.getProperty("xwiki.extension.knownFlavors");
112  0 flavorsString = flavorsString.replaceAll("[\r\n]", "");
113  0 flavors.addAll(this.converter.convert(ExtensionId.TYPE_LIST, flavorsString));
114    }
115   
116    // TODO: Get flavors from configuration
117    // flavors.addAll(configurationSource.getProperty("extension.flavor.known",
118    // Collections.<String>emptyList()));
119   
120  0 return flavors;
121    }
122   
 
123  0 toggle @Override
124    public ExtensionId getFlavorOfWiki(String wikiId)
125    {
126  0 String namespace = "wiki:" + wikiId;
127  0 try {
128  0 for (InstalledExtension extension : installedRepository.searchInstalledExtensions(namespace,
129    new FlavorQuery())) {
130    // Don't consider a dependency as the top level flavor, because a flavor can be a combination of other
131    // flavors
132  0 if (!extension.isDependency(namespace)) {
133    // There should be only one flavor per wiki
134  0 return extension.getId();
135    }
136    }
137    } catch (SearchException e) {
138    // It should never happen with the local repository
139    }
140   
141    // If nothing has been found, look for extensions that was not tagged as flavors but that are in the list of
142    // old flavors
143  0 for (String oldFlavor : getExtensionsConsideredAsFlavors()) {
144  0 InstalledExtension installedExtension = installedRepository.getInstalledExtension(oldFlavor, namespace);
145  0 if (installedExtension != null) {
146  0 return installedExtension.getId();
147    }
148    }
149   
150    // It seems there is no known UI on this wiki
151  0 return null;
152    }
153   
154    /**
155    * @return the list of old extensions that can be considered as flavors even if they are not tagged
156    */
 
157  0 toggle private Collection<String> getExtensionsConsideredAsFlavors()
158    {
159  0 return this.configurationSource.getProperty("extension.oldflavors", Collections.<String>emptyList());
160    }
161    }