This project has retired. For details please refer to its Attic page.
ConsumerWantsFilePredicate xref
View Javadoc
1   package org.apache.archiva.consumers.functors;
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.archiva.common.utils.BaseFile;
23  import org.apache.archiva.consumers.RepositoryContentConsumer;
24  import org.apache.archiva.repository.ManagedRepository;
25  import org.apache.archiva.repository.features.IndexCreationFeature;
26  import org.apache.commons.collections4.Predicate;
27  import org.apache.commons.io.FilenameUtils;
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.tools.ant.types.selectors.SelectorUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.nio.file.Paths;
34  import java.util.List;
35  
36  /**
37   * ConsumerWantsFilePredicate
38   */
39  public class ConsumerWantsFilePredicate
40      implements Predicate<RepositoryContentConsumer>
41  {
42      private BaseFile basefile;
43  
44      private boolean isCaseSensitive = true;
45  
46      private int wantedFileCount = 0;
47  
48      private long changesSince = 0;
49  
50      private ManagedRepository managedRepository;
51  
52      private Logger logger = LoggerFactory.getLogger( getClass( ) );
53  
54      /**
55       * @deprecated use constructor with ManagedRepository
56       */
57      public ConsumerWantsFilePredicate( )
58      {
59          // no-op
60      }
61  
62      public ConsumerWantsFilePredicate( ManagedRepository managedRepository )
63      {
64          this.managedRepository = managedRepository;
65      }
66  
67      @Override
68      public boolean evaluate( RepositoryContentConsumer object )
69      {
70          boolean satisfies = false;
71  
72          RepositoryContentConsumerache/archiva/consumers/RepositoryContentConsumer.html#RepositoryContentConsumer">RepositoryContentConsumer consumer = (RepositoryContentConsumer) object;
73          if ( wantsFile( consumer, FilenameUtils.separatorsToUnix( basefile.getRelativePath( ) ) ) )
74          {
75              satisfies = true;
76  
77              // regardless of the timestamp, we record that it was wanted so it doesn't get counted as invalid
78              wantedFileCount++;
79  
80              if ( !consumer.isProcessUnmodified( ) )
81              {
82                  // Timestamp finished points to the last successful scan, not this current one.
83                  if ( basefile.lastModified( ) < changesSince )
84                  {
85                      // Skip file as no change has occurred.
86                      satisfies = false;
87                  }
88              }
89          }
90  
91          return satisfies;
92      }
93  
94      public BaseFile getBasefile( )
95      {
96          return basefile;
97      }
98  
99      public int getWantedFileCount( )
100     {
101         return wantedFileCount;
102     }
103 
104     public boolean isCaseSensitive( )
105     {
106         return isCaseSensitive;
107     }
108 
109     public void setBasefile( BaseFile basefile )
110     {
111         this.basefile = basefile;
112         this.wantedFileCount = 0;
113     }
114 
115     public void setCaseSensitive( boolean isCaseSensitive )
116     {
117         this.isCaseSensitive = isCaseSensitive;
118     }
119 
120     private boolean wantsFile( RepositoryContentConsumer consumer, String relativePath )
121     {
122         // Test excludes first.
123         List<String> excludes = consumer.getExcludes( );
124         if ( excludes != null )
125         {
126             for ( String pattern : excludes )
127             {
128                 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
129                 {
130                     // Definately does NOT WANT FILE.
131                     return false;
132                 }
133             }
134         }
135 
136         if ( managedRepository != null )
137         {
138             String indexDirectory;
139             if ( managedRepository.supportsFeature( IndexCreationFeature.class ) )
140             {
141                 IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
142                 if ( icf.getIndexPath( ) == null )
143                 {
144                     indexDirectory = ".index";
145                 }
146                 else
147                 {
148                     indexDirectory = ( icf.getIndexPath( ).getScheme( ) == null ? Paths.get( icf.getIndexPath( ).getPath( ) ) : Paths.get( icf.getIndexPath( ) ) ).toString( );
149                 }
150             }
151             else
152             {
153                 indexDirectory = ".index";
154             }
155             if ( StringUtils.isEmpty( indexDirectory ) )
156             {
157                 indexDirectory = ".index";
158             }
159             if ( StringUtils.startsWith( relativePath, indexDirectory ) )
160             {
161                 logger.debug( "ignore file {} part of the index directory {}", relativePath, indexDirectory );
162                 return false;
163             }
164         }
165 
166         // Now test includes.
167         for ( String pattern : consumer.getIncludes( ) )
168         {
169             if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
170             {
171                 // Specifically WANTS FILE.
172                 return true;
173             }
174         }
175 
176         // Not included, and Not excluded?  Default to EXCLUDE.
177         return false;
178     }
179 
180     public void setChangesSince( long changesSince )
181     {
182         this.changesSince = changesSince;
183     }
184 }