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 javax.annotation.Nonnull;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.charset.Charset;
26  import java.nio.file.attribute.FileTime;
27  import java.util.ArrayList;
28  import java.util.Comparator;
29  import java.util.Date;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.codehaus.plexus.archiver.ArchiveEntry;
35  import org.codehaus.plexus.archiver.ArchivedFileSet;
36  import org.codehaus.plexus.archiver.Archiver;
37  import org.codehaus.plexus.archiver.ArchiverException;
38  import org.codehaus.plexus.archiver.FileSet;
39  import org.codehaus.plexus.archiver.ResourceIterator;
40  import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
41  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
42  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
43  import org.codehaus.plexus.util.StringUtils;
44  
45  /**
46   * A diagnostic archiver that keeps track of stuff that has been added.
47   */
48  public class TrackingArchiver implements Archiver {
49  
50      private File destFile;
51  
52      public final List<Addition> added = new ArrayList<>();
53  
54      private boolean useJvmChmod;
55  
56      private boolean ignorePermissions;
57  
58      private FileTime lastModifiedTime;
59  
60      private Comparator<String> filenameComparator;
61  
62      @Override
63      public void createArchive() throws ArchiverException, IOException {}
64  
65      @Override
66      public void addDirectory(final @Nonnull File directory) throws ArchiverException {
67          added.add(new Addition(directory, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
68      }
69  
70      @Override
71      public void addDirectory(final @Nonnull File directory, final String prefix) throws ArchiverException {
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          added.add(new Addition(directory, null, includes, excludes, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
79      }
80  
81      @Override
82      public void addDirectory(
83              final @Nonnull File directory, final String prefix, final String[] includes, final String[] excludes)
84              throws ArchiverException {
85          added.add(new Addition(directory, prefix, includes, excludes, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
86      }
87  
88      @Override
89      public void addFileSet(final @Nonnull FileSet fileSet) throws ArchiverException {
90          added.add(new Addition(fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
91      }
92  
93      @Override
94      public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName) throws ArchiverException {
95          added.add(new Addition(inputFile, destFileName, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
96      }
97  
98      @Override
99      public void addFile(final @Nonnull File inputFile, final @Nonnull String destFileName, final int permissions)
100             throws ArchiverException {
101         added.add(new Addition(inputFile, destFileName, null, null, permissions));
102     }
103 
104     @Override
105     public void addArchivedFileSet(final @Nonnull File archiveFile) throws ArchiverException {
106         added.add(new Addition(archiveFile, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
107     }
108 
109     @Override
110     public void addArchivedFileSet(final @Nonnull File archiveFile, final String prefix) throws ArchiverException {
111         added.add(new Addition(archiveFile, prefix, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
112     }
113 
114     @Override
115     public void addSymlink(String s, String s2) throws ArchiverException {
116         added.add(new Addition(s, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
117     }
118 
119     @Override
120     public void addSymlink(String s, int i, String s2) throws ArchiverException {
121         added.add(new Addition(s, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
122     }
123 
124     @Override
125     public void addArchivedFileSet(final File archiveFile, final String[] includes, final String[] excludes)
126             throws ArchiverException {
127         added.add(new Addition(archiveFile, null, includes, excludes, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
128     }
129 
130     @Override
131     public void addArchivedFileSet(
132             final @Nonnull File archiveFile, final String prefix, final String[] includes, final String[] excludes)
133             throws ArchiverException {
134         added.add(new Addition(archiveFile, prefix, includes, excludes, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
135     }
136 
137     @Override
138     public void addArchivedFileSet(final ArchivedFileSet fileSet) throws ArchiverException {
139         added.add(new Addition(fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
140     }
141 
142     @Override
143     public void addArchivedFileSet(final ArchivedFileSet fileSet, Charset charset) throws ArchiverException {
144         added.add(new Addition(fileSet, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
145     }
146 
147     @Override
148     public void addResource(final PlexusIoResource resource, final String destFileName, final int permissions)
149             throws ArchiverException {
150         added.add(new Addition(resource, destFileName, null, null, permissions));
151     }
152 
153     @Override
154     public void addResources(final PlexusIoResourceCollection resources) throws ArchiverException {
155         added.add(new Addition(resources, null, null, null, PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE));
156     }
157 
158     @Override
159     public File getDestFile() {
160         return destFile;
161     }
162 
163     @Override
164     public void setDestFile(final File destFile) {
165         this.destFile = destFile;
166     }
167 
168     @Override
169     public void setFileMode(final int mode) {}
170 
171     @Override
172     public int getFileMode() {
173         return Integer.parseInt("0644", 8);
174     }
175 
176     @Override
177     public int getOverrideFileMode() {
178         return Integer.parseInt("0644", 8);
179     }
180 
181     @Override
182     public void setDefaultFileMode(final int mode) {}
183 
184     @Override
185     public int getDefaultFileMode() {
186         return Integer.parseInt("0644", 8);
187     }
188 
189     @Override
190     public void setDirectoryMode(final int mode) {}
191 
192     @Override
193     public int getDirectoryMode() {
194         return Integer.parseInt("0755", 8);
195     }
196 
197     @Override
198     public int getOverrideDirectoryMode() {
199         return Integer.parseInt("0755", 8);
200     }
201 
202     @Override
203     public void setDefaultDirectoryMode(final int mode) {}
204 
205     @Override
206     public int getDefaultDirectoryMode() {
207         return Integer.parseInt("0755", 8);
208     }
209 
210     @Override
211     public boolean getIncludeEmptyDirs() {
212         return false;
213     }
214 
215     @Override
216     public void setIncludeEmptyDirs(final boolean includeEmptyDirs) {}
217 
218     @Override
219     public void setDotFileDirectory(final File dotFileDirectory) {}
220 
221     public @Nonnull @Override ResourceIterator getResources() throws ArchiverException {
222         throw new RuntimeException("Not implemented");
223     }
224 
225     @Override
226     public Map<String, ArchiveEntry> getFiles() {
227         return new HashMap<>();
228     }
229 
230     @Override
231     public boolean isForced() {
232         return false;
233     }
234 
235     @Override
236     public void setForced(final boolean forced) {}
237 
238     @Override
239     public boolean isSupportingForced() {
240         return true;
241     }
242 
243     @Override
244     public String getDuplicateBehavior() {
245         return null;
246     }
247 
248     @Override
249     public void setDuplicateBehavior(final String duplicate) {}
250 
251     public class Addition {
252 
253         /**
254          * {@inheritDoc}
255          *
256          * @see Object#toString()
257          */
258         @Override
259         public String toString() {
260             return "Addition (\n    resource= " + resource + "\n    directory= " + directory + "\n    destination= "
261                     + destination + "\n    permissions= " + permissions + "\n    includes= "
262                     + (includes == null ? "-none-" : StringUtils.join(includes, ", "))
263                     + "\n    excludes= "
264                     + (excludes == null ? "-none-" : StringUtils.join(excludes, ", ")) + "\n)";
265         }
266 
267         public final Object resource;
268 
269         public final File directory;
270 
271         public final String destination;
272 
273         public final int permissions;
274 
275         public final String[] includes;
276 
277         public final String[] excludes;
278 
279         public Addition(
280                 final Object resource,
281                 final String destination,
282                 final String[] includes,
283                 final String[] excludes,
284                 final int permissions) {
285             this.resource = resource;
286             if (resource instanceof FileSet) {
287                 final FileSet fs = (FileSet) resource;
288                 directory = fs.getDirectory();
289                 this.destination = fs.getPrefix();
290                 this.includes = fs.getIncludes();
291                 this.excludes = fs.getExcludes();
292                 this.permissions = permissions;
293             } else {
294                 if (resource instanceof File && ((File) resource).isDirectory()) {
295                     directory = (File) resource;
296                 } else {
297                     directory = null;
298                 }
299 
300                 this.destination = destination;
301                 this.includes = includes;
302                 this.excludes = excludes;
303                 this.permissions = permissions;
304             }
305         }
306     }
307 
308     @Override
309     public boolean isUseJvmChmod() {
310         return useJvmChmod;
311     }
312 
313     @Override
314     public void setUseJvmChmod(final boolean useJvmChmod) {
315         this.useJvmChmod = useJvmChmod;
316     }
317 
318     @Override
319     public boolean isIgnorePermissions() {
320         return ignorePermissions;
321     }
322 
323     @Override
324     public void setIgnorePermissions(final boolean ignorePermissions) {
325         this.ignorePermissions = ignorePermissions;
326     }
327 
328     /**
329      * @deprecated Use {@link #setLastModifiedTime(FileTime)} instead.
330      */
331     @Override
332     @Deprecated
333     public void setLastModifiedDate(final Date lastModifiedDate) {
334         this.lastModifiedTime = lastModifiedDate != null ? FileTime.fromMillis(lastModifiedDate.getTime()) : null;
335     }
336 
337     /**
338      * @deprecated Use {@link #getLastModifiedTime()} instead.
339      */
340     @Override
341     @Deprecated
342     public Date getLastModifiedDate() {
343         return lastModifiedTime != null ? new Date(lastModifiedTime.toMillis()) : null;
344     }
345 
346     @Override
347     public void setLastModifiedTime(final FileTime lastModifiedTime) {
348         this.lastModifiedTime = lastModifiedTime;
349     }
350 
351     @Override
352     public FileTime getLastModifiedTime() {
353         return lastModifiedTime;
354     }
355 
356     @Override
357     public void setFilenameComparator(final Comparator<String> filenameComparator) {
358         this.filenameComparator = filenameComparator;
359     }
360 
361     public Comparator<String> getFilenameComparator() {
362         return filenameComparator;
363     }
364 
365     @Override
366     public void setOverrideUid(int uid) {}
367 
368     @Override
369     public void setOverrideUserName(String userName) {}
370 
371     @Override
372     public int getOverrideUid() {
373         return 0;
374     }
375 
376     @Override
377     public String getOverrideUserName() {
378         return null;
379     }
380 
381     @Override
382     public void setOverrideGid(int gid) {}
383 
384     @Override
385     public void setOverrideGroupName(String groupName) {}
386 
387     @Override
388     public int getOverrideGid() {
389         return 0;
390     }
391 
392     @Override
393     public String getOverrideGroupName() {
394         return null;
395     }
396 
397     @Override
398     public void setUmask(int umask) {}
399 
400     @Override
401     public int getUmask() {
402         return 0;
403     }
404 
405     /**
406      * @deprecated Use {@link #configureReproducibleBuild(FileTime)} instead.
407      */
408     @Override
409     @Deprecated
410     public void configureReproducible(Date lastModifiedDate) {}
411 
412     @Override
413     public void configureReproducibleBuild(FileTime lastModifiedTime) {}
414 }