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.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   * Components XML file filter.
49   * 
50   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
51   * @version $Id: ComponentsXmlArchiverFileFilter.java 999612 2010-09-21 20:34:50Z jdcasey $
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 ( int i = 0; i < children.length; i++ )
79              {
80                  final Xpp3Dom component = children[i];
81  
82                  if ( components == null )
83                  {
84                      components = new LinkedHashMap<String, Xpp3Dom>();
85                  }
86  
87                  final String role = component.getChild( "role" )
88                                               .getValue();
89                  final Xpp3Dom child = component.getChild( "role-hint" );
90                  final String roleHint = child != null ? child.getValue() : "";
91  
92                  final String key = role + roleHint;
93                  if ( !components.containsKey( key ) )
94                  {
95                      System.out.println( "Adding " + key );
96                      components.put( key, component );
97                  }
98                  else
99                  {
100                     System.out.println( "Component: " + key + " is already defined. Skipping." );
101                 }
102             }
103         }
104     }
105 
106     // public void addComponentsXml( File componentsXml )
107     // throws IOException, XmlPullParserException
108     // {
109     // FileReader fileReader = null;
110     // try
111     // {
112     // fileReader = new FileReader( componentsXml );
113     //
114     // addComponentsXml( fileReader );
115     // }
116     // finally
117     // {
118     // IOUtil.close( fileReader );
119     // }
120     // }
121 
122     private void addToArchive( final Archiver archiver ) throws IOException, ArchiverException
123     {
124         if ( components != null )
125         {
126             final File f = File.createTempFile( "maven-assembly-plugin", "tmp" );
127             f.deleteOnExit();
128 
129             // TODO use WriterFactory.newXmlWriter() when plexus-utils is upgraded to 1.4.5+
130             final Writer fileWriter = new OutputStreamWriter( new FileOutputStream( f ), "UTF-8" );
131             try
132             {
133                 final Xpp3Dom dom = new Xpp3Dom( "component-set" );
134                 final Xpp3Dom componentDom = new Xpp3Dom( "components" );
135                 dom.addChild( componentDom );
136 
137                 for ( final Iterator<Xpp3Dom> i = components.values()
138                                                             .iterator(); i.hasNext(); )
139                 {
140                     final Xpp3Dom component = i.next();
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 ) throws ArchiverException
160     {
161         // this will prompt the isSelected() call, below, for all resources added to the archive.
162         // FIXME: This needs to be corrected in the AbstractArchiver, where
163         // runArchiveFinalizers() is called before regular resources are added...
164         // which is done because the manifest needs to be added first, and the
165         // manifest-creation component is a finalizer in the assembly plugin...
166         for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
167         {
168             it.next();
169         }
170 
171         try
172         {
173             addToArchive( archiver );
174         }
175         catch ( final IOException e )
176         {
177             throw new ArchiverException( "Error finalizing component-set for archive. Reason: " + e.getMessage(), e );
178         }
179     }
180 
181     public List<String> getVirtualFiles()
182     {
183         if ( ( components != null ) && !components.isEmpty() )
184         {
185             return Collections.singletonList( COMPONENTS_XML_PATH );
186         }
187 
188         return null;
189     }
190 
191     public boolean isSelected( final FileInfo fileInfo ) throws IOException
192     {
193         if ( fileInfo.isFile() )
194         {
195             if ( excludeOverride )
196             {
197                 return true;
198             }
199 
200             String entry = fileInfo.getName()
201                                    .replace( '\\', '/' );
202 
203             if ( entry.startsWith( "/" ) )
204             {
205                 entry = entry.substring( 1 );
206             }
207 
208             if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) )
209             {
210                 InputStream stream = null;
211                 InputStreamReader reader = null;
212 
213                 try
214                 {
215                     stream = fileInfo.getContents();
216                     // TODO use ReaderFactory.newXmlReader() when plexus-utils is upgraded to 1.4.5+
217                     reader = new InputStreamReader( stream, "UTF-8" );
218                     addComponentsXml( reader );
219                 }
220                 catch ( final XmlPullParserException e )
221                 {
222                     final IOException error =
223                         new IOException( "Error finalizing component-set for archive. Reason: " + e.getMessage() );
224                     error.initCause( e );
225 
226                     throw error;
227                 }
228                 finally
229                 {
230                     IOUtil.close( stream );
231                     IOUtil.close( reader );
232                 }
233 
234                 return false;
235             }
236             else
237             {
238                 return true;
239             }
240         }
241         else
242         {
243             return true;
244         }
245     }
246 
247     public void finalizeArchiveExtraction( final UnArchiver unarchiver ) throws ArchiverException
248     {
249     }
250 
251 }