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  public 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      protected 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      protected 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 = fileInfo.getName();
131         name = AssemblyFileUtils.normalizePath( name );
132         name = name.replace( File.separatorChar, '/' );
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     protected 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 = null;
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 }