| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.extension.test; |
| 21 |
|
|
| 22 |
|
import java.io.File; |
| 23 |
|
import java.io.FileOutputStream; |
| 24 |
|
import java.io.IOException; |
| 25 |
|
import java.io.InputStream; |
| 26 |
|
import java.net.URL; |
| 27 |
|
import java.net.URLEncoder; |
| 28 |
|
import java.util.Collection; |
| 29 |
|
import java.util.Collections; |
| 30 |
|
import java.util.HashMap; |
| 31 |
|
import java.util.Map; |
| 32 |
|
import java.util.Properties; |
| 33 |
|
import java.util.Set; |
| 34 |
|
import java.util.TreeMap; |
| 35 |
|
import java.util.jar.Attributes; |
| 36 |
|
import java.util.jar.JarOutputStream; |
| 37 |
|
import java.util.jar.Manifest; |
| 38 |
|
import java.util.zip.ZipEntry; |
| 39 |
|
import java.util.zip.ZipOutputStream; |
| 40 |
|
|
| 41 |
|
import org.apache.commons.io.IOUtils; |
| 42 |
|
import org.reflections.Reflections; |
| 43 |
|
import org.reflections.scanners.ResourcesScanner; |
| 44 |
|
import org.reflections.util.ClasspathHelper; |
| 45 |
|
import org.reflections.util.ConfigurationBuilder; |
| 46 |
|
import org.reflections.util.FilterBuilder; |
| 47 |
|
import org.reflections.vfs.Vfs; |
| 48 |
|
import org.xwiki.extension.ExtensionId; |
| 49 |
|
|
| 50 |
|
import com.google.common.base.Predicates; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@version |
| 56 |
|
|
| |
|
| 88.5% |
Uncovered Elements: 11 (96) |
Complexity: 17 |
Complexity Density: 0.24 |
|
| 57 |
|
public class ExtensionPackager |
| 58 |
|
{ |
| 59 |
|
public static final String PACKAGEFILE_PACKAGE = "packagefile"; |
| 60 |
|
|
| 61 |
|
public static final String PACKAGEFILE_DESCRIPTOR = "packagefile.properties"; |
| 62 |
|
|
| 63 |
|
private File workingDirectory; |
| 64 |
|
|
| 65 |
|
private Map<String, File> repositories; |
| 66 |
|
|
| 67 |
|
private Map<ExtensionId, File> extensionsFiles = new HashMap<ExtensionId, File>(); |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 69 |
7 |
public ExtensionPackager(File workingDirectory, File repository)... |
| 70 |
|
{ |
| 71 |
7 |
this(workingDirectory, Collections.<String, File>singletonMap(null, repository)); |
| 72 |
|
} |
| 73 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 74 |
169 |
public ExtensionPackager(File workingDirectory, Map<String, File> repositories)... |
| 75 |
|
{ |
| 76 |
169 |
this.workingDirectory = workingDirectory; |
| 77 |
169 |
this.repositories = repositories; |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
35 |
public File getExtensionFile(ExtensionId extensionId)... |
| 81 |
|
{ |
| 82 |
35 |
return this.extensionsFiles.get(extensionId); |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 85 |
171 |
public void generateExtensions() throws IOException... |
| 86 |
|
{ |
| 87 |
171 |
Collection<URL> urls = ClasspathHelper.forPackage(PACKAGEFILE_PACKAGE); |
| 88 |
|
|
| 89 |
171 |
if (!urls.isEmpty()) { |
| 90 |
59 |
Reflections reflections = |
| 91 |
|
new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(urls) |
| 92 |
|
.filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(PACKAGEFILE_PACKAGE)))); |
| 93 |
|
|
| 94 |
59 |
Set<String> descriptors = reflections.getResources(Predicates.equalTo(PACKAGEFILE_DESCRIPTOR)); |
| 95 |
|
|
| 96 |
59 |
for (String descriptor : descriptors) { |
| 97 |
402 |
String classPackageFolder = |
| 98 |
|
descriptor.substring(0, descriptor.length() - PACKAGEFILE_DESCRIPTOR.length()); |
| 99 |
402 |
generateExtension(classPackageFolder, getClass().getClassLoader().getResource(descriptor)); |
| 100 |
|
} |
| 101 |
|
} |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 82.8% |
Uncovered Elements: 11 (64) |
Complexity: 9 |
Complexity Density: 0.19 |
|
| 104 |
402 |
public void generateExtension(String classPackageFolder, URL descriptorUrl) throws IOException... |
| 105 |
|
{ |
| 106 |
402 |
String descriptorUrlStr = descriptorUrl.toString(); |
| 107 |
402 |
String descriptorFolderURL = |
| 108 |
|
descriptorUrlStr.substring(0, descriptorUrlStr.length() - PACKAGEFILE_DESCRIPTOR.length()); |
| 109 |
|
|
| 110 |
402 |
Properties descriptorProperties = new Properties(); |
| 111 |
|
|
| 112 |
402 |
InputStream descriptorStream = descriptorUrl.openStream(); |
| 113 |
402 |
try { |
| 114 |
402 |
descriptorProperties.load(descriptorStream); |
| 115 |
|
} finally { |
| 116 |
402 |
descriptorStream.close(); |
| 117 |
|
} |
| 118 |
402 |
String type = descriptorProperties.getProperty("type"); |
| 119 |
402 |
if (type == null) { |
| 120 |
0 |
type = "zip"; |
| 121 |
|
} |
| 122 |
402 |
String id = descriptorProperties.getProperty("id"); |
| 123 |
402 |
if (id == null) { |
| 124 |
197 |
id = descriptorFolderURL.substring(0, descriptorFolderURL.length() - 1); |
| 125 |
197 |
id = id.substring(id.lastIndexOf('/') + 1); |
| 126 |
|
} |
| 127 |
402 |
String version = descriptorProperties.getProperty("version"); |
| 128 |
402 |
if (version == null) { |
| 129 |
4 |
version = "1.0"; |
| 130 |
|
} |
| 131 |
402 |
File packageFile; |
| 132 |
402 |
String directory = descriptorProperties.getProperty("directory"); |
| 133 |
402 |
String fileName = descriptorProperties.getProperty("fileName"); |
| 134 |
402 |
String repositoryName = descriptorProperties.getProperty("repository"); |
| 135 |
402 |
if (directory == null) { |
| 136 |
402 |
if (fileName == null) { |
| 137 |
402 |
packageFile = |
| 138 |
|
new File(this.repositories.get(repositoryName), URLEncoder.encode(id + '-' + version + '.' + type, |
| 139 |
|
"UTF-8")); |
| 140 |
|
} else { |
| 141 |
0 |
packageFile = new File(this.repositories.get(repositoryName), fileName); |
| 142 |
|
} |
| 143 |
|
} else { |
| 144 |
0 |
if (fileName == null) { |
| 145 |
0 |
fileName = URLEncoder.encode(id + '-' + version + '.' + type, "UTF-8"); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
0 |
packageFile = new File(this.workingDirectory, directory); |
| 149 |
0 |
packageFile = new File(packageFile, fileName); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
402 |
packageFile.getParentFile().mkdirs(); |
| 156 |
|
|
| 157 |
402 |
FileOutputStream fos = new FileOutputStream(packageFile); |
| 158 |
402 |
try { |
| 159 |
402 |
ZipOutputStream zos; |
| 160 |
402 |
if (type.equals("jar")) { |
| 161 |
188 |
Manifest manifest = new Manifest(); |
| 162 |
188 |
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
| 163 |
188 |
zos = new JarOutputStream(fos, manifest); |
| 164 |
|
} else { |
| 165 |
214 |
zos = new ZipOutputStream(fos); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
402 |
try { |
| 169 |
|
|
| 170 |
402 |
TreeMap<String, Vfs.File> files = new TreeMap<>(); |
| 171 |
402 |
for (Vfs.File resourceFile : Vfs.fromURL(new URL(descriptorFolderURL)).getFiles()) { |
| 172 |
1879 |
files.put(resourceFile.getRelativePath(), resourceFile); |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
|
| 176 |
402 |
for (Vfs.File resourceFile : files.values()) { |
| 177 |
1879 |
if (!resourceFile.getRelativePath().equals(PACKAGEFILE_DESCRIPTOR)) { |
| 178 |
1477 |
addZipEntry(classPackageFolder, resourceFile, zos, type); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
} finally { |
| 182 |
402 |
zos.close(); |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
|
| 186 |
402 |
this.extensionsFiles.put(new ExtensionId(id, version), packageFile); |
| 187 |
|
} finally { |
| 188 |
402 |
fos.close(); |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 192 |
1477 |
private void addZipEntry(String classPackageFolder, Vfs.File resourceFile, ZipOutputStream zos, String type)... |
| 193 |
|
throws IOException |
| 194 |
|
{ |
| 195 |
1477 |
String zipPath; |
| 196 |
1477 |
if (type.equals("jar") && resourceFile.getName().endsWith(".class")) { |
| 197 |
446 |
zipPath = classPackageFolder + resourceFile.getRelativePath(); |
| 198 |
|
} else { |
| 199 |
1031 |
zipPath = resourceFile.getRelativePath(); |
| 200 |
|
} |
| 201 |
|
|
| 202 |
1477 |
ZipEntry entry = new ZipEntry(zipPath); |
| 203 |
1477 |
zos.putNextEntry(entry); |
| 204 |
|
|
| 205 |
1477 |
InputStream resourceStream = resourceFile.openInputStream(); |
| 206 |
1477 |
try { |
| 207 |
1477 |
IOUtils.copy(resourceStream, zos); |
| 208 |
|
} finally { |
| 209 |
1477 |
resourceStream.close(); |
| 210 |
|
} |
| 211 |
|
|
| 212 |
1477 |
zos.closeEntry(); |
| 213 |
|
} |
| 214 |
|
} |