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.ByteArrayInputStream; |
23 | |
import java.io.ByteArrayOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.jar.JarEntry; |
31 | |
import java.util.jar.JarOutputStream; |
32 | |
|
33 | |
import org.codehaus.plexus.util.IOUtil; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class ServicesResourceTransformer |
45 | |
implements ResourceTransformer |
46 | |
{ |
47 | |
private static final String SERVICES_PATH = "META-INF/services"; |
48 | |
|
49 | |
private ByteArrayOutputStream data; |
50 | |
|
51 | 0 | private Map serviceEntries = new HashMap(); |
52 | |
|
53 | |
public boolean canTransformResource( String resource ) |
54 | |
{ |
55 | 0 | if ( resource.startsWith( SERVICES_PATH ) ) |
56 | |
{ |
57 | 0 | data = (ByteArrayOutputStream) serviceEntries.get( resource ); |
58 | |
|
59 | 0 | if ( data == null ) |
60 | |
{ |
61 | 0 | data = new ByteArrayOutputStream(); |
62 | 0 | serviceEntries.put( resource, data ); |
63 | |
} |
64 | |
|
65 | 0 | return true; |
66 | |
} |
67 | |
|
68 | 0 | return false; |
69 | |
} |
70 | |
|
71 | |
public void processResource( String resource, InputStream is, List relocators ) |
72 | |
throws IOException |
73 | |
{ |
74 | 0 | IOUtil.copy( is, data ); |
75 | 0 | is.close(); |
76 | 0 | } |
77 | |
|
78 | |
public boolean hasTransformedResource() |
79 | |
{ |
80 | 0 | return serviceEntries.size() > 0; |
81 | |
} |
82 | |
|
83 | |
public void modifyOutputStream( JarOutputStream jos ) |
84 | |
throws IOException |
85 | |
{ |
86 | 0 | for ( Iterator i = serviceEntries.keySet().iterator(); i.hasNext(); ) |
87 | |
{ |
88 | 0 | String key = (String) i.next(); |
89 | 0 | ByteArrayOutputStream data = (ByteArrayOutputStream) serviceEntries.get( key ); |
90 | 0 | jos.putNextEntry( new JarEntry( key ) ); |
91 | 0 | IOUtil.copy( new ByteArrayInputStream( data.toByteArray() ), jos ); |
92 | 0 | data.reset(); |
93 | 0 | } |
94 | 0 | } |
95 | |
} |