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