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  
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.io.InputStreamReader;
34  import java.io.OutputStreamWriter;
35  import java.io.PrintWriter;
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  abstract class AbstractLineAggregatingHandler
42      implements ContainerDescriptorHandler
43  {
44  
45      private Map<String, List<String>> catalog = new HashMap<>();
46  
47      private boolean excludeOverride = false;
48  
49      protected abstract String getOutputPathPrefix( final FileInfo fileInfo );
50  
51      protected abstract boolean fileMatches( final FileInfo fileInfo );
52  
53      String getEncoding()
54      {
55          return "UTF-8";
56      }
57  
58      @Override
59      public void finalizeArchiveCreation( final Archiver archiver )
60      {
61          // this will prompt the isSelected() call, below, for all resources added to the archive.
62          // FIXME: This needs to be corrected in the AbstractArchiver, where
63          // runArchiveFinalizers() is called before regular resources are added...
64          // which is done because the manifest needs to be added first, and the
65          // manifest-creation component is a finalizer in the assembly plugin...
66          for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
67          {
68              it.next();
69          }
70  
71          addToArchive( archiver );
72      }
73  
74      void addToArchive( final Archiver archiver )
75      {
76          for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
77          {
78              final String name = entry.getKey();
79              final String fname = new File( name ).getName();
80  
81              File f;
82              try
83              {
84                  f = File.createTempFile( "assembly-" + fname, ".tmp" );
85                  f.deleteOnExit();
86  
87                  try ( PrintWriter writer =
88                      new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) ) )
89                  {
90                      for ( final String line : entry.getValue() )
91                      {
92                          writer.println( line );
93                      }
94                  }
95              }
96              catch ( final IOException e )
97              {
98                  throw new ArchiverException(
99                      "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
100                         + e.getMessage(), e );
101             }
102 
103             excludeOverride = true;
104             archiver.addFile( f, name );
105             excludeOverride = false;
106         }
107     }
108 
109     @Override
110     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
111     {
112     }
113 
114     @Override
115     public List<String> getVirtualFiles()
116     {
117         return new ArrayList<>( catalog.keySet() );
118     }
119 
120     @Override
121     public boolean isSelected( 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<>();
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         try ( BufferedReader reader =
154             new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) ) )
155         {
156             for ( String line = reader.readLine(); line != null; line = reader.readLine() )
157             {
158                 if ( !lines.contains( line ) )
159                 {
160                     lines.add( line );
161                 }
162             }
163         }
164     }
165 
166     protected final Map<String, List<String>> getCatalog()
167     {
168         return catalog;
169     }
170 
171     protected final void setCatalog( final Map<String, List<String>> catalog )
172     {
173         this.catalog = catalog;
174     }
175 
176 }