001package org.apache.archiva.consumers.core; 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 org.apache.archiva.admin.model.beans.ManagedRepository; 023import org.apache.archiva.configuration.ArchivaConfiguration; 024import org.apache.archiva.configuration.ConfigurationNames; 025import org.apache.archiva.configuration.FileTypes; 026import org.apache.archiva.consumers.AbstractMonitoredConsumer; 027import org.apache.archiva.consumers.ConsumerException; 028import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 029import org.apache.archiva.redback.components.registry.Registry; 030import org.apache.archiva.redback.components.registry.RegistryListener; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033import org.springframework.context.annotation.Scope; 034import org.springframework.stereotype.Service; 035 036import javax.annotation.PostConstruct; 037import javax.inject.Inject; 038import java.io.File; 039import java.util.ArrayList; 040import java.util.Date; 041import java.util.List; 042 043/** 044 * AutoRemoveConsumer 045 */ 046@Service( "knownRepositoryContentConsumer#auto-remove" ) 047@Scope( "prototype" ) 048public class AutoRemoveConsumer 049 extends AbstractMonitoredConsumer 050 implements KnownRepositoryContentConsumer, RegistryListener 051{ 052 053 private Logger log = LoggerFactory.getLogger( AutoRemoveConsumer.class ); 054 055 /** 056 * default-value="auto-remove" 057 */ 058 private String id = "auto-remove"; 059 060 /** 061 * default-value="Automatically Remove File from Filesystem." 062 */ 063 private String description = "Automatically Remove File from Filesystem."; 064 065 /** 066 * 067 */ 068 @Inject 069 private ArchivaConfiguration configuration; 070 071 /** 072 * 073 */ 074 @Inject 075 private FileTypes filetypes; 076 077 private File repositoryDir; 078 079 private List<String> includes = new ArrayList<>( 0 ); 080 081 @Override 082 public String getId( ) 083 { 084 return this.id; 085 } 086 087 @Override 088 public String getDescription( ) 089 { 090 return this.description; 091 } 092 093 @Override 094 public void beginScan( ManagedRepository repository, Date whenGathered ) 095 throws ConsumerException 096 { 097 this.repositoryDir = new File( repository.getLocation( ) ); 098 } 099 100 @Override 101 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) 102 throws ConsumerException 103 { 104 beginScan( repository, whenGathered ); 105 } 106 107 @Override 108 public void completeScan( ) 109 { 110 /* do nothing */ 111 } 112 113 @Override 114 public void completeScan( boolean executeOnEntireRepo ) 115 { 116 completeScan( ); 117 } 118 119 @Override 120 public List<String> getExcludes( ) 121 { 122 return null; 123 } 124 125 @Override 126 public List<String> getIncludes( ) 127 { 128 return includes; 129 } 130 131 @Override 132 public void processFile( String path ) 133 throws ConsumerException 134 { 135 File file = new File( this.repositoryDir, path ); 136 if ( file.exists( ) ) 137 { 138 log.info( "(Auto) Removing File: {}", file.getAbsolutePath( ) ); 139 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) ); 140 file.delete( ); 141 } 142 } 143 144 @Override 145 public void processFile( String path, boolean executeOnEntireRepo ) 146 throws ConsumerException 147 { 148 processFile( path ); 149 } 150 151 @Override 152 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) 153 { 154 if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) 155 { 156 initIncludes( ); 157 } 158 } 159 160 @Override 161 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) 162 { 163 /* do nothing */ 164 } 165 166 private void initIncludes( ) 167 { 168 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.AUTO_REMOVE ) ); 169 } 170 171 @PostConstruct 172 public void initialize( ) 173 { 174 configuration.addChangeListener( this ); 175 176 initIncludes( ); 177 } 178}