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          throws ArchiverException
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          throws ArchiverException
78      {
79          for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
80          {
81              final String name = entry.getKey();
82              final String fname = new File( name ).getName();
83  
84              PrintWriter writer = null;
85              File f;
86              try
87              {
88                  f = File.createTempFile( "assembly-" + fname, ".tmp" );
89                  f.deleteOnExit();
90  
91                  writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
92                  for ( final String line : entry.getValue() )
93                  {
94                      writer.println( line );
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             finally
104             {
105                 IOUtil.close( writer );
106             }
107 
108             excludeOverride = true;
109             archiver.addFile( f, name );
110             excludeOverride = false;
111         }
112     }
113 
114     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
115         throws ArchiverException
116     {
117     }
118 
119     public List<String> getVirtualFiles()
120     {
121         return new ArrayList<String>( catalog.keySet() );
122     }
123 
124     public boolean isSelected( final @Nonnull FileInfo fileInfo )
125         throws IOException
126     {
127         if ( excludeOverride )
128         {
129             return true;
130         }
131 
132         String name = AssemblyFileUtils.normalizeFileInfo( fileInfo );
133 
134         if ( fileInfo.isFile() && fileMatches( fileInfo ) )
135         {
136             name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
137 
138             List<String> lines = catalog.get( name );
139             if ( lines == null )
140             {
141                 lines = new ArrayList<String>();
142                 catalog.put( name, lines );
143             }
144 
145             readLines( fileInfo, lines );
146 
147             return false;
148         }
149 
150         return true;
151     }
152 
153     void readLines( final FileInfo fileInfo, final List<String> lines )
154         throws IOException
155     {
156         BufferedReader reader = null;
157         try
158         {
159             reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
160 
161             String line;
162             while ( ( line = reader.readLine() ) != null )
163             {
164                 if ( !lines.contains( line ) )
165                 {
166                     lines.add( line );
167                 }
168             }
169         }
170         finally
171         {
172             IOUtil.close( reader );
173         }
174     }
175 
176     protected final Map<String, List<String>> getCatalog()
177     {
178         return catalog;
179     }
180 
181     protected final void setCatalog( final Map<String, List<String>> catalog )
182     {
183         this.catalog = catalog;
184     }
185 
186 }