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.

AutoRemoveConsumer.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package org.apache.archiva.consumers.core;
  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.configuration.provider.ArchivaConfiguration;
  21. import org.apache.archiva.configuration.model.ConfigurationNames;
  22. import org.apache.archiva.configuration.provider.FileTypes;
  23. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  24. import org.apache.archiva.consumers.ConsumerException;
  25. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  26. import org.apache.archiva.components.registry.Registry;
  27. import org.apache.archiva.components.registry.RegistryListener;
  28. import org.apache.archiva.repository.ManagedRepository;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.context.annotation.Scope;
  32. import org.springframework.stereotype.Service;
  33. import javax.annotation.PostConstruct;
  34. import javax.inject.Inject;
  35. import java.io.IOException;
  36. import java.nio.file.Files;
  37. import java.nio.file.Path;
  38. import java.nio.file.Paths;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41. import java.util.List;
  42. /**
  43. * AutoRemoveConsumer
  44. */
  45. @Service( "knownRepositoryContentConsumer#auto-remove" )
  46. @Scope( "prototype" )
  47. public class AutoRemoveConsumer
  48. extends AbstractMonitoredConsumer
  49. implements KnownRepositoryContentConsumer, RegistryListener
  50. {
  51. private Logger log = LoggerFactory.getLogger( AutoRemoveConsumer.class );
  52. /**
  53. * default-value="auto-remove"
  54. */
  55. private String id = "auto-remove";
  56. /**
  57. * default-value="Automatically Remove File from Filesystem."
  58. */
  59. private String description = "Automatically Remove File from Filesystem.";
  60. /**
  61. *
  62. */
  63. @Inject
  64. private ArchivaConfiguration configuration;
  65. /**
  66. *
  67. */
  68. @Inject
  69. private FileTypes filetypes;
  70. private Path repositoryDir;
  71. private List<String> includes = new ArrayList<>( 0 );
  72. @Override
  73. public String getId( )
  74. {
  75. return this.id;
  76. }
  77. @Override
  78. public String getDescription( )
  79. {
  80. return this.description;
  81. }
  82. @Override
  83. public void beginScan( ManagedRepository repository, Date whenGathered )
  84. throws ConsumerException
  85. {
  86. this.repositoryDir = Paths.get( repository.getLocation( ) );
  87. }
  88. @Override
  89. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  90. throws ConsumerException
  91. {
  92. beginScan( repository, whenGathered );
  93. }
  94. @Override
  95. public void completeScan( )
  96. {
  97. /* do nothing */
  98. }
  99. @Override
  100. public void completeScan( boolean executeOnEntireRepo )
  101. {
  102. completeScan( );
  103. }
  104. @Override
  105. public List<String> getExcludes( )
  106. {
  107. return null;
  108. }
  109. @Override
  110. public List<String> getIncludes( )
  111. {
  112. return includes;
  113. }
  114. @Override
  115. public void processFile( String path )
  116. throws ConsumerException
  117. {
  118. Path file = this.repositoryDir.resolve(path );
  119. if ( Files.exists(file) )
  120. {
  121. log.info( "(Auto) Removing File: {}", file.toAbsolutePath( ) );
  122. triggerConsumerInfo( "(Auto) Removing File: " + file.toAbsolutePath( ) );
  123. try
  124. {
  125. Files.deleteIfExists( file );
  126. }
  127. catch ( IOException e )
  128. {
  129. log.error("Could not delete file {}: {}", file, e.getMessage(), e);
  130. throw new ConsumerException( "Could not delete file "+file );
  131. }
  132. }
  133. }
  134. @Override
  135. public void processFile( String path, boolean executeOnEntireRepo )
  136. throws ConsumerException
  137. {
  138. processFile( path );
  139. }
  140. @Override
  141. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  142. {
  143. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  144. {
  145. initIncludes( );
  146. }
  147. }
  148. @Override
  149. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  150. {
  151. /* do nothing */
  152. }
  153. private void initIncludes( )
  154. {
  155. includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.AUTO_REMOVE ) );
  156. }
  157. @PostConstruct
  158. public void initialize( )
  159. {
  160. configuration.addChangeListener( this );
  161. initIncludes( );
  162. }
  163. }