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.Date; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.apache.commons.collections.CollectionUtils; |
27 | |
import org.apache.commons.collections.IteratorUtils; |
28 | |
import org.apache.commons.collections.Predicate; |
29 | |
import org.apache.commons.collections.functors.NotPredicate; |
30 | |
import org.apache.maven.archiva.database.ArchivaDAO; |
31 | |
import org.apache.maven.archiva.database.ArchivaDatabaseException; |
32 | |
import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint; |
33 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
34 | |
import org.apache.maven.archiva.model.functors.UnprocessedArtifactPredicate; |
35 | |
import org.slf4j.Logger; |
36 | |
import org.slf4j.LoggerFactory; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class JdoDatabaseUpdater |
47 | |
implements DatabaseUpdater |
48 | |
{ |
49 | 0 | private Logger log = LoggerFactory.getLogger( JdoDatabaseUpdater.class ); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private ArchivaDAO dao; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private DatabaseConsumers dbConsumers; |
60 | |
|
61 | 0 | private ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure(); |
62 | |
|
63 | |
public void update() |
64 | |
throws ArchivaDatabaseException |
65 | |
{ |
66 | 0 | updateAllUnprocessed(); |
67 | 0 | updateAllProcessed(); |
68 | 0 | } |
69 | |
|
70 | |
@SuppressWarnings("unchecked") |
71 | |
public void updateAllUnprocessed() |
72 | |
throws ArchivaDatabaseException |
73 | |
{ |
74 | 0 | List<ArchivaArtifact> unprocessedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) ); |
75 | |
|
76 | 0 | beginConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() ); |
77 | |
|
78 | |
try |
79 | |
{ |
80 | |
|
81 | 0 | Predicate predicate = UnprocessedArtifactPredicate.getInstance(); |
82 | |
|
83 | 0 | Iterator<ArchivaArtifact> it = IteratorUtils.filteredIterator( unprocessedArtifacts.iterator(), predicate ); |
84 | 0 | while ( it.hasNext() ) |
85 | |
{ |
86 | 0 | ArchivaArtifact artifact = it.next(); |
87 | 0 | updateUnprocessed( artifact ); |
88 | 0 | } |
89 | |
} |
90 | |
finally |
91 | |
{ |
92 | 0 | endConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() ); |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
@SuppressWarnings("unchecked") |
97 | |
public void updateAllProcessed() |
98 | |
throws ArchivaDatabaseException |
99 | |
{ |
100 | 0 | List<ArchivaArtifact> processedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) ); |
101 | |
|
102 | 0 | beginConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() ); |
103 | |
|
104 | |
try |
105 | |
{ |
106 | |
|
107 | 0 | Predicate predicate = NotPredicate.getInstance( UnprocessedArtifactPredicate.getInstance() ); |
108 | |
|
109 | 0 | Iterator<ArchivaArtifact> it = IteratorUtils.filteredIterator( processedArtifacts.iterator(), predicate ); |
110 | 0 | while ( it.hasNext() ) |
111 | |
{ |
112 | 0 | ArchivaArtifact artifact = it.next(); |
113 | 0 | updateProcessed( artifact ); |
114 | 0 | } |
115 | |
} |
116 | |
finally |
117 | |
{ |
118 | 0 | endConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() ); |
119 | 0 | } |
120 | 0 | } |
121 | |
|
122 | |
private void endConsumerLifecycle( List<ArchivaArtifactConsumer> consumers ) |
123 | |
{ |
124 | 0 | for ( ArchivaArtifactConsumer consumer : consumers ) |
125 | |
{ |
126 | 0 | consumer.completeScan(); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
private void beginConsumerLifecycle( List<ArchivaArtifactConsumer> consumers ) |
131 | |
{ |
132 | 0 | for ( ArchivaArtifactConsumer consumer : consumers ) |
133 | |
{ |
134 | 0 | consumer.beginScan(); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
public void updateUnprocessed( ArchivaArtifact artifact ) |
139 | |
throws ArchivaDatabaseException |
140 | |
{ |
141 | 0 | List<ArchivaArtifactConsumer> consumers = dbConsumers.getSelectedUnprocessedConsumers(); |
142 | |
|
143 | 0 | if ( CollectionUtils.isEmpty( consumers ) ) |
144 | |
{ |
145 | 0 | log.warn( "There are no selected consumers for unprocessed artifacts." ); |
146 | 0 | return; |
147 | |
} |
148 | |
|
149 | 0 | this.processArtifactClosure.setArtifact( artifact ); |
150 | 0 | CollectionUtils.forAllDo( consumers, this.processArtifactClosure ); |
151 | |
|
152 | 0 | artifact.getModel().setWhenProcessed( new Date() ); |
153 | 0 | dao.getArtifactDAO().saveArtifact( artifact ); |
154 | 0 | } |
155 | |
|
156 | |
public void updateProcessed( ArchivaArtifact artifact ) |
157 | |
throws ArchivaDatabaseException |
158 | |
{ |
159 | 0 | List<ArchivaArtifactConsumer> consumers = dbConsumers.getSelectedCleanupConsumers(); |
160 | |
|
161 | 0 | if ( CollectionUtils.isEmpty( consumers ) ) |
162 | |
{ |
163 | 0 | log.warn( "There are no selected consumers for artifact cleanup." ); |
164 | 0 | return; |
165 | |
} |
166 | |
|
167 | 0 | this.processArtifactClosure.setArtifact( artifact ); |
168 | 0 | CollectionUtils.forAllDo( consumers, this.processArtifactClosure ); |
169 | 0 | } |
170 | |
} |