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