1 package org.apache.maven.archiva.database.updater;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
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.consumers.ArchivaArtifactConsumer;
31 import org.apache.maven.archiva.database.ArchivaDAO;
32 import org.apache.maven.archiva.database.ArchivaDatabaseException;
33 import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.functors.UnprocessedArtifactPredicate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
44 * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUpdater"
47 public class JdoDatabaseUpdater
48 implements DatabaseUpdater
50 private Logger log = LoggerFactory.getLogger( JdoDatabaseUpdater.class );
53 * @plexus.requirement role-hint="jdo"
55 private ArchivaDAO dao;
60 private DatabaseConsumers dbConsumers;
62 private ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure();
65 throws ArchivaDatabaseException
67 updateAllUnprocessed();
71 public void updateAllUnprocessed()
72 throws ArchivaDatabaseException
74 List unprocessedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) );
76 beginConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() );
80 // Process each consumer.
81 Predicate predicate = UnprocessedArtifactPredicate.getInstance();
83 Iterator it = IteratorUtils.filteredIterator( unprocessedArtifacts.iterator(), predicate );
84 while ( it.hasNext() )
86 ArchivaArtifact artifact = (ArchivaArtifact) it.next();
87 updateUnprocessed( artifact );
92 endConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() );
96 public void updateAllProcessed()
97 throws ArchivaDatabaseException
99 List processedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) );
101 beginConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() );
105 // Process each consumer.
106 Predicate predicate = NotPredicate.getInstance( UnprocessedArtifactPredicate.getInstance() );
108 Iterator it = IteratorUtils.filteredIterator( processedArtifacts.iterator(), predicate );
109 while ( it.hasNext() )
111 ArchivaArtifact artifact = (ArchivaArtifact) it.next();
112 updateProcessed( artifact );
117 endConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() );
121 private void endConsumerLifecycle( List consumers )
123 Iterator it = consumers.iterator();
124 while ( it.hasNext() )
126 ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next();
127 consumer.completeScan();
131 private void beginConsumerLifecycle( List consumers )
133 Iterator it = consumers.iterator();
134 while ( it.hasNext() )
136 ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next();
137 consumer.beginScan();
141 public void updateUnprocessed( ArchivaArtifact artifact )
142 throws ArchivaDatabaseException
144 List consumers = dbConsumers.getSelectedUnprocessedConsumers();
146 if ( CollectionUtils.isEmpty( consumers ) )
148 log.warn( "There are no selected consumers for unprocessed artifacts." );
152 this.processArtifactClosure.setArtifact( artifact );
153 CollectionUtils.forAllDo( consumers, this.processArtifactClosure );
155 artifact.getModel().setWhenProcessed( new Date() );
156 dao.getArtifactDAO().saveArtifact( artifact );
159 public void updateProcessed( ArchivaArtifact artifact )
160 throws ArchivaDatabaseException
162 List consumers = dbConsumers.getSelectedCleanupConsumers();
164 if ( CollectionUtils.isEmpty( consumers ) )
166 log.warn( "There are no selected consumers for artifact cleanup." );
170 this.processArtifactClosure.setArtifact( artifact );
171 CollectionUtils.forAllDo( consumers, this.processArtifactClosure );