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