1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.test

File ExtensionPackager.java

 

Coverage histogram

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

Code metrics

22
74
6
1
219
159
18
0.24
12.33
6
3

Classes

Class Line # Actions
ExtensionPackager 57 74 0% 18 13
0.87254987.3%
 

Contributing tests

This file is covered by 9 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.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    * Generate package based on information found in <code>packagefile.properties</code> files from the resources.
54    *
55    * @version $Id: 09efa262657256cb8bf395b6b0f0817af6bcb489 $
56    */
 
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, RepositorySerializer> repositories;
66   
67    private Map<ExtensionId, File> extensionsFiles = new HashMap<ExtensionId, File>();
68   
 
69  8 toggle public ExtensionPackager(File workingDirectory, File repository)
70    {
71  8 this(workingDirectory,
72    Collections.<String, RepositorySerializer>singletonMap(null, new DefaultRepositorySerializer(repository)));
73    }
74   
 
75  180 toggle public ExtensionPackager(File workingDirectory, Map<String, RepositorySerializer> repositories)
76    {
77  180 this.workingDirectory = workingDirectory;
78  180 this.repositories = repositories;
79    }
80   
 
81  37 toggle public File getExtensionFile(ExtensionId extensionId)
82    {
83  37 return this.extensionsFiles.get(extensionId);
84    }
85   
 
86  182 toggle public void generateExtensions() throws IOException
87    {
88  182 Collection<URL> urls = ClasspathHelper.forPackage(PACKAGEFILE_PACKAGE);
89   
90  182 if (!urls.isEmpty()) {
91  62 Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner())
92    .setUrls(urls).filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix(PACKAGEFILE_PACKAGE))));
93   
94  62 Set<String> descriptors = reflections.getResources(Predicates.equalTo(PACKAGEFILE_DESCRIPTOR));
95   
96  62 for (String descriptor : descriptors) {
97  412 String classPackageFolder =
98    descriptor.substring(0, descriptor.length() - PACKAGEFILE_DESCRIPTOR.length());
99  412 generateExtension(classPackageFolder, getClass().getClassLoader().getResource(descriptor));
100    }
101    }
102    }
103   
 
104  412 toggle public void generateExtension(String classPackageFolder, URL descriptorUrl) throws IOException
105    {
106  412 String descriptorUrlStr = descriptorUrl.toString();
107  412 String descriptorFolderURL =
108    descriptorUrlStr.substring(0, descriptorUrlStr.length() - PACKAGEFILE_DESCRIPTOR.length());
109   
110  412 Properties descriptorProperties = new Properties();
111   
112  412 InputStream descriptorStream = descriptorUrl.openStream();
113  412 try {
114  412 descriptorProperties.load(descriptorStream);
115    } finally {
116  412 descriptorStream.close();
117    }
118  412 String type = descriptorProperties.getProperty("type");
119  412 if (type == null) {
120  0 type = "zip";
121    }
122  412 String id = descriptorProperties.getProperty("id");
123  412 if (id == null) {
124  203 id = descriptorFolderURL.substring(0, descriptorFolderURL.length() - 1);
125  203 id = id.substring(id.lastIndexOf('/') + 1);
126    }
127  412 String version = descriptorProperties.getProperty("version");
128  412 if (version == null) {
129  4 version = "1.0";
130    }
131   
132  412 String repositoryName = descriptorProperties.getProperty("repository");
133  412 RepositorySerializer repositorySerializer = this.repositories.get(repositoryName);
134  412 if (repositorySerializer == null) {
135  0 throw new IOException("Unknown repository [" + repositoryName + "]");
136    }
137  412 id = repositorySerializer.resolveId(id);
138   
139  412 File packageFile;
140  412 String directory = descriptorProperties.getProperty("directory");
141  412 String fileName = descriptorProperties.getProperty("fileName");
142  412 if (directory == null) {
143  412 if (fileName == null) {
144  412 packageFile = repositorySerializer.getFile(id, version, type);
145    } else {
146  0 packageFile = new File(repositorySerializer.getRoot(), fileName);
147    }
148    } else {
149  0 if (fileName == null) {
150  0 fileName = URLEncoder.encode(id + '-' + version + '.' + type, "UTF-8");
151    }
152   
153  0 packageFile = new File(this.workingDirectory, directory);
154  0 packageFile = new File(packageFile, fileName);
155    }
156   
157    // generate
158   
159    // Make sure the folder exists
160  412 packageFile.getParentFile().mkdirs();
161   
162  412 FileOutputStream fos = new FileOutputStream(packageFile);
163  412 try {
164  412 ZipOutputStream zos;
165  412 if (type.equals("jar")) {
166  189 Manifest manifest = new Manifest();
167  189 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
168  189 zos = new JarOutputStream(fos, manifest);
169    } else {
170  223 zos = new ZipOutputStream(fos);
171    }
172   
173  412 try {
174    // Order files
175  412 TreeMap<String, Vfs.File> files = new TreeMap<>();
176  412 for (Vfs.File resourceFile : Vfs.fromURL(new URL(descriptorFolderURL)).getFiles()) {
177  1963 files.put(resourceFile.getRelativePath(), resourceFile);
178    }
179   
180    // Add files to zip
181  412 for (Vfs.File resourceFile : files.values()) {
182  1963 if (!resourceFile.getRelativePath().equals(PACKAGEFILE_DESCRIPTOR)) {
183  1551 addZipEntry(classPackageFolder, resourceFile, zos, type);
184    }
185    }
186    } finally {
187  412 zos.close();
188    }
189   
190    // Register the extension
191  412 this.extensionsFiles.put(new ExtensionId(id, version), packageFile);
192    } finally {
193  412 fos.close();
194    }
195    }
196   
 
197  1551 toggle private void addZipEntry(String classPackageFolder, Vfs.File resourceFile, ZipOutputStream zos, String type)
198    throws IOException
199    {
200  1551 String zipPath;
201  1551 if (type.equals("jar") && resourceFile.getName().endsWith(".class")) {
202  453 zipPath = classPackageFolder + resourceFile.getRelativePath();
203    } else {
204  1098 zipPath = resourceFile.getRelativePath();
205    }
206   
207  1551 ZipEntry entry = new ZipEntry(zipPath);
208  1551 zos.putNextEntry(entry);
209   
210  1551 InputStream resourceStream = resourceFile.openInputStream();
211  1551 try {
212  1551 IOUtils.copy(resourceStream, zos);
213    } finally {
214  1551 resourceStream.close();
215    }
216   
217  1551 zos.closeEntry();
218    }
219    }