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 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<String, List<String>>();
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      public void finalizeArchiveCreation( final Archiver archiver )
60          throws ArchiverException
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          throws ArchiverException
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              catch ( final IOException e )
97              {
98                  throw new ArchiverException( "Error adding aggregated content for: " + fname
99                                  + " to finalize archive creation. Reason: " + 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         throws ArchiverException
114     {
115     }
116 
117     public List<String> getVirtualFiles()
118     {
119         return new ArrayList<String>( catalog.keySet() );
120     }
121 
122     public boolean isSelected( 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<String>();
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         BufferedReader reader = null;
155         try
156         {
157             reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
158 
159             String line;
160             while ( ( line = reader.readLine() ) != null )
161             {
162                 if ( !lines.contains( line ) )
163                 {
164                     lines.add( line );
165                 }
166             }
167         }
168         finally
169         {
170             IOUtil.close( reader );
171         }
172     }
173 
174     protected final Map<String, List<String>> getCatalog()
175     {
176         return catalog;
177     }
178 
179     protected final void setCatalog( final Map<String, List<String>> catalog )
180     {
181         this.catalog = catalog;
182     }
183 
184 }