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