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
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.common.utils.VersionUtil;
28 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.maven.archiva.consumers.ConsumerException;
30 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
31 import org.apache.maven.archiva.database.ArchivaDAO;
32 import org.apache.maven.archiva.database.ArchivaDatabaseException;
33 import org.apache.maven.archiva.database.ObjectNotFoundException;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.ArchivaProjectModel;
36 import org.apache.maven.archiva.model.Keys;
37 import org.apache.maven.archiva.model.RepositoryProblem;
38 import org.apache.maven.archiva.reporting.artifact.CorruptArtifactReport;
39 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
40 import org.apache.maven.archiva.repository.RepositoryContentFactory;
41 import org.apache.maven.archiva.repository.RepositoryException;
42 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
43 import org.apache.maven.archiva.repository.project.ProjectModelException;
44 import org.apache.maven.archiva.repository.project.ProjectModelFilter;
45 import org.apache.maven.archiva.repository.project.ProjectModelReader;
46 import org.apache.maven.archiva.repository.project.filters.EffectiveProjectModelFilter;
47 import org.codehaus.plexus.cache.Cache;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * ProjectModelToDatabaseConsumer
54 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
56 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer"
57 * role-hint="update-db-project"
58 * instantiation-strategy="per-lookup"
60 public class ProjectModelToDatabaseConsumer
61 extends AbstractMonitoredConsumer
62 implements DatabaseUnprocessedArtifactConsumer
64 private Logger log = LoggerFactory.getLogger( ProjectModelToDatabaseConsumer.class );
67 * @plexus.configuration default-value="update-db-project"
72 * @plexus.configuration default-value="Update database with project model information."
74 private String description;
77 * @plexus.requirement role-hint="jdo"
79 private ArchivaDAO dao;
84 private RepositoryContentFactory repositoryFactory;
87 * @plexus.requirement role-hint="model400"
89 private ProjectModelReader project400Reader;
92 * @plexus.requirement role-hint="model300"
94 private ProjectModelReader project300Reader;
97 * @plexus.requirement role-hint="expression"
99 private ProjectModelFilter expressionModelFilter;
102 * @plexus.requirement role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
103 * role-hint="effective"
105 private EffectiveProjectModelFilter effectiveModelFilter;
107 private List<String> includes;
110 * @plexus.requirement role-hint="effective-project-cache"
112 private Cache effectiveProjectCache;
114 public ProjectModelToDatabaseConsumer()
116 includes = new ArrayList<String>();
117 includes.add( "pom" );
120 public void beginScan()
122 /* nothing to do here */
125 public void completeScan()
127 /* nothing to do here */
130 public List<String> getIncludedTypes()
135 public void processArchivaArtifact( ArchivaArtifact artifact )
136 throws ConsumerException
138 if ( !StringUtils.equals( "pom", artifact.getType() ) )
140 // Not a pom. Skip it.
144 ArchivaProjectModel model = null;
146 // remove old project model if it already exists in the database
148 getProjectModelFromDatabase( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ) ) != null )
150 removeOldProjectModel( model );
154 ManagedRepositoryContent repo = getRepository( artifact );
155 File artifactFile = repo.toFile( artifact );
156 ProjectModelReader reader = project400Reader;
158 if ( repo instanceof ManagedLegacyRepositoryContent )
160 reader = project300Reader;
165 model = reader.read( artifactFile );
167 model.setOrigin( "filesystem" );
169 // The version should be updated to the artifact/filename version if it is a unique snapshot
170 if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
172 model.setVersion( artifact.getVersion() );
176 model = expressionModelFilter.filter( model );
178 // Resolve the project model
179 model = effectiveModelFilter.filter( model );
181 if ( isValidModel( model, repo, artifact ) )
183 log.debug( "Adding project model to database - " + Keys.toKey( model ) );
184 dao.getProjectModelDAO().saveProjectModel( model );
188 log.warn( "Invalid or corrupt pom. Project model not added to database - " + Keys.toKey( model ) );
192 catch ( ProjectModelException e )
194 log.warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
196 addProblem( artifact, "Unable to read project model " + artifactFile + " : " + e.getMessage() );
198 catch ( ArchivaDatabaseException e )
200 log.warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(), e );
202 catch ( Throwable t )
204 // Catch the other errors in the process to allow the rest of the process to complete.
205 log.error( "Unable to process model " + artifactFile + " due to : " + t.getClass().getName() + " : " +
210 private ArchivaProjectModel getProjectModelFromDatabase( String groupId, String artifactId, String version )
214 ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
217 catch ( ObjectNotFoundException e )
221 catch ( ArchivaDatabaseException e )
227 private ManagedRepositoryContent getRepository( ArchivaArtifact artifact )
228 throws ConsumerException
230 String repoId = artifact.getModel().getRepositoryId();
233 return repositoryFactory.getManagedRepositoryContent( repoId );
235 catch ( RepositoryException e )
237 throw new ConsumerException( "Unable to process project model: " + e.getMessage(), e );
241 public String getDescription()
246 public String getId()
251 public boolean isPermanent()
253 // Tells the configuration that this consumer cannot be disabled.
257 private boolean isValidModel( ArchivaProjectModel model, ManagedRepositoryContent repo, ArchivaArtifact artifact )
258 throws ConsumerException
260 File artifactFile = repo.toFile( artifact );
262 if ( !artifact.getArtifactId().equalsIgnoreCase( model.getArtifactId() ) )
264 StringBuffer emsg = new StringBuffer();
265 emsg.append( "File " ).append( artifactFile.getName() );
266 emsg.append( " has an invalid project model [" );
267 appendModel( emsg, model );
268 emsg.append( "]: The model artifactId [" ).append( model.getArtifactId() );
269 emsg.append( "] does not match the artifactId portion of the filename: " ).append( artifact.getArtifactId() );
271 log.warn( emsg.toString() );
272 addProblem( artifact, emsg.toString() );
277 if ( !artifact.getVersion().equalsIgnoreCase( model.getVersion() ) &&
278 !VersionUtil.getBaseVersion( artifact.getVersion() ).equalsIgnoreCase( model.getVersion() ) )
280 StringBuffer emsg = new StringBuffer();
281 emsg.append( "File " ).append( artifactFile.getName() );
282 emsg.append( " has an invalid project model [" );
283 appendModel( emsg, model );
284 emsg.append( "]; The model version [" ).append( model.getVersion() );
285 emsg.append( "] does not match the version portion of the filename: " ).append( artifact.getVersion() );
287 log.warn( emsg.toString() );
288 addProblem( artifact, emsg.toString() );
296 private void appendModel( StringBuffer buf, ArchivaProjectModel model )
298 buf.append( "groupId:" ).append( model.getGroupId() );
299 buf.append( "|artifactId:" ).append( model.getArtifactId() );
300 buf.append( "|version:" ).append( model.getVersion() );
301 buf.append( "|packaging:" ).append( model.getPackaging() );
304 private void addProblem( ArchivaArtifact artifact, String msg )
305 throws ConsumerException
307 ManagedRepositoryContent repo = getRepository( artifact );
309 RepositoryProblem problem = new RepositoryProblem();
310 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
311 problem.setPath( repo.toPath( artifact ) );
312 problem.setGroupId( artifact.getGroupId() );
313 problem.setArtifactId( artifact.getArtifactId() );
314 problem.setVersion( artifact.getVersion() );
315 problem.setType( CorruptArtifactReport.PROBLEM_TYPE_CORRUPT_ARTIFACT );
316 problem.setOrigin( getId() );
317 problem.setMessage( msg );
321 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
323 catch ( ArchivaDatabaseException e )
325 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
327 throw new ConsumerException( emsg, e );
331 private String toProjectKey( ArchivaProjectModel project )
333 StringBuilder key = new StringBuilder();
335 key.append( project.getGroupId() ).append( ":" );
336 key.append( project.getArtifactId() ).append( ":" );
337 key.append( project.getVersion() );
339 return key.toString();
342 private void removeOldProjectModel( ArchivaProjectModel model )
346 dao.getProjectModelDAO().deleteProjectModel( model );
348 catch ( ArchivaDatabaseException ae )
350 log.error( "Unable to delete existing project model." );
353 // Force removal of project model from effective cache
354 String projectKey = toProjectKey( model );
355 synchronized ( effectiveProjectCache )
357 if ( effectiveProjectCache.hasKey( projectKey ) )
359 effectiveProjectCache.remove( projectKey );