1 package org.apache.archiva.repository.metadata;
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.FileUtils;
25 import org.apache.archiva.common.utils.PathUtil;
26 import org.apache.archiva.common.utils.VersionComparator;
27 import org.apache.archiva.common.utils.VersionUtil;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.ConfigurationNames;
30 import org.apache.archiva.configuration.FileTypes;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
33 import org.apache.archiva.model.*;
34 import org.apache.archiva.redback.components.registry.Registry;
35 import org.apache.archiva.redback.components.registry.RegistryListener;
36 import org.apache.archiva.repository.ContentNotFoundException;
37 import org.apache.archiva.repository.ManagedRepositoryContent;
38 import org.apache.archiva.repository.RemoteRepositoryContent;
39 import org.apache.archiva.repository.layout.LayoutException;
40 import org.apache.archiva.xml.XMLException;
41 import org.apache.commons.collections.CollectionUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.commons.lang.math.NumberUtils;
44 import org.apache.commons.lang.time.DateUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Service;
49 import javax.annotation.PostConstruct;
50 import javax.inject.Inject;
51 import javax.inject.Named;
52 import java.io.IOException;
53 import java.nio.file.Files;
54 import java.nio.file.Path;
55 import java.nio.file.Paths;
56 import java.text.ParseException;
57 import java.text.SimpleDateFormat;
59 import java.util.regex.Matcher;
60 import java.util.stream.Stream;
67 @Service( "metadataTools#default" )
68 public class MetadataTools
69 implements RegistryListener
71 private Logger log = LoggerFactory.getLogger( getClass() );
73 public static final String MAVEN_METADATA = "maven-metadata.xml";
75 public static final String MAVEN_ARCHETYPE_CATALOG ="archetype-catalog.xml";
77 private static final char PATH_SEPARATOR = '/';
79 private static final char GROUP_SEPARATOR = '.';
85 @Named( value = "archivaConfiguration#default" )
86 private ArchivaConfiguration configuration;
92 @Named( value = "fileTypes" )
93 private FileTypes filetypes;
95 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
97 private List<String> artifactPatterns;
99 private Map<String, Set<String>> proxies;
101 private static final char NUMS[] = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
103 private SimpleDateFormat lastUpdatedFormat;
105 public MetadataTools()
107 lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
108 lastUpdatedFormat.setTimeZone( DateUtils.UTC_TIME_ZONE );
112 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
114 if ( ConfigurationNames.isProxyConnector( propertyName ) )
116 initConfigVariables();
121 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
127 * Gather the set of snapshot versions found in a particular versioned reference.
129 * @return the Set of snapshot artifact versions found.
130 * @throws LayoutException
131 * @throws ContentNotFoundException
133 public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
134 VersionedReference reference )
135 throws LayoutException, IOException, ContentNotFoundException
137 Set<String> foundVersions = managedRepository.getVersions( reference );
139 // Next gather up the referenced 'latest' versions found in any proxied repositories
140 // maven-metadata-${proxyId}.xml files that may be present.
142 // Does this repository have a set of remote proxied repositories?
143 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
145 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
147 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
148 baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
150 // Add in the proxied repo version ids too.
151 Iterator<String> it = proxiedRepoIds.iterator();
152 while ( it.hasNext() )
154 String proxyId = it.next();
156 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
157 if ( proxyMetadata == null )
159 // There is no proxy metadata, skip it.
163 // Is there some snapshot info?
164 SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
165 if ( snapshot != null )
167 String timestamp = snapshot.getTimestamp();
168 int buildNumber = snapshot.getBuildNumber();
170 // Only interested in the timestamp + buildnumber.
171 if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
173 foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
179 return foundVersions;
183 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
188 public VersionedReference toVersionedReference( String path )
189 throws RepositoryMetadataException
191 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
193 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
196 VersionedReference reference = new VersionedReference();
198 String normalizedPath = StringUtils.replace( path, "\\", "/" );
199 String pathParts[] = StringUtils.split( normalizedPath, '/' );
201 int versionOffset = pathParts.length - 2;
202 int artifactIdOffset = versionOffset - 1;
203 int groupIdEnd = artifactIdOffset - 1;
205 reference.setVersion( pathParts[versionOffset] );
207 if ( !hasNumberAnywhere( reference.getVersion() ) )
209 // Scary check, but without it, all paths are version references;
210 throw new RepositoryMetadataException(
211 "Not a versioned reference, as version id on path has no number in it." );
214 reference.setArtifactId( pathParts[artifactIdOffset] );
216 StringBuilder gid = new StringBuilder();
217 for ( int i = 0; i <= groupIdEnd; i++ )
223 gid.append( pathParts[i] );
226 reference.setGroupId( gid.toString() );
231 private boolean hasNumberAnywhere( String version )
233 return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
236 public ProjectReference toProjectReference( String path )
237 throws RepositoryMetadataException
239 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
241 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
244 ProjectReference reference = new ProjectReference();
246 String normalizedPath = StringUtils.replace( path, "\\", "/" );
247 String pathParts[] = StringUtils.split( normalizedPath, '/' );
249 // Assume last part of the path is the version.
251 int artifactIdOffset = pathParts.length - 2;
252 int groupIdEnd = artifactIdOffset - 1;
254 reference.setArtifactId( pathParts[artifactIdOffset] );
256 StringBuilder gid = new StringBuilder();
257 for ( int i = 0; i <= groupIdEnd; i++ )
263 gid.append( pathParts[i] );
266 reference.setGroupId( gid.toString() );
271 public String toPath( ProjectReference reference )
273 StringBuilder path = new StringBuilder();
275 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
276 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
277 path.append( MAVEN_METADATA );
279 return path.toString();
282 public String toPath( VersionedReference reference )
284 StringBuilder path = new StringBuilder();
286 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
287 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
288 if ( reference.getVersion() != null )
290 // add the version only if it is present
291 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
293 path.append( MAVEN_METADATA );
295 return path.toString();
298 private String formatAsDirectory( String directory )
300 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
304 * Adjusts a path for a metadata.xml file to its repository specific path.
306 * @param repository the repository to base new path off of.
307 * @param path the path to the metadata.xml file to adjust the name of.
308 * @return the newly adjusted path reference to the repository specific metadata path.
310 public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
312 return getRepositorySpecificName( repository.getId(), path );
316 * Adjusts a path for a metadata.xml file to its repository specific path.
318 * @param proxyId the repository id to base new path off of.
319 * @param path the path to the metadata.xml file to adjust the name of.
320 * @return the newly adjusted path reference to the repository specific metadata path.
322 public String getRepositorySpecificName( String proxyId, String path )
324 StringBuilder ret = new StringBuilder();
326 int idx = path.lastIndexOf( '/' );
329 ret.append( path.substring( 0, idx + 1 ) );
332 // TODO: need to filter out 'bad' characters from the proxy id.
333 ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
335 return ret.toString();
339 public void initialize()
341 this.artifactPatterns = new ArrayList<>();
342 this.proxies = new HashMap<>();
343 initConfigVariables();
345 configuration.addChangeListener( this );
348 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
349 ProjectReference reference, String proxyId )
351 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
352 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
354 if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile ))
356 // Nothing to do. return null.
362 return MavenMetadataReader.read( metadataFile );
364 catch ( XMLException e )
366 // TODO: [monitor] consider a monitor for this event.
367 // TODO: consider a read-redo on monitor return code?
368 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
373 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
374 String logicalResource, String proxyId )
376 String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
377 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
379 if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile))
381 // Nothing to do. return null.
387 return MavenMetadataReader.read( metadataFile );
389 catch ( XMLException e )
391 // TODO: [monitor] consider a monitor for this event.
392 // TODO: consider a read-redo on monitor return code?
393 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
398 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
399 VersionedReference reference, String proxyId )
401 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
402 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
404 if ( !Files.exists(metadataFile) || !Files.isRegularFile(metadataFile))
406 // Nothing to do. return null.
412 return MavenMetadataReader.read( metadataFile );
414 catch ( XMLException e )
416 // TODO: [monitor] consider a monitor for this event.
417 // TODO: consider a read-redo on monitor return code?
418 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
423 public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
424 throws RepositoryMetadataException
426 final Path metadataFile = Paths.get( managedRepository.getRepoRoot(), logicalResource );
427 ArchivaRepositoryMetadata metadata = null;
429 //Gather and merge all metadata available
430 List<ArchivaRepositoryMetadata> metadatas =
431 getMetadatasForManagedRepository( managedRepository, logicalResource );
432 for ( ArchivaRepositoryMetadata proxiedMetadata : metadatas )
434 if ( metadata == null )
436 metadata = proxiedMetadata;
439 metadata = RepositoryMetadataMerge.merge( metadata, proxiedMetadata );
442 if ( metadata == null )
444 log.debug( "No metadata to update for {}", logicalResource );
448 Set<String> availableVersions = new HashSet<String>();
449 List<String> metadataAvailableVersions = metadata.getAvailableVersions();
450 if ( metadataAvailableVersions != null )
452 availableVersions.addAll( metadataAvailableVersions );
454 availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() );
456 if ( availableVersions.size() > 0 )
458 updateMetadataVersions( availableVersions, metadata );
461 RepositoryMetadataWriter.write( metadata, metadataFile );
463 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
464 checksum.fixChecksums( algorithms );
468 * Skims the parent directory of a metadata in vain hope of finding
469 * subdirectories that contain poms.
471 * @param metadataParentDirectory
472 * @return origional set plus newly found versions
474 private Set<String> findPossibleVersions( Set<String> versions, Path metadataParentDirectory )
477 Set<String> result = new HashSet<String>( versions );
479 try (Stream<Path> stream = Files.list( metadataParentDirectory )) {
480 stream.filter( Files::isDirectory ).filter(
483 try(Stream<Path> substream = Files.list(p))
485 return substream.anyMatch( f -> Files.isRegularFile( f ) && f.toString().endsWith( ".pom" ));
487 catch ( IOException e )
493 p -> result.add(p.getFileName().toString())
495 } catch (IOException e) {
501 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(
502 ManagedRepositoryContent managedRepository, String logicalResource )
504 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
505 Path file = Paths.get( managedRepository.getRepoRoot(), logicalResource );
506 if ( Files.exists(file) )
510 ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file );
511 if ( existingMetadata != null )
513 metadatas.add( existingMetadata );
516 catch ( XMLException e )
518 log.debug( "Could not read metadata at {}. Metadata will be removed.", file.toAbsolutePath() );
519 FileUtils.deleteQuietly( file );
523 Set<String> proxyIds = proxies.get( managedRepository.getId() );
524 if ( proxyIds != null )
526 for ( String proxyId : proxyIds )
528 ArchivaRepositoryMetadata proxyMetadata =
529 readProxyMetadata( managedRepository, logicalResource, proxyId );
530 if ( proxyMetadata != null )
532 metadatas.add( proxyMetadata );
542 * Update the metadata to represent the all versions/plugins of
543 * the provided groupId:artifactId project or group reference,
544 * based off of information present in the repository,
545 * the maven-metadata.xml files, and the proxy/repository specific
546 * metadata file contents.
548 * We must treat this as a group or a project metadata file as there is no way to know in advance
550 * @param managedRepository the managed repository where the metadata is kept.
551 * @param reference the reference to update.
552 * @throws LayoutException
553 * @throws RepositoryMetadataException
554 * @throws IOException
555 * @throws ContentNotFoundException
558 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
559 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
561 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
563 long lastUpdated = getExistingLastUpdated( metadataFile );
565 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
566 metadata.setGroupId( reference.getGroupId() );
567 metadata.setArtifactId( reference.getArtifactId() );
569 // Gather up all versions found in the managed repository.
570 Set<String> allVersions = managedRepository.getVersions( reference );
572 // Gather up all plugins found in the managed repository.
573 // TODO: do we know this information instead?
574 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
575 Set<Plugin> allPlugins;
576 if ( Files.exists(metadataFile))
580 allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() );
582 catch ( XMLException e )
584 throw new RepositoryMetadataException( e.getMessage(), e );
589 allPlugins = new LinkedHashSet<Plugin>();
592 // Does this repository have a set of remote proxied repositories?
593 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
595 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
597 // Add in the proxied repo version ids too.
598 Iterator<String> it = proxiedRepoIds.iterator();
599 while ( it.hasNext() )
601 String proxyId = it.next();
603 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
604 if ( proxyMetadata != null )
606 allVersions.addAll( proxyMetadata.getAvailableVersions() );
607 allPlugins.addAll( proxyMetadata.getPlugins() );
608 long proxyLastUpdated = getLastUpdated( proxyMetadata );
610 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
615 if ( !allVersions.isEmpty() )
617 updateMetadataVersions( allVersions, metadata );
621 // Add the plugins to the metadata model.
622 metadata.setPlugins( new ArrayList<>( allPlugins ) );
624 // artifact ID was actually the last part of the group
625 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
626 metadata.setArtifactId( null );
629 if ( lastUpdated > 0 )
631 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
634 // Save the metadata model to disk.
635 RepositoryMetadataWriter.write( metadata, metadataFile );
636 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
637 checksum.fixChecksums( algorithms );
640 private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
643 List<String> sortedVersions = new ArrayList<>( allVersions );
644 Collections.sort( sortedVersions, VersionComparator.getInstance() );
646 // Split the versions into released and snapshots.
647 List<String> releasedVersions = new ArrayList<>();
648 List<String> snapshotVersions = new ArrayList<>();
650 for ( String version : sortedVersions )
652 if ( VersionUtil.isSnapshot( version ) )
654 snapshotVersions.add( version );
658 releasedVersions.add( version );
662 Collections.sort( releasedVersions, VersionComparator.getInstance() );
663 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
665 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
666 String releaseVersion = null;
668 if ( CollectionUtils.isNotEmpty( releasedVersions ) )
670 releaseVersion = releasedVersions.get( releasedVersions.size() - 1 );
673 // Add the versions to the metadata model.
674 metadata.setAvailableVersions( sortedVersions );
676 metadata.setLatestVersion( latestVersion );
677 metadata.setReleasedVersion( releaseVersion );
680 private Date toLastUpdatedDate( long lastUpdated )
682 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
683 cal.setTimeInMillis( lastUpdated );
685 return cal.getTime();
688 private long toLastUpdatedLong( String timestampString )
692 Date date = lastUpdatedFormat.parse( timestampString );
693 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
696 return cal.getTimeInMillis();
698 catch ( ParseException e )
704 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
706 if ( metadata == null )
714 String lastUpdated = metadata.getLastUpdated();
715 if ( StringUtils.isBlank( lastUpdated ) )
721 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
722 return lastUpdatedDate.getTime();
724 catch ( ParseException e )
726 // Bad format on the last updated string.
731 private long getExistingLastUpdated( Path metadataFile )
733 if ( !Files.exists(metadataFile) )
741 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
743 return getLastUpdated( metadata );
745 catch ( XMLException e )
753 * Update the metadata based on the following rules.
755 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
756 * metadata files to represent the current / latest SNAPSHOT available.
757 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
758 * create the metadata file with contents required of the VersionedReference
760 * @param managedRepository the managed repository where the metadata is kept.
761 * @param reference the versioned reference to update
762 * @throws LayoutException
763 * @throws RepositoryMetadataException
764 * @throws IOException
765 * @throws ContentNotFoundException
768 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
769 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
771 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
773 long lastUpdated = getExistingLastUpdated( metadataFile );
775 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
776 metadata.setGroupId( reference.getGroupId() );
777 metadata.setArtifactId( reference.getArtifactId() );
779 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
781 // Do SNAPSHOT handling.
782 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
784 // Gather up all of the versions found in the reference dir, and any
785 // proxied maven-metadata.xml files.
786 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
788 if ( snapshotVersions.isEmpty() )
790 throw new ContentNotFoundException(
791 "No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
794 // sort the list to determine to aide in determining the Latest version.
795 List<String> sortedVersions = new ArrayList<>();
796 sortedVersions.addAll( snapshotVersions );
797 Collections.sort( sortedVersions, new VersionComparator() );
799 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
801 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
803 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
804 // This needs to be broken down into ${base}-${timestamp}-${build_number}
806 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
809 metadata.setSnapshotVersion( new SnapshotVersion() );
810 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
811 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
813 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
814 if ( mtimestamp.matches() )
816 String tsDate = mtimestamp.group( 1 );
817 String tsTime = mtimestamp.group( 2 );
819 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
821 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
823 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
827 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
829 // The latestVersion ends with the generic version string.
830 // Example: 1.0-alpha-5-SNAPSHOT
832 metadata.setSnapshotVersion( new SnapshotVersion() );
834 /* Disabled due to decision in [MRM-535].
835 * Do not set metadata.lastUpdated to file.lastModified.
837 * Should this be the last updated timestamp of the file, or in the case of an
838 * archive, the most recent timestamp in the archive?
840 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
842 if ( artifact == null )
844 throw new IOException( "Not snapshot artifact found to reference in " + reference );
847 File artifactFile = managedRepository.toFile( artifact );
849 if ( artifactFile.exists() )
851 Date lastModified = new Date( artifactFile.lastModified() );
852 metadata.setLastUpdatedTimestamp( lastModified );
858 throw new RepositoryMetadataException(
859 "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
864 // Do RELEASE handling.
865 metadata.setVersion( reference.getVersion() );
869 if ( lastUpdated > 0 )
871 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
874 // Save the metadata model to disk.
875 RepositoryMetadataWriter.write( metadata, metadataFile );
876 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
877 checksum.fixChecksums( algorithms );
880 private void initConfigVariables()
882 synchronized ( this.artifactPatterns )
884 this.artifactPatterns.clear();
886 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
889 synchronized ( proxies )
891 this.proxies.clear();
893 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
894 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
896 String key = proxyConfig.getSourceRepoId();
898 Set<String> remoteRepoIds = this.proxies.get( key );
900 if ( remoteRepoIds == null )
902 remoteRepoIds = new HashSet<String>();
905 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
907 this.proxies.put( key, remoteRepoIds );
913 * Get the first Artifact found in the provided VersionedReference location.
915 * @param managedRepository the repository to search within.
916 * @param reference the reference to the versioned reference to search within
917 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
918 * no artifact was found within the versioned reference.
919 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
920 * @throws LayoutException
922 public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository,
923 VersionedReference reference )
924 throws LayoutException, IOException
926 String path = toPath( reference );
928 int idx = path.lastIndexOf( '/' );
931 path = path.substring( 0, idx );
934 Path repoDir = Paths.get( managedRepository.getRepoRoot(), path );
936 if ( !Files.exists(repoDir))
938 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
939 + repoDir.toAbsolutePath() );
942 if ( !Files.isDirectory( repoDir ))
944 throw new IOException(
945 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
948 try(Stream<Path> stream = Files.list(repoDir)) {
949 String result = stream.filter( Files::isRegularFile ).map( path1 ->
950 PathUtil.getRelative( managedRepository.getRepoRoot(), path1 )
951 ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
953 return managedRepository.toArtifactReference( result );
956 // No artifact was found.
960 public ArchivaConfiguration getConfiguration()
962 return configuration;
965 public void setConfiguration( ArchivaConfiguration configuration )
967 this.configuration = configuration;
970 public FileTypes getFiletypes()
975 public void setFiletypes( FileTypes filetypes )
977 this.filetypes = filetypes;