View Javadoc
1   package org.apache.maven.plugin.assembly.filter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.component.annotations.Component;
27  import org.codehaus.plexus.components.io.fileselectors.FileInfo;
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.xml.Xpp3Dom;
30  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
31  import org.codehaus.plexus.util.xml.Xpp3DomWriter;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  import javax.annotation.Nonnull;
35  import java.io.BufferedReader;
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.io.InputStreamReader;
41  import java.io.OutputStreamWriter;
42  import java.io.Reader;
43  import java.io.Writer;
44  import java.util.Collections;
45  import java.util.LinkedHashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  /**
50   * Components XML file filter.
51   *
52   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
53   * @version $Id: ComponentsXmlArchiverFileFilter.java 1639422 2014-11-13 18:08:07Z krosenvold $
54   */
55  @Component( role = ContainerDescriptorHandler.class, hint = "plexus", instantiationStrategy = "per-lookup" )
56  public class ComponentsXmlArchiverFileFilter
57      implements ContainerDescriptorHandler
58  {
59      // [jdcasey] Switched visibility to protected to allow testing. Also, because this class isn't final, it should
60      // allow
61      // some minimal access to the components accumulated for extending classes.
62      Map<String, Xpp3Dom> components;
63  
64      private boolean excludeOverride = false;
65  
66      public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";
67  
68      void addComponentsXml( final Reader componentsReader )
69          throws XmlPullParserException, IOException
70      {
71          Xpp3Dom newDom = Xpp3DomBuilder.build( componentsReader );
72  
73          if ( newDom != null )
74          {
75              newDom = newDom.getChild( "components" );
76          }
77  
78          if ( newDom != null )
79          {
80              final Xpp3Dom[] children = newDom.getChildren();
81  
82              for ( final Xpp3Dom component : children )
83              {
84                  if ( components == null )
85                  {
86                      components = new LinkedHashMap<String, Xpp3Dom>();
87                  }
88  
89                  final String role = component.getChild( "role" ).getValue();
90                  final Xpp3Dom child = component.getChild( "role-hint" );
91                  final String roleHint = child != null ? child.getValue() : "";
92  
93                  final String key = role + roleHint;
94                  if ( !components.containsKey( key ) )
95                  {
96                      System.out.println( "Adding " + key );
97                      components.put( key, component );
98                  }
99                  else
100                 {
101                     System.out.println( "Component: " + key + " is already defined. Skipping." );
102                 }
103             }
104         }
105     }
106 
107     // public void addComponentsXml( File componentsXml )
108     // throws IOException, XmlPullParserException
109     // {
110     // FileReader fileReader = null;
111     // try
112     // {
113     // fileReader = new FileReader( componentsXml );
114     //
115     // addComponentsXml( fileReader );
116     // }
117     // finally
118     // {
119     // IOUtil.close( fileReader );
120     // }
121     // }
122 
123     private void addToArchive( final Archiver archiver )
124         throws IOException, ArchiverException
125     {
126         if ( components != null )
127         {
128             final File f = File.createTempFile( "maven-assembly-plugin", "tmp" );
129             f.deleteOnExit();
130 
131             // TODO use WriterFactory.newXmlWriter() when plexus-utils is upgraded to 1.4.5+
132             final Writer fileWriter = new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" );
133             try
134             {
135                 final Xpp3Dom dom = new Xpp3Dom( "component-set" );
136                 final Xpp3Dom componentDom = new Xpp3Dom( "components" );
137                 dom.addChild( componentDom );
138 
139                 for ( final Xpp3Dom component : components.values() )
140                 {
141                     componentDom.addChild( component );
142                 }
143 
144                 Xpp3DomWriter.write( fileWriter, dom );
145             }
146             finally
147             {
148                 IOUtil.close( fileWriter );
149             }
150 
151             excludeOverride = true;
152 
153             archiver.addFile( f, COMPONENTS_XML_PATH );
154 
155             excludeOverride = false;
156         }
157     }
158 
159     public void finalizeArchiveCreation( final Archiver archiver )
160         throws ArchiverException
161     {
162         // this will prompt the isSelected() call, below, for all resources added to the archive.
163         // FIXME: This needs to be corrected in the AbstractArchiver, where
164         // runArchiveFinalizers() is called before regular resources are added...
165         // which is done because the manifest needs to be added first, and the
166         // manifest-creation component is a finalizer in the assembly plugin...
167         for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
168         {
169             it.next();
170         }
171 
172         try
173         {
174             addToArchive( archiver );
175         }
176         catch ( final IOException e )
177         {
178             throw new ArchiverException( "Error finalizing component-set for archive. Reason: " + e.getMessage(), e );
179         }
180     }
181 
182     public List<String> getVirtualFiles()
183     {
184         if ( ( components != null ) && !components.isEmpty() )
185         {
186             return Collections.singletonList( COMPONENTS_XML_PATH );
187         }
188 
189         return null;
190     }
191 
192     public boolean isSelected( final @Nonnull FileInfo fileInfo )
193         throws IOException
194     {
195         if ( fileInfo.isFile() )
196         {
197             if ( excludeOverride )
198             {
199                 return true;
200             }
201 
202             String entry = fileInfo.getName().replace( '\\', '/' );
203 
204             if ( entry.startsWith( "/" ) )
205             {
206                 entry = entry.substring( 1 );
207             }
208 
209             if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) )
210             {
211                 InputStream stream = null;
212                 InputStreamReader reader = null;
213 
214                 try
215                 {
216                     stream = fileInfo.getContents();
217                     // TODO use ReaderFactory.newXmlReader() when plexus-utils is upgraded to 1.4.5+
218                     reader = new InputStreamReader( stream, "UTF-8" );
219                     addComponentsXml( new BufferedReader( reader, 8192 ) );
220                 }
221                 catch ( final XmlPullParserException e )
222                 {
223                     final IOException error =
224                         new IOException( "Error finalizing component-set for archive. Reason: " + e.getMessage() );
225                     error.initCause( e );
226 
227                     throw error;
228                 }
229                 finally
230                 {
231                     IOUtil.close( stream );
232                     IOUtil.close( reader );
233                 }
234 
235                 return false;
236             }
237             else
238             {
239                 return true;
240             }
241         }
242         else
243         {
244             return true;
245         }
246     }
247 
248     public void finalizeArchiveExtraction( final UnArchiver unarchiver )
249         throws ArchiverException
250     {
251     }
252 
253 }