View Javadoc
1   package org.codehaus.plexus.compiler.util.scan;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
26  
27  /**
28   * @author jdcasey
29   */
30  public class StaleSourceScanner extends AbstractSourceInclusionScanner {
31      private final long lastUpdatedWithinMsecs;
32  
33      private final Set<String> sourceIncludes;
34  
35      private final Set<String> sourceExcludes;
36  
37      // ----------------------------------------------------------------------
38      //
39      // ----------------------------------------------------------------------
40  
41      public StaleSourceScanner() {
42          this(0, Collections.singleton("**/*"), Collections.emptySet());
43      }
44  
45      public StaleSourceScanner(long lastUpdatedWithinMsecs) {
46          this(lastUpdatedWithinMsecs, Collections.singleton("**/*"), Collections.emptySet());
47      }
48  
49      public StaleSourceScanner(long lastUpdatedWithinMsecs, Set<String> sourceIncludes, Set<String> sourceExcludes) {
50          this.lastUpdatedWithinMsecs = lastUpdatedWithinMsecs;
51  
52          this.sourceIncludes = sourceIncludes;
53  
54          this.sourceExcludes = sourceExcludes;
55      }
56  
57      // ----------------------------------------------------------------------
58      // SourceInclusionScanner Implementation
59      // ----------------------------------------------------------------------
60  
61      public Set<File> getIncludedSources(File sourceDir, File targetDir) throws InclusionScanException {
62          List<SourceMapping> srcMappings = getSourceMappings();
63  
64          if (srcMappings.isEmpty()) {
65              return Collections.emptySet();
66          }
67  
68          String[] potentialIncludes = scanForSources(sourceDir, sourceIncludes, sourceExcludes);
69  
70          Set<File> matchingSources = new HashSet<>();
71  
72          for (String path : potentialIncludes) {
73              File sourceFile = new File(sourceDir, path);
74  
75              staleSourceFileTesting:
76              for (SourceMapping mapping : srcMappings) {
77                  Set<File> targetFiles = mapping.getTargetFiles(targetDir, path);
78  
79                  // never include files that don't have corresponding target mappings.
80                  // the targets don't have to exist on the filesystem, but the
81                  // mappers must tell us to look for them.
82                  for (File targetFile : targetFiles) {
83                      if (!targetFile.exists()
84                              || (targetFile.lastModified() + lastUpdatedWithinMsecs < sourceFile.lastModified())) {
85                          matchingSources.add(sourceFile);
86                          break staleSourceFileTesting;
87                      }
88                  }
89              }
90          }
91  
92          return matchingSources;
93      }
94  }