You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConsumerWantsFilePredicate.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package org.apache.archiva.consumers.functors;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.BaseFile;
  21. import org.apache.archiva.consumers.RepositoryContentConsumer;
  22. import org.apache.archiva.repository.ManagedRepository;
  23. import org.apache.archiva.repository.features.IndexCreationFeature;
  24. import org.apache.commons.collections.Predicate;
  25. import org.apache.commons.io.FilenameUtils;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.tools.ant.types.selectors.SelectorUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import java.nio.file.Paths;
  31. import java.util.List;
  32. /**
  33. * ConsumerWantsFilePredicate
  34. */
  35. public class ConsumerWantsFilePredicate
  36. implements Predicate
  37. {
  38. private BaseFile basefile;
  39. private boolean isCaseSensitive = true;
  40. private int wantedFileCount = 0;
  41. private long changesSince = 0;
  42. private ManagedRepository managedRepository;
  43. private Logger logger = LoggerFactory.getLogger( getClass() );
  44. /**
  45. * @deprecated use constructor with ManagedRepository
  46. */
  47. public ConsumerWantsFilePredicate()
  48. {
  49. // no-op
  50. }
  51. public ConsumerWantsFilePredicate( ManagedRepository managedRepository )
  52. {
  53. this.managedRepository = managedRepository;
  54. }
  55. @Override
  56. public boolean evaluate( Object object )
  57. {
  58. boolean satisfies = false;
  59. if ( object instanceof RepositoryContentConsumer )
  60. {
  61. RepositoryContentConsumer consumer = (RepositoryContentConsumer) object;
  62. if ( wantsFile( consumer, FilenameUtils.separatorsToUnix( basefile.getRelativePath() ) ) )
  63. {
  64. satisfies = true;
  65. // regardless of the timestamp, we record that it was wanted so it doesn't get counted as invalid
  66. wantedFileCount++;
  67. if ( !consumer.isProcessUnmodified() )
  68. {
  69. // Timestamp finished points to the last successful scan, not this current one.
  70. if ( basefile.lastModified() < changesSince )
  71. {
  72. // Skip file as no change has occurred.
  73. satisfies = false;
  74. }
  75. }
  76. }
  77. }
  78. return satisfies;
  79. }
  80. public BaseFile getBasefile()
  81. {
  82. return basefile;
  83. }
  84. public int getWantedFileCount()
  85. {
  86. return wantedFileCount;
  87. }
  88. public boolean isCaseSensitive()
  89. {
  90. return isCaseSensitive;
  91. }
  92. public void setBasefile( BaseFile basefile )
  93. {
  94. this.basefile = basefile;
  95. this.wantedFileCount = 0;
  96. }
  97. public void setCaseSensitive( boolean isCaseSensitive )
  98. {
  99. this.isCaseSensitive = isCaseSensitive;
  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. if ( managedRepository != null )
  117. {
  118. String indexDirectory;
  119. if (managedRepository.supportsFeature( IndexCreationFeature.class )) {
  120. IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get();
  121. if (icf.getIndexPath()==null) {
  122. indexDirectory=".index";
  123. } else
  124. {
  125. indexDirectory = ( icf.getIndexPath( ).getScheme( ) == null ? Paths.get( icf.getIndexPath( ).getPath( ) ) : Paths.get( icf.getIndexPath( ) ) ).toString( );
  126. }
  127. } else {
  128. indexDirectory = ".index";
  129. }
  130. if (StringUtils.isEmpty( indexDirectory )) {
  131. indexDirectory = ".index";
  132. }
  133. if ( StringUtils.startsWith( relativePath, indexDirectory ) )
  134. {
  135. logger.debug( "ignore file {} part of the index directory {}", relativePath, indexDirectory );
  136. return false;
  137. }
  138. }
  139. // Now test includes.
  140. for ( String pattern : consumer.getIncludes() )
  141. {
  142. if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
  143. {
  144. // Specifically WANTS FILE.
  145. return true;
  146. }
  147. }
  148. // Not included, and Not excluded? Default to EXCLUDE.
  149. return false;
  150. }
  151. public void setChangesSince( long changesSince )
  152. {
  153. this.changesSince = changesSince;
  154. }
  155. }