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