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 org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  /**
28   * @author jdcasey
29   */
30  public class StaleSourceScanner
31      extends AbstractSourceInclusionScanner
32  {
33      private final long lastUpdatedWithinMsecs;
34  
35      private final Set<String> sourceIncludes;
36  
37      private final Set<String> sourceExcludes;
38  
39      // ----------------------------------------------------------------------
40      //
41      // ----------------------------------------------------------------------
42  
43      public StaleSourceScanner()
44      {
45          this( 0, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
46      }
47  
48      public StaleSourceScanner( long lastUpdatedWithinMsecs )
49      {
50          this( lastUpdatedWithinMsecs, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
51      }
52  
53      public StaleSourceScanner( long lastUpdatedWithinMsecs, Set<String> sourceIncludes, Set<String> sourceExcludes )
54      {
55          this.lastUpdatedWithinMsecs = lastUpdatedWithinMsecs;
56  
57          this.sourceIncludes = sourceIncludes;
58  
59          this.sourceExcludes = sourceExcludes;
60      }
61  
62      // ----------------------------------------------------------------------
63      // SourceInclusionScanner Implementation
64      // ----------------------------------------------------------------------
65  
66      public Set<File> getIncludedSources( File sourceDir, File targetDir )
67          throws InclusionScanException
68      {
69          List<SourceMapping> srcMappings = getSourceMappings();
70  
71          if ( srcMappings.isEmpty() )
72          {
73              return Collections.emptySet();
74          }
75  
76          String[] potentialIncludes = scanForSources( sourceDir, sourceIncludes, sourceExcludes );
77  
78          Set<File> matchingSources = new HashSet<File>();
79  
80          for ( String path : potentialIncludes )
81          {
82              File sourceFile = new File( sourceDir, path );
83  
84              staleSourceFileTesting:
85              for ( SourceMapping mapping : srcMappings )
86              {
87                  Set<File> targetFiles = mapping.getTargetFiles( targetDir, path );
88  
89                  // never include files that don't have corresponding target mappings.
90                  // the targets don't have to exist on the filesystem, but the
91                  // mappers must tell us to look for them.
92                  for ( File targetFile : targetFiles )
93                  {
94                      if ( !targetFile.exists() || ( targetFile.lastModified() + lastUpdatedWithinMsecs
95                          < sourceFile.lastModified() ) )
96                      {
97                          matchingSources.add( sourceFile );
98                          break staleSourceFileTesting;
99                      }
100                 }
101             }
102         }
103 
104         return matchingSources;
105     }
106 }