001    package org.apache.archiva.consumers.functors;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.List;
023    
024    import org.apache.commons.collections.Predicate;
025    import org.apache.commons.io.FilenameUtils;
026    import org.apache.archiva.common.utils.BaseFile;
027    import org.apache.archiva.consumers.RepositoryContentConsumer;
028    import org.apache.tools.ant.types.selectors.SelectorUtils;
029    
030    /**
031     * ConsumerWantsFilePredicate 
032     *
033     *
034     */
035    public class ConsumerWantsFilePredicate
036        implements Predicate
037    {
038        private BaseFile basefile;
039    
040        private boolean isCaseSensitive = true;
041    
042        private int wantedFileCount = 0;
043    
044        private long changesSince = 0;
045    
046        public boolean evaluate( Object object )
047        {
048            boolean satisfies = false;
049    
050            if ( object instanceof RepositoryContentConsumer )
051            {
052                RepositoryContentConsumer consumer = (RepositoryContentConsumer) object;
053                if ( wantsFile( consumer, FilenameUtils.separatorsToUnix( basefile.getRelativePath() ) ) )
054                {
055                    satisfies = true;
056                    
057                    // regardless of the timestamp, we record that it was wanted so it doesn't get counted as invalid
058                    wantedFileCount++;
059    
060                    if ( !consumer.isProcessUnmodified() )
061                    {
062                        // Timestamp finished points to the last successful scan, not this current one.
063                        if ( basefile.lastModified() < changesSince )
064                        {
065                            // Skip file as no change has occurred.
066                            satisfies = false;
067                        }
068                    }
069                }
070            }
071    
072            return satisfies;
073        }
074    
075        public BaseFile getBasefile()
076        {
077            return basefile;
078        }
079    
080        public int getWantedFileCount()
081        {
082            return wantedFileCount;
083        }
084    
085        public boolean isCaseSensitive()
086        {
087            return isCaseSensitive;
088        }
089    
090        public void setBasefile( BaseFile basefile )
091        {
092            this.basefile = basefile;
093            this.wantedFileCount = 0;
094        }
095    
096        public void setCaseSensitive( boolean isCaseSensitive )
097        {
098            this.isCaseSensitive = isCaseSensitive;
099        }
100    
101        private boolean wantsFile( RepositoryContentConsumer consumer, String relativePath )
102        {
103            // Test excludes first.
104            List<String> excludes = consumer.getExcludes();
105            if ( excludes != null )
106            {
107                for ( String pattern : excludes )
108                {
109                    if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
110                    {
111                        // Definately does NOT WANT FILE.
112                        return false;
113                    }
114                }
115            }
116    
117            // Now test includes.
118            for ( String pattern : consumer.getIncludes() )
119            {
120                if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
121                {
122                    // Specifically WANTS FILE.
123                    return true;
124                }
125            }
126    
127            // Not included, and Not excluded?  Default to EXCLUDE.
128            return false;
129        }
130    
131        public void setChangesSince( long changesSince )
132        {
133            this.changesSince = changesSince;
134        }
135    }