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 java.util.List;
23  
24  import org.apache.archiva.admin.model.beans.ManagedRepository;
25  import org.apache.commons.collections.Predicate;
26  import org.apache.commons.io.FilenameUtils;
27  import org.apache.archiva.common.utils.BaseFile;
28  import org.apache.archiva.consumers.RepositoryContentConsumer;
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.tools.ant.types.selectors.SelectorUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * ConsumerWantsFilePredicate
36   */
37  public class ConsumerWantsFilePredicate
38      implements Predicate
39  {
40      private BaseFile basefile;
41  
42      private boolean isCaseSensitive = true;
43  
44      private int wantedFileCount = 0;
45  
46      private long changesSince = 0;
47  
48      private ManagedRepository managedRepository;
49  
50      private Logger logger = LoggerFactory.getLogger( getClass() );
51  
52      /**
53       * @deprecated use constructor with ManagedRepository
54       */
55      public ConsumerWantsFilePredicate()
56      {
57          // no-op
58      }
59  
60      public ConsumerWantsFilePredicate( ManagedRepository managedRepository )
61      {
62          this.managedRepository = managedRepository;
63      }
64  
65      @Override
66      public boolean evaluate( Object object )
67      {
68          boolean satisfies = false;
69  
70          if ( object instanceof RepositoryContentConsumer )
71          {
72              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  
92          return satisfies;
93      }
94  
95      public BaseFile getBasefile()
96      {
97          return basefile;
98      }
99  
100     public int getWantedFileCount()
101     {
102         return wantedFileCount;
103     }
104 
105     public boolean isCaseSensitive()
106     {
107         return isCaseSensitive;
108     }
109 
110     public void setBasefile( BaseFile basefile )
111     {
112         this.basefile = basefile;
113         this.wantedFileCount = 0;
114     }
115 
116     public void setCaseSensitive( boolean isCaseSensitive )
117     {
118         this.isCaseSensitive = isCaseSensitive;
119     }
120 
121     private boolean wantsFile( RepositoryContentConsumer consumer, String relativePath )
122     {
123         // Test excludes first.
124         List<String> excludes = consumer.getExcludes();
125         if ( excludes != null )
126         {
127             for ( String pattern : excludes )
128             {
129                 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
130                 {
131                     // Definitely does NOT WANT FILE.
132                     return false;
133                 }
134             }
135         }
136 
137         if ( managedRepository != null )
138         {
139             String indexDirectory = managedRepository.getIndexDirectory();
140             if ( StringUtils.startsWith( relativePath, indexDirectory ) )
141             {
142                 logger.debug( "ignore file {} part of the index directory {}", relativePath, indexDirectory );
143                 return false;
144             }
145         }
146 
147         // Now test includes.
148         for ( String pattern : consumer.getIncludes() )
149         {
150             if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
151             {
152                 // Specifically WANTS FILE.
153                 return true;
154             }
155         }
156 
157         // Not included, and Not excluded?  Default to EXCLUDE.
158         return false;
159     }
160 
161     public void setChangesSince( long changesSince )
162     {
163         this.changesSince = changesSince;
164     }
165 }