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