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

File ExtensionPackager.java

 

Coverage histogram

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

Code metrics

20
70
6
1
214
156
17
0.24
11.67
6
2.83

Classes

Class Line # Actions
ExtensionPackager 57 70 0% 17 11
0.885416788.5%
 

Contributing tests

This file is covered by 19 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: 1323df68c8b08164fafdae99b4ca3edb07441efd $
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, File> repositories;
66   
67    private Map<ExtensionId, File> extensionsFiles = new HashMap<ExtensionId, File>();
68   
 
69  7 toggle public ExtensionPackager(File workingDirectory, File repository)
70    {
71  7 this(workingDirectory, Collections.<String, File>singletonMap(null, repository));
72    }
73   
 
74  169 toggle public ExtensionPackager(File workingDirectory, Map<String, File> repositories)
75    {
76  169 this.workingDirectory = workingDirectory;
77  169 this.repositories = repositories;
78    }
79   
 
80  35 toggle public File getExtensionFile(ExtensionId extensionId)
81    {
82  35 return this.extensionsFiles.get(extensionId);
83    }
84   
 
85  171 toggle 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   
 
104  402 toggle 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    // generate
153   
154    // Make sure the folder exists
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    // Order files
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    // Add files to zip
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    // Register the extension
186  402 this.extensionsFiles.put(new ExtensionId(id, version), packageFile);
187    } finally {
188  402 fos.close();
189    }
190    }
191   
 
192  1477 toggle 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    }