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