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 org.apache.maven.plugins.shade.relocation.Relocator; |
23 | |
import org.codehaus.plexus.util.IOUtil; |
24 | |
|
25 | |
import java.io.ByteArrayInputStream; |
26 | |
import java.io.ByteArrayOutputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.InputStream; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
import java.util.jar.JarEntry; |
33 | |
import java.util.jar.JarOutputStream; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class ServicesResourceTransformer |
45 | |
implements ResourceTransformer |
46 | |
{ |
47 | |
|
48 | |
private static final String SERVICES_PATH = "META-INF/services"; |
49 | |
|
50 | 0 | private Map<String, ServiceStream> serviceEntries = new HashMap<String, ServiceStream>(); |
51 | |
|
52 | |
public boolean canTransformResource( String resource ) |
53 | |
{ |
54 | 0 | if ( resource.startsWith( SERVICES_PATH ) ) |
55 | |
{ |
56 | 0 | return true; |
57 | |
} |
58 | |
|
59 | 0 | return false; |
60 | |
} |
61 | |
|
62 | |
public void processResource( String resource, InputStream is, List<Relocator> relocators ) |
63 | |
throws IOException |
64 | |
{ |
65 | 0 | ServiceStream out = serviceEntries.get( resource ); |
66 | 0 | if ( out == null ) |
67 | |
{ |
68 | 0 | out = new ServiceStream(); |
69 | 0 | serviceEntries.put( resource, out ); |
70 | |
} |
71 | |
|
72 | 0 | out.append( is ); |
73 | 0 | is.close(); |
74 | 0 | } |
75 | |
|
76 | |
public boolean hasTransformedResource() |
77 | |
{ |
78 | 0 | return serviceEntries.size() > 0; |
79 | |
} |
80 | |
|
81 | |
public void modifyOutputStream( JarOutputStream jos ) |
82 | |
throws IOException |
83 | |
{ |
84 | 0 | for ( Map.Entry<String, ServiceStream> entry : serviceEntries.entrySet() ) |
85 | |
{ |
86 | 0 | String key = entry.getKey(); |
87 | 0 | ServiceStream data = entry.getValue(); |
88 | |
|
89 | 0 | jos.putNextEntry( new JarEntry( key ) ); |
90 | 0 | IOUtil.copy( data.toInputStream(), jos ); |
91 | 0 | data.reset(); |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | 0 | static class ServiceStream |
96 | |
extends ByteArrayOutputStream |
97 | |
{ |
98 | |
|
99 | |
public ServiceStream() |
100 | |
{ |
101 | 0 | super( 1024 ); |
102 | 0 | } |
103 | |
|
104 | |
public void append( InputStream is ) |
105 | |
throws IOException |
106 | |
{ |
107 | 0 | if ( count > 0 && buf[count - 1] != '\n' && buf[count - 1] != '\r' ) |
108 | |
{ |
109 | 0 | write( '\n' ); |
110 | |
} |
111 | |
|
112 | 0 | IOUtil.copy( is, this ); |
113 | 0 | } |
114 | |
|
115 | |
public InputStream toInputStream() |
116 | |
{ |
117 | 0 | return new ByteArrayInputStream( buf, 0, count ); |
118 | |
} |
119 | |
|
120 | |
} |
121 | |
|
122 | |
} |