This project has retired. For details please refer to its Attic page.
AutoRemoveConsumer xref
View Javadoc
1   package org.apache.archiva.consumers.core;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.configuration.ArchivaConfiguration;
23  import org.apache.archiva.configuration.ConfigurationNames;
24  import org.apache.archiva.configuration.FileTypes;
25  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26  import org.apache.archiva.consumers.ConsumerException;
27  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28  import org.apache.archiva.components.registry.Registry;
29  import org.apache.archiva.components.registry.RegistryListener;
30  import org.apache.archiva.repository.ManagedRepository;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.context.annotation.Scope;
34  import org.springframework.stereotype.Service;
35  
36  import javax.annotation.PostConstruct;
37  import javax.inject.Inject;
38  import java.io.IOException;
39  import java.nio.file.Files;
40  import java.nio.file.Path;
41  import java.nio.file.Paths;
42  import java.util.ArrayList;
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   * AutoRemoveConsumer
48   */
49  @Service( "knownRepositoryContentConsumer#auto-remove" )
50  @Scope( "prototype" )
51  public class AutoRemoveConsumer
52      extends AbstractMonitoredConsumer
53      implements KnownRepositoryContentConsumer, RegistryListener
54  {
55  
56      private Logger log = LoggerFactory.getLogger( AutoRemoveConsumer.class );
57  
58      /**
59       * default-value="auto-remove"
60       */
61      private String id = "auto-remove";
62  
63      /**
64       * default-value="Automatically Remove File from Filesystem."
65       */
66      private String description = "Automatically Remove File from Filesystem.";
67  
68      /**
69       *
70       */
71      @Inject
72      private ArchivaConfiguration configuration;
73  
74      /**
75       *
76       */
77      @Inject
78      private FileTypes filetypes;
79  
80      private Path repositoryDir;
81  
82      private List<String> includes = new ArrayList<>( 0 );
83  
84      @Override
85      public String getId( )
86      {
87          return this.id;
88      }
89  
90      @Override
91      public String getDescription( )
92      {
93          return this.description;
94      }
95  
96      @Override
97      public void beginScan( ManagedRepository repository, Date whenGathered )
98          throws ConsumerException
99      {
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 }