1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Singleton |
|
|
| 91.2% |
Uncovered Elements: 3 (34) |
Complexity: 7 |
Complexity Density: 0.26 |
|
57 |
|
public class DefaultExtensionLicenseManager implements ExtensionLicenseManager, Initializable |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String ALIAS_PREFIX = ".alias="; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final String LICENSE_PACKAGE = "extension.licenses"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private Logger logger; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
private Map<String, ExtensionLicense> licenses = new ConcurrentHashMap<String, ExtensionLicense>(); |
79 |
|
|
|
|
| 96% |
Uncovered Elements: 1 (25) |
Complexity: 3 |
Complexity Density: 0.13 |
|
80 |
281 |
@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 |
|
|
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 |
|
|
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 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@return |
135 |
|
@throws |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
8149 |
private String decode(String path) throws UnsupportedEncodingException... |
138 |
|
{ |
139 |
8149 |
return URLDecoder.decode(path, "UTF-8"); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
142 |
1 |
@Override... |
143 |
|
public List<ExtensionLicense> getLicenses() |
144 |
|
{ |
145 |
1 |
return new ArrayList<ExtensionLicense>(this.licenses.values()); |
146 |
|
} |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
148 |
43239 |
@Override... |
149 |
|
public ExtensionLicense getLicense(String name) |
150 |
|
{ |
151 |
43239 |
return this.licenses.get(name.toLowerCase()); |
152 |
|
} |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
0 |
@Override... |
155 |
|
public void addLicense(ExtensionLicense license) |
156 |
|
{ |
157 |
0 |
this.licenses.put(license.getName().toLowerCase(), license); |
158 |
|
} |
159 |
|
} |