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.ArchivaModelCloner;
36 import org.apache.maven.archiva.model.ArchivaProjectModel;
37 import org.apache.maven.archiva.model.Keys;
38 import org.apache.maven.archiva.model.RepositoryProblem;
39 import org.apache.maven.archiva.reporting.artifact.CorruptArtifactReport;
40 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
41 import org.apache.maven.archiva.repository.RepositoryContentFactory;
42 import org.apache.maven.archiva.repository.RepositoryException;
43 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
44 import org.apache.maven.archiva.repository.project.ProjectModelException;
45 import org.apache.maven.archiva.repository.project.ProjectModelReader;
46 import org.apache.maven.archiva.repository.project.filters.EffectiveProjectModelFilter;
47 import org.apache.maven.archiva.repository.project.readers.ProjectModel300Reader;
48 import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
49 import org.codehaus.plexus.cache.Cache;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * ProjectModelToDatabaseConsumer
57 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer"
58 * role-hint="update-db-project"
59 * instantiation-strategy="per-lookup"
61 public class ProjectModelToDatabaseConsumer
62 extends AbstractMonitoredConsumer
63 implements DatabaseUnprocessedArtifactConsumer
65 private Logger log = LoggerFactory.getLogger( ProjectModelToDatabaseConsumer.class );
68 * @plexus.configuration default-value="update-db-project"
73 * @plexus.configuration default-value="Update database with project model information."
75 private String description;
78 * @plexus.requirement role-hint="jdo"
80 private ArchivaDAO dao;
85 private RepositoryContentFactory repositoryFactory;
88 * @plexus.requirement role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
89 * role-hint="effective"
91 private EffectiveProjectModelFilter effectiveModelFilter;
93 private List<String> includes;
96 * @plexus.requirement role-hint="effective-project-cache"
98 private Cache effectiveProjectCache;
100 public ProjectModelToDatabaseConsumer()
102 includes = new ArrayList<String>();
103 includes.add( "pom" );
106 public void beginScan()
108 /* nothing to do here */
111 public void completeScan()
113 /* nothing to do here */
116 public List<String> getIncludedTypes()
121 public void processArchivaArtifact( ArchivaArtifact artifact )
122 throws ConsumerException
124 if ( !StringUtils.equals( "pom", artifact.getType() ) )
126 // Not a pom. Skip it.
130 ArchivaProjectModel model = null;
132 // remove old project model if it already exists in the database
134 getProjectModelFromDatabase( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ) ) != null )
136 removeOldProjectModel( model );
140 ManagedRepositoryContent repo = getRepository( artifact );
141 File artifactFile = repo.toFile( artifact );
143 ProjectModelReader reader;
144 if ( repo instanceof ManagedLegacyRepositoryContent )
146 reader = new ProjectModel300Reader();
150 reader = new ProjectModel400Reader();
155 model = reader.read( artifactFile );
157 // The version should be updated to the artifact/filename version if it is a unique snapshot
158 if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
160 model.setVersion( artifact.getVersion() );
163 // Resolve the project model (build effective model, resolve expressions)
164 model = effectiveModelFilter.filter( model );
166 if ( isValidModel( model, repo, artifact ) )
168 log.debug( "Adding project model to database - " + Keys.toKey( model ) );
170 // Clone model, since DAO while detachingCopy resets its contents
171 // This changes contents of the cache in EffectiveProjectModelFilter
172 model = ArchivaModelCloner.clone( model );
173 dao.getProjectModelDAO().saveProjectModel( model );
177 log.warn( "Invalid or corrupt pom. Project model not added to database - " + Keys.toKey( model ) );
181 catch ( ProjectModelException e )
183 log.warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
185 addProblem( artifact, "Unable to read project model " + artifactFile + " : " + e.getMessage() );
187 catch ( ArchivaDatabaseException e )
189 log.warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(), e );
191 catch ( Throwable t )
193 // Catch the other errors in the process to allow the rest of the process to complete.
194 log.error( "Unable to process model " + artifactFile + " due to : " + t.getClass().getName() + " : " +
199 private ArchivaProjectModel getProjectModelFromDatabase( String groupId, String artifactId, String version )
203 ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
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 log.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 log.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();
316 throw new ConsumerException( emsg, e );
320 private String toProjectKey( ArchivaProjectModel project )
322 StringBuilder key = new StringBuilder();
324 key.append( project.getGroupId() ).append( ":" );
325 key.append( project.getArtifactId() ).append( ":" );
326 key.append( project.getVersion() );
328 return key.toString();
331 private void removeOldProjectModel( ArchivaProjectModel model )
335 dao.getProjectModelDAO().deleteProjectModel( model );
337 catch ( ArchivaDatabaseException ae )
339 log.error( "Unable to delete existing project model." );
342 // Force removal of project model from effective cache
343 String projectKey = toProjectKey( model );
344 synchronized ( effectiveProjectCache )
346 if ( effectiveProjectCache.hasKey( projectKey ) )
348 effectiveProjectCache.remove( projectKey );