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.BufferedInputStream; |
23 | |
import java.io.ByteArrayOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.Reader; |
27 | |
import java.io.Writer; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.LinkedHashMap; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
import java.util.jar.JarEntry; |
33 | |
import java.util.jar.JarOutputStream; |
34 | |
|
35 | |
import org.apache.maven.plugins.shade.relocation.Relocator; |
36 | |
import org.codehaus.plexus.util.IOUtil; |
37 | |
import org.codehaus.plexus.util.ReaderFactory; |
38 | |
import org.codehaus.plexus.util.WriterFactory; |
39 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
40 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
41 | |
import org.codehaus.plexus.util.xml.Xpp3DomWriter; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 6 | public class ComponentsXmlResourceTransformer |
48 | |
implements ResourceTransformer |
49 | |
{ |
50 | 6 | private Map components = new LinkedHashMap(); |
51 | |
|
52 | |
public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml"; |
53 | |
|
54 | |
public boolean canTransformResource( String resource ) |
55 | |
{ |
56 | 55 | return COMPONENTS_XML_PATH.equals( resource ); |
57 | |
} |
58 | |
|
59 | |
public void processResource( String resource, InputStream is, List relocators ) |
60 | |
throws IOException |
61 | |
{ |
62 | |
Xpp3Dom newDom; |
63 | |
|
64 | |
try |
65 | |
{ |
66 | 7 | BufferedInputStream bis = new BufferedInputStream( is ) |
67 | |
{ |
68 | 7 | public void close() |
69 | |
throws IOException |
70 | |
{ |
71 | |
|
72 | 7 | } |
73 | |
}; |
74 | |
|
75 | 7 | Reader reader = ReaderFactory.newXmlReader( bis ); |
76 | |
|
77 | 7 | newDom = Xpp3DomBuilder.build( reader ); |
78 | |
} |
79 | 0 | catch ( Exception e ) |
80 | |
{ |
81 | 0 | throw (IOException) new IOException( "Error parsing components.xml in " + is ).initCause( e ); |
82 | 7 | } |
83 | |
|
84 | |
|
85 | 7 | if ( newDom.getChild( "components" ) == null ) |
86 | |
{ |
87 | 0 | return; |
88 | |
} |
89 | |
|
90 | 7 | Xpp3Dom[] children = newDom.getChild( "components" ).getChildren( "component" ); |
91 | |
|
92 | 96 | for ( int i = 0; i < children.length; i++ ) |
93 | |
{ |
94 | 89 | Xpp3Dom component = children[i]; |
95 | |
|
96 | 89 | String role = getValue( component, "role" ); |
97 | 89 | role = getRelocatedClass( role, relocators ); |
98 | 89 | setValue( component, "role", role ); |
99 | |
|
100 | 89 | String roleHint = getValue( component, "role-hint" ); |
101 | |
|
102 | 89 | String impl = getValue( component, "implementation" ); |
103 | 89 | impl = getRelocatedClass( impl, relocators ); |
104 | 89 | setValue( component, "implementation", impl ); |
105 | |
|
106 | 89 | String key = role + ':' + roleHint; |
107 | 89 | if ( components.containsKey( key ) ) |
108 | |
{ |
109 | |
|
110 | |
|
111 | |
|
112 | 2 | Xpp3Dom dom = (Xpp3Dom) components.get( key ); |
113 | 2 | if ( dom.getChild( "configuration" ) != null ) |
114 | |
{ |
115 | 1 | component.addChild( dom.getChild( "configuration" ) ); |
116 | |
} |
117 | |
} |
118 | |
|
119 | 89 | Xpp3Dom requirements = component.getChild( "requirements" ); |
120 | 89 | if ( requirements != null && requirements.getChildCount() > 0 ) |
121 | |
{ |
122 | 20 | for ( int r = requirements.getChildCount() - 1; r >= 0; r-- ) |
123 | |
{ |
124 | 10 | Xpp3Dom requirement = requirements.getChild( r ); |
125 | |
|
126 | 10 | String requiredRole = getValue( requirement, "role" ); |
127 | 10 | requiredRole = getRelocatedClass( requiredRole, relocators ); |
128 | 10 | setValue( requirement, "role", requiredRole ); |
129 | |
} |
130 | |
} |
131 | |
|
132 | 89 | components.put( key, component ); |
133 | |
} |
134 | 7 | } |
135 | |
|
136 | |
public void modifyOutputStream( JarOutputStream jos ) |
137 | |
throws IOException |
138 | |
{ |
139 | 5 | byte[] data = getTransformedResource(); |
140 | |
|
141 | 5 | jos.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) ); |
142 | |
|
143 | 5 | IOUtil.copy( data, jos ); |
144 | |
|
145 | 5 | components.clear(); |
146 | 5 | } |
147 | |
|
148 | |
public boolean hasTransformedResource() |
149 | |
{ |
150 | 5 | return !components.isEmpty(); |
151 | |
} |
152 | |
|
153 | |
byte[] getTransformedResource() |
154 | |
throws IOException |
155 | |
{ |
156 | 6 | ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 * 4 ); |
157 | |
|
158 | 6 | Writer writer = WriterFactory.newXmlWriter( baos ); |
159 | |
try |
160 | |
{ |
161 | 6 | Xpp3Dom dom = new Xpp3Dom( "component-set" ); |
162 | |
|
163 | 6 | Xpp3Dom componentDom = new Xpp3Dom( "components" ); |
164 | |
|
165 | 6 | dom.addChild( componentDom ); |
166 | |
|
167 | 6 | for ( Iterator i = components.values().iterator(); i.hasNext(); ) |
168 | |
{ |
169 | 87 | Xpp3Dom component = (Xpp3Dom) i.next(); |
170 | 87 | componentDom.addChild( component ); |
171 | 87 | } |
172 | |
|
173 | 6 | Xpp3DomWriter.write( writer, dom ); |
174 | |
} |
175 | |
finally |
176 | |
{ |
177 | 6 | IOUtil.close( writer ); |
178 | 6 | } |
179 | |
|
180 | 6 | return baos.toByteArray(); |
181 | |
} |
182 | |
|
183 | |
private String getRelocatedClass( String className, List relocators ) |
184 | |
{ |
185 | 188 | if ( className != null && className.length() > 0 && relocators != null ) |
186 | |
{ |
187 | 188 | for ( Iterator it = relocators.iterator(); it.hasNext(); ) |
188 | |
{ |
189 | 180 | Relocator relocator = (Relocator) it.next(); |
190 | |
|
191 | 180 | if ( relocator.canRelocateClass( className ) ) |
192 | |
{ |
193 | 0 | return relocator.relocateClass( className ); |
194 | |
} |
195 | 180 | } |
196 | |
} |
197 | |
|
198 | 188 | return className; |
199 | |
} |
200 | |
|
201 | |
private static String getValue( Xpp3Dom dom, String element ) |
202 | |
{ |
203 | 277 | Xpp3Dom child = dom.getChild( element ); |
204 | |
|
205 | 277 | return ( child != null && child.getValue() != null ) ? child.getValue() : ""; |
206 | |
} |
207 | |
|
208 | |
private static void setValue( Xpp3Dom dom, String element, String value ) |
209 | |
{ |
210 | 188 | Xpp3Dom child = dom.getChild( element ); |
211 | |
|
212 | 188 | if ( child == null || value == null || value.length() <= 0 ) |
213 | |
{ |
214 | 0 | return; |
215 | |
} |
216 | |
|
217 | 188 | child.setValue( value ); |
218 | 188 | } |
219 | |
|
220 | |
} |