View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.codehaus.plexus.archiver.diags;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.charset.Charset;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import javax.annotation.Nonnull;
29  import org.codehaus.plexus.archiver.ArchiveEntry;
30  import org.codehaus.plexus.archiver.ArchivedFileSet;
31  import org.codehaus.plexus.archiver.Archiver;
32  import org.codehaus.plexus.archiver.ArchiverException;
33  import org.codehaus.plexus.archiver.FileSet;
34  import org.codehaus.plexus.archiver.ResourceIterator;
35  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
36  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
37  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * A diagnostic archiver that keeps track of stuff that has been added.
42   */
43  public class TrackingArchiver
44      implements Archiver
45  {
46  
47      private File destFile;
48  
49      public final List<Addition> added = new ArrayList<Addition>();
50  
51      private boolean useJvmChmod;
52  
53      private boolean ignorePermissions;
54  
55      @Override
56      public void createArchive()
57          throws ArchiverException, IOException
58      {
59      }
60  
61      @Override
62      public void addDirectory( final @Nonnull File directory )
63          throws ArchiverException
64      {
65          added.add( new Addition( directory, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
66      }
67  
68      @Override
69      public void addDirectory( final @Nonnull File directory, final String prefix )
70          throws ArchiverException
71      {
72          added.add( new Addition( directory, prefix, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
73      }
74  
75      @Override
76      public void addDirectory( final @Nonnull File directory, final String[] includes, final String[] excludes )
77          throws ArchiverException
78      {
79          added.add( new Addition( directory, null, includes, excludes, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
80      }
81  
82      @Override
83      public void addDirectory( final @Nonnull File directory, final String prefix, final String[] includes,
84                                final String[] excludes )
85          throws ArchiverException
86      {
87          added.add( new Addition( directory, prefix, includes, excludes,
88                                   PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
89  
90      }
91  
92      @Override
93      public void addFileSet( final @Nonnull FileSet fileSet )
94          throws ArchiverException
95      {
96          added.add( new Addition( fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
97      }
98  
99      @Override
100     public void addFile( final @Nonnull File inputFile, final @Nonnull String destFileName )
101         throws ArchiverException
102     {
103         added.add( new Addition( inputFile, destFileName, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
104     }
105 
106     @Override
107     public void addFile( final @Nonnull File inputFile, final @Nonnull String destFileName, final int permissions )
108         throws ArchiverException
109     {
110         added.add( new Addition( inputFile, destFileName, null, null, permissions ) );
111     }
112 
113     @Override
114     public void addArchivedFileSet( final @Nonnull File archiveFile )
115         throws ArchiverException
116     {
117         added.add( new Addition( archiveFile, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
118     }
119 
120     @Override
121     public void addArchivedFileSet( final @Nonnull File archiveFile, final String prefix )
122         throws ArchiverException
123     {
124         added.add( new Addition( archiveFile, prefix, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
125     }
126 
127     @Override
128     public void addSymlink( String s, String s2 )
129         throws ArchiverException
130     {
131         added.add( new Addition( s, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
132     }
133 
134     @Override
135     public void addSymlink( String s, int i, String s2 )
136         throws ArchiverException
137     {
138         added.add( new Addition( s, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
139 
140     }
141 
142     @Override
143     public void addArchivedFileSet( final File archiveFile, final String[] includes, final String[] excludes )
144         throws ArchiverException
145     {
146         added.add( new Addition( archiveFile, null, includes, excludes,
147                                  PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
148 
149     }
150 
151     @Override
152     public void addArchivedFileSet( final @Nonnull File archiveFile, final String prefix, final String[] includes,
153                                     final String[] excludes )
154         throws ArchiverException
155     {
156         added.add( new Addition( archiveFile, prefix, includes, excludes,
157                                  PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
158 
159     }
160 
161     @Override
162     public void addArchivedFileSet( final ArchivedFileSet fileSet )
163         throws ArchiverException
164     {
165         added.add( new Addition( fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
166     }
167 
168     @Override
169     public void addArchivedFileSet( final ArchivedFileSet fileSet, Charset charset )
170         throws ArchiverException
171     {
172         added.add( new Addition( fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
173     }
174 
175     @Override
176     public void addResource( final PlexusIoResource resource, final String destFileName, final int permissions )
177         throws ArchiverException
178     {
179         added.add( new Addition( resource, destFileName, null, null, permissions ) );
180     }
181 
182     @Override
183     public void addResources( final PlexusIoResourceCollection resources )
184         throws ArchiverException
185     {
186         added.add( new Addition( resources, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE ) );
187     }
188 
189     @Override
190     public File getDestFile()
191     {
192         return destFile;
193     }
194 
195     @Override
196     public void setDestFile( final File destFile )
197     {
198         this.destFile = destFile;
199     }
200 
201     @Override
202     public void setFileMode( final int mode )
203     {
204     }
205 
206     @Override
207     public int getFileMode()
208     {
209         return Integer.parseInt( "0644", 8 );
210     }
211 
212     @Override
213     public int getOverrideFileMode()
214     {
215         return Integer.parseInt( "0644", 8 );
216     }
217 
218     @Override
219     public void setDefaultFileMode( final int mode )
220     {
221     }
222 
223     @Override
224     public int getDefaultFileMode()
225     {
226         return Integer.parseInt( "0644", 8 );
227     }
228 
229     @Override
230     public void setDirectoryMode( final int mode )
231     {
232     }
233 
234     @Override
235     public int getDirectoryMode()
236     {
237         return Integer.parseInt( "0755", 8 );
238     }
239 
240     @Override
241     public int getOverrideDirectoryMode()
242     {
243         return Integer.parseInt( "0755", 8 );
244     }
245 
246     @Override
247     public void setDefaultDirectoryMode( final int mode )
248     {
249     }
250 
251     @Override
252     public int getDefaultDirectoryMode()
253     {
254         return Integer.parseInt( "0755", 8 );
255     }
256 
257     @Override
258     public boolean getIncludeEmptyDirs()
259     {
260         return false;
261     }
262 
263     @Override
264     public void setIncludeEmptyDirs( final boolean includeEmptyDirs )
265     {
266     }
267 
268     @Override
269     public void setDotFileDirectory( final File dotFileDirectory )
270     {
271     }
272 
273     public @Nonnull
274     @Override
275     ResourceIterator getResources()
276         throws ArchiverException
277     {
278         throw new RuntimeException( "Not implemented" );
279     }
280 
281     @SuppressWarnings( "rawtypes" )
282     @Override
283     public Map<String, ArchiveEntry> getFiles()
284     {
285         return new HashMap<String, ArchiveEntry>();
286     }
287 
288     @Override
289     public boolean isForced()
290     {
291         return false;
292     }
293 
294     @Override
295     public void setForced( final boolean forced )
296     {
297     }
298 
299     @Override
300     public boolean isSupportingForced()
301     {
302         return true;
303     }
304 
305     @Override
306     public String getDuplicateBehavior()
307     {
308         return null;
309     }
310 
311     @Override
312     public void setDuplicateBehavior( final String duplicate )
313     {
314     }
315 
316     public class Addition
317     {
318 
319         /**
320          * {@inheritDoc}
321          *
322          * @see Object#toString()
323          */
324         @Override
325         public String toString()
326         {
327             return "Addition (\n    resource= " + resource + "\n    directory= " + directory + "\n    destination= "
328                        + destination + "\n    permissions= " + permissions + "\n    includes= "
329                        + ( includes == null ? "-none-" : StringUtils.join( includes, ", " ) )
330                        + "\n    excludes= "
331                        + ( excludes == null ? "-none-" : StringUtils.join( excludes, ", " ) ) + "\n)";
332 
333         }
334 
335         public final Object resource;
336 
337         public final File directory;
338 
339         public final String destination;
340 
341         public final int permissions;
342 
343         public final String[] includes;
344 
345         public final String[] excludes;
346 
347         public Addition( final Object resource, final String destination, final String[] includes,
348                          final String[] excludes, final int permissions )
349         {
350             this.resource = resource;
351             if ( resource instanceof FileSet )
352             {
353                 final FileSet fs = (FileSet) resource;
354                 directory = fs.getDirectory();
355                 this.destination = fs.getPrefix();
356                 this.includes = fs.getIncludes();
357                 this.excludes = fs.getExcludes();
358                 this.permissions = permissions;
359             }
360             else
361             {
362                 if ( resource instanceof File && ( (File) resource ).isDirectory() )
363                 {
364                     directory = (File) resource;
365                 }
366                 else
367                 {
368                     directory = null;
369                 }
370 
371                 this.destination = destination;
372                 this.includes = includes;
373                 this.excludes = excludes;
374                 this.permissions = permissions;
375             }
376         }
377 
378     }
379 
380     @Override
381     public boolean isUseJvmChmod()
382     {
383         return useJvmChmod;
384     }
385 
386     @Override
387     public void setUseJvmChmod( final boolean useJvmChmod )
388     {
389         this.useJvmChmod = useJvmChmod;
390     }
391 
392     @Override
393     public boolean isIgnorePermissions()
394     {
395         return ignorePermissions;
396     }
397 
398     @Override
399     public void setIgnorePermissions( final boolean ignorePermissions )
400     {
401         this.ignorePermissions = ignorePermissions;
402     }
403 
404 }