36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Test zip compressor
|
|
27 |
* @author Jean-Francois Denise
|
|
28 |
* @modules java.base/jdk.internal.jimage.decompressor
|
|
29 |
* jdk.jlink/jdk.tools.jlink.internal
|
|
30 |
* jdk.jlink/jdk.tools.jlink.internal.plugins
|
|
31 |
* @run main CompressorPluginTest
|
|
32 |
*/
|
|
33 |
import java.net.URI;
|
|
34 |
import java.nio.ByteOrder;
|
|
35 |
import java.nio.file.FileSystem;
|
|
36 |
import java.nio.file.FileSystemNotFoundException;
|
|
37 |
import java.nio.file.FileSystems;
|
|
38 |
import java.nio.file.Files;
|
|
39 |
import java.nio.file.Path;
|
|
40 |
import java.nio.file.ProviderNotFoundException;
|
|
41 |
import java.util.Collections;
|
|
42 |
import java.util.HashMap;
|
|
43 |
import java.util.Iterator;
|
|
44 |
import java.util.List;
|
|
45 |
import java.util.Map;
|
|
46 |
import java.util.Properties;
|
|
47 |
import java.util.regex.Pattern;
|
|
48 |
import java.util.stream.Collectors;
|
|
49 |
import java.util.stream.Stream;
|
|
50 |
|
|
51 |
import jdk.internal.jimage.decompressor.CompressedResourceHeader;
|
|
52 |
import jdk.internal.jimage.decompressor.ResourceDecompressor;
|
|
53 |
import jdk.internal.jimage.decompressor.ResourceDecompressorFactory;
|
|
54 |
import jdk.internal.jimage.decompressor.StringSharingDecompressorFactory;
|
|
55 |
import jdk.internal.jimage.decompressor.ZipDecompressorFactory;
|
38320
|
56 |
import jdk.tools.jlink.internal.ModulePoolImpl;
|
36511
|
57 |
import jdk.tools.jlink.internal.StringTable;
|
|
58 |
import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
|
|
59 |
import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
|
|
60 |
import jdk.tools.jlink.internal.plugins.ZipPlugin;
|
|
61 |
import jdk.tools.jlink.plugin.Plugin;
|
38320
|
62 |
import jdk.tools.jlink.plugin.ModuleEntry;
|
|
63 |
import jdk.tools.jlink.plugin.ModulePool;
|
36511
|
64 |
import jdk.tools.jlink.plugin.TransformerPlugin;
|
|
65 |
|
|
66 |
public class CompressorPluginTest {
|
|
67 |
|
|
68 |
private static int strID = 1;
|
|
69 |
|
|
70 |
public static void main(String[] args) throws Exception {
|
|
71 |
new CompressorPluginTest().test();
|
|
72 |
}
|
|
73 |
|
|
74 |
public void test() throws Exception {
|
|
75 |
FileSystem fs;
|
|
76 |
try {
|
|
77 |
fs = FileSystems.getFileSystem(URI.create("jrt:/"));
|
|
78 |
} catch (ProviderNotFoundException | FileSystemNotFoundException e) {
|
|
79 |
System.err.println("Not an image build, test skipped.");
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
Path javabase = fs.getPath("/modules/java.base");
|
|
83 |
|
|
84 |
checkCompress(gatherResources(javabase), new ZipPlugin(), null,
|
|
85 |
new ResourceDecompressorFactory[]{
|
|
86 |
new ZipDecompressorFactory()
|
|
87 |
});
|
|
88 |
|
38320
|
89 |
ModulePool classes = gatherClasses(javabase);
|
36511
|
90 |
// compress = String sharing
|
|
91 |
checkCompress(classes, new StringSharingPlugin(), null,
|
|
92 |
new ResourceDecompressorFactory[]{
|
|
93 |
new StringSharingDecompressorFactory()});
|
|
94 |
|
|
95 |
// compress == ZIP + String sharing
|
|
96 |
Properties options = new Properties();
|
|
97 |
options.setProperty(ZipPlugin.NAME, "");
|
|
98 |
checkCompress(classes, new DefaultCompressPlugin(), options,
|
|
99 |
new ResourceDecompressorFactory[]{
|
|
100 |
new ZipDecompressorFactory(),
|
|
101 |
new StringSharingDecompressorFactory()
|
|
102 |
});
|
|
103 |
|
|
104 |
// compress == ZIP + String sharing + filter
|
|
105 |
options.setProperty(DefaultCompressPlugin.FILTER,
|
|
106 |
"*Exception.class,^*IOException.class");
|
|
107 |
checkCompress(classes, new DefaultCompressPlugin(), options,
|
|
108 |
new ResourceDecompressorFactory[]{
|
|
109 |
new ZipDecompressorFactory(),
|
|
110 |
new StringSharingDecompressorFactory()
|
|
111 |
}, Collections.singletonList(".*Exception.class"),
|
|
112 |
Collections.singletonList(".*IOException.class"));
|
|
113 |
|
|
114 |
// compress level 1 == ZIP
|
|
115 |
Properties options1 = new Properties();
|
|
116 |
options1.setProperty(DefaultCompressPlugin.NAME,
|
|
117 |
"1");
|
|
118 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
119 |
options1,
|
|
120 |
new ResourceDecompressorFactory[]{
|
|
121 |
new ZipDecompressorFactory()
|
|
122 |
});
|
|
123 |
|
|
124 |
// compress level 1 == ZIP
|
|
125 |
options1.setProperty(DefaultCompressPlugin.FILTER,
|
|
126 |
"*Exception.class,^*IOException.class");
|
|
127 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
128 |
options1,
|
|
129 |
new ResourceDecompressorFactory[]{
|
|
130 |
new ZipDecompressorFactory()
|
|
131 |
}, Collections.singletonList(".*Exception.class"),
|
|
132 |
Collections.singletonList(".*IOException.class"));
|
|
133 |
|
|
134 |
// compress level 2 == ZIP + String sharing
|
|
135 |
Properties options2 = new Properties();
|
|
136 |
options2.setProperty(DefaultCompressPlugin.NAME,
|
|
137 |
"2");
|
|
138 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
139 |
options2,
|
|
140 |
new ResourceDecompressorFactory[]{
|
|
141 |
new ZipDecompressorFactory(),
|
|
142 |
new StringSharingDecompressorFactory()
|
|
143 |
});
|
|
144 |
|
|
145 |
// compress level 2 == ZIP + String sharing + filter
|
|
146 |
options2.setProperty(DefaultCompressPlugin.FILTER,
|
|
147 |
"*Exception.class,^*IOException.class");
|
|
148 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
149 |
options2,
|
|
150 |
new ResourceDecompressorFactory[]{
|
|
151 |
new ZipDecompressorFactory(),
|
|
152 |
new StringSharingDecompressorFactory()
|
|
153 |
}, Collections.singletonList(".*Exception.class"),
|
|
154 |
Collections.singletonList(".*IOException.class"));
|
|
155 |
|
|
156 |
// compress level 0 == String sharing
|
|
157 |
Properties options0 = new Properties();
|
|
158 |
options0.setProperty(DefaultCompressPlugin.NAME, "0");
|
|
159 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
160 |
options0,
|
|
161 |
new ResourceDecompressorFactory[]{
|
|
162 |
new StringSharingDecompressorFactory()
|
|
163 |
});
|
|
164 |
|
|
165 |
// compress level 0 == String sharing + filter
|
|
166 |
options0.setProperty(DefaultCompressPlugin.FILTER,
|
|
167 |
"*Exception.class,^*IOException.class");
|
|
168 |
checkCompress(classes, new DefaultCompressPlugin(),
|
|
169 |
options0,
|
|
170 |
new ResourceDecompressorFactory[]{
|
|
171 |
new StringSharingDecompressorFactory()
|
|
172 |
}, Collections.singletonList(".*Exception.class"),
|
|
173 |
Collections.singletonList(".*IOException.class"));
|
|
174 |
}
|
|
175 |
|
38320
|
176 |
private ModulePool gatherResources(Path module) throws Exception {
|
|
177 |
ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
|
36511
|
178 |
|
|
179 |
@Override
|
|
180 |
public int addString(String str) {
|
|
181 |
return -1;
|
|
182 |
}
|
|
183 |
|
|
184 |
@Override
|
|
185 |
public String getString(int id) {
|
|
186 |
return null;
|
|
187 |
}
|
|
188 |
});
|
|
189 |
try (Stream<Path> stream = Files.walk(module)) {
|
|
190 |
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
|
|
191 |
Path p = iterator.next();
|
|
192 |
if (Files.isRegularFile(p)) {
|
|
193 |
byte[] content = Files.readAllBytes(p);
|
38320
|
194 |
pool.add(ModuleEntry.create(p.toString(), content));
|
36511
|
195 |
}
|
|
196 |
}
|
|
197 |
}
|
|
198 |
return pool;
|
|
199 |
}
|
|
200 |
|
38320
|
201 |
private ModulePool gatherClasses(Path module) throws Exception {
|
|
202 |
ModulePool pool = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
|
36511
|
203 |
|
|
204 |
@Override
|
|
205 |
public int addString(String str) {
|
|
206 |
return -1;
|
|
207 |
}
|
|
208 |
|
|
209 |
@Override
|
|
210 |
public String getString(int id) {
|
|
211 |
return null;
|
|
212 |
}
|
|
213 |
});
|
|
214 |
try (Stream<Path> stream = Files.walk(module)) {
|
|
215 |
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
|
|
216 |
Path p = iterator.next();
|
|
217 |
if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
|
|
218 |
byte[] content = Files.readAllBytes(p);
|
38320
|
219 |
pool.add(ModuleEntry.create(p.toString(), content));
|
36511
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|
|
223 |
return pool;
|
|
224 |
}
|
|
225 |
|
38320
|
226 |
private void checkCompress(ModulePool resources, Plugin prov,
|
36511
|
227 |
Properties config,
|
|
228 |
ResourceDecompressorFactory[] factories) throws Exception {
|
|
229 |
checkCompress(resources, prov, config, factories, Collections.emptyList(), Collections.emptyList());
|
|
230 |
}
|
|
231 |
|
38320
|
232 |
private void checkCompress(ModulePool resources, Plugin prov,
|
36511
|
233 |
Properties config,
|
|
234 |
ResourceDecompressorFactory[] factories,
|
|
235 |
List<String> includes,
|
|
236 |
List<String> excludes) throws Exception {
|
38320
|
237 |
long[] original = new long[1];
|
|
238 |
long[] compressed = new long[1];
|
|
239 |
resources.entries().forEach(resource -> {
|
36511
|
240 |
List<Pattern> includesPatterns = includes.stream()
|
|
241 |
.map(Pattern::compile)
|
|
242 |
.collect(Collectors.toList());
|
|
243 |
List<Pattern> excludesPatterns = excludes.stream()
|
|
244 |
.map(Pattern::compile)
|
|
245 |
.collect(Collectors.toList());
|
|
246 |
|
|
247 |
Map<String, String> props = new HashMap<>();
|
|
248 |
if (config != null) {
|
|
249 |
for (String p : config.stringPropertyNames()) {
|
|
250 |
props.put(p, config.getProperty(p));
|
|
251 |
}
|
|
252 |
}
|
|
253 |
prov.configure(props);
|
|
254 |
final Map<Integer, String> strings = new HashMap<>();
|
38320
|
255 |
ModulePoolImpl inputResources = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() {
|
36511
|
256 |
@Override
|
|
257 |
public int addString(String str) {
|
|
258 |
int id = strID;
|
|
259 |
strID += 1;
|
|
260 |
strings.put(id, str);
|
|
261 |
return id;
|
|
262 |
}
|
|
263 |
|
|
264 |
@Override
|
|
265 |
public String getString(int id) {
|
|
266 |
return strings.get(id);
|
|
267 |
}
|
|
268 |
});
|
|
269 |
inputResources.add(resource);
|
38320
|
270 |
ModulePool compressedResources = applyCompressor(prov, inputResources, resource, includesPatterns, excludesPatterns);
|
|
271 |
original[0] += resource.getLength();
|
|
272 |
compressed[0] += compressedResources.findEntry(resource.getPath()).get().getLength();
|
36511
|
273 |
applyDecompressors(factories, inputResources, compressedResources, strings, includesPatterns, excludesPatterns);
|
38320
|
274 |
});
|
36511
|
275 |
String compressors = Stream.of(factories)
|
|
276 |
.map(Object::getClass)
|
|
277 |
.map(Class::getSimpleName)
|
|
278 |
.collect(Collectors.joining(", "));
|
38320
|
279 |
String size = "Compressed size: " + compressed[0] + ", original size: " + original[0];
|
36511
|
280 |
System.out.println("Used " + compressors + ". " + size);
|
38320
|
281 |
if (original[0] <= compressed[0]) {
|
36511
|
282 |
throw new AssertionError("java.base not compressed.");
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
38320
|
286 |
private ModulePool applyCompressor(Plugin plugin,
|
|
287 |
ModulePoolImpl inputResources,
|
|
288 |
ModuleEntry res,
|
36511
|
289 |
List<Pattern> includesPatterns,
|
38320
|
290 |
List<Pattern> excludesPatterns) {
|
36511
|
291 |
TransformerPlugin compressor = (TransformerPlugin) plugin;
|
38320
|
292 |
ModulePool compressedModulePool = new ModulePoolImpl(ByteOrder.nativeOrder(), inputResources.getStringTable());
|
|
293 |
compressor.visit(inputResources, compressedModulePool);
|
36511
|
294 |
String path = res.getPath();
|
38320
|
295 |
ModuleEntry compressed = compressedModulePool.findEntry(path).get();
|
36511
|
296 |
CompressedResourceHeader header
|
|
297 |
= CompressedResourceHeader.readFromResource(ByteOrder.nativeOrder(), compressed.getBytes());
|
|
298 |
if (isIncluded(includesPatterns, excludesPatterns, path)) {
|
|
299 |
if (header == null) {
|
|
300 |
throw new AssertionError("Path should be compressed: " + path);
|
|
301 |
}
|
|
302 |
if (header.getDecompressorNameOffset() == 0) {
|
|
303 |
throw new AssertionError("Invalid plugin offset "
|
|
304 |
+ header.getDecompressorNameOffset());
|
|
305 |
}
|
|
306 |
if (header.getResourceSize() <= 0) {
|
|
307 |
throw new AssertionError("Invalid compressed size "
|
|
308 |
+ header.getResourceSize());
|
|
309 |
}
|
|
310 |
} else if (header != null) {
|
|
311 |
throw new AssertionError("Path should not be compressed: " + path);
|
|
312 |
}
|
38320
|
313 |
return compressedModulePool;
|
36511
|
314 |
}
|
|
315 |
|
|
316 |
private void applyDecompressors(ResourceDecompressorFactory[] decompressors,
|
38320
|
317 |
ModulePool inputResources,
|
|
318 |
ModulePool compressedResources,
|
36511
|
319 |
Map<Integer, String> strings,
|
|
320 |
List<Pattern> includesPatterns,
|
38320
|
321 |
List<Pattern> excludesPatterns) {
|
|
322 |
compressedResources.entries().forEach(compressed -> {
|
36511
|
323 |
CompressedResourceHeader header = CompressedResourceHeader.readFromResource(
|
|
324 |
ByteOrder.nativeOrder(), compressed.getBytes());
|
|
325 |
String path = compressed.getPath();
|
38320
|
326 |
ModuleEntry orig = inputResources.findEntry(path).get();
|
36511
|
327 |
if (!isIncluded(includesPatterns, excludesPatterns, path)) {
|
38320
|
328 |
return;
|
36511
|
329 |
}
|
|
330 |
byte[] decompressed = compressed.getBytes();
|
|
331 |
for (ResourceDecompressorFactory factory : decompressors) {
|
38320
|
332 |
try {
|
|
333 |
ResourceDecompressor decompressor = factory.newDecompressor(new Properties());
|
|
334 |
decompressed = decompressor.decompress(
|
36511
|
335 |
strings::get, decompressed,
|
|
336 |
CompressedResourceHeader.getSize(), header.getUncompressedSize());
|
38320
|
337 |
} catch (Exception exp) {
|
|
338 |
throw new RuntimeException(exp);
|
|
339 |
}
|
36511
|
340 |
}
|
|
341 |
|
|
342 |
if (decompressed.length != orig.getLength()) {
|
|
343 |
throw new AssertionError("Invalid uncompressed size "
|
|
344 |
+ header.getUncompressedSize());
|
|
345 |
}
|
|
346 |
byte[] origContent = orig.getBytes();
|
|
347 |
for (int i = 0; i < decompressed.length; i++) {
|
|
348 |
if (decompressed[i] != origContent[i]) {
|
|
349 |
throw new AssertionError("Decompressed and original differ at index " + i);
|
|
350 |
}
|
|
351 |
}
|
38320
|
352 |
});
|
36511
|
353 |
}
|
|
354 |
|
|
355 |
private boolean isIncluded(List<Pattern> includesPatterns, List<Pattern> excludesPatterns, String path) {
|
|
356 |
return !excludesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches())
|
|
357 |
&& (includesPatterns.isEmpty()
|
|
358 |
|| includesPatterns.stream().anyMatch((pattern) -> pattern.matcher(path).matches()));
|
|
359 |
}
|
|
360 |
}
|