This project has retired. For details please refer to its
Attic page.
AutoRemoveConsumer xref
1 package org.apache.archiva.consumers.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
60
61 private String id = "auto-remove";
62
63
64
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
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
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 }