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