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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationNames;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.archiva.redback.components.registry.Registry;
30 import org.apache.archiva.redback.components.registry.RegistryListener;
31 import org.springframework.context.annotation.Scope;
32 import org.springframework.stereotype.Service;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import javax.annotation.PostConstruct;
37 import javax.inject.Inject;
38 import java.io.File;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42
43
44
45
46
47
48 @Service( "knownRepositoryContentConsumer#auto-remove" )
49 @Scope( "prototype" )
50 public class AutoRemoveConsumer
51 extends AbstractMonitoredConsumer
52 implements KnownRepositoryContentConsumer, RegistryListener
53 {
54
55 private Logger log = LoggerFactory.getLogger( AutoRemoveConsumer.class );
56
57
58
59
60 private String id = "auto-remove";
61
62
63
64
65 private String description = "Automatically Remove File from Filesystem.";
66
67
68
69
70 @Inject
71 private ArchivaConfiguration configuration;
72
73
74
75
76 @Inject
77 private FileTypes filetypes;
78
79 private File repositoryDir;
80
81 private List<String> includes = new ArrayList<>( 0 );
82
83 @Override
84 public String getId()
85 {
86 return this.id;
87 }
88
89 @Override
90 public String getDescription()
91 {
92 return this.description;
93 }
94
95 @Override
96 public void beginScan( ManagedRepository repository, Date whenGathered )
97 throws ConsumerException
98 {
99 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
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
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 }