View Javadoc
1   package org.apache.maven.plugins.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.plugins.assembly.utils.AssemblyFileUtils;
23  import org.codehaus.plexus.archiver.Archiver;
24  import org.codehaus.plexus.archiver.ArchiverException;
25  import org.codehaus.plexus.archiver.ResourceIterator;
26  import org.codehaus.plexus.archiver.UnArchiver;
27  import org.codehaus.plexus.components.io.fileselectors.FileInfo;
28  import org.codehaus.plexus.util.IOUtil;
29  
30  import javax.annotation.Nonnull;
31  import java.io.BufferedReader;
32  import java.io.File;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  import java.io.InputStreamReader;
36  import java.io.OutputStreamWriter;
37  import java.io.PrintWriter;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  abstract class AbstractLineAggregatingHandler
44      implements ContainerDescriptorHandler
45  {
46  
47      private Map<String, List<String>> catalog = new HashMap<String, List<String>>();
48  
49      private boolean excludeOverride = false;
50  
51      protected abstract String getOutputPathPrefix( final FileInfo fileInfo );
52  
53      protected abstract boolean fileMatches( final FileInfo fileInfo );
54  
55      String getEncoding()
56      {
57          return "UTF-8";
58      }
59  
60      @Override
61      public void finalizeArchiveCreation( final Archiver archiver )
62      {
63          // this will prompt the isSelected() call, below, for all resources added to the archive.
64          // FIXME: This needs to be corrected in the AbstractArchiver, where
65          // runArchiveFinalizers() is called before regular resources are added...
66          // which is done because the manifest needs to be added first, and the
67          // manifest-creation component is a finalizer in the assembly plugin...
68          for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
69          {
70              it.next();
71          }
72  
73          addToArchive( archiver );
74      }
75  
76      void addToArchive( final Archiver archiver )
77      {
78          for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
79          {
80              final String name = entry.getKey();
81              final String fname = new File( name ).getName();
82  
83              PrintWriter writer = null;
84              File f;
85              try
86              {
87                  f = File.createTempFile( "assembly-" + fname, ".tmp" );
88                  f.deleteOnExit();
89  
90                  writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
91                  for ( final String line : entry.getValue() )
92                  {
93                      writer.println( line );
94                  }
95  
96                  writer.close();
97                  writer = null;
98              }
99              catch ( final IOException e )
100             {
101                 throw new ArchiverException(
102                     "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
103                         + e.getMessage(), e );
104             }
105             finally
106             {
107                 IOUtil.close( writer );
108             }
109 
110             excludeOverride = true;
111             archiver.addFile( f, name );
112             excludeOverride = false;
113         }
114     }
115 
116     @Override
117     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
118     {
119     }
120 
121     @Override
122     public List<String> getVirtualFiles()
123     {
124         return new ArrayList<String>( catalog.keySet() );
125     }
126 
127     @Override
128     public boolean isSelected( @Nonnull final FileInfo fileInfo )
129         throws IOException
130     {
131         if ( excludeOverride )
132         {
133             return true;
134         }
135 
136         String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
137 
138         if ( fileInfo.isFile() && fileMatches( fileInfo ) )
139         {
140             name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
141 
142             List<String> lines = catalog.get( name );
143             if ( lines == null )
144             {
145                 lines = new ArrayList<String>();
146                 catalog.put( name, lines );
147             }
148 
149             readLines( fileInfo, lines );
150 
151             return false;
152         }
153 
154         return true;
155     }
156 
157     void readLines( final FileInfo fileInfo, final List<String> lines )
158         throws IOException
159     {
160         BufferedReader reader = null;
161         try
162         {
163             reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
164 
165             for ( String line = reader.readLine(); line != null; line = reader.readLine() )
166             {
167                 if ( !lines.contains( line ) )
168                 {
169                     lines.add( line );
170                 }
171             }
172 
173             reader.close();
174             reader = null;
175         }
176         finally
177         {
178             IOUtil.close( reader );
179         }
180     }
181 
182     protected final Map<String, List<String>> getCatalog()
183     {
184         return catalog;
185     }
186 
187     protected final void setCatalog( final Map<String, List<String>> catalog )
188     {
189         this.catalog = catalog;
190     }
191 
192 }