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 4.4KB

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