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.ArchivaRepositoryMetadata;
34 import org.apache.archiva.model.ArtifactReference;
35 import org.apache.archiva.model.Plugin;
36 import org.apache.archiva.model.ProjectReference;
37 import org.apache.archiva.model.SnapshotVersion;
38 import org.apache.archiva.model.VersionedReference;
39 import org.apache.archiva.redback.components.registry.Registry;
40 import org.apache.archiva.redback.components.registry.RegistryListener;
41 import org.apache.archiva.repository.ContentNotFoundException;
42 import org.apache.archiva.repository.LayoutException;
43 import org.apache.archiva.repository.ManagedRepositoryContent;
44 import org.apache.archiva.repository.RemoteRepositoryContent;
45 import org.apache.archiva.xml.XMLException;
46 import org.apache.commons.collections.CollectionUtils;
47 import org.apache.commons.lang.StringUtils;
48 import org.apache.commons.lang.math.NumberUtils;
49 import org.apache.commons.lang.time.DateUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.stereotype.Service;
54 import javax.annotation.PostConstruct;
55 import javax.inject.Inject;
56 import javax.inject.Named;
57 import java.io.IOException;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.nio.file.Paths;
61 import java.text.ParseException;
62 import java.text.SimpleDateFormat;
63 import java.util.ArrayList;
64 import java.util.Calendar;
65 import java.util.Collection;
66 import java.util.Collections;
67 import java.util.Date;
68 import java.util.HashMap;
69 import java.util.HashSet;
70 import java.util.Iterator;
71 import java.util.LinkedHashSet;
72 import java.util.List;
75 import java.util.regex.Matcher;
76 import java.util.stream.Stream;
83 @Service( "metadataTools#default" )
84 public class MetadataTools
85 implements RegistryListener
87 private Logger log = LoggerFactory.getLogger( getClass() );
89 public static final String MAVEN_METADATA = "maven-metadata.xml";
91 public static final String MAVEN_ARCHETYPE_CATALOG ="archetype-catalog.xml";
93 private static final char PATH_SEPARATOR = '/';
95 private static final char GROUP_SEPARATOR = '.';
101 @Named( value = "archivaConfiguration#default" )
102 private ArchivaConfiguration configuration;
108 @Named( value = "fileTypes" )
109 private FileTypes filetypes;
111 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
113 private List<String> artifactPatterns;
115 private Map<String, Set<String>> proxies;
117 private static final char NUMS[] = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
119 private SimpleDateFormat lastUpdatedFormat;
121 public MetadataTools()
123 lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
124 lastUpdatedFormat.setTimeZone( DateUtils.UTC_TIME_ZONE );
128 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
130 if ( ConfigurationNames.isProxyConnector( propertyName ) )
132 initConfigVariables();
137 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
143 * Gather the set of snapshot versions found in a particular versioned reference.
145 * @return the Set of snapshot artifact versions found.
146 * @throws LayoutException
147 * @throws ContentNotFoundException
149 public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
150 VersionedReference reference )
151 throws LayoutException, IOException, ContentNotFoundException
153 Set<String> foundVersions = managedRepository.getVersions( reference );
155 // Next gather up the referenced 'latest' versions found in any proxied repositories
156 // maven-metadata-${proxyId}.xml files that may be present.
158 // Does this repository have a set of remote proxied repositories?
159 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
161 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
163 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
164 baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
166 // Add in the proxied repo version ids too.
167 Iterator<String> it = proxiedRepoIds.iterator();
168 while ( it.hasNext() )
170 String proxyId = it.next();
172 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
173 if ( proxyMetadata == null )
175 // There is no proxy metadata, skip it.
179 // Is there some snapshot info?
180 SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
181 if ( snapshot != null )
183 String timestamp = snapshot.getTimestamp();
184 int buildNumber = snapshot.getBuildNumber();
186 // Only interested in the timestamp + buildnumber.
187 if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
189 foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
195 return foundVersions;
199 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
204 public VersionedReference toVersionedReference( String path )
205 throws RepositoryMetadataException
207 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
209 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
212 VersionedReference reference = new VersionedReference();
214 String normalizedPath = StringUtils.replace( path, "\\", "/" );
215 String pathParts[] = StringUtils.split( normalizedPath, '/' );
217 int versionOffset = pathParts.length - 2;
218 int artifactIdOffset = versionOffset - 1;
219 int groupIdEnd = artifactIdOffset - 1;
221 reference.setVersion( pathParts[versionOffset] );
223 if ( !hasNumberAnywhere( reference.getVersion() ) )
225 // Scary check, but without it, all paths are version references;
226 throw new RepositoryMetadataException(
227 "Not a versioned reference, as version id on path has no number in it." );
230 reference.setArtifactId( pathParts[artifactIdOffset] );
232 StringBuilder gid = new StringBuilder();
233 for ( int i = 0; i <= groupIdEnd; i++ )
239 gid.append( pathParts[i] );
242 reference.setGroupId( gid.toString() );
247 private boolean hasNumberAnywhere( String version )
249 return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
252 public ProjectReference toProjectReference( String path )
253 throws RepositoryMetadataException
255 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
257 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
260 ProjectReference reference = new ProjectReference();
262 String normalizedPath = StringUtils.replace( path, "\\", "/" );
263 String pathParts[] = StringUtils.split( normalizedPath, '/' );
265 // Assume last part of the path is the version.
267 int artifactIdOffset = pathParts.length - 2;
268 int groupIdEnd = artifactIdOffset - 1;
270 reference.setArtifactId( pathParts[artifactIdOffset] );
272 StringBuilder gid = new StringBuilder();
273 for ( int i = 0; i <= groupIdEnd; i++ )
279 gid.append( pathParts[i] );
282 reference.setGroupId( gid.toString() );
287 public String toPath( ProjectReference reference )
289 StringBuilder path = new StringBuilder();
291 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
292 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
293 path.append( MAVEN_METADATA );
295 return path.toString();
298 public String toPath( VersionedReference reference )
300 StringBuilder path = new StringBuilder();
302 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
303 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
304 if ( reference.getVersion() != null )
306 // add the version only if it is present
307 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
309 path.append( MAVEN_METADATA );
311 return path.toString();
314 private String formatAsDirectory( String directory )
316 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
320 * Adjusts a path for a metadata.xml file to its repository specific path.
322 * @param repository the repository to base new path off of.
323 * @param path the path to the metadata.xml file to adjust the name of.
324 * @return the newly adjusted path reference to the repository specific metadata path.
326 public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
328 return getRepositorySpecificName( repository.getId(), path );
332 * Adjusts a path for a metadata.xml file to its repository specific path.
334 * @param proxyId the repository id to base new path off of.
335 * @param path the path to the metadata.xml file to adjust the name of.
336 * @return the newly adjusted path reference to the repository specific metadata path.
338 public String getRepositorySpecificName( String proxyId, String path )
340 StringBuilder ret = new StringBuilder();
342 int idx = path.lastIndexOf( '/' );
345 ret.append( path.substring( 0, idx + 1 ) );
348 // TODO: need to filter out 'bad' characters from the proxy id.
349 ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
351 return ret.toString();
355 public void initialize()
357 this.artifactPatterns = new ArrayList<>();
358 this.proxies = new HashMap<>();
359 initConfigVariables();
361 configuration.addChangeListener( this );
364 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
365 ProjectReference reference, String proxyId )
367 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
368 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
370 if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile ))
372 // Nothing to do. return null.
378 return MavenMetadataReader.read( metadataFile );
380 catch ( XMLException e )
382 // TODO: [monitor] consider a monitor for this event.
383 // TODO: consider a read-redo on monitor return code?
384 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
389 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
390 String logicalResource, String proxyId )
392 String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
393 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
395 if ( !Files.exists(metadataFile) || !Files.isRegularFile( metadataFile))
397 // Nothing to do. return null.
403 return MavenMetadataReader.read( metadataFile );
405 catch ( XMLException e )
407 // TODO: [monitor] consider a monitor for this event.
408 // TODO: consider a read-redo on monitor return code?
409 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
414 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
415 VersionedReference reference, String proxyId )
417 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
418 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), metadataPath );
420 if ( !Files.exists(metadataFile) || !Files.isRegularFile(metadataFile))
422 // Nothing to do. return null.
428 return MavenMetadataReader.read( metadataFile );
430 catch ( XMLException e )
432 // TODO: [monitor] consider a monitor for this event.
433 // TODO: consider a read-redo on monitor return code?
434 log.warn( "Unable to read metadata: {}", metadataFile.toAbsolutePath(), e );
439 public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
440 throws RepositoryMetadataException
442 final Path metadataFile = Paths.get( managedRepository.getRepoRoot(), logicalResource );
443 ArchivaRepositoryMetadata metadata = null;
445 //Gather and merge all metadata available
446 List<ArchivaRepositoryMetadata> metadatas =
447 getMetadatasForManagedRepository( managedRepository, logicalResource );
448 for ( ArchivaRepositoryMetadata proxiedMetadata : metadatas )
450 if ( metadata == null )
452 metadata = proxiedMetadata;
455 metadata = RepositoryMetadataMerge.merge( metadata, proxiedMetadata );
458 if ( metadata == null )
460 log.debug( "No metadata to update for {}", logicalResource );
464 Set<String> availableVersions = new HashSet<String>();
465 List<String> metadataAvailableVersions = metadata.getAvailableVersions();
466 if ( metadataAvailableVersions != null )
468 availableVersions.addAll( metadataAvailableVersions );
470 availableVersions = findPossibleVersions( availableVersions, metadataFile.getParent() );
472 if ( availableVersions.size() > 0 )
474 updateMetadataVersions( availableVersions, metadata );
477 RepositoryMetadataWriter.write( metadata, metadataFile );
479 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
480 checksum.fixChecksums( algorithms );
484 * Skims the parent directory of a metadata in vain hope of finding
485 * subdirectories that contain poms.
487 * @param metadataParentDirectory
488 * @return origional set plus newly found versions
490 private Set<String> findPossibleVersions( Set<String> versions, Path metadataParentDirectory )
493 Set<String> result = new HashSet<String>( versions );
495 try (Stream<Path> stream = Files.list( metadataParentDirectory )) {
496 stream.filter( Files::isDirectory ).filter(
499 try(Stream<Path> substream = Files.list(p))
501 return substream.anyMatch( f -> Files.isRegularFile( f ) && f.toString().endsWith( ".pom" ));
503 catch ( IOException e )
509 p -> result.add(p.getFileName().toString())
511 } catch (IOException e) {
517 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(
518 ManagedRepositoryContent managedRepository, String logicalResource )
520 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
521 Path file = Paths.get( managedRepository.getRepoRoot(), logicalResource );
522 if ( Files.exists(file) )
526 ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read( file );
527 if ( existingMetadata != null )
529 metadatas.add( existingMetadata );
532 catch ( XMLException e )
534 log.debug( "Could not read metadata at {}. Metadata will be removed.", file.toAbsolutePath() );
535 FileUtils.deleteQuietly( file );
539 Set<String> proxyIds = proxies.get( managedRepository.getId() );
540 if ( proxyIds != null )
542 for ( String proxyId : proxyIds )
544 ArchivaRepositoryMetadata proxyMetadata =
545 readProxyMetadata( managedRepository, logicalResource, proxyId );
546 if ( proxyMetadata != null )
548 metadatas.add( proxyMetadata );
558 * Update the metadata to represent the all versions/plugins of
559 * the provided groupId:artifactId project or group reference,
560 * based off of information present in the repository,
561 * the maven-metadata.xml files, and the proxy/repository specific
562 * metadata file contents.
564 * We must treat this as a group or a project metadata file as there is no way to know in advance
566 * @param managedRepository the managed repository where the metadata is kept.
567 * @param reference the reference to update.
568 * @throws LayoutException
569 * @throws RepositoryMetadataException
570 * @throws IOException
571 * @throws ContentNotFoundException
574 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
575 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
577 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
579 long lastUpdated = getExistingLastUpdated( metadataFile );
581 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
582 metadata.setGroupId( reference.getGroupId() );
583 metadata.setArtifactId( reference.getArtifactId() );
585 // Gather up all versions found in the managed repository.
586 Set<String> allVersions = managedRepository.getVersions( reference );
588 // Gather up all plugins found in the managed repository.
589 // TODO: do we know this information instead?
590 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
591 Set<Plugin> allPlugins;
592 if ( Files.exists(metadataFile))
596 allPlugins = new LinkedHashSet<Plugin>( MavenMetadataReader.read( metadataFile ).getPlugins() );
598 catch ( XMLException e )
600 throw new RepositoryMetadataException( e.getMessage(), e );
605 allPlugins = new LinkedHashSet<Plugin>();
608 // Does this repository have a set of remote proxied repositories?
609 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
611 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
613 // Add in the proxied repo version ids too.
614 Iterator<String> it = proxiedRepoIds.iterator();
615 while ( it.hasNext() )
617 String proxyId = it.next();
619 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
620 if ( proxyMetadata != null )
622 allVersions.addAll( proxyMetadata.getAvailableVersions() );
623 allPlugins.addAll( proxyMetadata.getPlugins() );
624 long proxyLastUpdated = getLastUpdated( proxyMetadata );
626 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
631 if ( !allVersions.isEmpty() )
633 updateMetadataVersions( allVersions, metadata );
637 // Add the plugins to the metadata model.
638 metadata.setPlugins( new ArrayList<>( allPlugins ) );
640 // artifact ID was actually the last part of the group
641 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
642 metadata.setArtifactId( null );
645 if ( lastUpdated > 0 )
647 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
650 // Save the metadata model to disk.
651 RepositoryMetadataWriter.write( metadata, metadataFile );
652 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
653 checksum.fixChecksums( algorithms );
656 private void updateMetadataVersions( Collection<String> allVersions, ArchivaRepositoryMetadata metadata )
659 List<String> sortedVersions = new ArrayList<>( allVersions );
660 Collections.sort( sortedVersions, VersionComparator.getInstance() );
662 // Split the versions into released and snapshots.
663 List<String> releasedVersions = new ArrayList<>();
664 List<String> snapshotVersions = new ArrayList<>();
666 for ( String version : sortedVersions )
668 if ( VersionUtil.isSnapshot( version ) )
670 snapshotVersions.add( version );
674 releasedVersions.add( version );
678 Collections.sort( releasedVersions, VersionComparator.getInstance() );
679 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
681 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
682 String releaseVersion = null;
684 if ( CollectionUtils.isNotEmpty( releasedVersions ) )
686 releaseVersion = releasedVersions.get( releasedVersions.size() - 1 );
689 // Add the versions to the metadata model.
690 metadata.setAvailableVersions( sortedVersions );
692 metadata.setLatestVersion( latestVersion );
693 metadata.setReleasedVersion( releaseVersion );
696 private Date toLastUpdatedDate( long lastUpdated )
698 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
699 cal.setTimeInMillis( lastUpdated );
701 return cal.getTime();
704 private long toLastUpdatedLong( String timestampString )
708 Date date = lastUpdatedFormat.parse( timestampString );
709 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
712 return cal.getTimeInMillis();
714 catch ( ParseException e )
720 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
722 if ( metadata == null )
730 String lastUpdated = metadata.getLastUpdated();
731 if ( StringUtils.isBlank( lastUpdated ) )
737 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
738 return lastUpdatedDate.getTime();
740 catch ( ParseException e )
742 // Bad format on the last updated string.
747 private long getExistingLastUpdated( Path metadataFile )
749 if ( !Files.exists(metadataFile) )
757 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
759 return getLastUpdated( metadata );
761 catch ( XMLException e )
769 * Update the metadata based on the following rules.
771 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
772 * metadata files to represent the current / latest SNAPSHOT available.
773 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
774 * create the metadata file with contents required of the VersionedReference
776 * @param managedRepository the managed repository where the metadata is kept.
777 * @param reference the versioned reference to update
778 * @throws LayoutException
779 * @throws RepositoryMetadataException
780 * @throws IOException
781 * @throws ContentNotFoundException
784 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
785 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
787 Path metadataFile = Paths.get( managedRepository.getRepoRoot(), toPath( reference ) );
789 long lastUpdated = getExistingLastUpdated( metadataFile );
791 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
792 metadata.setGroupId( reference.getGroupId() );
793 metadata.setArtifactId( reference.getArtifactId() );
795 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
797 // Do SNAPSHOT handling.
798 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
800 // Gather up all of the versions found in the reference dir, and any
801 // proxied maven-metadata.xml files.
802 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
804 if ( snapshotVersions.isEmpty() )
806 throw new ContentNotFoundException(
807 "No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
810 // sort the list to determine to aide in determining the Latest version.
811 List<String> sortedVersions = new ArrayList<>();
812 sortedVersions.addAll( snapshotVersions );
813 Collections.sort( sortedVersions, new VersionComparator() );
815 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
817 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
819 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
820 // This needs to be broken down into ${base}-${timestamp}-${build_number}
822 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
825 metadata.setSnapshotVersion( new SnapshotVersion() );
826 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
827 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
829 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
830 if ( mtimestamp.matches() )
832 String tsDate = mtimestamp.group( 1 );
833 String tsTime = mtimestamp.group( 2 );
835 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
837 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
839 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
843 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
845 // The latestVersion ends with the generic version string.
846 // Example: 1.0-alpha-5-SNAPSHOT
848 metadata.setSnapshotVersion( new SnapshotVersion() );
850 /* Disabled due to decision in [MRM-535].
851 * Do not set metadata.lastUpdated to file.lastModified.
853 * Should this be the last updated timestamp of the file, or in the case of an
854 * archive, the most recent timestamp in the archive?
856 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
858 if ( artifact == null )
860 throw new IOException( "Not snapshot artifact found to reference in " + reference );
863 File artifactFile = managedRepository.toFile( artifact );
865 if ( artifactFile.exists() )
867 Date lastModified = new Date( artifactFile.lastModified() );
868 metadata.setLastUpdatedTimestamp( lastModified );
874 throw new RepositoryMetadataException(
875 "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
880 // Do RELEASE handling.
881 metadata.setVersion( reference.getVersion() );
885 if ( lastUpdated > 0 )
887 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
890 // Save the metadata model to disk.
891 RepositoryMetadataWriter.write( metadata, metadataFile );
892 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
893 checksum.fixChecksums( algorithms );
896 private void initConfigVariables()
898 synchronized ( this.artifactPatterns )
900 this.artifactPatterns.clear();
902 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
905 synchronized ( proxies )
907 this.proxies.clear();
909 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
910 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
912 String key = proxyConfig.getSourceRepoId();
914 Set<String> remoteRepoIds = this.proxies.get( key );
916 if ( remoteRepoIds == null )
918 remoteRepoIds = new HashSet<String>();
921 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
923 this.proxies.put( key, remoteRepoIds );
929 * Get the first Artifact found in the provided VersionedReference location.
931 * @param managedRepository the repository to search within.
932 * @param reference the reference to the versioned reference to search within
933 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
934 * no artifact was found within the versioned reference.
935 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
936 * @throws LayoutException
938 public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository,
939 VersionedReference reference )
940 throws LayoutException, IOException
942 String path = toPath( reference );
944 int idx = path.lastIndexOf( '/' );
947 path = path.substring( 0, idx );
950 Path repoDir = Paths.get( managedRepository.getRepoRoot(), path );
952 if ( !Files.exists(repoDir))
954 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
955 + repoDir.toAbsolutePath() );
958 if ( !Files.isDirectory( repoDir ))
960 throw new IOException(
961 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath() );
964 try(Stream<Path> stream = Files.list(repoDir)) {
965 String result = stream.filter( Files::isRegularFile ).map( path1 ->
966 PathUtil.getRelative( managedRepository.getRepoRoot(), path1 )
967 ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
969 return managedRepository.toArtifactReference( result );
972 // No artifact was found.
976 public ArchivaConfiguration getConfiguration()
978 return configuration;
981 public void setConfiguration( ArchivaConfiguration configuration )
983 this.configuration = configuration;
986 public FileTypes getFiletypes()
991 public void setFiletypes( FileTypes filetypes )
993 this.filetypes = filetypes;