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