1 package org.apache.archiva.metadata.repository.storage.maven2;
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.io.FilenameFilter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.List;
32 import org.apache.archiva.checksum.ChecksumAlgorithm;
33 import org.apache.archiva.checksum.ChecksummedFile;
34 import org.apache.archiva.metadata.model.ArtifactMetadata;
35 import org.apache.archiva.metadata.model.ProjectMetadata;
36 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
37 import org.apache.archiva.metadata.model.ProjectVersionReference;
38 import org.apache.archiva.metadata.repository.MetadataRepository;
39 import org.apache.archiva.metadata.repository.MetadataResolverException;
40 import org.apache.archiva.metadata.repository.filter.AllFilter;
41 import org.apache.archiva.metadata.repository.filter.Filter;
42 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
43 import org.apache.archiva.metadata.repository.storage.StorageMetadataResolver;
44 import org.apache.archiva.reports.RepositoryProblemFacet;
45 import org.apache.maven.archiva.common.utils.VersionUtil;
46 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
47 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
48 import org.apache.maven.archiva.xml.XMLException;
49 import org.apache.maven.model.CiManagement;
50 import org.apache.maven.model.Dependency;
51 import org.apache.maven.model.IssueManagement;
52 import org.apache.maven.model.License;
53 import org.apache.maven.model.MailingList;
54 import org.apache.maven.model.Model;
55 import org.apache.maven.model.Organization;
56 import org.apache.maven.model.Scm;
57 import org.apache.maven.model.building.DefaultModelBuildingRequest;
58 import org.apache.maven.model.building.ModelBuilder;
59 import org.apache.maven.model.building.ModelBuildingException;
60 import org.apache.maven.model.building.ModelBuildingRequest;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * @plexus.component role="org.apache.archiva.metadata.repository.storage.StorageMetadataResolver" role-hint="maven2"
67 public class Maven2RepositoryMetadataResolver
68 implements StorageMetadataResolver
73 private ModelBuilder builder;
78 private ArchivaConfiguration archivaConfiguration;
81 * @plexus.requirement role-hint="maven2"
83 private RepositoryPathTranslator pathTranslator;
88 private MetadataRepository metadataRepository;
90 private final static Logger log = LoggerFactory.getLogger( Maven2RepositoryMetadataResolver.class );
92 private static final String METADATA_FILENAME = "maven-metadata.xml";
94 private static final Filter<String> ALL = new AllFilter<String>();
96 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
98 // TODO: could natively implement the "shared model" concept from the browse action to avoid needing it there?
102 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
103 String projectVersion )
104 throws MetadataResolverException
106 ManagedRepositoryConfiguration repositoryConfiguration =
107 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
109 String artifactVersion = projectVersion;
111 File basedir = new File( repositoryConfiguration.getLocation() );
112 if ( VersionUtil.isSnapshot( projectVersion ) )
115 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, METADATA_FILENAME );
118 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
120 // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
121 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
122 if ( snapshotVersion != null )
125 artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
127 artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
130 catch ( XMLException e )
132 // unable to parse metadata - log it, and continue with the version as the original SNAPSHOT version
133 log.warn( "Invalid metadata: " + metadataFile + " - " + e.getMessage() );
137 String id = projectId + "-" + artifactVersion + ".pom";
138 File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion, id );
140 if ( !file.exists() )
142 // TODO: an event mechanism would remove coupling to the problem reporting plugin
143 addProblemReport( repoId, namespace, projectId, projectVersion, "missing-pom",
144 "The artifact's POM file '" + file + "' was missing" );
146 // metadata could not be resolved
150 ModelBuildingRequest req = new DefaultModelBuildingRequest();
151 req.setProcessPlugins( false );
152 req.setPomFile( file );
153 req.setModelResolver( new RepositoryModelResolver( basedir, pathTranslator ) );
154 req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
159 model = builder.build( req ).getEffectiveModel();
161 catch ( ModelBuildingException e )
163 addProblemReport( repoId, namespace, projectId, projectVersion, "invalid-pom",
164 "The artifact's POM file '" + file + "' was invalid: " + e.getMessage() );
166 // metadata could not be resolved
170 // Check if the POM is in the correct location
171 boolean correctGroupId = namespace.equals( model.getGroupId() );
172 boolean correctArtifactId = projectId.equals( model.getArtifactId() );
173 boolean correctVersion = projectVersion.equals( model.getVersion() );
174 if ( !correctGroupId || !correctArtifactId || !correctVersion )
176 StringBuilder message = new StringBuilder( "Incorrect POM coordinates in '" + file + "':" );
177 if ( !correctGroupId )
179 message.append( "\nIncorrect group ID: " ).append( model.getGroupId() );
181 if ( !correctArtifactId )
183 message.append( "\nIncorrect artifact ID: " ).append( model.getArtifactId() );
185 if ( !correctVersion )
187 message.append( "\nIncorrect version: " ).append( model.getVersion() );
190 addProblemReport( repoId, namespace, projectId, projectVersion, "mislocated-pom", message.toString() );
192 // metadata could not be resolved
196 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
197 metadata.setCiManagement( convertCiManagement( model.getCiManagement() ) );
198 metadata.setDescription( model.getDescription() );
199 metadata.setId( projectVersion );
200 metadata.setIssueManagement( convertIssueManagement( model.getIssueManagement() ) );
201 metadata.setLicenses( convertLicenses( model.getLicenses() ) );
202 metadata.setMailingLists( convertMailingLists( model.getMailingLists() ) );
203 metadata.setDependencies( convertDependencies( model.getDependencies() ) );
204 metadata.setName( model.getName() );
205 metadata.setOrganization( convertOrganization( model.getOrganization() ) );
206 metadata.setScm( convertScm( model.getScm() ) );
207 metadata.setUrl( model.getUrl() );
209 MavenProjectFacet facet = new MavenProjectFacet();
210 facet.setGroupId( model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId() );
211 facet.setArtifactId( model.getArtifactId() );
212 facet.setPackaging( model.getPackaging() );
213 if ( model.getParent() != null )
215 MavenProjectParent parent = new MavenProjectParent();
216 parent.setGroupId( model.getParent().getGroupId() );
217 parent.setArtifactId( model.getParent().getArtifactId() );
218 parent.setVersion( model.getParent().getVersion() );
219 facet.setParent( parent );
221 metadata.addFacet( facet );
226 private void addProblemReport( String repoId, String namespace, String projectId, String projectVersion,
227 String problemId, String message )
229 // TODO: an event mechanism would remove coupling to the problem reporting plugin
230 RepositoryProblemFacet problem = new RepositoryProblemFacet();
231 problem.setProblem( problemId );
232 problem.setMessage( message );
233 problem.setProject( projectId );
234 problem.setNamespace( namespace );
235 problem.setRepositoryId( repoId );
236 problem.setVersion( projectVersion );
238 metadataRepository.addMetadataFacet( repoId, problem );
241 private List<org.apache.archiva.metadata.model.Dependency> convertDependencies( List<Dependency> dependencies )
243 List<org.apache.archiva.metadata.model.Dependency> l =
244 new ArrayList<org.apache.archiva.metadata.model.Dependency>();
245 for ( Dependency dependency : dependencies )
247 org.apache.archiva.metadata.model.Dependency newDependency =
248 new org.apache.archiva.metadata.model.Dependency();
249 newDependency.setArtifactId( dependency.getArtifactId() );
250 newDependency.setClassifier( dependency.getClassifier() );
251 newDependency.setGroupId( dependency.getGroupId() );
252 newDependency.setOptional( dependency.isOptional() );
253 newDependency.setScope( dependency.getScope() );
254 newDependency.setSystemPath( dependency.getSystemPath() );
255 newDependency.setType( dependency.getType() );
256 newDependency.setVersion( dependency.getVersion() );
257 l.add( newDependency );
262 private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
264 org.apache.archiva.metadata.model.Scm newScm = null;
267 newScm = new org.apache.archiva.metadata.model.Scm();
268 newScm.setConnection( scm.getConnection() );
269 newScm.setDeveloperConnection( scm.getDeveloperConnection() );
270 newScm.setUrl( scm.getUrl() );
275 private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
277 org.apache.archiva.metadata.model.Organization org = null;
278 if ( organization != null )
280 org = new org.apache.archiva.metadata.model.Organization();
281 org.setName( organization.getName() );
282 org.setUrl( organization.getUrl() );
287 private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
289 List<org.apache.archiva.metadata.model.License> l = new ArrayList<org.apache.archiva.metadata.model.License>();
290 for ( License license : licenses )
292 org.apache.archiva.metadata.model.License newLicense = new org.apache.archiva.metadata.model.License();
293 newLicense.setName( license.getName() );
294 newLicense.setUrl( license.getUrl() );
300 private List<org.apache.archiva.metadata.model.MailingList> convertMailingLists( List<MailingList> mailingLists )
302 List<org.apache.archiva.metadata.model.MailingList> l =
303 new ArrayList<org.apache.archiva.metadata.model.MailingList>();
304 for ( MailingList mailingList : mailingLists )
306 org.apache.archiva.metadata.model.MailingList newMailingList =
307 new org.apache.archiva.metadata.model.MailingList();
308 newMailingList.setName( mailingList.getName() );
309 newMailingList.setMainArchiveUrl( mailingList.getArchive() );
310 newMailingList.setPostAddress( mailingList.getPost() );
311 newMailingList.setSubscribeAddress( mailingList.getSubscribe() );
312 newMailingList.setUnsubscribeAddress( mailingList.getUnsubscribe() );
313 newMailingList.setOtherArchives( mailingList.getOtherArchives() );
314 l.add( newMailingList );
319 private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
321 org.apache.archiva.metadata.model.IssueManagement im = null;
322 if ( issueManagement != null )
324 im = new org.apache.archiva.metadata.model.IssueManagement();
325 im.setSystem( issueManagement.getSystem() );
326 im.setUrl( issueManagement.getUrl() );
331 private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
333 org.apache.archiva.metadata.model.CiManagement ci = null;
334 if ( ciManagement != null )
336 ci = new org.apache.archiva.metadata.model.CiManagement();
337 ci.setSystem( ciManagement.getSystem() );
338 ci.setUrl( ciManagement.getUrl() );
343 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
344 String projectVersion )
346 // TODO: useful, but not implemented yet, not called from DefaultMetadataResolver
347 throw new UnsupportedOperationException();
350 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
351 String projectVersion )
353 // Can't be determined on a Maven 2 repository
354 throw new UnsupportedOperationException();
357 public Collection<String> getRootNamespaces( String repoId )
359 return getRootNamespaces( repoId, ALL );
362 public Collection<String> getRootNamespaces( String repoId, Filter<String> filter )
364 File dir = getRepositoryBasedir( repoId );
366 String[] files = dir.list( new DirectoryFilter( filter ) );
367 return files != null ? Arrays.asList( files ) : Collections.<String>emptyList();
370 private File getRepositoryBasedir( String repoId )
372 ManagedRepositoryConfiguration repositoryConfiguration =
373 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
375 return new File( repositoryConfiguration.getLocation() );
378 public Collection<String> getNamespaces( String repoId, String namespace )
380 return getNamespaces( repoId, namespace, ALL );
383 public Collection<String> getNamespaces( String repoId, String namespace, Filter<String> filter )
385 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
387 // scan all the directories which are potential namespaces. Any directories known to be projects are excluded
388 Collection<String> namespaces = new ArrayList<String>();
389 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
392 for ( File file : files )
394 if ( !isProject( file, filter ) )
396 namespaces.add( file.getName() );
403 public Collection<String> getProjects( String repoId, String namespace )
405 return getProjects( repoId, namespace, ALL );
408 public Collection<String> getProjects( String repoId, String namespace, Filter<String> filter )
410 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
412 // scan all directories in the namespace, and only include those that are known to be projects
413 Collection<String> projects = new ArrayList<String>();
414 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
417 for ( File file : files )
419 if ( isProject( file, filter ) )
421 projects.add( file.getName() );
428 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
430 return getProjectVersions( repoId, namespace, projectId, ALL );
433 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
434 String projectVersion )
436 return getArtifacts( repoId, namespace, projectId, projectVersion, ALL );
439 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId,
440 Filter<String> filter )
442 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace, projectId );
444 // all directories in a project directory can be considered a version
445 String[] files = dir.list( new DirectoryFilter( filter ) );
446 return files != null ? Arrays.asList( files ) : Collections.<String>emptyList();
449 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
450 String projectVersion, Filter<String> filter )
452 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace, projectId, projectVersion );
454 // all files that are not metadata and not a checksum / signature are considered artifacts
455 File[] files = dir.listFiles( new ArtifactDirectoryFilter( filter ) );
457 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
460 for ( File file : files )
462 ArtifactMetadata metadata = new ArtifactMetadata();
463 metadata.setId( file.getName() );
464 metadata.setProject( projectId );
465 metadata.setNamespace( namespace );
466 metadata.setRepositoryId( repoId );
467 metadata.setWhenGathered( new Date() );
468 metadata.setFileLastModified( file.lastModified() );
469 ChecksummedFile checksummedFile = new ChecksummedFile( file );
472 metadata.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
474 catch ( IOException e )
476 log.error( "Unable to checksum file " + file + ": " + e.getMessage() );
480 metadata.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
482 catch ( IOException e )
484 log.error( "Unable to checksum file " + file + ": " + e.getMessage() );
486 metadata.setSize( file.length() );
487 metadata.setVersion( projectVersion );
488 artifacts.add( metadata );
494 private boolean isProject( File dir, Filter<String> filter )
496 // scan directories for a valid project version subdirectory, meaning this must be a project directory
497 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
500 for ( File file : files )
502 if ( isProjectVersion( file ) )
509 // if a metadata file is present, check if this is the "artifactId" directory, marking it as a project
510 MavenRepositoryMetadata metadata = readMetadata( dir );
511 if ( metadata != null && dir.getName().equals( metadata.getArtifactId() ) )
519 private boolean isProjectVersion( File dir )
521 final String artifactId = dir.getParentFile().getName();
522 final String projectVersion = dir.getName();
524 // check if there is a POM artifact file to ensure it is a version directory
526 if ( VersionUtil.isSnapshot( projectVersion ) )
528 files = dir.listFiles( new FilenameFilter()
530 public boolean accept( File dir, String name )
532 if ( name.startsWith( artifactId + "-" ) && name.endsWith( ".pom" ) )
534 String v = name.substring( artifactId.length() + 1, name.length() - 4 );
535 v = VersionUtil.getBaseVersion( v );
536 if ( v.equals( projectVersion ) )
547 final String pomFile = artifactId + "-" + projectVersion + ".pom";
548 files = dir.listFiles( new FilenameFilter()
550 public boolean accept( File dir, String name )
552 return pomFile.equals( name );
556 if ( files != null && files.length > 0 )
561 // if a metadata file is present, check if this is the "version" directory, marking it as a project version
562 MavenRepositoryMetadata metadata = readMetadata( dir );
563 if ( metadata != null && projectVersion.equals( metadata.getVersion() ) )
571 private MavenRepositoryMetadata readMetadata( File directory )
573 MavenRepositoryMetadata metadata = null;
574 File metadataFile = new File( directory, METADATA_FILENAME );
575 if ( metadataFile.exists() )
579 metadata = MavenRepositoryMetadataReader.read( metadataFile );
581 catch ( XMLException e )
583 // ignore missing or invalid metadata
589 public void setConfiguration( ArchivaConfiguration configuration )
591 this.archivaConfiguration = configuration;
594 private static class DirectoryFilter
595 implements FilenameFilter
597 private final Filter<String> filter;
599 public DirectoryFilter( Filter<String> filter )
601 this.filter = filter;
604 public boolean accept( File dir, String name )
606 if ( !filter.accept( name ) )
610 else if ( name.startsWith( "." ) )
614 else if ( !new File( dir, name ).isDirectory() )
622 private class ArtifactDirectoryFilter
623 implements FilenameFilter
625 private final Filter<String> filter;
627 public ArtifactDirectoryFilter( Filter<String> filter )
629 this.filter = filter;
632 public boolean accept( File dir, String name )
634 // TODO compare to logic in maven-repository-layer
635 if ( !filter.accept( name ) )
639 else if ( name.startsWith( "." ) )
643 else if ( name.endsWith( ".md5" ) || name.endsWith( ".sha1" ) || name.endsWith( ".asc" ) )
647 else if ( name.equals( METADATA_FILENAME ) )
651 else if ( new File( dir, name ).isDirectory() )