View Javadoc
1   package org.apache.maven.shared.io.scan;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.maven.shared.io.scan.mapping.SourceMapping;
29  
30  /**
31   * @author jdcasey
32   * @version $Id: StaleResourceScanner.java 1719477 2015-12-11 17:33:58Z khmarbaise $
33   */
34  public class StaleResourceScanner
35      extends AbstractResourceInclusionScanner
36  {
37      private final long lastUpdatedWithinMsecs;
38  
39      private final Set<String> sourceIncludes;
40  
41      private final Set<String> sourceExcludes;
42  
43      // ----------------------------------------------------------------------
44      //
45      // ----------------------------------------------------------------------
46  
47      /**
48       * Create instance with defaults.
49       */
50      public StaleResourceScanner()
51      {
52          this( 0, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
53      }
54  
55      /**
56       * @param lastUpdatedWithinMsecs last update within milli seconds.
57       */
58      public StaleResourceScanner( long lastUpdatedWithinMsecs )
59      {
60          this( lastUpdatedWithinMsecs, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
61      }
62  
63      /**
64       * @param lastUpdatedWithinMsecs last update within milli seconds.
65       * @param sourceIncludes source includes.
66       * @param sourceExcludes source excludes.
67       */
68      public StaleResourceScanner( long lastUpdatedWithinMsecs, Set<String> sourceIncludes, Set<String> sourceExcludes )
69      {
70          this.lastUpdatedWithinMsecs = lastUpdatedWithinMsecs;
71  
72          this.sourceIncludes = sourceIncludes;
73  
74          this.sourceExcludes = sourceExcludes;
75      }
76  
77      // ----------------------------------------------------------------------
78      // SourceInclusionScanner Implementation
79      // ----------------------------------------------------------------------
80  
81      /** {@inheritDoc} */
82      public Set<File> getIncludedSources( File sourceDir, File targetDir )
83          throws InclusionScanException
84      {
85          List<SourceMapping> srcMappings = getSourceMappings();
86  
87          if ( srcMappings.isEmpty() )
88          {
89              return Collections.<File>emptySet();
90          }
91  
92          String[] potentialIncludes = scanForSources( sourceDir, sourceIncludes, sourceExcludes );
93  
94          Set<File> matchingSources = new HashSet<File>();
95  
96          for ( int i = 0; i < potentialIncludes.length; i++ )
97          {
98              String path = potentialIncludes[i];
99  
100             File sourceFile = new File( sourceDir, path );
101 
102             staleSourceFileTesting: for ( SourceMapping mapping : srcMappings )
103             {
104                 Set<File> targetFiles = mapping.getTargetFiles( targetDir, path );
105 
106                 // never include files that don't have corresponding target mappings.
107                 // the targets don't have to exist on the filesystem, but the
108                 // mappers must tell us to look for them.
109                 for ( File targetFile : targetFiles )
110                 {
111                     if ( !targetFile.exists()
112                         || ( targetFile.lastModified() + lastUpdatedWithinMsecs < sourceFile.lastModified() ) )
113                     {
114                         matchingSources.add( sourceFile );
115                         break staleSourceFileTesting;
116                     }
117                 }
118             }
119         }
120 
121         return matchingSources;
122     }
123 }