1 | |
package org.apache.maven.plugin.assembly.filter; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.codehaus.plexus.archiver.Archiver; |
23 | |
import org.codehaus.plexus.archiver.ArchiverException; |
24 | |
import org.codehaus.plexus.archiver.ResourceIterator; |
25 | |
import org.codehaus.plexus.archiver.UnArchiver; |
26 | |
import org.codehaus.plexus.components.io.fileselectors.FileInfo; |
27 | |
import org.codehaus.plexus.util.IOUtil; |
28 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
29 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
30 | |
import org.codehaus.plexus.util.xml.Xpp3DomWriter; |
31 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.FileOutputStream; |
35 | |
import java.io.IOException; |
36 | |
import java.io.InputStream; |
37 | |
import java.io.InputStreamReader; |
38 | |
import java.io.OutputStreamWriter; |
39 | |
import java.io.Reader; |
40 | |
import java.io.Writer; |
41 | |
import java.util.Collections; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.LinkedHashMap; |
44 | |
import java.util.List; |
45 | |
import java.util.Map; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 24 | public class ComponentsXmlArchiverFileFilter |
54 | |
implements ContainerDescriptorHandler |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
protected Map<String, Xpp3Dom> components; |
60 | |
|
61 | 24 | private boolean excludeOverride = false; |
62 | |
|
63 | |
public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml"; |
64 | |
|
65 | |
protected void addComponentsXml( final Reader componentsReader ) throws XmlPullParserException, IOException |
66 | |
{ |
67 | 9 | Xpp3Dom newDom = Xpp3DomBuilder.build( componentsReader ); |
68 | |
|
69 | 9 | if ( newDom != null ) |
70 | |
{ |
71 | 9 | newDom = newDom.getChild( "components" ); |
72 | |
} |
73 | |
|
74 | 9 | if ( newDom != null ) |
75 | |
{ |
76 | 9 | final Xpp3Dom[] children = newDom.getChildren(); |
77 | |
|
78 | 21 | for ( int i = 0; i < children.length; i++ ) |
79 | |
{ |
80 | 12 | final Xpp3Dom component = children[i]; |
81 | |
|
82 | 12 | if ( components == null ) |
83 | |
{ |
84 | 9 | components = new LinkedHashMap<String, Xpp3Dom>(); |
85 | |
} |
86 | |
|
87 | 12 | final String role = component.getChild( "role" ) |
88 | |
.getValue(); |
89 | 12 | final Xpp3Dom child = component.getChild( "role-hint" ); |
90 | 12 | final String roleHint = child != null ? child.getValue() : ""; |
91 | |
|
92 | 12 | final String key = role + roleHint; |
93 | 12 | if ( !components.containsKey( key ) ) |
94 | |
{ |
95 | 12 | System.out.println( "Adding " + key ); |
96 | 12 | components.put( key, component ); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | System.out.println( "Component: " + key + " is already defined. Skipping." ); |
101 | |
} |
102 | |
} |
103 | |
} |
104 | 9 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
private void addToArchive( final Archiver archiver ) throws IOException, ArchiverException |
123 | |
{ |
124 | 12 | if ( components != null ) |
125 | |
{ |
126 | 12 | final File f = File.createTempFile( "maven-assembly-plugin", "tmp" ); |
127 | 12 | f.deleteOnExit(); |
128 | |
|
129 | |
|
130 | 12 | final Writer fileWriter = new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" ); |
131 | |
try |
132 | |
{ |
133 | 12 | final Xpp3Dom dom = new Xpp3Dom( "component-set" ); |
134 | 12 | final Xpp3Dom componentDom = new Xpp3Dom( "components" ); |
135 | 12 | dom.addChild( componentDom ); |
136 | |
|
137 | 12 | for ( final Iterator<Xpp3Dom> i = components.values() |
138 | 30 | .iterator(); i.hasNext(); ) |
139 | |
{ |
140 | 18 | final Xpp3Dom component = i.next(); |
141 | 18 | componentDom.addChild( component ); |
142 | 18 | } |
143 | |
|
144 | 12 | Xpp3DomWriter.write( fileWriter, dom ); |
145 | |
} |
146 | |
finally |
147 | |
{ |
148 | 12 | IOUtil.close( fileWriter ); |
149 | 12 | } |
150 | |
|
151 | 12 | excludeOverride = true; |
152 | |
|
153 | 12 | archiver.addFile( f, COMPONENTS_XML_PATH ); |
154 | |
|
155 | 12 | excludeOverride = false; |
156 | |
} |
157 | 12 | } |
158 | |
|
159 | |
public void finalizeArchiveCreation( final Archiver archiver ) throws ArchiverException |
160 | |
{ |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | 12 | for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); ) |
167 | |
{ |
168 | 0 | it.next(); |
169 | |
} |
170 | |
|
171 | |
try |
172 | |
{ |
173 | 12 | addToArchive( archiver ); |
174 | |
} |
175 | 0 | catch ( final IOException e ) |
176 | |
{ |
177 | 0 | throw new ArchiverException( "Error finalizing component-set for archive. Reason: " + e.getMessage(), e ); |
178 | 12 | } |
179 | 12 | } |
180 | |
|
181 | |
public List<String> getVirtualFiles() |
182 | |
{ |
183 | 0 | if ( ( components != null ) && !components.isEmpty() ) |
184 | |
{ |
185 | 0 | return Collections.singletonList( COMPONENTS_XML_PATH ); |
186 | |
} |
187 | |
|
188 | 0 | return null; |
189 | |
} |
190 | |
|
191 | |
public boolean isSelected( final FileInfo fileInfo ) throws IOException |
192 | |
{ |
193 | 0 | if ( fileInfo.isFile() ) |
194 | |
{ |
195 | 0 | if ( excludeOverride ) |
196 | |
{ |
197 | 0 | return true; |
198 | |
} |
199 | |
|
200 | 0 | String entry = fileInfo.getName() |
201 | |
.replace( '\\', '/' ); |
202 | |
|
203 | 0 | if ( entry.startsWith( "/" ) ) |
204 | |
{ |
205 | 0 | entry = entry.substring( 1 ); |
206 | |
} |
207 | |
|
208 | 0 | if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) ) |
209 | |
{ |
210 | 0 | InputStream stream = null; |
211 | 0 | InputStreamReader reader = null; |
212 | |
|
213 | |
try |
214 | |
{ |
215 | 0 | stream = fileInfo.getContents(); |
216 | |
|
217 | 0 | reader = new InputStreamReader( stream, "UTF-8" ); |
218 | 0 | addComponentsXml( reader ); |
219 | |
} |
220 | 0 | catch ( final XmlPullParserException e ) |
221 | |
{ |
222 | 0 | final IOException error = |
223 | |
new IOException( "Error finalizing component-set for archive. Reason: " + e.getMessage() ); |
224 | 0 | error.initCause( e ); |
225 | |
|
226 | 0 | throw error; |
227 | |
} |
228 | |
finally |
229 | |
{ |
230 | 0 | IOUtil.close( stream ); |
231 | 0 | IOUtil.close( reader ); |
232 | 0 | } |
233 | |
|
234 | 0 | return false; |
235 | |
} |
236 | |
else |
237 | |
{ |
238 | 0 | return true; |
239 | |
} |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | return true; |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
public void finalizeArchiveExtraction( final UnArchiver unarchiver ) throws ArchiverException |
248 | |
{ |
249 | 0 | } |
250 | |
|
251 | |
} |