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 ) );
171 dao.getProjectModelDAO().saveProjectModel( model );
176 "Invalid or corrupt pom. Project model not added to database - " + Keys.toKey( model ) );
180 catch ( ProjectModelException e )
182 getLogger().warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
184 addProblem( artifact, "Unable to read project model " + artifactFile + " : " + e.getMessage() );
186 catch ( ArchivaDatabaseException e )
188 getLogger().warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(),
191 catch ( Throwable t )
193 // Catch the other errors in the process to allow the rest of the process to complete.
194 getLogger().error( "Unable to process model " + artifactFile + " due to : " + t.getClass().getName() +
195 " : " + t.getMessage(), t );
199 private boolean hasProjectModelInDatabase( String groupId, String artifactId, String version )
203 ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
204 return ( model != null );
206 catch ( ObjectNotFoundException e )
210 catch ( ArchivaDatabaseException e )
216 private ManagedRepositoryContent getRepository( ArchivaArtifact artifact )
217 throws ConsumerException
219 String repoId = artifact.getModel().getRepositoryId();
222 return repositoryFactory.getManagedRepositoryContent( repoId );
224 catch ( RepositoryException e )
226 throw new ConsumerException( "Unable to process project model: " + e.getMessage(), e );
230 public String getDescription()
235 public String getId()
240 public boolean isPermanent()
242 // Tells the configuration that this consumer cannot be disabled.
246 private boolean isValidModel( ArchivaProjectModel model, ManagedRepositoryContent repo, ArchivaArtifact artifact )
247 throws ConsumerException
249 File artifactFile = repo.toFile( artifact );
251 if ( !artifact.getArtifactId().equalsIgnoreCase( model.getArtifactId() ) )
253 StringBuffer emsg = new StringBuffer();
254 emsg.append( "File " ).append( artifactFile.getName() );
255 emsg.append( " has an invalid project model [" );
256 appendModel( emsg, model );
257 emsg.append( "]: The model artifactId [" ).append( model.getArtifactId() );
258 emsg.append( "] does not match the artifactId portion of the filename: " ).append( artifact.getArtifactId() );
260 getLogger().warn(emsg.toString() );
261 addProblem( artifact, emsg.toString() );
266 if ( !artifact.getVersion().equalsIgnoreCase( model.getVersion() ) &&
267 !VersionUtil.getBaseVersion( artifact.getVersion() ).equalsIgnoreCase( model.getVersion() ) )
269 StringBuffer emsg = new StringBuffer();
270 emsg.append( "File " ).append( artifactFile.getName() );
271 emsg.append( " has an invalid project model [" );
272 appendModel( emsg, model );
273 emsg.append( "]; The model version [" ).append( model.getVersion() );
274 emsg.append( "] does not match the version portion of the filename: " ).append( artifact.getVersion() );
276 getLogger().warn(emsg.toString() );
277 addProblem( artifact, emsg.toString() );
285 private void appendModel( StringBuffer buf, ArchivaProjectModel model )
287 buf.append( "groupId:" ).append( model.getGroupId() );
288 buf.append( "|artifactId:" ).append( model.getArtifactId() );
289 buf.append( "|version:" ).append( model.getVersion() );
290 buf.append( "|packaging:" ).append( model.getPackaging() );
293 private void addProblem( ArchivaArtifact artifact, String msg )
294 throws ConsumerException
296 ManagedRepositoryContent repo = getRepository( artifact );
298 RepositoryProblem problem = new RepositoryProblem();
299 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
300 problem.setPath( repo.toPath( artifact ) );
301 problem.setGroupId( artifact.getGroupId() );
302 problem.setArtifactId( artifact.getArtifactId() );
303 problem.setVersion( artifact.getVersion() );
304 problem.setType( CorruptArtifactReport.PROBLEM_TYPE_CORRUPT_ARTIFACT );
305 problem.setOrigin( getId() );
306 problem.setMessage( msg );
310 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
312 catch ( ArchivaDatabaseException e )
314 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
315 getLogger().warn( emsg, e );
316 throw new ConsumerException( emsg, e );