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
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.List;
40
41
42
43
44
45
46 @Service( "knownRepositoryContentConsumer#auto-remove" )
47 @Scope( "prototype" )
48 public class AutoRemoveConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer, RegistryListener
51 {
52
53
54
55 private String id = "auto-remove";
56
57
58
59
60 private String description = "Automatically Remove File from Filesystem.";
61
62
63
64
65 @Inject
66 private ArchivaConfiguration configuration;
67
68
69
70
71 @Inject
72 private FileTypes filetypes;
73
74 private File repositoryDir;
75
76 private List<String> includes = new ArrayList<String>( 0 );
77
78 public String getId()
79 {
80 return this.id;
81 }
82
83 public String getDescription()
84 {
85 return this.description;
86 }
87
88 public boolean isPermanent()
89 {
90 return false;
91 }
92
93 public void beginScan( ManagedRepository repository, Date whenGathered )
94 throws ConsumerException
95 {
96 this.repositoryDir = new File( repository.getLocation() );
97 }
98
99 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
100 throws ConsumerException
101 {
102 beginScan( repository, whenGathered );
103 }
104
105 public void completeScan()
106 {
107
108 }
109
110 public void completeScan( boolean executeOnEntireRepo )
111 {
112 completeScan();
113 }
114
115 public List<String> getExcludes()
116 {
117 return null;
118 }
119
120 public List<String> getIncludes()
121 {
122 return includes;
123 }
124
125 public void processFile( String path )
126 throws ConsumerException
127 {
128 File file = new File( this.repositoryDir, path );
129 if ( file.exists() )
130 {
131 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
132 file.delete();
133 }
134 }
135
136 public void processFile( String path, boolean executeOnEntireRepo )
137 throws ConsumerException
138 {
139 processFile( path );
140 }
141
142 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
143 {
144 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
145 {
146 initIncludes();
147 }
148 }
149
150 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
151 {
152
153 }
154
155 private void initIncludes()
156 {
157 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.AUTO_REMOVE ) );
158 }
159
160 @PostConstruct
161 public void initialize()
162 {
163 configuration.addChangeListener( this );
164
165 initIncludes();
166 }
167 }