1 package org.apache.maven.archiva.consumers.database;
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 org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
24 import org.apache.maven.archiva.consumers.ConsumerException;
25 import org.apache.maven.archiva.consumers.DatabaseCleanupConsumer;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.model.ArchivaProjectModel;
28 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
29 import org.apache.maven.archiva.repository.RepositoryContentFactory;
30 import org.apache.maven.archiva.repository.RepositoryException;
31 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
32 import org.apache.maven.archiva.repository.layout.LayoutException;
33 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
34 import org.apache.maven.archiva.database.ProjectModelDAO;
35 import org.apache.maven.archiva.database.ArchivaDatabaseException;
37 import java.util.List;
38 import java.util.ArrayList;
42 * Consumer for removing or deleting from the database the project models fo artifacts that have been
43 * deleted/removed from the repository.
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 * <a href="mailto:oching@apache.org">Maria Odea Ching</a>
49 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseCleanupConsumer"
50 * role-hint="not-present-remove-db-project"
51 * instantiation-strategy="per-lookup"
53 public class DatabaseCleanupRemoveProjectConsumer
54 extends AbstractMonitoredConsumer
55 implements DatabaseCleanupConsumer
58 * @plexus.configuration default-value="not-present-remove-db-project"
63 * @plexus.configuration default-value="Remove project from database if not present on filesystem."
65 private String description;
68 * @plexus.requirement role-hint="jdo"
70 private ProjectModelDAO projectModelDAO;
75 private BidirectionalRepositoryLayoutFactory layoutFactory;
80 private RepositoryContentFactory repositoryFactory;
82 public void beginScan()
84 // TODO Auto-generated method stub
87 public void completeScan()
89 // TODO Auto-generated method stub
92 public List<String> getIncludedTypes()
97 public void processArchivaArtifact( ArchivaArtifact artifact )
98 throws ConsumerException
100 if ( !StringUtils.equals( "pom", artifact.getType() ) )
102 // Not a pom. Skip it.
108 ManagedRepositoryContent repositoryContent =
109 repositoryFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() );
111 File file = new File( repositoryContent.getRepoRoot(), toPath( artifact ) );
115 ArchivaProjectModel projectModel = projectModelDAO.getProjectModel(
116 artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
118 projectModelDAO.deleteProjectModel( projectModel );
121 catch ( RepositoryException re )
123 throw new ConsumerException( "Can't run database cleanup remove artifact consumer: " +
126 catch ( ArchivaDatabaseException e )
128 throw new ConsumerException( e.getMessage() );
133 public String getDescription()
138 public String getId()
143 public boolean isPermanent()
148 private String toPath( ArchivaArtifact artifact )
152 BidirectionalRepositoryLayout layout = layoutFactory.getLayout( artifact );
154 return layout.toPath( artifact );
156 catch ( LayoutException e )
158 getLogger().warn( "Unable to calculate path for artifact: " + artifact );
163 public void setProjectModelDAO( ProjectModelDAO projectModelDAO )
165 this.projectModelDAO = projectModelDAO;
168 public void setBidirectionalRepositoryLayoutFactory( BidirectionalRepositoryLayoutFactory layoutFactory )
170 this.layoutFactory = layoutFactory;
173 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
175 this.repositoryFactory = repositoryFactory;