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.database.ArchivaDAO;
31 import org.apache.maven.archiva.database.ArchivaDatabaseException;
32 import org.apache.maven.archiva.database.ObjectNotFoundException;
33 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
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.CiManagement;
38 import org.apache.maven.archiva.model.IssueManagement;
39 import org.apache.maven.archiva.model.Keys;
40 import org.apache.maven.archiva.model.Organization;
41 import org.apache.maven.archiva.model.RepositoryProblem;
42 import org.apache.maven.archiva.reporting.artifact.CorruptArtifactReport;
43 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
44 import org.apache.maven.archiva.repository.RepositoryContentFactory;
45 import org.apache.maven.archiva.repository.RepositoryException;
46 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
47 import org.apache.maven.archiva.repository.project.ProjectModelReader;
48 import org.apache.maven.archiva.repository.project.filters.EffectiveProjectModelFilter;
49 import org.apache.maven.archiva.repository.project.readers.ProjectModel300Reader;
50 import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
51 import org.apache.maven.archiva.xml.XMLException;
52 import org.codehaus.plexus.cache.Cache;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 * ProjectModelToDatabaseConsumer
60 * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer"
61 * role-hint="update-db-project"
62 * instantiation-strategy="per-lookup"
64 public class ProjectModelToDatabaseConsumer
65 extends AbstractMonitoredConsumer
66 implements DatabaseUnprocessedArtifactConsumer
68 private Logger log = LoggerFactory.getLogger( ProjectModelToDatabaseConsumer.class );
71 * @plexus.configuration default-value="update-db-project"
76 * @plexus.configuration default-value="Update database with project model information."
78 private String description;
81 * @plexus.requirement role-hint="jdo"
83 private ArchivaDAO dao;
88 private RepositoryContentFactory repositoryFactory;
91 * @plexus.requirement role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
92 * role-hint="effective"
94 private EffectiveProjectModelFilter effectiveModelFilter;
96 private List<String> includes;
99 * @plexus.requirement role-hint="effective-project-cache"
101 private Cache effectiveProjectCache;
103 public ProjectModelToDatabaseConsumer()
105 includes = new ArrayList<String>();
106 includes.add( "pom" );
109 public void beginScan()
111 /* nothing to do here */
114 public void completeScan()
116 /* nothing to do here */
119 public List<String> getIncludedTypes()
124 public void processArchivaArtifact( ArchivaArtifact artifact )
125 throws ConsumerException
127 if ( !StringUtils.equals( "pom", artifact.getType() ) )
129 // Not a pom. Skip it.
133 ArchivaProjectModel model = null;
135 // remove old project model if it already exists in the database
137 getProjectModelFromDatabase( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ) ) != null )
139 removeOldProjectModel( model );
143 ManagedRepositoryContent repo = getRepository( artifact );
144 File artifactFile = repo.toFile( artifact );
146 ProjectModelReader reader;
147 if ( repo instanceof ManagedLegacyRepositoryContent )
149 reader = new ProjectModel300Reader();
153 reader = new ProjectModel400Reader();
158 model = reader.read( artifactFile );
160 Organization organization = model.getOrganization();
161 if( organization != null )
163 log.info( "++++ [AfterRead] organization NAME --> " + organization.getName() + " : " + organization.getOrganizationName() );
166 IssueManagement iM = model.getIssueManagement();
169 log.info( "++++ [AfterRead] issueMgnt url --> " + iM.getUrl() + " : " + iM.getIssueManagementUrl() );
172 CiManagement ci = model.getCiManagement();
175 log.info( "++++ [AfterRead] ci url --> " + ci.getUrl() + " : " + ci.getCiUrl() );
178 // The version should be updated to the artifact/filename version if it is a unique snapshot
179 if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
181 model.setVersion( artifact.getVersion() );
184 // Resolve the project model (build effective model, resolve expressions)
185 model = effectiveModelFilter.filter( model );
187 organization = model.getOrganization();
188 if( organization != null )
190 log.info( "++++ [AfterFilter] organization NAME --> " + organization.getName() + " : " + organization.getOrganizationName() );
193 iM = model.getIssueManagement();
196 log.info( "++++ [AfterFilter] issueMgnt url --> " + iM.getUrl() + " : " + iM.getIssueManagementUrl() );
199 ci = model.getCiManagement();
202 log.info( "++++ [AfterFilter] ci url --> " + ci.getUrl() + " : " + ci.getCiUrl() );
205 if ( isValidModel( model, repo, artifact ) )
207 log.debug( "Adding project model to database - " + Keys.toKey( model ) );
209 // Clone model, since DAO while detachingCopy resets its contents
210 // This changes contents of the cache in EffectiveProjectModelFilter
211 model = ArchivaModelCloner.clone( model );
213 organization = model.getOrganization();
214 if( organization != null )
216 log.info( "++++ [AfterClone] organization NAME --> " + organization.getName() + " : " + organization.getOrganizationName() );
219 iM = model.getIssueManagement();
222 log.info( "++++ [AfterClone] issueMgnt url --> " + iM.getUrl() + " : " + iM.getIssueManagementUrl() );
225 ci = model.getCiManagement();
228 log.info( "++++ [AfterClone] ci url --> " + ci.getUrl() + " : " + ci.getCiUrl() );
231 dao.getProjectModelDAO().saveProjectModel( model );
235 log.warn( "Invalid or corrupt pom. Project model not added to database - " + Keys.toKey( model ) );
239 catch ( XMLException e )
241 log.warn( "Unable to read project model " + artifactFile + " : " + e.getMessage() );
243 addProblem( artifact, "Unable to read project model " + artifactFile + " : " + e.getMessage() );
245 catch ( ArchivaDatabaseException e )
247 log.warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(), e );
249 catch ( Throwable t )
251 // Catch the other errors in the process to allow the rest of the process to complete.
252 log.error( "Unable to process model " + artifactFile + " due to : " + t.getClass().getName() + " : " +
257 private ArchivaProjectModel getProjectModelFromDatabase( String groupId, String artifactId, String version )
261 ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
264 catch ( ObjectNotFoundException e )
268 catch ( ArchivaDatabaseException e )
274 private ManagedRepositoryContent getRepository( ArchivaArtifact artifact )
275 throws ConsumerException
277 String repoId = artifact.getModel().getRepositoryId();
280 return repositoryFactory.getManagedRepositoryContent( repoId );
282 catch ( RepositoryException e )
284 throw new ConsumerException( "Unable to process project model: " + e.getMessage(), e );
288 public String getDescription()
293 public String getId()
298 public boolean isPermanent()
300 // Tells the configuration that this consumer cannot be disabled.
304 private boolean isValidModel( ArchivaProjectModel model, ManagedRepositoryContent repo, ArchivaArtifact artifact )
305 throws ConsumerException
307 File artifactFile = repo.toFile( artifact );
309 if ( !artifact.getArtifactId().equalsIgnoreCase( model.getArtifactId() ) )
311 StringBuffer emsg = new StringBuffer();
312 emsg.append( "File " ).append( artifactFile.getName() );
313 emsg.append( " has an invalid project model [" );
314 appendModel( emsg, model );
315 emsg.append( "]: The model artifactId [" ).append( model.getArtifactId() );
316 emsg.append( "] does not match the artifactId portion of the filename: " ).append( artifact.getArtifactId() );
318 log.warn( emsg.toString() );
319 addProblem( artifact, emsg.toString() );
324 if ( !artifact.getVersion().equalsIgnoreCase( model.getVersion() ) &&
325 !VersionUtil.getBaseVersion( artifact.getVersion() ).equalsIgnoreCase( model.getVersion() ) )
327 StringBuffer emsg = new StringBuffer();
328 emsg.append( "File " ).append( artifactFile.getName() );
329 emsg.append( " has an invalid project model [" );
330 appendModel( emsg, model );
331 emsg.append( "]; The model version [" ).append( model.getVersion() );
332 emsg.append( "] does not match the version portion of the filename: " ).append( artifact.getVersion() );
334 log.warn( emsg.toString() );
335 addProblem( artifact, emsg.toString() );
343 private void appendModel( StringBuffer buf, ArchivaProjectModel model )
345 buf.append( "groupId:" ).append( model.getGroupId() );
346 buf.append( "|artifactId:" ).append( model.getArtifactId() );
347 buf.append( "|version:" ).append( model.getVersion() );
348 buf.append( "|packaging:" ).append( model.getPackaging() );
351 private void addProblem( ArchivaArtifact artifact, String msg )
352 throws ConsumerException
354 ManagedRepositoryContent repo = getRepository( artifact );
356 RepositoryProblem problem = new RepositoryProblem();
357 problem.setRepositoryId( artifact.getModel().getRepositoryId() );
358 problem.setPath( repo.toPath( artifact ) );
359 problem.setGroupId( artifact.getGroupId() );
360 problem.setArtifactId( artifact.getArtifactId() );
361 problem.setVersion( artifact.getVersion() );
362 problem.setType( CorruptArtifactReport.PROBLEM_TYPE_CORRUPT_ARTIFACT );
363 problem.setOrigin( getId() );
364 problem.setMessage( msg );
368 dao.getRepositoryProblemDAO().saveRepositoryProblem( problem );
370 catch ( ArchivaDatabaseException e )
372 String emsg = "Unable to save problem with artifact location to DB: " + e.getMessage();
374 throw new ConsumerException( emsg, e );
378 private String toProjectKey( ArchivaProjectModel project )
380 StringBuilder key = new StringBuilder();
382 key.append( project.getGroupId() ).append( ":" );
383 key.append( project.getArtifactId() ).append( ":" );
384 key.append( project.getVersion() );
386 return key.toString();
389 private void removeOldProjectModel( ArchivaProjectModel model )
393 dao.getProjectModelDAO().deleteProjectModel( model );
395 catch ( ArchivaDatabaseException ae )
397 log.error( "Unable to delete existing project model." );
400 // Force removal of project model from effective cache
401 String projectKey = toProjectKey( model );
402 synchronized ( effectiveProjectCache )
404 if ( effectiveProjectCache.hasKey( projectKey ) )
406 effectiveProjectCache.remove( projectKey );