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.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      public void finalizeArchiveCreation( final Archiver archiver )
61      {
62          // this will prompt the isSelected() call, below, for all resources added to the archive.
63          // FIXME: This needs to be corrected in the AbstractArchiver, where
64          // runArchiveFinalizers() is called before regular resources are added...
65          // which is done because the manifest needs to be added first, and the
66          // manifest-creation component is a finalizer in the assembly plugin...
67          for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
68          {
69              it.next();
70          }
71  
72          addToArchive( archiver );
73      }
74  
75      void addToArchive( final Archiver archiver )
76      {
77          for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
78          {
79              final String name = entry.getKey();
80              final String fname = new File( name ).getName();
81  
82              PrintWriter writer = null;
83              File f;
84              try
85              {
86                  f = File.createTempFile( "assembly-" + fname, ".tmp" );
87                  f.deleteOnExit();
88  
89                  writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
90                  for ( final String line : entry.getValue() )
91                  {
92                      writer.println( line );
93                  }
94              }
95              catch ( final IOException e )
96              {
97                  throw new ArchiverException(
98                      "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
99                          + e.getMessage(), e );
100             }
101             finally
102             {
103                 IOUtil.close( writer );
104             }
105 
106             excludeOverride = true;
107             archiver.addFile( f, name );
108             excludeOverride = false;
109         }
110     }
111 
112     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
113     {
114     }
115 
116     public List<String> getVirtualFiles()
117     {
118         return new ArrayList<String>( catalog.keySet() );
119     }
120 
121     public boolean isSelected( @Nonnull final FileInfo fileInfo )
122         throws IOException
123     {
124         if ( excludeOverride )
125         {
126             return true;
127         }
128 
129         String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
130 
131         if ( fileInfo.isFile() && fileMatches( fileInfo ) )
132         {
133             name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
134 
135             List<String> lines = catalog.get( name );
136             if ( lines == null )
137             {
138                 lines = new ArrayList<String>();
139                 catalog.put( name, lines );
140             }
141 
142             readLines( fileInfo, lines );
143 
144             return false;
145         }
146 
147         return true;
148     }
149 
150     void readLines( final FileInfo fileInfo, final List<String> lines )
151         throws IOException
152     {
153         BufferedReader reader = null;
154         try
155         {
156             reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
157 
158             String line;
159             while ( ( line = reader.readLine() ) != null )
160             {
161                 if ( !lines.contains( line ) )
162                 {
163                     lines.add( line );
164                 }
165             }
166         }
167         finally
168         {
169             IOUtil.close( reader );
170         }
171     }
172 
173     protected final Map<String, List<String>> getCatalog()
174     {
175         return catalog;
176     }
177 
178     protected final void setCatalog( final Map<String, List<String>> catalog )
179     {
180         this.catalog = catalog;
181     }
182 
183 }