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.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
23  import org.codehaus.plexus.archiver.Archiver;
24  import org.codehaus.plexus.archiver.ArchiverException;
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.logging.LogEnabled;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.logging.console.ConsoleLogger;
31  import org.codehaus.plexus.util.IOUtil;
32  
33  import javax.annotation.Nonnull;
34  import java.io.File;
35  import java.io.FileOutputStream;
36  import java.io.IOException;
37  import java.io.InputStreamReader;
38  import java.io.OutputStreamWriter;
39  import java.io.Reader;
40  import java.io.StringWriter;
41  import java.io.Writer;
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Date;
45  import java.util.List;
46  
47  /**
48   * @version $Id: SimpleAggregatingDescriptorHandler.java 1639422 2014-11-13 18:08:07Z krosenvold $
49   */
50  @Component( role = ContainerDescriptorHandler.class, hint = "file-aggregator", instantiationStrategy = "per-lookup" )
51  public class SimpleAggregatingDescriptorHandler
52      implements ContainerDescriptorHandler, LogEnabled
53  {
54  
55      // component configuration.
56  
57      private String filePattern;
58  
59      private String outputPath;
60  
61      @SuppressWarnings( "FieldCanBeLocal" )
62      private final String commentChars = "#";
63  
64      // calculated, temporary values.
65  
66      private boolean overrideFilterAction;
67  
68      private final StringWriter aggregateWriter = new StringWriter();
69  
70      private final List<String> filenames = new ArrayList<String>();
71  
72      // injected by the container.
73  
74      private Logger logger;
75  
76      public void finalizeArchiveCreation( final Archiver archiver )
77          throws ArchiverException
78      {
79          checkConfig();
80  
81          if ( outputPath.endsWith( "/" ) )
82          {
83              throw new ArchiverException(
84                  "Cannot write aggregated properties to a directory. You must specify a file name in the outputPath configuration for this handler. (handler: "
85                      + getClass().getName() );
86          }
87  
88          if ( outputPath.startsWith( "/" ) )
89          {
90              outputPath = outputPath.substring( 1 );
91          }
92  
93          final File temp = writePropertiesFile();
94  
95          overrideFilterAction = true;
96  
97          archiver.addFile( temp, outputPath );
98  
99          overrideFilterAction = false;
100     }
101 
102     private File writePropertiesFile()
103         throws ArchiverException
104     {
105         File f;
106 
107         Writer writer = null;
108         try
109         {
110             f = File.createTempFile( "maven-assembly-plugin", "tmp" );
111             f.deleteOnExit();
112 
113             boolean isProperty = AssemblyFileUtils.isPropertyFile( f );
114             FileOutputStream fos = new FileOutputStream( f );
115             writer = isProperty
116                 ? new OutputStreamWriter( fos, "ISO-8859-1" )
117                 : new OutputStreamWriter( fos ); // Still platform encoding
118 
119             writer.write( commentChars + " Aggregated on " + new Date() + " from: " );
120 
121             for ( final String filename : filenames )
122             {
123                 writer.write( "\n" + commentChars + " " + filename );
124             }
125 
126             writer.write( "\n\n" );
127 
128             writer.write( aggregateWriter.toString() );
129         }
130         catch ( final IOException e )
131         {
132             throw new ArchiverException(
133                 "Error adding aggregated properties to finalize archive creation. Reason: " + e.getMessage(), e );
134         }
135         finally
136         {
137             IOUtil.close( writer );
138         }
139 
140         return f;
141     }
142 
143     public void finalizeArchiveExtraction( final UnArchiver unarchiver )
144         throws ArchiverException
145     {
146     }
147 
148     public List<String> getVirtualFiles()
149     {
150         checkConfig();
151 
152         return Collections.singletonList( outputPath );
153     }
154 
155     public boolean isSelected( final @Nonnull FileInfo fileInfo )
156         throws IOException
157     {
158         checkConfig();
159 
160         if ( overrideFilterAction )
161         {
162             System.out.println( "Filtering overridden. Returning true." );
163             return true;
164         }
165 
166         String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
167 
168         if ( fileInfo.isFile() && name.matches( filePattern ) )
169         {
170             readProperties( fileInfo );
171             filenames.add( name );
172 
173             return false;
174         }
175 
176         return true;
177     }
178 
179     private void checkConfig()
180     {
181         if ( filePattern == null || outputPath == null )
182         {
183             throw new IllegalStateException(
184                 "You must configure filePattern and outputPath in your containerDescriptorHandler declaration." );
185         }
186     }
187 
188     private void readProperties( final FileInfo fileInfo )
189         throws IOException
190     {
191         final StringWriter writer = new StringWriter();
192         Reader reader = null;
193         try
194         {
195             boolean isProperty = AssemblyFileUtils.isPropertyFile( fileInfo.getName() );
196 
197             reader = isProperty
198                 ? new InputStreamReader( fileInfo.getContents(), "ISO-8859-1" )
199                 : new InputStreamReader( fileInfo.getContents() ); // platform encoding
200 
201             IOUtil.copy( reader, writer );
202         }
203         finally
204         {
205             IOUtil.close( reader );
206         }
207 
208         final String content = writer.toString();
209 
210         aggregateWriter.write( "\n" );
211         aggregateWriter.write( content );
212     }
213 
214     protected final Logger getLogger()
215     {
216         if ( logger == null )
217         {
218             logger = new ConsoleLogger( Logger.LEVEL_INFO, "" );
219         }
220 
221         return logger;
222     }
223 
224     public void enableLogging( final Logger logger )
225     {
226         this.logger = logger;
227     }
228 
229     @SuppressWarnings( "UnusedDeclaration" )
230     public String getFilePattern()
231     {
232         return filePattern;
233     }
234 
235     @SuppressWarnings( "UnusedDeclaration" )
236     public void setFilePattern( final String filePattern )
237     {
238         this.filePattern = filePattern;
239     }
240 
241     @SuppressWarnings( "UnusedDeclaration" )
242     public String getOutputPath()
243     {
244         return outputPath;
245     }
246 
247     @SuppressWarnings( "UnusedDeclaration" )
248     public void setOutputPath( final String outputPath )
249     {
250         this.outputPath = outputPath;
251     }
252 
253 }