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.common.utils.VersionUtil;
24 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
25 import org.apache.maven.archiva.consumers.ConsumerException;
26 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
27 import org.apache.maven.archiva.database.ArchivaDAO;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.apache.maven.archiva.database.ObjectNotFoundException;
30 import org.apache.maven.archiva.model.ArchivaArtifact;
31 import org.apache.maven.archiva.model.ArchivaProjectModel;
32 import org.apache.maven.archiva.model.Keys;
33 import org.apache.maven.archiva.model.RepositoryProblem;
34 import org.apache.maven.archiva.reporting.artifact.CorruptArtifactReport;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.archiva.repository.RepositoryContentFactory;
37 import org.apache.maven.archiva.repository.RepositoryException;
38 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
39 import org.apache.maven.archiva.repository.project.ProjectModelException;
40 import org.apache.maven.archiva.repository.project.ProjectModelFilter;
41 import org.apache.maven.archiva.repository.project.ProjectModelReader;
42 import org.apache.maven.archiva.repository.project.filters.EffectiveProjectModelFilter;
45 import java.util.ArrayList;
46 import java.util.List;
49 * ProjectModelToDatabaseConsumer
51 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
53 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer"
54 * role-hint="update-db-project"
55 * instantiation-strategy="per-lookup"
57 public class ProjectModelToDatabaseConsumer
58 extends AbstractMonitoredConsumer
59 implements DatabaseUnprocessedArtifactConsumer
62 * @plexus.configuration default-value="update-db-project"
67 * @plexus.configuration default-value="Update database with project model information."
69 private String description;
72 * @plexus.requirement role-hint="jdo"
74 private ArchivaDAO dao;
79 private RepositoryContentFactory repositoryFactory;
82 * @plexus.requirement role-hint="model400"
84 private ProjectModelReader project400Reader;
87 * @plexus.requirement role-hint="model300"
89 private ProjectModelReader project300Reader;
92 * @plexus.requirement role-hint="expression"
94 private ProjectModelFilter expressionModelFilter;
97 * @plexus.requirement role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
98 * role-hint="effective"
100 private EffectiveProjectModelFilter effectiveModelFilter;
102 private List<String> includes;
104 public ProjectModelToDatabaseConsumer()
106 includes = new ArrayList<String>();
107 includes.add( "pom" );
110 public void beginScan()
112 /* nothing to do here */
115 public void completeScan()
117 /* nothing to do here */
120 public List<String> getIncludedTypes()
125 public void processArchivaArtifact( ArchivaArtifact artifact )
126 throws ConsumerException
128 if ( !StringUtils.equals( "pom", artifact.getType() ) )
130 // Not a pom. Skip it.
134 if ( hasProjectModelInDatabase( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ) )
136 // Already in the database. Skip it.
140 ManagedRepositoryContent repo = getRepository( artifact );
141 File artifactFile = repo.toFile( artifact );
142 ProjectModelReader reader = project400Reader;
144 if ( repo instanceof ManagedLegacyRepositoryContent )
146 reader = project300Reader;
151 ArchivaProjectModel model = reader.read( artifactFile );
153 model.setOrigin( "filesystem" );
155 // The version should be updated to the artifact/filename version if it is a unique snapshot
156 if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
158 model.setVersion( artifact.getVersion() );
162 model = expressionModelFilter.filter( model );
164 // Resolve the project model
165 model = effectiveModelFilter.filter( model );
167 if ( isValidModel( model, repo, artifact ) )
169 getLogger().info( "Adding project model to database - " + Keys.toKey( model ) );
170 dao.getProjectModelDAO().saveProjectModel( model );
175 "Invalid or corrupt pom. Project model not added to database - " + Keys.toKey( model ) );
179 catch ( ProjectModelException e )
181 getLogger().warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
183 addProblem( artifact, "Unable to read project model " + artifactFile + " : " + e.getMessage() );
185 catch ( ArchivaDatabaseException e )
187 getLogger().warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(),
190 catch ( Throwable t )
192 // Catch the other errors in the process to allow the rest of the process to complete.
193 getLogger().error( "Unable to process model " + artifactFile + " due to : " + t.getClass().getName() +
194 " : " + t.getMessage(), t );
198 private boolean hasProjectModelInDatabase( String groupId, String artifactId, String version )
202 ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
203 return ( model != null );
205 catch ( ObjectNotFoundException e )
209 catch ( ArchivaDatabaseException e )
215 private ManagedRepositoryContent getRepository( ArchivaArtifact artifact )
216 throws ConsumerException
218 String repoId = artifact.getModel().getRepositoryId();
221 return repositoryFactory.getManagedRepositoryContent( repoId );
223 catch ( RepositoryException e )
225 throw new ConsumerException( "Unable to process project model: " + e.getMessage(), e );
229 public String getDescription()
234 public String getId()
239 public boolean isPermanent()
241 // Tells the configuration that this consumer cannot be disabled.
245 private boolean isValidModel( ArchivaProjectModel model, ManagedRepositoryContent repo, ArchivaArtifact artifact )
246 throws ConsumerException
248 File artifactFile = repo.toFile( artifact );
250 if ( !artifact.getArtifactId().equalsIgnoreCase( model.getArtifactId() ) )
252 StringBuffer emsg = new StringBuffer();
253 emsg.append( "File " ).append( artifactFile.getName() );
254 emsg.append( " has an invalid project model [" );
255 appendModel( emsg, model );
256 emsg.append( "]: The model artifactId [" ).append( model.getArtifactId() );
257 emsg.append( "] does not match the artifactId portion of the filename: " ).append( artifact.getArtifactId() );
259 getLogger().warn(emsg.toString() );
260 addProblem( artifact, emsg.toString() );
265 if ( !artifact.getVersion().equalsIgnoreCase( model.getVersion() ) &&
266 !VersionUtil.getBaseVersion( artifact.getVersion() ).equalsIgnoreCase( model.getVersion() ) )
268 StringBuffer emsg = new StringBuffer();
269 emsg.append( "File " ).append( artifactFile.getName() );
270 emsg.append( " has an invalid project model [" );
271 appendModel( emsg, model );
272 emsg.append( "]; The model version [" ).append( model.getVersion() );
273 emsg.append( "] does not match the version portion of the filename: " ).append( artifact.getVersion() );
275 getLogger().warn(emsg.toString() );
276 addProblem( artifact, emsg.toString() );
284 private void appendModel( StringBuffer buf, ArchivaProjectModel model )
286 buf.append( "groupId:" ).append( model.getGroupId() );
287 buf.append( "|artifactId:" ).append( model.getArtifactId() );
288 buf.append( "|version:" ).append( model.getVersion() );
289 buf.append( "|packaging:" ).append( model.getPackaging() );
292 private void addProblem( ArchivaArtifact artifact, String msg )
293 throws ConsumerException
295 ManagedRepositoryContent repo = getRepository( artifact );
297 RepositoryProblem problem = new RepositoryProblem();
298 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
299 problem.setPath( repo.toPath( artifact ) );
300 problem.setGroupId( artifact.getGroupId() );
301 problem.setArtifactId( artifact.getArtifactId() );
302 problem.setVersion( artifact.getVersion() );
303 problem.setType( CorruptArtifactReport.PROBLEM_TYPE_CORRUPT_ARTIFACT );
304 problem.setOrigin( getId() );
305 problem.setMessage( msg );
309 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
311 catch ( ArchivaDatabaseException e )
313 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
314 getLogger().warn( emsg, e );
315 throw new ConsumerException( emsg, e );