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 final File metadataFile = new File(managedRepository.getRepoRoot(), logicalResource);
428 ArchivaRepositoryMetadata metadata = null;
430 //Gather and merge all metadata available
431 List<ArchivaRepositoryMetadata> metadatas = getMetadatasForManagedRepository(managedRepository, logicalResource);
432 for (ArchivaRepositoryMetadata proxiedMetadata : metadatas)
434 if (metadata == null)
436 metadata = proxiedMetadata;
439 metadata = RepositoryMetadataMerge.merge(metadata, proxiedMetadata);
442 Set<String> availableVersions = new HashSet<String>(metadata.getAvailableVersions());
443 availableVersions = findPossibleVersions(availableVersions, metadataFile.getParentFile());
445 if (availableVersions.size() > 0)
447 updateMetadataVersions(availableVersions, metadata);
450 RepositoryMetadataWriter.write(metadata, metadataFile);
452 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
453 checksum.fixChecksums( algorithms );
457 * Skims the parent directory of a metadata in vain hope of finding
458 * subdirectories that contain poms.
460 * @param metadataParentDirectory
461 * @return origional set plus newley found versions
463 private Set<String> findPossibleVersions(Set<String> versions, File metadataParentDirectory)
465 Set<String> result = new HashSet<String>(versions);
466 for (File directory : metadataParentDirectory.listFiles())
468 if (directory.isDirectory())
470 for (File possiblePom : directory.listFiles())
472 if (possiblePom.getName().endsWith(".pom"))
474 result.add(directory.getName());
482 private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository( ManagedRepositoryContent managedRepository, String logicalResource )
484 List<ArchivaRepositoryMetadata> metadatas = new ArrayList<ArchivaRepositoryMetadata>();
485 File file = new File(managedRepository.getRepoRoot(), logicalResource);
490 ArchivaRepositoryMetadata existingMetadata = RepositoryMetadataReader.read(file);
491 if (existingMetadata != null)
493 metadatas.add(existingMetadata);
496 catch (RepositoryMetadataException e)
498 log.debug("Could not read metadata at " + file.getAbsolutePath() + ". Metadata will be removed.");
499 FileUtils.deleteQuietly(file);
503 for (String proxyId : proxies.get(managedRepository.getId()))
505 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, logicalResource, proxyId );
506 if (proxyMetadata != null)
508 metadatas.add(proxyMetadata);
517 * Update the metadata to represent the all versions/plugins of
518 * the provided groupId:artifactId project or group reference,
519 * based off of information present in the repository,
520 * the maven-metadata.xml files, and the proxy/repository specific
521 * metadata file contents.
523 * We must treat this as a group or a project metadata file as there is no way to know in advance
526 * @param managedRepository the managed repository where the metadata is kept.
527 * @param reference the reference to update.
528 * @throws LayoutException
529 * @throws RepositoryMetadataException
530 * @throws IOException
531 * @throws ContentNotFoundException
533 public void updateMetadata( ManagedRepositoryContent managedRepository, ProjectReference reference )
534 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
536 File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
538 long lastUpdated = getExistingLastUpdated( metadataFile );
540 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
541 metadata.setGroupId( reference.getGroupId() );
542 metadata.setArtifactId( reference.getArtifactId() );
544 // Gather up all versions found in the managed repository.
545 Set<String> allVersions = managedRepository.getVersions( reference );
547 // Gather up all plugins found in the managed repository.
548 // TODO: do we know this information instead?
549 // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
550 Set<Plugin> allPlugins;
551 if ( metadataFile.exists() )
553 allPlugins = new LinkedHashSet<Plugin>( RepositoryMetadataReader.read( metadataFile ).getPlugins() );
557 allPlugins = new LinkedHashSet<Plugin>();
560 // Does this repository have a set of remote proxied repositories?
561 Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
563 if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
565 // Add in the proxied repo version ids too.
566 Iterator<String> it = proxiedRepoIds.iterator();
567 while ( it.hasNext() )
569 String proxyId = it.next();
571 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
572 if ( proxyMetadata != null )
574 allVersions.addAll( proxyMetadata.getAvailableVersions() );
575 allPlugins.addAll( proxyMetadata.getPlugins() );
576 long proxyLastUpdated = getLastUpdated( proxyMetadata );
578 lastUpdated = Math.max( lastUpdated, proxyLastUpdated );
583 if ( !allVersions.isEmpty() )
585 updateMetadataVersions( allVersions ,metadata );
589 // Add the plugins to the metadata model.
590 metadata.setPlugins( new ArrayList<Plugin>( allPlugins ) );
592 // artifact ID was actually the last part of the group
593 metadata.setGroupId( metadata.getGroupId() + "." + metadata.getArtifactId() );
594 metadata.setArtifactId( null );
597 if ( lastUpdated > 0 )
599 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
602 // Save the metadata model to disk.
603 RepositoryMetadataWriter.write( metadata, metadataFile );
604 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
605 checksum.fixChecksums( algorithms );
608 private void updateMetadataVersions(Collection<String> allVersions, ArchivaRepositoryMetadata metadata)
611 List<String> sortedVersions = new ArrayList<String>(allVersions);
612 Collections.sort(sortedVersions, VersionComparator.getInstance());
614 // Split the versions into released and snapshots.
615 List<String> releasedVersions = new ArrayList<String>();
616 List<String> snapshotVersions = new ArrayList<String>();
618 for (String version : sortedVersions)
620 if (VersionUtil.isSnapshot(version))
622 snapshotVersions.add(version);
626 releasedVersions.add(version);
630 Collections.sort(releasedVersions, VersionComparator.getInstance());
631 Collections.sort(snapshotVersions, VersionComparator.getInstance());
633 String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
634 String releaseVersion = null;
636 if (CollectionUtils.isNotEmpty(releasedVersions))
638 releaseVersion = releasedVersions.get(releasedVersions.size() - 1);
641 // Add the versions to the metadata model.
642 metadata.setAvailableVersions(sortedVersions);
644 metadata.setLatestVersion(latestVersion);
645 metadata.setReleasedVersion(releaseVersion);
648 private Date toLastUpdatedDate( long lastUpdated )
650 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
651 cal.setTimeInMillis( lastUpdated );
653 return cal.getTime();
656 private long toLastUpdatedLong( String timestampString )
660 Date date = lastUpdatedFormat.parse( timestampString );
661 Calendar cal = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
664 return cal.getTimeInMillis();
666 catch ( ParseException e )
672 private long getLastUpdated( ArchivaRepositoryMetadata metadata )
674 if ( metadata == null )
682 String lastUpdated = metadata.getLastUpdated();
683 if ( StringUtils.isBlank( lastUpdated ) )
689 Date lastUpdatedDate = lastUpdatedFormat.parse( lastUpdated );
690 return lastUpdatedDate.getTime();
692 catch ( ParseException e )
694 // Bad format on the last updated string.
699 private long getExistingLastUpdated( File metadataFile )
701 if ( !metadataFile.exists() )
709 ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( metadataFile );
711 return getLastUpdated( metadata );
713 catch ( RepositoryMetadataException e )
721 * Update the metadata based on the following rules.
723 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
724 * metadata files to represent the current / latest SNAPSHOT available.
725 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
726 * create the metadata file with contents required of the VersionedReference
729 * @param managedRepository the managed repository where the metadata is kept.
730 * @param reference the versioned reference to update
731 * @throws LayoutException
732 * @throws RepositoryMetadataException
733 * @throws IOException
734 * @throws ContentNotFoundException
736 public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
737 throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
739 File metadataFile = new File( managedRepository.getRepoRoot(), toPath( reference ) );
741 long lastUpdated = getExistingLastUpdated( metadataFile );
743 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
744 metadata.setGroupId( reference.getGroupId() );
745 metadata.setArtifactId( reference.getArtifactId() );
747 if ( VersionUtil.isSnapshot( reference.getVersion() ) )
749 // Do SNAPSHOT handling.
750 metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
752 // Gather up all of the versions found in the reference dir, and any
753 // proxied maven-metadata.xml files.
754 Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
756 if ( snapshotVersions.isEmpty() )
758 throw new ContentNotFoundException( "No snapshot versions found on reference ["
759 + VersionedReference.toKey( reference ) + "]." );
762 // sort the list to determine to aide in determining the Latest version.
763 List<String> sortedVersions = new ArrayList<String>();
764 sortedVersions.addAll( snapshotVersions );
765 Collections.sort( sortedVersions, new VersionComparator() );
767 String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
769 if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
771 // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
772 // This needs to be broken down into ${base}-${timestamp}-${build_number}
774 Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
777 metadata.setSnapshotVersion( new SnapshotVersion() );
778 int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
779 metadata.getSnapshotVersion().setBuildNumber( buildNumber );
781 Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
782 if ( mtimestamp.matches() )
784 String tsDate = mtimestamp.group( 1 );
785 String tsTime = mtimestamp.group( 2 );
787 long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
789 lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
791 metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
795 else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
797 // The latestVersion ends with the generic version string.
798 // Example: 1.0-alpha-5-SNAPSHOT
800 metadata.setSnapshotVersion( new SnapshotVersion() );
802 /* Disabled due to decision in [MRM-535].
803 * Do not set metadata.lastUpdated to file.lastModified.
805 * Should this be the last updated timestamp of the file, or in the case of an
806 * archive, the most recent timestamp in the archive?
808 ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
810 if ( artifact == null )
812 throw new IOException( "Not snapshot artifact found to reference in " + reference );
815 File artifactFile = managedRepository.toFile( artifact );
817 if ( artifactFile.exists() )
819 Date lastModified = new Date( artifactFile.lastModified() );
820 metadata.setLastUpdatedTimestamp( lastModified );
826 throw new RepositoryMetadataException( "Unable to process snapshot version <" + latestVersion
827 + "> reference <" + reference + ">" );
832 // Do RELEASE handling.
833 metadata.setVersion( reference.getVersion() );
837 if ( lastUpdated > 0 )
839 metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
842 // Save the metadata model to disk.
843 RepositoryMetadataWriter.write( metadata, metadataFile );
844 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
845 checksum.fixChecksums( algorithms );
848 private void initConfigVariables()
850 synchronized ( this.artifactPatterns )
852 this.artifactPatterns.clear();
854 this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
857 synchronized ( proxies )
859 this.proxies.clear();
861 List<ProxyConnectorConfiguration> proxyConfigs = configuration.getConfiguration().getProxyConnectors();
862 for( ProxyConnectorConfiguration proxyConfig: proxyConfigs )
864 String key = proxyConfig.getSourceRepoId();
866 Set<String> remoteRepoIds = this.proxies.get( key );
868 if ( remoteRepoIds == null )
870 remoteRepoIds = new HashSet<String>();
873 remoteRepoIds.add( proxyConfig.getTargetRepoId() );
875 this.proxies.put( key, remoteRepoIds );
881 * Get the first Artifact found in the provided VersionedReference location.
883 * @param managedRepository the repository to search within.
884 * @param reference the reference to the versioned reference to search within
885 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
886 * no artifact was found within the versioned reference.
887 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
888 * @throws LayoutException
890 public ArtifactReference getFirstArtifact( ManagedRepositoryContent managedRepository, VersionedReference reference )
891 throws LayoutException, IOException
893 String path = toPath( reference );
895 int idx = path.lastIndexOf( '/' );
898 path = path.substring( 0, idx );
901 File repoDir = new File( managedRepository.getRepoRoot(), path );
903 if ( !repoDir.exists() )
905 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
906 + repoDir.getAbsolutePath() );
909 if ( !repoDir.isDirectory() )
911 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
912 + repoDir.getAbsolutePath() );
915 File repoFiles[] = repoDir.listFiles();
916 for ( int i = 0; i < repoFiles.length; i++ )
918 if ( repoFiles[i].isDirectory() )
920 // Skip it. it's a directory.
924 String relativePath = PathUtil.getRelative( managedRepository.getRepoRoot(), repoFiles[i] );
926 if ( filetypes.matchesArtifactPattern( relativePath ) )
928 ArtifactReference artifact = managedRepository.toArtifactReference( relativePath );
934 // No artifact was found.