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 javax.annotation.Nonnull;
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  import java.io.InputStreamReader;
35  import java.io.OutputStreamWriter;
36  import java.io.PrintWriter;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  abstract class AbstractLineAggregatingHandler
43      implements ContainerDescriptorHandler
44  {
45  
46      private Map<String, List<String>> catalog = new HashMap<>();
47  
48      private boolean excludeOverride = false;
49  
50      protected abstract String getOutputPathPrefix( final FileInfo fileInfo );
51  
52      protected abstract boolean fileMatches( final FileInfo fileInfo );
53  
54      String getEncoding()
55      {
56          return "UTF-8";
57      }
58  
59      @Override
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              File f;
83              try
84              {
85                  f = File.createTempFile( "assembly-" + fname, ".tmp" );
86                  f.deleteOnExit();
87  
88                  try ( PrintWriter writer =
89                      new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) ) )
90                  {
91                      for ( final String line : entry.getValue() )
92                      {
93                          writer.println( line );
94                      }
95                  }
96              }
97              catch ( final IOException e )
98              {
99                  throw new ArchiverException(
100                     "Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
101                         + e.getMessage(), e );
102             }
103 
104             excludeOverride = true;
105             archiver.addFile( f, name );
106             excludeOverride = false;
107         }
108     }
109 
110     @Override
111     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
112     {
113     }
114 
115     @Override
116     public List<String> getVirtualFiles()
117     {
118         return new ArrayList<>( catalog.keySet() );
119     }
120 
121     @Override
122     public boolean isSelected( @Nonnull final FileInfo fileInfo )
123         throws IOException
124     {
125         if ( excludeOverride )
126         {
127             return true;
128         }
129 
130         String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
131 
132         if ( fileInfo.isFile() && fileMatches( fileInfo ) )
133         {
134             name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
135 
136             List<String> lines = catalog.get( name );
137             if ( lines == null )
138             {
139                 lines = new ArrayList<>();
140                 catalog.put( name, lines );
141             }
142 
143             readLines( fileInfo, lines );
144 
145             return false;
146         }
147 
148         return true;
149     }
150 
151     void readLines( final FileInfo fileInfo, final List<String> lines )
152         throws IOException
153     {
154         try ( BufferedReader reader =
155             new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) ) )
156         {
157             for ( String line = reader.readLine(); line != null; line = reader.readLine() )
158             {
159                 if ( !lines.contains( line ) )
160                 {
161                     lines.add( line );
162                 }
163             }
164         }
165     }
166 
167     protected final Map<String, List<String>> getCatalog()
168     {
169         return catalog;
170     }
171 
172     protected final void setCatalog( final Map<String, List<String>> catalog )
173     {
174         this.catalog = catalog;
175     }
176 
177 }