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

File DefaultExtensionLicenseManager.java

 

Coverage histogram

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

Code metrics

2
27
5
1
159
91
7
0.26
5.4
5
1.4

Classes

Class Line # Actions
DefaultExtensionLicenseManager 57 27 0% 7 3
0.911764791.2%
 

Contributing tests

This file is covered by 14 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.extension.internal;
21   
22    import java.io.InputStream;
23    import java.io.UnsupportedEncodingException;
24    import java.net.URL;
25    import java.net.URLDecoder;
26    import java.util.ArrayList;
27    import java.util.Collection;
28    import java.util.List;
29    import java.util.Map;
30    import java.util.concurrent.ConcurrentHashMap;
31    import java.util.regex.Pattern;
32   
33    import javax.inject.Inject;
34    import javax.inject.Singleton;
35   
36    import org.apache.commons.io.IOUtils;
37    import org.reflections.Reflections;
38    import org.reflections.scanners.ResourcesScanner;
39    import org.reflections.util.ClasspathHelper;
40    import org.reflections.util.ConfigurationBuilder;
41    import org.reflections.util.FilterBuilder;
42    import org.slf4j.Logger;
43    import org.xwiki.component.annotation.Component;
44    import org.xwiki.component.phase.Initializable;
45    import org.xwiki.component.phase.InitializationException;
46    import org.xwiki.extension.ExtensionLicense;
47    import org.xwiki.extension.ExtensionLicenseManager;
48   
49    /**
50    * Default implementation of {@link ExtensionLicenseManager}.
51    *
52    * @version $Id: 46e5aaa1ee200778553e5e9dadf9de03c82da1e0 $
53    * @since 4.0M1
54    */
55    @Component
56    @Singleton
 
57    public class DefaultExtensionLicenseManager implements ExtensionLicenseManager, Initializable
58    {
59    /**
60    * The prefix used to mark license name alias.
61    */
62    private static final String ALIAS_PREFIX = ".alias=";
63   
64    /**
65    * The package where license files are located.
66    */
67    private static final String LICENSE_PACKAGE = "extension.licenses";
68   
69    /**
70    * The logger to log.
71    */
72    @Inject
73    private Logger logger;
74   
75    /**
76    * The known licenses.
77    */
78    private Map<String, ExtensionLicense> licenses = new ConcurrentHashMap<String, ExtensionLicense>();
79   
 
80  281 toggle @Override
81    public void initialize() throws InitializationException
82    {
83  281 Collection<URL> licenseURLs = ClasspathHelper.forPackage(LICENSE_PACKAGE);
84   
85  281 Reflections reflections =
86    new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(licenseURLs)
87    .filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(LICENSE_PACKAGE))));
88   
89  281 for (String licenseFile : reflections.getResources(Pattern.compile(".*\\.license"))) {
90  8149 URL licenseUrl = getClass().getClassLoader().getResource(licenseFile);
91   
92  8149 try {
93    // Get name
94  8149 String path = decode(licenseUrl.getPath());
95  8149 String name = path.substring(path.lastIndexOf('/') + 1);
96  8149 name = name.substring(0, name.length() - ".license".length());
97   
98    // Get content
99  8149 InputStream is = licenseUrl.openStream();
100  8149 try {
101  8149 List<String> content = IOUtils.readLines(is);
102   
103  8149 List<String> aliases = new ArrayList<String>();
104  8149 aliases.add(name);
105   
106  8149 for (String line : content) {
107  26133 if (!line.startsWith(ALIAS_PREFIX)) {
108  8149 break;
109    }
110   
111  17984 aliases.add(line.substring(ALIAS_PREFIX.length()));
112    }
113   
114  8149 content = content.subList(aliases.size() - 1, content.size());
115   
116  8149 ExtensionLicense license = new ExtensionLicense(name, content);
117   
118  8149 for (String alias : aliases) {
119  26133 this.licenses.put(alias.toLowerCase(), license);
120    }
121    } finally {
122  8149 is.close();
123    }
124    } catch (Exception e) {
125  0 this.logger.error("Failed to load license file at [" + licenseUrl + "]", e);
126    }
127    }
128    }
129   
130    /**
131    * Decode URL path.
132    *
133    * @param path the URL path
134    * @return the decoded path
135    * @throws UnsupportedEncodingException error when unescaping provided path
136    */
 
137  8149 toggle private String decode(String path) throws UnsupportedEncodingException
138    {
139  8149 return URLDecoder.decode(path, "UTF-8");
140    }
141   
 
142  1 toggle @Override
143    public List<ExtensionLicense> getLicenses()
144    {
145  1 return new ArrayList<ExtensionLicense>(this.licenses.values());
146    }
147   
 
148  43239 toggle @Override
149    public ExtensionLicense getLicense(String name)
150    {
151  43239 return this.licenses.get(name.toLowerCase());
152    }
153   
 
154  0 toggle @Override
155    public void addLicense(ExtensionLicense license)
156    {
157  0 this.licenses.put(license.getName().toLowerCase(), license);
158    }
159    }