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.model.ArchivaArtifact;
26 import org.apache.maven.archiva.model.ArchivaProjectModel;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.RepositoryException;
30 import org.apache.maven.archiva.database.ProjectModelDAO;
31 import org.apache.maven.archiva.database.ArchivaDatabaseException;
32 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
33 import org.codehaus.plexus.cache.Cache;
35 import java.util.List;
39 * Consumer for removing or deleting from the database the project models fo artifacts that have been
40 * deleted/removed from the repository.
42 * <a href="mailto:oching@apache.org">Maria Odea Ching</a>
45 * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer"
46 * role-hint="not-present-remove-db-project"
47 * instantiation-strategy="per-lookup"
49 public class DatabaseCleanupRemoveProjectConsumer
50 extends AbstractMonitoredConsumer
51 implements DatabaseCleanupConsumer
54 * @plexus.configuration default-value="not-present-remove-db-project"
59 * @plexus.configuration default-value="Remove project from database if not present on filesystem."
61 private String description;
64 * @plexus.requirement role-hint="jdo"
66 private ProjectModelDAO projectModelDAO;
71 private RepositoryContentFactory repositoryFactory;
74 * @plexus.requirement role-hint="effective-project-cache"
76 private Cache effectiveProjectCache;
78 public void beginScan()
80 // TODO Auto-generated method stub
83 public void completeScan()
85 // TODO Auto-generated method stub
88 public List<String> getIncludedTypes()
93 public void processArchivaArtifact( ArchivaArtifact artifact )
94 throws ConsumerException
96 if ( !StringUtils.equals( "pom", artifact.getType() ) )
98 // Not a pom. Skip it.
104 ManagedRepositoryContent repositoryContent =
105 repositoryFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() );
107 File file = new File( repositoryContent.getRepoRoot(), repositoryContent.toPath( artifact ) );
109 if ( !file.exists() )
111 ArchivaProjectModel projectModel =
112 projectModelDAO.getProjectModel( artifact.getGroupId(), artifact.getArtifactId(),
113 artifact.getVersion() );
115 projectModelDAO.deleteProjectModel( projectModel );
117 // Force removal of project model from effective cache
118 String projectKey = toProjectKey( projectModel );
119 synchronized ( effectiveProjectCache )
121 if ( effectiveProjectCache.hasKey( projectKey ) )
123 effectiveProjectCache.remove( projectKey );
128 catch ( RepositoryException re )
130 throw new ConsumerException( "Can't run database cleanup remove artifact consumer: " + re.getMessage() );
132 catch ( ArchivaDatabaseException e )
134 throw new ConsumerException( e.getMessage() );
139 public String getDescription()
144 public String getId()
149 public boolean isPermanent()
154 public void setProjectModelDAO( ProjectModelDAO projectModelDAO )
156 this.projectModelDAO = projectModelDAO;
159 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
161 this.repositoryFactory = repositoryFactory;
164 public void setEffectiveProjectCache( Cache effectiveProjectCache )
166 this.effectiveProjectCache = effectiveProjectCache;
169 private String toProjectKey( ArchivaProjectModel project )
171 StringBuilder key = new StringBuilder();
173 key.append( project.getGroupId() ).append( ":" );
174 key.append( project.getArtifactId() ).append( ":" );
175 key.append( project.getVersion() );
177 return key.toString();