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