View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.swid.plugin.entry.resource;
28  
29  import gov.nist.secauto.swid.plugin.entry.AbstractFileEntryProcessor;
30  import gov.nist.secauto.swid.plugin.entry.FileEntry;
31  import gov.nist.secauto.swid.plugin.entry.FileFileEntry;
32  import gov.nist.secauto.swid.plugin.entry.FileSelectorPredicate;
33  
34  import org.apache.maven.model.Resource;
35  import org.apache.maven.plugin.logging.Log;
36  import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
37  
38  import java.io.IOException;
39  import java.nio.file.Files;
40  import java.nio.file.Path;
41  import java.nio.file.Paths;
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.Collections;
45  import java.util.List;
46  import java.util.Objects;
47  import java.util.stream.Collectors;
48  
49  public class ResourceFileEntryProcessor extends AbstractFileEntryProcessor<Resource> {
50  
51    private IncludeExcludeFileSelector selector = new IncludeExcludeFileSelector();
52    private final String buildOutputDirectory;
53  
54    /**
55     * Construct a new entry processor.
56     * 
57     * @param buildOutputDirectory
58     *          the directory where built classes are stored
59     * @param log
60     *          the Maven logger instance
61     */
62    public ResourceFileEntryProcessor(String buildOutputDirectory, Log log) {
63      super(log);
64      Objects.requireNonNull(buildOutputDirectory);
65      this.buildOutputDirectory = buildOutputDirectory;
66    }
67  
68    public void setIncludes(String[] includes) {
69      selector.setIncludes(includes);
70    }
71  
72    public void setExcludes(String[] excludes) {
73      selector.setExcludes(excludes);
74    }
75  
76    @Override
77    public List<FileEntry> process(List<? extends Resource> resources) throws IOException {
78      List<FileEntry> processedResources = super.process(resources);
79      List<FileEntry> processedClasses = generateClassFileEntries();
80  
81      int size = processedResources.size() + processedClasses.size();
82      List<FileEntry> retval;
83      if (size == 0) {
84        retval = Collections.emptyList();
85      } else {
86        retval = new ArrayList<>(size);
87        retval.addAll(processedResources);
88        retval.addAll(processedClasses);
89      }
90      return retval;
91    }
92  
93    private List<FileEntry> generateClassFileEntries() throws IOException {
94      getLog().debug("Processing classes: " + buildOutputDirectory);
95  
96      Path path = Paths.get(buildOutputDirectory);
97  
98      List<FileEntry> retval;
99      if (path.toFile().exists()) {
100       retval = Files.walk(path).filter(Files::isRegularFile).map(p -> new FileFileEntry(p, path))
101           .filter(new FileSelectorPredicate(selector)).collect(Collectors.toList());
102     } else {
103       retval = Collections.emptyList();
104     }
105     return retval;
106   }
107 
108   @Override
109   protected Collection<? extends FileEntry> generateFileEntries(Resource resource) throws IOException {
110     getLog().debug("Processing resource: " + resource.getDirectory());
111 
112     Path path = Paths.get(resource.getDirectory());
113 
114     List<FileEntry> retval;
115     if (path.toFile().exists()) {
116       retval = Files.walk(path).filter(Files::isRegularFile).map(p -> new ResourceFileEntry(resource, p))
117           .filter(new FileSelectorPredicate(selector)).collect(Collectors.toList());
118     } else {
119       retval = Collections.emptyList();
120     }
121     return retval;
122   }
123 
124 }