1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.shade.resource;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Scanner;
27 import java.util.jar.JarEntry;
28 import java.util.jar.JarOutputStream;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.maven.plugins.shade.relocation.Relocator;
32
33
34
35
36
37
38
39 public class SisuIndexResourceTransformer extends AbstractCompatibilityTransformer {
40 private static final String SISU_INDEX_PATH = "META-INF/sisu/javax.inject.Named";
41
42 private final ArrayList<String> indexEntries = new ArrayList<>();
43
44 private long time = Long.MIN_VALUE;
45
46 @Override
47 public boolean canTransformResource(final String resource) {
48 return resource.equals(SISU_INDEX_PATH);
49 }
50
51 @Override
52 public void processResource(
53 final String resource, final InputStream is, final List<Relocator> relocators, long time)
54 throws IOException {
55 Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name());
56 while (scanner.hasNextLine()) {
57 String relContent = scanner.nextLine();
58 for (Relocator relocator : relocators) {
59 if (relocator.canRelocateClass(relContent)) {
60 relContent = relocator.applyToSourceContent(relContent);
61 }
62 }
63 indexEntries.add(relContent);
64 }
65
66 if (time > this.time) {
67 this.time = time;
68 }
69 }
70
71 @Override
72 public boolean hasTransformedResource() {
73 return !indexEntries.isEmpty();
74 }
75
76 @Override
77 public void modifyOutputStream(final JarOutputStream jos) throws IOException {
78 JarEntry jarEntry = new JarEntry(SISU_INDEX_PATH);
79 jarEntry.setTime(time);
80 jos.putNextEntry(jarEntry);
81 IOUtils.writeLines(indexEntries, "\n", jos, StandardCharsets.UTF_8);
82 jos.flush();
83 indexEntries.clear();
84 }
85 }