1 package org.apache.archiva.repository.metadata.base;
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.archiva.checksum.ChecksumAlgorithm;
23 import org.apache.archiva.checksum.ChecksummedFile;
24 import org.apache.archiva.common.utils.PathUtil;
25 import org.apache.archiva.common.utils.VersionComparator;
26 import org.apache.archiva.common.utils.VersionUtil;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.ConfigurationEvent;
29 import org.apache.archiva.configuration.ConfigurationListener;
30 import org.apache.archiva.configuration.ConfigurationNames;
31 import org.apache.archiva.configuration.FileTypes;
32 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
33 // import org.apache.archiva.maven2.metadata.MavenMetadataReader;
34 import org.apache.archiva.model.ArchivaRepositoryMetadata;
35 import org.apache.archiva.model.ArtifactReference;
36 import org.apache.archiva.model.Plugin;
37 import org.apache.archiva.model.ProjectReference;
38 import org.apache.archiva.model.SnapshotVersion;
39 import org.apache.archiva.model.VersionedReference;
40 import org.apache.archiva.components.registry.Registry;
41 import org.apache.archiva.components.registry.RegistryListener;
42 import org.apache.archiva.repository.ContentNotFoundException;
43 import org.apache.archiva.repository.ManagedRepositoryContent;
44 import org.apache.archiva.repository.LayoutException;
45 import org.apache.archiva.repository.BaseRepositoryContentLayout;
46 import org.apache.archiva.repository.RemoteRepositoryContent;
47 import org.apache.archiva.repository.RepositoryRegistry;
48 import org.apache.archiva.repository.RepositoryType;
49 import org.apache.archiva.repository.content.Artifact;
50 import org.apache.archiva.repository.content.ItemSelector;
51 import org.apache.archiva.repository.content.Project;
52 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
53 import org.apache.archiva.repository.metadata.MetadataReader;
54 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
55 import org.apache.archiva.repository.storage.StorageAsset;
56 import org.apache.commons.collections4.CollectionUtils;
57 import org.apache.commons.lang3.StringUtils;
58 import org.apache.commons.lang3.math.NumberUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61 import org.springframework.stereotype.Service;
63 import javax.annotation.PostConstruct;
64 import javax.inject.Inject;
65 import javax.inject.Named;
66 import java.io.IOException;
67 import java.nio.file.Files;
68 import java.nio.file.Path;
69 import java.nio.file.Paths;
70 import java.text.ParseException;
71 import java.text.SimpleDateFormat;
73 import java.util.regex.Matcher;
74 import java.util.stream.Collectors;
75 import java.util.stream.Stream;
82 @Service( "metadataTools#default" )
83 public class MetadataTools
84 implements RegistryListener, ConfigurationListener
86 private static final Logger log = LoggerFactory.getLogger( MetadataTools.class );
88 public static final String MAVEN_METADATA = "maven-metadata.xml";
90 public static final String MAVEN_ARCHETYPE_CATALOG ="archetype-catalog.xml";
92 private static final char PATH_SEPARATOR = '/';
94 private static final char GROUP_SEPARATOR = '.';
97 private RepositoryRegistry repositoryRegistry;
103 @Named( value = "archivaConfiguration#default" )
104 private ArchivaConfiguration configuration;
110 @Named( value = "fileTypes" )
111 private FileTypes filetypes;
113 private List<ChecksumAlgorithm> algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
115 private List<String> artifactPatterns;
117 private Map<String, Set<String>> proxies;
119 private static final char NUMS[] = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
121 private SimpleDateFormat lastUpdatedFormat;
123 public MetadataTools()
125 lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
126 lastUpdatedFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
130 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
132 if ( ConfigurationNames.isProxyConnector( propertyName ) )
134 initConfigVariables();
139 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
145 * Gather the set of snapshot versions found in a particular versioned reference.
147 * @return the Set of snapshot artifact versions found.
148 * @throws LayoutException
149 * @throws ContentNotFoundException
151 public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
152 VersionedReference reference )
153 throws LayoutException, IOException, ContentNotFoundException
155 Set<String> foundVersions = null;
158 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
159 .withNamespace( reference.getGroupId( ) )
160 .withProjectId( reference.getArtifactId( ) )
161 .withArtifactId( reference.getArtifactId( ) )
162 .withVersion( reference.getVersion( ) )
164 try(Stream<? extends Artifact> stream = managedRepository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector )) {
165 foundVersions = stream.map( a -> a.getArtifactVersion( ) )
166 .filter( StringUtils::isNotEmpty )
167 .collect( Collectors.toSet( ) );
170 catch ( org.apache.archiva.repository.ContentAccessException e )
172 log.error( "Error while accessing content {}", e.getMessage( ) );
173 throw new IOException( "Could not access repository content: " + e.getMessage( ) );
176 // Next gather up the referenced 'latest' versions found in any proxied repositories
177 // maven-metadata-${proxyId}.xml files that may be present.
179 // Does this repository have a set of remote proxied repositories?
180 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
182 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
184 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
185 baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
187 // Add in the proxied repo version ids too.
188 Iterator<String> it = proxiedRepoIds.iterator();
189 while ( it.hasNext() )
191 String proxyId = it.next();
193 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
194 if ( proxyMetadata == null )
196 // There is no proxy metadata, skip it.
200 // Is there some snapshot info?
201 SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
202 if ( snapshot != null )
204 String timestamp = snapshot.getTimestamp();
205 int buildNumber = snapshot.getBuildNumber();
207 // Only interested in the timestamp + buildnumber.
208 if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
210 foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
216 return foundVersions;
220 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
225 public VersionedReference toVersionedReference( String path )
226 throws RepositoryMetadataException
228 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
230 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
233 VersionedReference reference = new VersionedReference();
235 String normalizedPath = StringUtils.replace( path, "\\", "/" );
236 String pathParts[] = StringUtils.split( normalizedPath, '/' );
238 int versionOffset = pathParts.length - 2;
239 int artifactIdOffset = versionOffset - 1;
240 int groupIdEnd = artifactIdOffset - 1;
242 reference.setVersion( pathParts[versionOffset] );
244 if ( !hasNumberAnywhere( reference.getVersion() ) )
246 // Scary check, but without it, all paths are version references;
247 throw new RepositoryMetadataException(
248 "Not a versioned reference, as version id on path has no number in it." );
251 reference.setArtifactId( pathParts[artifactIdOffset] );
253 StringBuilder gid = new StringBuilder();
254 for ( int i = 0; i <= groupIdEnd; i++ )
260 gid.append( pathParts[i] );
263 reference.setGroupId( gid.toString() );
268 private boolean hasNumberAnywhere( String version )
270 return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
273 public ProjectReference toProjectReference( String path )
274 throws RepositoryMetadataException
276 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
278 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
281 ProjectReference reference = new ProjectReference();
283 String normalizedPath = StringUtils.replace( path, "\\", "/" );
284 String pathParts[] = StringUtils.split( normalizedPath, '/' );
286 // Assume last part of the path is the version.
288 int artifactIdOffset = pathParts.length - 2;
289 int groupIdEnd = artifactIdOffset - 1;
291 reference.setArtifactId( pathParts[artifactIdOffset] );
293 StringBuilder gid = new StringBuilder();
294 for ( int i = 0; i <= groupIdEnd; i++ )
300 gid.append( pathParts[i] );
303 reference.setGroupId( gid.toString() );
310 public String toPath( ProjectReference reference )
312 StringBuilder path = new StringBuilder();
314 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
315 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
316 path.append( MAVEN_METADATA );
318 return path.toString();
321 public String toPath( VersionedReference reference )
323 StringBuilder path = new StringBuilder();
325 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
326 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
327 if ( reference.getVersion() != null )
329 // add the version only if it is present
330 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
332 path.append( MAVEN_METADATA );
334 return path.toString();
337 private String formatAsDirectory( String directory )
339 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
343 * Adjusts a path for a metadata.xml file to its repository specific path.
345 * @param repository the repository to base new path off of.
346 * @param path the path to the metadata.xml file to adjust the name of.
347 * @return the newly adjusted path reference to the repository specific metadata path.
349 public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
351 return getRepositorySpecificName( repository.getId(), path );
355 * Adjusts a path for a metadata.xml file to its repository specific path.
357 * @param proxyId the repository id to base new path off of.
358 * @param path the path to the metadata.xml file to adjust the name of.
359 * @return the newly adjusted path reference to the repository specific metadata path.
361 public String getRepositorySpecificName( String proxyId, String path )
363 StringBuilder ret = new StringBuilder();
365 int idx = path.lastIndexOf( '/' );
368 ret.append( path.substring( 0, idx + 1 ) );
371 // TODO: need to filter out 'bad' characters from the proxy id.
372 ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
374 return ret.toString();
378 public void initialize()
380 assert(configuration != null);
381 this.artifactPatterns = new ArrayList<>();
382 this.proxies = new HashMap<>();
383 initConfigVariables();
385 configuration.addChangeListener( this );
386 configuration.addListener( this );
389 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
390 ProjectReference reference, String proxyId )
392 MetadataReader reader = getMetadataReader( managedRepository );
394 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
395 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
397 return readMetadataFile( managedRepository, metadataFile );
400 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
401 String logicalResource, String proxyId )
403 String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
404 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
405 return readMetadataFile( managedRepository, metadataFile );
408 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
409 VersionedReference reference, String proxyId )
411 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
412 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
413 return readMetadataFile( managedRepository, metadataFile );
416 public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
417 throws RepositoryMetadataException
419 final StorageAsset metadataFile = managedRepository.getRepository().getAsset( logicalResource );
420 ArchivaRepositoryMetadata metadata = null;
422 //Gather and merge all metadata available
423 List<ArchivaRepositoryMetadata> metadatas =
424 getMetadatasForManagedRepository( managedRepository, logicalResource );
425 for ( ArchivaRepositoryMetadata proxiedMetadata : metadatas )
427 if ( metadata == null )
429 metadata = proxiedMetadata;
432 metadata = RepositoryMetadataMerge.merge( metadata, proxiedMetadata );
435 if ( metadata == null )
437 log.debug( "No metadata to update for {}", logicalResource );
441 Set<String> availableVersions = new HashSet<String>();
442 List<String> metadataAvailableVersions = metadata.getAvailableVersions();
443 if ( metadataAvailableVersions != null )
445 availableVersions.addAll( metadataAvailableVersions );
447 availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() );
449 if ( availableVersions.size() > 0 )
451 updateMetadataVersions( availableVersions, metadata );
454 RepositoryMetadataWriter.write( metadata, metadataFile );
456 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
457 checksum.fixChecksums( algorithms );
461 * Skims the parent directory of a metadata in vain hope of finding
462 * subdirectories that contain poms.
464 * @param metadataParentDirectory
465 * @return origional set plus newly found versions
467 private Set<String> findPossibleVersions( Set<String> versions, StorageAsset metadataParentDirectory )
470 Set<String> result = new HashSet<String>( versions );
472 metadataParentDirectory.list().stream().filter(asset ->
473 asset.isContainer()).filter(asset -> {
474 return asset.list().stream().anyMatch(f -> !f.isContainer() && f.getName().endsWith(".pom"));
476 ).forEach( p -> result.add(p.getName()));
481 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(
482 ManagedRepositoryContent managedRepository, String logicalResource )
484 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
485 StorageAsset file = managedRepository.getRepository().getAsset( logicalResource );
489 ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, file );
490 if ( existingMetadata != null )
492 metadatas.add( existingMetadata );
496 Set<String> proxyIds = proxies.get( managedRepository.getId() );
497 if ( proxyIds != null )
499 for ( String proxyId : proxyIds )
501 ArchivaRepositoryMetadata proxyMetadata =
502 readProxyMetadata( managedRepository, logicalResource, proxyId );
503 if ( proxyMetadata != null )
505 metadatas.add( proxyMetadata );
515 * Update the metadata to represent the all versions/plugins of
516 * the provided groupId:artifactId project or group reference,
517 * based off of information present in the repository,
518 * the maven-metadata.xml files, and the proxy/repository specific
519 * metadata file contents.
521 * We must treat this as a group or a project metadata file as there is no way to know in advance
523 * @param managedRepository the managed repository where the metadata is kept.
524 * @param reference the reference to update.
525 * @throws LayoutException
526 * @throws RepositoryMetadataException
527 * @throws IOException
528 * @throws ContentNotFoundException
531 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
532 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
535 StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
536 ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, metadataFile );
537 BaseRepositoryContentLayout layout = managedRepository.getLayout( BaseRepositoryContentLayout.class );
539 long lastUpdated = getExistingLastUpdated( existingMetadata );
541 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
542 metadata.setGroupId( reference.getGroupId() );
543 metadata.setArtifactId( reference.getArtifactId() );
545 // Gather up all versions found in the managed repository.
546 ItemSelector selector = ArchivaItemSelector.builder( )
547 .withNamespace( reference.getGroupId( ) )
548 .withProjectId( reference.getArtifactId( ) )
550 Set<String> allVersions = null;
553 Project project = layout.getProject( selector );
554 allVersions = layout.getVersions( project ).stream()
555 .map( v -> v.getVersion() ).collect( Collectors.toSet());
557 catch ( org.apache.archiva.repository.ContentAccessException e )
559 log.error( "Error while accessing repository: {}", e.getMessage( ), e );
560 throw new RepositoryMetadataException( "Error while accessing repository " + e.getMessage( ), e );
563 // Gather up all plugins found in the managed repository.
564 // TODO: do we know this information instead?
565 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
566 Set<Plugin> allPlugins;
567 if ( existingMetadata!=null)
569 allPlugins = new LinkedHashSet<Plugin>( existingMetadata.getPlugins() );
573 allPlugins = new LinkedHashSet<Plugin>();
576 // Does this repository have a set of remote proxied repositories?
577 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
579 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
581 // Add in the proxied repo version ids too.
582 Iterator<String> it = proxiedRepoIds.iterator();
583 while ( it.hasNext() )
585 String proxyId = it.next();
587 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
588 if ( proxyMetadata != null )
590 allVersions.addAll( proxyMetadata.getAvailableVersions() );
591 allPlugins.addAll( proxyMetadata.getPlugins() );
592 long proxyLastUpdated = getLastUpdated( proxyMetadata );
594 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
599 if ( !allVersions.isEmpty() )
601 updateMetadataVersions( allVersions, metadata );
605 // Add the plugins to the metadata model.
606 metadata.setPlugins( new ArrayList<>( allPlugins ) );
608 // artifact ID was actually the last part of the group
609 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
610 metadata.setArtifactId( null );
613 if ( lastUpdated > 0 )
615 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
618 // Save the metadata model to disk.
619 RepositoryMetadataWriter.write( metadata, metadataFile );
620 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
621 checksum.fixChecksums( algorithms );
624 public MetadataReader getMetadataReader( ManagedRepositoryContent managedRepository )
626 if (managedRepository!=null)
628 return repositoryRegistry.getMetadataReader( managedRepository.getRepository( ).getType( ) );
630 return repositoryRegistry.getMetadataReader( RepositoryType.MAVEN );
634 private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
637 List<String> sortedVersions = new ArrayList<>( allVersions );
638 Collections.sort( sortedVersions, VersionComparator.getInstance() );
640 // Split the versions into released and snapshots.
641 List<String> releasedVersions = new ArrayList<>();
642 List<String> snapshotVersions = new ArrayList<>();
644 for ( String version : sortedVersions )
646 if ( VersionUtil.isSnapshot( version ) )
648 snapshotVersions.add( version );
652 releasedVersions.add( version );
656 Collections.sort( releasedVersions, VersionComparator.getInstance() );
657 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
659 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
660 String releaseVersion = null;
662 if ( CollectionUtils.isNotEmpty( releasedVersions ) )
664 releaseVersion = releasedVersions.get( releasedVersions.size() - 1 );
667 // Add the versions to the metadata model.
668 metadata.setAvailableVersions( sortedVersions );
670 metadata.setLatestVersion( latestVersion );
671 metadata.setReleasedVersion( releaseVersion );
674 private Date toLastUpdatedDate( long lastUpdated )
676 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("UTC") );
677 cal.setTimeInMillis( lastUpdated );
679 return cal.getTime();
682 private long toLastUpdatedLong( String timestampString )
686 Date date = lastUpdatedFormat.parse( timestampString );
687 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("UTC"));
690 return cal.getTimeInMillis();
692 catch ( ParseException e )
698 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
700 if ( metadata == null )
708 String lastUpdated = metadata.getLastUpdated();
709 if ( StringUtils.isBlank( lastUpdated ) )
715 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
716 return lastUpdatedDate.getTime();
718 catch ( ParseException e )
720 // Bad format on the last updated string.
725 ArchivaRepositoryMetadata readMetadataFile( ManagedRepositoryContent repository, StorageAsset asset) {
726 MetadataReader reader = getMetadataReader( repository );
729 if (asset.exists() && !asset.isContainer())
731 return reader.read( asset );
733 log.error( "Trying to read metadata from container: {}", asset.getPath( ) );
737 catch ( RepositoryMetadataException e )
739 log.error( "Could not read metadata file {}", asset, e );
744 private long getExistingLastUpdated( ArchivaRepositoryMetadata metadata )
746 if ( metadata==null )
752 return getLastUpdated( metadata );
756 * Update the metadata based on the following rules.
758 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
759 * metadata files to represent the current / latest SNAPSHOT available.
760 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
761 * create the metadata file with contents required of the VersionedReference
763 * @param managedRepository the managed repository where the metadata is kept.
764 * @param reference the versioned reference to update
765 * @throws LayoutException
766 * @throws RepositoryMetadataException
767 * @throws IOException
768 * @throws ContentNotFoundException
771 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
772 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
774 StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
775 ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile );
777 long lastUpdated = getExistingLastUpdated( existingMetadata );
779 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
780 metadata.setGroupId( reference.getGroupId() );
781 metadata.setArtifactId( reference.getArtifactId() );
783 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
785 // Do SNAPSHOT handling.
786 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
788 // Gather up all of the versions found in the reference dir, and any
789 // proxied maven-metadata.xml files.
790 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
792 if ( snapshotVersions.isEmpty() )
794 throw new ContentNotFoundException(
795 "No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
798 // sort the list to determine to aide in determining the Latest version.
799 List<String> sortedVersions = new ArrayList<>();
800 sortedVersions.addAll( snapshotVersions );
801 Collections.sort( sortedVersions, new VersionComparator() );
803 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
805 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
807 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
808 // This needs to be broken down into ${base}-${timestamp}-${build_number}
810 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
813 metadata.setSnapshotVersion( new SnapshotVersion() );
814 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
815 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
817 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
818 if ( mtimestamp.matches() )
820 String tsDate = mtimestamp.group( 1 );
821 String tsTime = mtimestamp.group( 2 );
823 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
825 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
827 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
831 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
833 // The latestVersion ends with the generic version string.
834 // Example: 1.0-alpha-5-SNAPSHOT
836 metadata.setSnapshotVersion( new SnapshotVersion() );
838 /* Disabled due to decision in [MRM-535].
839 * Do not set metadata.lastUpdated to file.lastModified.
841 * Should this be the last updated timestamp of the file, or in the case of an
842 * archive, the most recent timestamp in the archive?
844 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
846 if ( artifact == null )
848 throw new IOException( "Not snapshot artifact found to reference in " + reference );
851 File artifactFile = managedRepository.toFile( artifact );
853 if ( artifactFile.exists() )
855 Date lastModified = new Date( artifactFile.lastModified() );
856 metadata.setLastUpdatedTimestamp( lastModified );
862 throw new RepositoryMetadataException(
863 "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
868 // Do RELEASE handling.
869 metadata.setVersion( reference.getVersion() );
873 if ( lastUpdated > 0 )
875 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
878 // Save the metadata model to disk.
879 RepositoryMetadataWriter.write( metadata, metadataFile );
880 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
881 checksum.fixChecksums( algorithms );
884 private void initConfigVariables()
886 assert(this.artifactPatterns!=null);
887 assert(proxies!=null);
888 synchronized ( this.artifactPatterns )
890 this.artifactPatterns.clear();
892 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
895 synchronized ( proxies )
897 this.proxies.clear();
899 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
900 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
902 String key = proxyConfig.getSourceRepoId();
904 Set<String> remoteRepoIds = this.proxies.get( key );
906 if ( remoteRepoIds == null )
908 remoteRepoIds = new HashSet<String>();
911 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
913 this.proxies.put( key, remoteRepoIds );
919 * Get the first Artifact found in the provided VersionedReference location.
921 * @param managedRepository the repository to search within.
922 * @param reference the reference to the versioned reference to search within
923 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
924 * no artifact was found within the versioned reference.
925 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
926 * @throws LayoutException
928 public ArtifactReference getFirstArtifact( BaseRepositoryContentLayout managedRepository,
929 VersionedReference reference )
930 throws LayoutException, IOException
932 String path = toPath( reference );
934 int idx = path.lastIndexOf( '/' );
937 path = path.substring( 0, idx );
940 Path repoDir = Paths.get( managedRepository.getRepoRoot(), path );
942 if ( !Files.exists(repoDir))
944 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
945 + repoDir.toAbsolutePath() );
948 if ( !Files.isDirectory( repoDir ))
950 throw new IOException(
951 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
954 try(Stream<Path> stream = Files.list(repoDir)) {
955 String result = stream.filter( Files::isRegularFile ).map( path1 ->
956 PathUtil.getRelative( managedRepository.getRepoRoot(), path1 )
957 ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
959 return managedRepository.getGenericContent().toArtifactReference( result );
962 // No artifact was found.
966 public ArchivaConfiguration getConfiguration()
968 return configuration;
971 public void setConfiguration( ArchivaConfiguration configuration )
973 this.configuration = configuration;
976 public FileTypes getFiletypes()
981 public void setFiletypes( FileTypes filetypes )
983 this.filetypes = filetypes;
987 public void configurationEvent( ConfigurationEvent event )
989 log.debug( "Configuration event {}", event );