This project has retired. For details please refer to its Attic page.
Source code
001package 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
022import java.util.List;
023
024import org.apache.archiva.admin.model.beans.ManagedRepository;
025import org.apache.commons.collections.Predicate;
026import org.apache.commons.io.FilenameUtils;
027import org.apache.archiva.common.utils.BaseFile;
028import org.apache.archiva.consumers.RepositoryContentConsumer;
029import org.apache.commons.lang.StringUtils;
030import org.apache.tools.ant.types.selectors.SelectorUtils;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * ConsumerWantsFilePredicate
036 */
037public class ConsumerWantsFilePredicate
038    implements Predicate
039{
040    private BaseFile basefile;
041
042    private boolean isCaseSensitive = true;
043
044    private int wantedFileCount = 0;
045
046    private long changesSince = 0;
047
048    private ManagedRepository managedRepository;
049
050    private Logger logger = LoggerFactory.getLogger( getClass() );
051
052    /**
053     * @deprecated use constructor with ManagedRepository
054     */
055    public ConsumerWantsFilePredicate()
056    {
057        // no-op
058    }
059
060    public ConsumerWantsFilePredicate( ManagedRepository managedRepository )
061    {
062        this.managedRepository = managedRepository;
063    }
064
065    @Override
066    public boolean evaluate( Object object )
067    {
068        boolean satisfies = false;
069
070        if ( object instanceof RepositoryContentConsumer )
071        {
072            RepositoryContentConsumer consumer = (RepositoryContentConsumer) object;
073            if ( wantsFile( consumer, FilenameUtils.separatorsToUnix( basefile.getRelativePath() ) ) )
074            {
075                satisfies = true;
076
077                // regardless of the timestamp, we record that it was wanted so it doesn't get counted as invalid
078                wantedFileCount++;
079
080                if ( !consumer.isProcessUnmodified() )
081                {
082                    // Timestamp finished points to the last successful scan, not this current one.
083                    if ( basefile.lastModified() < changesSince )
084                    {
085                        // Skip file as no change has occurred.
086                        satisfies = false;
087                    }
088                }
089            }
090        }
091
092        return satisfies;
093    }
094
095    public BaseFile getBasefile()
096    {
097        return basefile;
098    }
099
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}