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