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.LayoutException;
44 import org.apache.archiva.repository.ManagedRepositoryContent;
45 import org.apache.archiva.repository.RemoteRepositoryContent;
46 import org.apache.archiva.repository.RepositoryRegistry;
47 import org.apache.archiva.repository.RepositoryType;
48 import org.apache.archiva.repository.metadata.MetadataReader;
49 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
50 import org.apache.archiva.repository.storage.StorageAsset;
51 import org.apache.commons.collections4.CollectionUtils;
52 import org.apache.commons.lang3.StringUtils;
53 import org.apache.commons.lang3.math.NumberUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.stereotype.Service;
58 import javax.annotation.PostConstruct;
59 import javax.inject.Inject;
60 import javax.inject.Named;
61 import java.io.IOException;
62 import java.nio.file.Files;
63 import java.nio.file.Path;
64 import java.nio.file.Paths;
65 import java.text.ParseException;
66 import java.text.SimpleDateFormat;
68 import java.util.regex.Matcher;
69 import java.util.stream.Stream;
76 @Service( "metadataTools#default" )
77 public class MetadataTools
78 implements RegistryListener, ConfigurationListener
80 private static final Logger log = LoggerFactory.getLogger( MetadataTools.class );
82 public static final String MAVEN_METADATA = "maven-metadata.xml";
84 public static final String MAVEN_ARCHETYPE_CATALOG ="archetype-catalog.xml";
86 private static final char PATH_SEPARATOR = '/';
88 private static final char GROUP_SEPARATOR = '.';
91 private RepositoryRegistry repositoryRegistry;
97 @Named( value = "archivaConfiguration#default" )
98 private ArchivaConfiguration configuration;
104 @Named( value = "fileTypes" )
105 private FileTypes filetypes;
107 private List<ChecksumAlgorithm> algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
109 private List<String> artifactPatterns;
111 private Map<String, Set<String>> proxies;
113 private static final char NUMS[] = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
115 private SimpleDateFormat lastUpdatedFormat;
117 public MetadataTools()
119 lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
120 lastUpdatedFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
124 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
126 if ( ConfigurationNames.isProxyConnector( propertyName ) )
128 initConfigVariables();
133 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
139 * Gather the set of snapshot versions found in a particular versioned reference.
141 * @return the Set of snapshot artifact versions found.
142 * @throws LayoutException
143 * @throws ContentNotFoundException
145 public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
146 VersionedReference reference )
147 throws LayoutException, IOException, ContentNotFoundException
149 Set<String> foundVersions = null;
152 foundVersions = managedRepository.getVersions( reference );
154 catch ( org.apache.archiva.repository.ContentAccessException e )
156 e.printStackTrace( );
159 // Next gather up the referenced 'latest' versions found in any proxied repositories
160 // maven-metadata-${proxyId}.xml files that may be present.
162 // Does this repository have a set of remote proxied repositories?
163 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
165 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
167 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
168 baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
170 // Add in the proxied repo version ids too.
171 Iterator<String> it = proxiedRepoIds.iterator();
172 while ( it.hasNext() )
174 String proxyId = it.next();
176 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
177 if ( proxyMetadata == null )
179 // There is no proxy metadata, skip it.
183 // Is there some snapshot info?
184 SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
185 if ( snapshot != null )
187 String timestamp = snapshot.getTimestamp();
188 int buildNumber = snapshot.getBuildNumber();
190 // Only interested in the timestamp + buildnumber.
191 if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
193 foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
199 return foundVersions;
203 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
208 public VersionedReference toVersionedReference( String path )
209 throws RepositoryMetadataException
211 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
213 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
216 VersionedReference reference = new VersionedReference();
218 String normalizedPath = StringUtils.replace( path, "\\", "/" );
219 String pathParts[] = StringUtils.split( normalizedPath, '/' );
221 int versionOffset = pathParts.length - 2;
222 int artifactIdOffset = versionOffset - 1;
223 int groupIdEnd = artifactIdOffset - 1;
225 reference.setVersion( pathParts[versionOffset] );
227 if ( !hasNumberAnywhere( reference.getVersion() ) )
229 // Scary check, but without it, all paths are version references;
230 throw new RepositoryMetadataException(
231 "Not a versioned reference, as version id on path has no number in it." );
234 reference.setArtifactId( pathParts[artifactIdOffset] );
236 StringBuilder gid = new StringBuilder();
237 for ( int i = 0; i <= groupIdEnd; i++ )
243 gid.append( pathParts[i] );
246 reference.setGroupId( gid.toString() );
251 private boolean hasNumberAnywhere( String version )
253 return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
256 public ProjectReference toProjectReference( String path )
257 throws RepositoryMetadataException
259 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
261 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
264 ProjectReference reference = new ProjectReference();
266 String normalizedPath = StringUtils.replace( path, "\\", "/" );
267 String pathParts[] = StringUtils.split( normalizedPath, '/' );
269 // Assume last part of the path is the version.
271 int artifactIdOffset = pathParts.length - 2;
272 int groupIdEnd = artifactIdOffset - 1;
274 reference.setArtifactId( pathParts[artifactIdOffset] );
276 StringBuilder gid = new StringBuilder();
277 for ( int i = 0; i <= groupIdEnd; i++ )
283 gid.append( pathParts[i] );
286 reference.setGroupId( gid.toString() );
293 public String toPath( ProjectReference reference )
295 StringBuilder path = new StringBuilder();
297 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
298 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
299 path.append( MAVEN_METADATA );
301 return path.toString();
304 public String toPath( VersionedReference reference )
306 StringBuilder path = new StringBuilder();
308 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
309 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
310 if ( reference.getVersion() != null )
312 // add the version only if it is present
313 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
315 path.append( MAVEN_METADATA );
317 return path.toString();
320 private String formatAsDirectory( String directory )
322 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
326 * Adjusts a path for a metadata.xml file to its repository specific path.
328 * @param repository the repository to base new path off of.
329 * @param path the path to the metadata.xml file to adjust the name of.
330 * @return the newly adjusted path reference to the repository specific metadata path.
332 public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
334 return getRepositorySpecificName( repository.getId(), path );
338 * Adjusts a path for a metadata.xml file to its repository specific path.
340 * @param proxyId the repository id to base new path off of.
341 * @param path the path to the metadata.xml file to adjust the name of.
342 * @return the newly adjusted path reference to the repository specific metadata path.
344 public String getRepositorySpecificName( String proxyId, String path )
346 StringBuilder ret = new StringBuilder();
348 int idx = path.lastIndexOf( '/' );
351 ret.append( path.substring( 0, idx + 1 ) );
354 // TODO: need to filter out 'bad' characters from the proxy id.
355 ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
357 return ret.toString();
361 public void initialize()
363 assert(configuration != null);
364 this.artifactPatterns = new ArrayList<>();
365 this.proxies = new HashMap<>();
366 initConfigVariables();
368 configuration.addChangeListener( this );
369 configuration.addListener( this );
372 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
373 ProjectReference reference, String proxyId )
375 MetadataReader reader = getMetadataReader( managedRepository );
377 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
378 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
380 return readMetadataFile( managedRepository, metadataFile );
383 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
384 String logicalResource, String proxyId )
386 String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
387 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
388 return readMetadataFile( managedRepository, metadataFile );
391 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
392 VersionedReference reference, String proxyId )
394 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
395 StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
396 return readMetadataFile( managedRepository, metadataFile );
399 public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
400 throws RepositoryMetadataException
402 final StorageAsset metadataFile = managedRepository.getRepository().getAsset( logicalResource );
403 ArchivaRepositoryMetadata metadata = null;
405 //Gather and merge all metadata available
406 List<ArchivaRepositoryMetadata> metadatas =
407 getMetadatasForManagedRepository( managedRepository, logicalResource );
408 for ( ArchivaRepositoryMetadata proxiedMetadata : metadatas )
410 if ( metadata == null )
412 metadata = proxiedMetadata;
415 metadata = RepositoryMetadataMerge.merge( metadata, proxiedMetadata );
418 if ( metadata == null )
420 log.debug( "No metadata to update for {}", logicalResource );
424 Set<String> availableVersions = new HashSet<String>();
425 List<String> metadataAvailableVersions = metadata.getAvailableVersions();
426 if ( metadataAvailableVersions != null )
428 availableVersions.addAll( metadataAvailableVersions );
430 availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() );
432 if ( availableVersions.size() > 0 )
434 updateMetadataVersions( availableVersions, metadata );
437 RepositoryMetadataWriter.write( metadata, metadataFile );
439 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
440 checksum.fixChecksums( algorithms );
444 * Skims the parent directory of a metadata in vain hope of finding
445 * subdirectories that contain poms.
447 * @param metadataParentDirectory
448 * @return origional set plus newly found versions
450 private Set<String> findPossibleVersions( Set<String> versions, StorageAsset metadataParentDirectory )
453 Set<String> result = new HashSet<String>( versions );
455 metadataParentDirectory.list().stream().filter(asset ->
456 asset.isContainer()).filter(asset -> {
457 return asset.list().stream().anyMatch(f -> !f.isContainer() && f.getName().endsWith(".pom"));
459 ).forEach( p -> result.add(p.getName()));
464 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(
465 ManagedRepositoryContent managedRepository, String logicalResource )
467 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
468 StorageAsset file = managedRepository.getRepository().getAsset( logicalResource );
472 ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, file );
473 if ( existingMetadata != null )
475 metadatas.add( existingMetadata );
479 Set<String> proxyIds = proxies.get( managedRepository.getId() );
480 if ( proxyIds != null )
482 for ( String proxyId : proxyIds )
484 ArchivaRepositoryMetadata proxyMetadata =
485 readProxyMetadata( managedRepository, logicalResource, proxyId );
486 if ( proxyMetadata != null )
488 metadatas.add( proxyMetadata );
498 * Update the metadata to represent the all versions/plugins of
499 * the provided groupId:artifactId project or group reference,
500 * based off of information present in the repository,
501 * the maven-metadata.xml files, and the proxy/repository specific
502 * metadata file contents.
504 * We must treat this as a group or a project metadata file as there is no way to know in advance
506 * @param managedRepository the managed repository where the metadata is kept.
507 * @param reference the reference to update.
508 * @throws LayoutException
509 * @throws RepositoryMetadataException
510 * @throws IOException
511 * @throws ContentNotFoundException
514 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
515 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
518 StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
519 ArchivaRepositoryMetadata existingMetadata = readMetadataFile( managedRepository, metadataFile );
521 long lastUpdated = getExistingLastUpdated( existingMetadata );
523 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
524 metadata.setGroupId( reference.getGroupId() );
525 metadata.setArtifactId( reference.getArtifactId() );
527 // Gather up all versions found in the managed repository.
528 Set<String> allVersions = null;
531 allVersions = managedRepository.getVersions( reference );
533 catch ( org.apache.archiva.repository.ContentAccessException e )
535 e.printStackTrace( );
538 // Gather up all plugins found in the managed repository.
539 // TODO: do we know this information instead?
540 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
541 Set<Plugin> allPlugins;
542 if ( existingMetadata!=null)
544 allPlugins = new LinkedHashSet<Plugin>( existingMetadata.getPlugins() );
548 allPlugins = new LinkedHashSet<Plugin>();
551 // Does this repository have a set of remote proxied repositories?
552 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
554 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
556 // Add in the proxied repo version ids too.
557 Iterator<String> it = proxiedRepoIds.iterator();
558 while ( it.hasNext() )
560 String proxyId = it.next();
562 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
563 if ( proxyMetadata != null )
565 allVersions.addAll( proxyMetadata.getAvailableVersions() );
566 allPlugins.addAll( proxyMetadata.getPlugins() );
567 long proxyLastUpdated = getLastUpdated( proxyMetadata );
569 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
574 if ( !allVersions.isEmpty() )
576 updateMetadataVersions( allVersions, metadata );
580 // Add the plugins to the metadata model.
581 metadata.setPlugins( new ArrayList<>( allPlugins ) );
583 // artifact ID was actually the last part of the group
584 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
585 metadata.setArtifactId( null );
588 if ( lastUpdated > 0 )
590 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
593 // Save the metadata model to disk.
594 RepositoryMetadataWriter.write( metadata, metadataFile );
595 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
596 checksum.fixChecksums( algorithms );
599 public MetadataReader getMetadataReader( ManagedRepositoryContent managedRepository )
601 if (managedRepository!=null)
603 return repositoryRegistry.getMetadataReader( managedRepository.getRepository( ).getType( ) );
605 return repositoryRegistry.getMetadataReader( RepositoryType.MAVEN );
609 private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
612 List<String> sortedVersions = new ArrayList<>( allVersions );
613 Collections.sort( sortedVersions, VersionComparator.getInstance() );
615 // Split the versions into released and snapshots.
616 List<String> releasedVersions = new ArrayList<>();
617 List<String> snapshotVersions = new ArrayList<>();
619 for ( String version : sortedVersions )
621 if ( VersionUtil.isSnapshot( version ) )
623 snapshotVersions.add( version );
627 releasedVersions.add( version );
631 Collections.sort( releasedVersions, VersionComparator.getInstance() );
632 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
634 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
635 String releaseVersion = null;
637 if ( CollectionUtils.isNotEmpty( releasedVersions ) )
639 releaseVersion = releasedVersions.get( releasedVersions.size() - 1 );
642 // Add the versions to the metadata model.
643 metadata.setAvailableVersions( sortedVersions );
645 metadata.setLatestVersion( latestVersion );
646 metadata.setReleasedVersion( releaseVersion );
649 private Date toLastUpdatedDate( long lastUpdated )
651 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("UTC") );
652 cal.setTimeInMillis( lastUpdated );
654 return cal.getTime();
657 private long toLastUpdatedLong( String timestampString )
661 Date date = lastUpdatedFormat.parse( timestampString );
662 Calendar cal = Calendar.getInstance( TimeZone.getTimeZone("UTC"));
665 return cal.getTimeInMillis();
667 catch ( ParseException e )
673 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
675 if ( metadata == null )
683 String lastUpdated = metadata.getLastUpdated();
684 if ( StringUtils.isBlank( lastUpdated ) )
690 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
691 return lastUpdatedDate.getTime();
693 catch ( ParseException e )
695 // Bad format on the last updated string.
700 ArchivaRepositoryMetadata readMetadataFile( ManagedRepositoryContent repository, StorageAsset asset) {
701 MetadataReader reader = getMetadataReader( repository );
704 if (asset.exists() && !asset.isContainer())
706 return reader.read( asset );
708 log.error( "Trying to read metadata from container: {}", asset.getPath( ) );
712 catch ( RepositoryMetadataException e )
714 log.error( "Could not read metadata file {}", asset, e );
719 private long getExistingLastUpdated( ArchivaRepositoryMetadata metadata )
721 if ( metadata==null )
727 return getLastUpdated( metadata );
731 * Update the metadata based on the following rules.
733 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
734 * metadata files to represent the current / latest SNAPSHOT available.
735 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
736 * create the metadata file with contents required of the VersionedReference
738 * @param managedRepository the managed repository where the metadata is kept.
739 * @param reference the versioned reference to update
740 * @throws LayoutException
741 * @throws RepositoryMetadataException
742 * @throws IOException
743 * @throws ContentNotFoundException
746 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
747 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
749 StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
750 ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile );
752 long lastUpdated = getExistingLastUpdated( existingMetadata );
754 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
755 metadata.setGroupId( reference.getGroupId() );
756 metadata.setArtifactId( reference.getArtifactId() );
758 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
760 // Do SNAPSHOT handling.
761 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
763 // Gather up all of the versions found in the reference dir, and any
764 // proxied maven-metadata.xml files.
765 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
767 if ( snapshotVersions.isEmpty() )
769 throw new ContentNotFoundException(
770 "No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
773 // sort the list to determine to aide in determining the Latest version.
774 List<String> sortedVersions = new ArrayList<>();
775 sortedVersions.addAll( snapshotVersions );
776 Collections.sort( sortedVersions, new VersionComparator() );
778 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
780 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
782 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
783 // This needs to be broken down into ${base}-${timestamp}-${build_number}
785 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
788 metadata.setSnapshotVersion( new SnapshotVersion() );
789 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
790 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
792 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
793 if ( mtimestamp.matches() )
795 String tsDate = mtimestamp.group( 1 );
796 String tsTime = mtimestamp.group( 2 );
798 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
800 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
802 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
806 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
808 // The latestVersion ends with the generic version string.
809 // Example: 1.0-alpha-5-SNAPSHOT
811 metadata.setSnapshotVersion( new SnapshotVersion() );
813 /* Disabled due to decision in [MRM-535].
814 * Do not set metadata.lastUpdated to file.lastModified.
816 * Should this be the last updated timestamp of the file, or in the case of an
817 * archive, the most recent timestamp in the archive?
819 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
821 if ( artifact == null )
823 throw new IOException( "Not snapshot artifact found to reference in " + reference );
826 File artifactFile = managedRepository.toFile( artifact );
828 if ( artifactFile.exists() )
830 Date lastModified = new Date( artifactFile.lastModified() );
831 metadata.setLastUpdatedTimestamp( lastModified );
837 throw new RepositoryMetadataException(
838 "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
843 // Do RELEASE handling.
844 metadata.setVersion( reference.getVersion() );
848 if ( lastUpdated > 0 )
850 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
853 // Save the metadata model to disk.
854 RepositoryMetadataWriter.write( metadata, metadataFile );
855 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
856 checksum.fixChecksums( algorithms );
859 private void initConfigVariables()
861 assert(this.artifactPatterns!=null);
862 assert(proxies!=null);
863 synchronized ( this.artifactPatterns )
865 this.artifactPatterns.clear();
867 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
870 synchronized ( proxies )
872 this.proxies.clear();
874 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
875 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
877 String key = proxyConfig.getSourceRepoId();
879 Set<String> remoteRepoIds = this.proxies.get( key );
881 if ( remoteRepoIds == null )
883 remoteRepoIds = new HashSet<String>();
886 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
888 this.proxies.put( key, remoteRepoIds );
894 * Get the first Artifact found in the provided VersionedReference location.
896 * @param managedRepository the repository to search within.
897 * @param reference the reference to the versioned reference to search within
898 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
899 * no artifact was found within the versioned reference.
900 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
901 * @throws LayoutException
903 public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository,
904 VersionedReference reference )
905 throws LayoutException, IOException
907 String path = toPath( reference );
909 int idx = path.lastIndexOf( '/' );
912 path = path.substring( 0, idx );
915 Path repoDir = Paths.get( managedRepository.getRepoRoot(), path );
917 if ( !Files.exists(repoDir))
919 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
920 + repoDir.toAbsolutePath() );
923 if ( !Files.isDirectory( repoDir ))
925 throw new IOException(
926 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
929 try(Stream<Path> stream = Files.list(repoDir)) {
930 String result = stream.filter( Files::isRegularFile ).map( path1 ->
931 PathUtil.getRelative( managedRepository.getRepoRoot(), path1 )
932 ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
934 return managedRepository.toArtifactReference( result );
937 // No artifact was found.
941 public ArchivaConfiguration getConfiguration()
943 return configuration;
946 public void setConfiguration( ArchivaConfiguration configuration )
948 this.configuration = configuration;
951 public FileTypes getFiletypes()
956 public void setFiletypes( FileTypes filetypes )
958 this.filetypes = filetypes;
962 public void configurationEvent( ConfigurationEvent event )
964 log.debug( "Configuration event {}", event );