1 package org.apache.maven.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.commons.collections.CollectionUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.lang.math.NumberUtils;
27 import org.apache.commons.lang.time.DateUtils;
28 import org.apache.maven.archiva.common.utils.PathUtil;
29 import org.apache.maven.archiva.common.utils.VersionComparator;
30 import org.apache.maven.archiva.common.utils.VersionUtil;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.ConfigurationNames;
33 import org.apache.maven.archiva.configuration.FileTypes;
34 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
35 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
36 import org.apache.maven.archiva.model.ArtifactReference;
37 import org.apache.maven.archiva.model.Plugin;
38 import org.apache.maven.archiva.model.ProjectReference;
39 import org.apache.maven.archiva.model.SnapshotVersion;
40 import org.apache.maven.archiva.model.VersionedReference;
41 import org.apache.maven.archiva.repository.ContentNotFoundException;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
44 import org.apache.maven.archiva.repository.layout.LayoutException;
45 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
46 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
47 import org.codehaus.plexus.registry.Registry;
48 import org.codehaus.plexus.registry.RegistryListener;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 import java.io.IOException;
54 import java.text.ParseException;
55 import java.text.SimpleDateFormat;
56 import java.util.ArrayList;
57 import java.util.Calendar;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.Date;
61 import java.util.HashMap;
62 import java.util.HashSet;
63 import java.util.Iterator;
64 import java.util.LinkedHashSet;
65 import java.util.List;
68 import java.util.regex.Matcher;
69 import org.apache.commons.io.FileUtils;
74 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
77 * @plexus.component role="org.apache.maven.archiva.repository.metadata.MetadataTools"
79 public class MetadataTools
80 implements RegistryListener, Initializable
82 private static Logger log = LoggerFactory.getLogger( MetadataTools.class );
84 public static final String MAVEN_METADATA = "maven-metadata.xml";
86 private static final char PATH_SEPARATOR = '/';
88 private static final char GROUP_SEPARATOR = '.';
93 private ArchivaConfiguration configuration;
98 private FileTypes filetypes;
100 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
102 private List<String> artifactPatterns;
104 private Map<String, Set<String>> proxies;
106 private static final char NUMS[] = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
108 private SimpleDateFormat lastUpdatedFormat;
110 public MetadataTools()
112 lastUpdatedFormat = new SimpleDateFormat( "yyyyMMddHHmmss" );
113 lastUpdatedFormat.setTimeZone( DateUtils.UTC_TIME_ZONE );
116 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
118 if ( ConfigurationNames.isProxyConnector( propertyName ) )
120 initConfigVariables();
124 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
130 * Gather the set of snapshot versions found in a particular versioned reference.
132 * @return the Set of snapshot artifact versions found.
133 * @throws LayoutException
134 * @throws ContentNotFoundException
136 public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository, VersionedReference reference )
137 throws LayoutException, IOException, ContentNotFoundException
139 Set<String> foundVersions = managedRepository.getVersions( reference );
141 // Next gather up the referenced 'latest' versions found in any proxied repositories
142 // maven-metadata-${proxyId}.xml files that may be present.
144 // Does this repository have a set of remote proxied repositories?
145 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
147 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
149 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
150 baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
152 // Add in the proxied repo version ids too.
153 Iterator<String> it = proxiedRepoIds.iterator();
154 while ( it.hasNext() )
156 String proxyId = it.next();
158 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
159 if ( proxyMetadata == null )
161 // There is no proxy metadata, skip it.
165 // Is there some snapshot info?
166 SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
167 if ( snapshot != null )
169 String timestamp = snapshot.getTimestamp();
170 int buildNumber = snapshot.getBuildNumber();
172 // Only interested in the timestamp + buildnumber.
173 if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
175 foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
181 return foundVersions;
185 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
190 public VersionedReference toVersionedReference( String path )
191 throws RepositoryMetadataException
193 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
195 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
198 VersionedReference reference = new VersionedReference();
200 String normalizedPath = StringUtils.replace( path, "\\", "/" );
201 String pathParts[] = StringUtils.split( normalizedPath, '/' );
203 int versionOffset = pathParts.length - 2;
204 int artifactIdOffset = versionOffset - 1;
205 int groupIdEnd = artifactIdOffset - 1;
207 reference.setVersion( pathParts[versionOffset] );
209 if ( !hasNumberAnywhere( reference.getVersion() ) )
211 // Scary check, but without it, all paths are version references;
212 throw new RepositoryMetadataException(
213 "Not a versioned reference, as version id on path has no number in it." );
216 reference.setArtifactId( pathParts[artifactIdOffset] );
218 StringBuffer gid = new StringBuffer();
219 for ( int i = 0; i <= groupIdEnd; i++ )
225 gid.append( pathParts[i] );
228 reference.setGroupId( gid.toString() );
233 private boolean hasNumberAnywhere( String version )
235 return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
238 public ProjectReference toProjectReference( String path )
239 throws RepositoryMetadataException
241 if ( !path.endsWith( "/" + MAVEN_METADATA ) )
243 throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
246 ProjectReference reference = new ProjectReference();
248 String normalizedPath = StringUtils.replace( path, "\\", "/" );
249 String pathParts[] = StringUtils.split( normalizedPath, '/' );
251 // Assume last part of the path is the version.
253 int artifactIdOffset = pathParts.length - 2;
254 int groupIdEnd = artifactIdOffset - 1;
256 reference.setArtifactId( pathParts[artifactIdOffset] );
258 StringBuffer gid = new StringBuffer();
259 for ( int i = 0; i <= groupIdEnd; i++ )
265 gid.append( pathParts[i] );
268 reference.setGroupId( gid.toString() );
273 public String toPath( ProjectReference reference )
275 StringBuffer path = new StringBuffer();
277 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
278 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
279 path.append( MAVEN_METADATA );
281 return path.toString();
284 public String toPath( VersionedReference reference )
286 StringBuffer path = new StringBuffer();
288 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
289 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
290 if ( reference.getVersion() != null )
292 // add the version only if it is present
293 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
295 path.append( MAVEN_METADATA );
297 return path.toString();
300 private String formatAsDirectory( String directory )
302 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
306 * Adjusts a path for a metadata.xml file to its repository specific path.
308 * @param repository the repository to base new path off of.
309 * @param path the path to the metadata.xml file to adjust the name of.
310 * @return the newly adjusted path reference to the repository specific metadata path.
312 public String getRepositorySpecificName( RemoteRepositoryContent repository, String path )
314 return getRepositorySpecificName( repository.getId(), path );
318 * Adjusts a path for a metadata.xml file to its repository specific path.
320 * @param proxyId the repository id to base new path off of.
321 * @param path the path to the metadata.xml file to adjust the name of.
322 * @return the newly adjusted path reference to the repository specific metadata path.
324 public String getRepositorySpecificName( String proxyId, String path )
326 StringBuffer ret = new StringBuffer();
328 int idx = path.lastIndexOf( "/" );
331 ret.append( path.substring( 0, idx + 1 ) );
334 // TODO: need to filter out 'bad' characters from the proxy id.
335 ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
337 return ret.toString();
340 public void initialize()
341 throws InitializationException
343 this.artifactPatterns = new ArrayList<String>();
344 this.proxies = new HashMap<String, Set<String>>();
345 initConfigVariables();
347 configuration.addChangeListener( this );
350 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
351 ProjectReference reference, String proxyId )
353 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
354 File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
356 if ( !metadataFile.exists() || !metadataFile.isFile() )
358 // Nothing to do. return null.
364 return RepositoryMetadataReader.read( metadataFile );
366 catch ( RepositoryMetadataException e )
368 // TODO: [monitor] consider a monitor for this event.
369 // TODO: consider a read-redo on monitor return code?
370 log.warn( "Unable to read metadata: " + metadataFile.getAbsolutePath(), e );
375 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
376 String logicalResource, String proxyId )
378 String metadataPath = getRepositorySpecificName( proxyId, logicalResource );
379 File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
381 if ( !metadataFile.exists() || !metadataFile.isFile() )
383 // Nothing to do. return null.
389 return RepositoryMetadataReader.read( metadataFile );
391 catch ( RepositoryMetadataException e )
393 // TODO: [monitor] consider a monitor for this event.
394 // TODO: consider a read-redo on monitor return code?
395 log.warn( "Unable to read metadata: " + metadataFile.getAbsolutePath(), e );
400 public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
401 VersionedReference reference, String proxyId )
403 String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
404 File metadataFile = new File( managedRepository.getRepoRoot(), metadataPath );
406 if ( !metadataFile.exists() || !metadataFile.isFile() )
408 // Nothing to do. return null.
414 return RepositoryMetadataReader.read( metadataFile );
416 catch ( RepositoryMetadataException e )
418 // TODO: [monitor] consider a monitor for this event.
419 // TODO: consider a read-redo on monitor return code?
420 log.warn( "Unable to read metadata: " + metadataFile.getAbsolutePath(), e );
425 public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource) throws RepositoryMetadataException
427 ArchivaRepositoryMetadata metadata = null;
428 final File metadataFile = new File(managedRepository.getRepoRoot(), logicalResource);
429 // final long lastUpdated = getExistingLastUpdated( metadataFile );
431 //Gather and merge all metadata available
432 List<ArchivaRepositoryMetadata> metadatas = getMetadatasForManagedRepository(managedRepository, logicalResource);
433 for (ArchivaRepositoryMetadata proxiedMetadata : metadatas)
435 if (metadata == null)
437 metadata = proxiedMetadata;
440 metadata = RepositoryMetadataMerge.merge(metadata, proxiedMetadata);
443 Set<String> availableVersions = new HashSet<String>(metadata.getAvailableVersions());
444 availableVersions = findPossibleVersions(availableVersions, metadataFile.getParentFile());
446 if (availableVersions.size() > 0)
448 updateMetadataVersions(availableVersions, metadata);
451 // if ( lastUpdated > 0 )
453 // metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
456 RepositoryMetadataWriter.write(metadata, metadataFile);
460 * Skims the parent directory of a metadata in vain hope of finding
461 * subdirectories that contain poms.
463 * @param metadataParentDirectory
464 * @return origional set plus newley found versions
466 private Set<String> findPossibleVersions(Set<String> versions, File metadataParentDirectory)
468 Set<String> result = new HashSet<String>(versions);
469 for (File directory : metadataParentDirectory.listFiles())
471 if (directory.isDirectory())
473 for (File possiblePom : directory.listFiles())
475 if (possiblePom.getName().endsWith(".pom"))
477 result.add(directory.getName());
485 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository( ManagedRepositoryContent managedRepository, String logicalResource )
487 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<ArchivaRepositoryMetadata>();
488 File file = new File(managedRepository.getRepoRoot(), logicalResource);
493 ArchivaRepositoryMetadata existingMetadata = RepositoryMetadataReader.read(file);
494 if (existingMetadata != null)
496 metadatas.add(existingMetadata);
499 catch (RepositoryMetadataException e)
501 log.debug("Could not read metadata at " + file.getAbsolutePath() + ". Metadata will be removed.");
502 FileUtils.deleteQuietly(file);
506 for (String proxyId : proxies.get(managedRepository.getId()))
508 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, logicalResource, proxyId );
509 if (proxyMetadata != null)
511 metadatas.add(proxyMetadata);
520 * Update the metadata to represent the all versions/plugins of
521 * the provided groupId:artifactId project or group reference,
522 * based off of information present in the repository,
523 * the maven-metadata.xml files, and the proxy/repository specific
524 * metadata file contents.
526 * We must treat this as a group or a project metadata file as there is no way to know in advance
529 * @param managedRepository the managed repository where the metadata is kept.
530 * @param reference the reference to update.
531 * @throws LayoutException
532 * @throws RepositoryMetadataException
533 * @throws IOException
534 * @throws ContentNotFoundException
536 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
537 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
539 File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
541 long lastUpdated = getExistingLastUpdated( metadataFile );
543 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
544 metadata.setGroupId( reference.getGroupId() );
545 metadata.setArtifactId( reference.getArtifactId() );
547 // Gather up all versions found in the managed repository.
548 Set<String> allVersions = managedRepository.getVersions( reference );
550 // Gather up all plugins found in the managed repository.
551 // TODO: do we know this information instead?
552 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
553 Set<Plugin> allPlugins;
554 if ( metadataFile.exists() )
556 allPlugins = new LinkedHashSet<Plugin>( RepositoryMetadataReader.read( metadataFile ).getPlugins() );
560 allPlugins = new LinkedHashSet<Plugin>();
563 // Does this repository have a set of remote proxied repositories?
564 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
566 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
568 // Add in the proxied repo version ids too.
569 Iterator<String> it = proxiedRepoIds.iterator();
570 while ( it.hasNext() )
572 String proxyId = it.next();
574 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
575 if ( proxyMetadata != null )
577 allVersions.addAll( proxyMetadata.getAvailableVersions() );
578 allPlugins.addAll( proxyMetadata.getPlugins() );
579 long proxyLastUpdated = getLastUpdated( proxyMetadata );
581 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
586 if ( !allVersions.isEmpty() )
588 updateMetadataVersions( allVersions ,metadata );
592 // Add the plugins to the metadata model.
593 metadata.setPlugins( new ArrayList<Plugin>( allPlugins ) );
595 // artifact ID was actually the last part of the group
596 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
597 metadata.setArtifactId( null );
600 if ( lastUpdated > 0 )
602 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
605 // Save the metadata model to disk.
606 RepositoryMetadataWriter.write( metadata, metadataFile );
607 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
608 checksum.fixChecksums( algorithms );
611 private void updateMetadataVersions(Collection<String> allVersions, ArchivaRepositoryMetadata metadata)
614 List<String> sortedVersions = new ArrayList<String>(allVersions);
615 Collections.sort(sortedVersions, VersionComparator.getInstance());
617 // Split the versions into released and snapshots.
618 List<String> releasedVersions = new ArrayList<String>();
619 List<String> snapshotVersions = new ArrayList<String>();
621 for (String version : sortedVersions)
623 if (VersionUtil.isSnapshot(version))
625 snapshotVersions.add(version);
629 releasedVersions.add(version);
633 Collections.sort(releasedVersions, VersionComparator.getInstance());
634 Collections.sort(snapshotVersions, VersionComparator.getInstance());
636 String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
637 String releaseVersion = null;
639 if (CollectionUtils.isNotEmpty(releasedVersions))
641 releaseVersion = releasedVersions.get(releasedVersions.size() - 1);
644 // Add the versions to the metadata model.
645 metadata.setAvailableVersions(sortedVersions);
647 metadata.setLatestVersion(latestVersion);
648 metadata.setReleasedVersion(releaseVersion);
651 private Date toLastUpdatedDate( long lastUpdated )
653 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
654 cal.setTimeInMillis( lastUpdated );
656 return cal.getTime();
659 private long toLastUpdatedLong( String timestampString )
663 Date date = lastUpdatedFormat.parse( timestampString );
664 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
667 return cal.getTimeInMillis();
669 catch ( ParseException e )
675 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
677 if ( metadata == null )
685 String lastUpdated = metadata.getLastUpdated();
686 if ( StringUtils.isBlank( lastUpdated ) )
692 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
693 return lastUpdatedDate.getTime();
695 catch ( ParseException e )
697 // Bad format on the last updated string.
702 private long getExistingLastUpdated( File metadataFile )
704 if ( !metadataFile.exists() )
712 ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( metadataFile );
714 return getLastUpdated( metadata );
716 catch ( RepositoryMetadataException e )
724 * Update the metadata based on the following rules.
726 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
727 * metadata files to represent the current / latest SNAPSHOT available.
728 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
729 * create the metadata file with contents required of the VersionedReference
732 * @param managedRepository the managed repository where the metadata is kept.
733 * @param reference the versioned reference to update
734 * @throws LayoutException
735 * @throws RepositoryMetadataException
736 * @throws IOException
737 * @throws ContentNotFoundException
739 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
740 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
742 File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
744 long lastUpdated = getExistingLastUpdated( metadataFile );
746 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
747 metadata.setGroupId( reference.getGroupId() );
748 metadata.setArtifactId( reference.getArtifactId() );
750 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
752 // Do SNAPSHOT handling.
753 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
755 // Gather up all of the versions found in the reference dir, and any
756 // proxied maven-metadata.xml files.
757 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
759 if ( snapshotVersions.isEmpty() )
761 throw new ContentNotFoundException( "No snapshot versions found on reference ["
762 + VersionedReference.toKey( reference ) + "]." );
765 // sort the list to determine to aide in determining the Latest version.
766 List<String> sortedVersions = new ArrayList<String>();
767 sortedVersions.addAll( snapshotVersions );
768 Collections.sort( sortedVersions, new VersionComparator() );
770 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
772 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
774 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
775 // This needs to be broken down into ${base}-${timestamp}-${build_number}
777 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
780 metadata.setSnapshotVersion( new SnapshotVersion() );
781 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
782 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
784 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
785 if ( mtimestamp.matches() )
787 String tsDate = mtimestamp.group( 1 );
788 String tsTime = mtimestamp.group( 2 );
790 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
792 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
794 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
798 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
800 // The latestVersion ends with the generic version string.
801 // Example: 1.0-alpha-5-SNAPSHOT
803 metadata.setSnapshotVersion( new SnapshotVersion() );
805 /* Disabled due to decision in [MRM-535].
806 * Do not set metadata.lastUpdated to file.lastModified.
808 * Should this be the last updated timestamp of the file, or in the case of an
809 * archive, the most recent timestamp in the archive?
811 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
813 if ( artifact == null )
815 throw new IOException( "Not snapshot artifact found to reference in " + reference );
818 File artifactFile = managedRepository.toFile( artifact );
820 if ( artifactFile.exists() )
822 Date lastModified = new Date( artifactFile.lastModified() );
823 metadata.setLastUpdatedTimestamp( lastModified );
829 throw new RepositoryMetadataException( "Unable to process snapshot version <" + latestVersion
830 + "> reference <" + reference + ">" );
835 // Do RELEASE handling.
836 metadata.setVersion( reference.getVersion() );
840 if ( lastUpdated > 0 )
842 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
845 // Save the metadata model to disk.
846 RepositoryMetadataWriter.write( metadata, metadataFile );
847 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
848 checksum.fixChecksums( algorithms );
851 private void initConfigVariables()
853 synchronized ( this.artifactPatterns )
855 this.artifactPatterns.clear();
857 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
860 synchronized ( proxies )
862 this.proxies.clear();
864 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
865 for( ProxyConnectorConfiguration proxyConfig: proxyConfigs )
867 String key = proxyConfig.getSourceRepoId();
869 Set<String> remoteRepoIds = this.proxies.get( key );
871 if ( remoteRepoIds == null )
873 remoteRepoIds = new HashSet<String>();
876 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
878 this.proxies.put( key, remoteRepoIds );
884 * Get the first Artifact found in the provided VersionedReference location.
886 * @param managedRepository the repository to search within.
887 * @param reference the reference to the versioned reference to search within
888 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
889 * no artifact was found within the versioned reference.
890 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
891 * @throws LayoutException
893 public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository, VersionedReference reference )
894 throws LayoutException, IOException
896 String path = toPath( reference );
898 int idx = path.lastIndexOf( '/' );
901 path = path.substring( 0, idx );
904 File repoDir = new File( managedRepository.getRepoRoot(), path );
906 if ( !repoDir.exists() )
908 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
909 + repoDir.getAbsolutePath() );
912 if ( !repoDir.isDirectory() )
914 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
915 + repoDir.getAbsolutePath() );
918 File repoFiles[] = repoDir.listFiles();
919 for ( int i = 0; i < repoFiles.length; i++ )
921 if ( repoFiles[i].isDirectory() )
923 // Skip it. it's a directory.
927 String relativePath = PathUtil.getRelative( managedRepository.getRepoRoot(), repoFiles[i] );
929 if ( filetypes.matchesArtifactPattern( relativePath ) )
931 ArtifactReference artifact = managedRepository.toArtifactReference( relativePath );
937 // No artifact was found.