1 | |
package org.apache.maven.archiva.database.updater; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.apache.commons.collections.CollectionUtils; |
26 | |
import org.apache.commons.collections.Predicate; |
27 | |
import org.apache.commons.collections.functors.OrPredicate; |
28 | |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; |
29 | |
import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration; |
30 | |
import org.apache.maven.archiva.consumers.functors.PermanentConsumerPredicate; |
31 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
32 | |
import org.slf4j.Logger; |
33 | |
import org.slf4j.LoggerFactory; |
34 | |
import org.springframework.beans.BeansException; |
35 | |
import org.springframework.context.ApplicationContext; |
36 | |
import org.springframework.context.ApplicationContextAware; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class DatabaseConsumers |
44 | |
implements ApplicationContextAware |
45 | |
{ |
46 | 0 | private Logger log = LoggerFactory.getLogger( DatabaseConsumers.class ); |
47 | |
|
48 | |
private ArchivaConfiguration archivaConfiguration; |
49 | |
|
50 | |
private Predicate selectedCleanupConsumers; |
51 | |
|
52 | |
private Predicate selectedUnprocessedConsumers; |
53 | |
|
54 | |
private ApplicationContext applicationContext; |
55 | |
|
56 | |
public DatabaseConsumers( ArchivaConfiguration archivaConfiguration ) |
57 | 0 | { |
58 | 0 | this.archivaConfiguration = archivaConfiguration; |
59 | |
|
60 | 0 | Predicate permanentConsumers = new PermanentConsumerPredicate(); |
61 | |
|
62 | 0 | selectedCleanupConsumers = new OrPredicate( permanentConsumers, new SelectedCleanupConsumersPredicate() ); |
63 | 0 | selectedUnprocessedConsumers = new OrPredicate( permanentConsumers, new SelectedUnprocessedConsumersPredicate() ); |
64 | 0 | } |
65 | |
|
66 | 0 | class SelectedUnprocessedConsumersPredicate |
67 | |
implements Predicate |
68 | |
{ |
69 | |
public boolean evaluate( Object object ) |
70 | |
{ |
71 | 0 | boolean satisfies = false; |
72 | |
|
73 | 0 | if ( object instanceof DatabaseUnprocessedArtifactConsumer ) |
74 | |
{ |
75 | 0 | DatabaseUnprocessedArtifactConsumer consumer = (DatabaseUnprocessedArtifactConsumer) object; |
76 | 0 | DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning(); |
77 | |
|
78 | 0 | return config.getUnprocessedConsumers().contains( consumer.getId() ); |
79 | |
} |
80 | |
|
81 | 0 | return satisfies; |
82 | |
} |
83 | |
} |
84 | |
|
85 | 0 | class SelectedCleanupConsumersPredicate |
86 | |
implements Predicate |
87 | |
{ |
88 | |
public boolean evaluate( Object object ) |
89 | |
{ |
90 | 0 | boolean satisfies = false; |
91 | |
|
92 | 0 | if ( object instanceof DatabaseCleanupConsumer ) |
93 | |
{ |
94 | 0 | DatabaseCleanupConsumer consumer = (DatabaseCleanupConsumer) object; |
95 | 0 | DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning(); |
96 | |
|
97 | 0 | return config.getCleanupConsumers().contains( consumer.getId() ); |
98 | |
} |
99 | |
|
100 | 0 | return satisfies; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
public void setApplicationContext( ApplicationContext applicationContext ) |
105 | |
throws BeansException |
106 | |
{ |
107 | 0 | this.applicationContext = applicationContext; |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@SuppressWarnings("unchecked") |
117 | |
public List<ArchivaArtifactConsumer> getSelectedUnprocessedConsumers() |
118 | |
{ |
119 | 0 | List<ArchivaArtifactConsumer> ret = new ArrayList<ArchivaArtifactConsumer>(); |
120 | 0 | ret.addAll( CollectionUtils.select( getAvailableUnprocessedConsumers(), selectedUnprocessedConsumers ) ); |
121 | 0 | return ret; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
@SuppressWarnings("unchecked") |
131 | |
public List<ArchivaArtifactConsumer> getSelectedCleanupConsumers() |
132 | |
{ |
133 | 0 | List<ArchivaArtifactConsumer> ret = new ArrayList<ArchivaArtifactConsumer>(); |
134 | 0 | ret.addAll( CollectionUtils.select( getAvailableCleanupConsumers(), selectedCleanupConsumers ) ); |
135 | 0 | return ret; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
@SuppressWarnings("unchecked") |
145 | |
public List<DatabaseUnprocessedArtifactConsumer> getAvailableUnprocessedConsumers() |
146 | |
{ |
147 | 0 | return new ArrayList<DatabaseUnprocessedArtifactConsumer>( applicationContext.getBeansOfType( DatabaseUnprocessedArtifactConsumer.class ).values() ); |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
@SuppressWarnings("unchecked") |
157 | |
public List<DatabaseCleanupConsumer> getAvailableCleanupConsumers() |
158 | |
{ |
159 | 0 | return new ArrayList<DatabaseCleanupConsumer>( applicationContext.getBeansOfType( DatabaseCleanupConsumer.class ).values() ); |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void executeCleanupConsumer( ArchivaArtifact artifact ) |
168 | |
{ |
169 | 0 | List<ArchivaArtifactConsumer> consumers = getSelectedCleanupConsumers(); |
170 | 0 | for ( ArchivaArtifactConsumer consumer : consumers ) |
171 | |
{ |
172 | 0 | consumer.beginScan(); |
173 | |
} |
174 | |
|
175 | 0 | if ( CollectionUtils.isEmpty( consumers ) ) |
176 | |
{ |
177 | 0 | log.warn( "There are no selected consumers for artifact cleanup." ); |
178 | 0 | return; |
179 | |
} |
180 | |
|
181 | 0 | ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure(); |
182 | 0 | processArtifactClosure.setArtifact( artifact ); |
183 | |
|
184 | 0 | CollectionUtils.forAllDo( consumers, processArtifactClosure ); |
185 | |
|
186 | 0 | for ( ArchivaArtifactConsumer consumer : consumers ) |
187 | |
{ |
188 | 0 | consumer.completeScan(); |
189 | |
} |
190 | 0 | } |
191 | |
} |