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