/**
* VersionComparator - compare the parts of two version strings.
- *
+ * <p/>
* Technique.
- *
- * * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
- *
+ * <p/>
+ * * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
+ * <p/>
* <code>
- * "1.0" = "1", "0"
- * "1.0-alpha-1" = "1", "0", "alpha", "1"
- * "2.0-rc2" = "2", "0", "rc", "2"
- * "1.3-m2" = "1", "3", "m", "3"
+ * "1.0" = "1", "0"
+ * "1.0-alpha-1" = "1", "0", "alpha", "1"
+ * "2.0-rc2" = "2", "0", "rc", "2"
+ * "1.3-m2" = "1", "3", "m", "3"
* </code>
- *
+ * <p/>
* compare each part individually, and when they do not match, perform the following test.
- *
+ * <p/>
* Numbers are calculated per normal comparison rules.
* Words that are part of the "special word list" will be treated as their index within that heirarchy.
* Words that cannot be identified as special, are treated using normal case-insensitive comparison rules.
* @version $Id$
*/
public class VersionComparator
- implements Comparator
+ implements Comparator<String>
{
- private static Comparator INSTANCE = new VersionComparator();
+ private static Comparator<String> INSTANCE = new VersionComparator();
- private List specialWords;
+ private List<String> specialWords;
public VersionComparator()
{
- specialWords = new ArrayList();
+ specialWords = new ArrayList<String>();
// ids that refer to LATEST
specialWords.add( "final" );
specialWords.add( "snapshot" );
}
- public static Comparator getInstance()
+ public static Comparator<String> getInstance()
{
return INSTANCE;
}
- public int compare( Object o1, Object o2 )
+ public int compare( String o1, String o2 )
{
if ( o1 == null && o2 == null )
{
return 0;
}
- if ( o1 == null && o2 != null )
+ if ( o1 == null )
{
return 1;
}
- if ( o1 != null && o2 == null )
+ if ( o2 == null )
{
return -1;
}
- if ( ( o1 instanceof String ) && ( o2 instanceof String ) )
- {
- String s1 = ( (String) o1 );
- String s2 = ( (String) o2 );
-
- String parts1[] = toParts( s1 );
- String parts2[] = toParts( s2 );
-
- int diff;
- int partLen = Math.max( parts1.length, parts2.length );
- for ( int i = 0; i < partLen; i++ )
- {
- diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
- if ( diff != 0 )
- {
- return diff;
- }
- }
-
- diff = parts2.length - parts1.length;
+ String[] parts1 = toParts( o1 );
+ String[] parts2 = toParts( o2 );
+ int diff;
+ int partLen = Math.max( parts1.length, parts2.length );
+ for ( int i = 0; i < partLen; i++ )
+ {
+ diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
if ( diff != 0 )
{
return diff;
}
+ }
- return s1.compareToIgnoreCase( s2 );
+ diff = parts2.length - parts1.length;
+
+ if ( diff != 0 )
+ {
+ return diff;
}
- return 0;
+ return o1.compareToIgnoreCase( o2 );
}
- private String safePart( String parts[], int idx )
+ private String safePart( String[] parts, int idx )
{
if ( idx < parts.length )
{
// Only operate perform index based operation, if both strings
// are found in the specialWords index.
- if ( ( idx1 >= 0 ) && ( idx2 >= 0 ) )
+ if ( idx1 >= 0 && idx2 >= 0 )
{
return idx1 - idx2;
}
return ArrayUtils.EMPTY_STRING_ARRAY;
}
- final int modeOther = 0;
- final int modeDigit = 1;
- final int modeText = 2;
+ int modeOther = 0;
+ int modeDigit = 1;
+ int modeText = 2;
- List parts = new ArrayList();
+ List<String> parts = new ArrayList<String>();
int len = version.length();
int i = 0;
int start = 0;
// Other.
if ( mode != modeOther )
{
- if ( mode != modeOther )
- {
- parts.add( version.substring( start, i ) );
- }
+ parts.add( version.substring( start, i ) );
mode = modeOther;
}
}
parts.add( version.substring( start, i ) );
}
- return (String[]) parts.toArray( new String[parts.size()] );
+ return parts.toArray( new String[parts.size()] );
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
public void updateMetadata( ArchivaRepository managedRepository, ProjectReference reference )
throws LayoutException, RepositoryMetadataException, IOException
{
+ Comparator<String> comparator = VersionComparator.getInstance();
+
File metadataFile = new File( managedRepository.getUrl().getPath(), toPath( reference ) );
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
// Does this repository have a set of remote proxied repositories?
Set proxiedRepoIds = this.proxies.get( managedRepository.getId() );
+ String latestVersion = null;
+ String releaseVersion = null;
if ( proxiedRepoIds != null )
{
// Add in the proxied repo version ids too.
String proxyId = it.next();
ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
- if ( proxyMetadata == null )
+ if ( proxyMetadata != null )
{
- // There is no proxy metadata, skip it.
- continue;
- }
+ availableVersions.addAll( proxyMetadata.getAvailableVersions() );
+
+ if ( latestVersion == null ||
+ comparator.compare( proxyMetadata.getLatestVersion(), latestVersion ) > 0 )
+ {
+ latestVersion = proxyMetadata.getLatestVersion();
+ }
- availableVersions.addAll( proxyMetadata.getAvailableVersions() );
+ if ( releaseVersion == null ||
+ comparator.compare( proxyMetadata.getReleasedVersion(), releaseVersion ) > 0 )
+ {
+ releaseVersion = proxyMetadata.getReleasedVersion();
+ }
+ }
}
}
// Add the versions to the metadata model.
metadata.setAvailableVersions( sortedVersions );
+ metadata.setLatestVersion( latestVersion );
+ metadata.setReleasedVersion( releaseVersion );
+
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
}
assertUpdatedProjectMetadata( "proxied_multi", new String[]{"1.0-spec" /* in java.net */, "1.0"
/* in managed, and central */, "1.0.1" /* in central */, "1.1" /* in managed */, "2.0-proposal-beta"
/* in java.net */, "2.0-spec" /* in java.net */, "2.0" /* in central, and java.net */, "2.0.1"
- /* in java.net */, "2.1" /* in managed */, "3.0" /* in central */, "3.1" /* in central */} );
+ /* in java.net */, "2.1" /* in managed */, "3.0" /* in central */, "3.1" /* in central */}, "3.1", "3.1" );
}
public void testUpdateProjectSimpleYetIncomplete()
private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
+ {
+ assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
+ }
+
+ private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
+ String releaseVersion )
+ throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
{
ArchivaRepository testRepo = createTestRepo();
ProjectReference reference = new ProjectReference();
if ( expectedVersions != null )
{
buf.append( " <versioning>\n" );
+ if ( latestVersion != null )
+ {
+ buf.append( " <latest>" ).append( latestVersion ).append( "</latest>\n" );
+ }
+ if ( releaseVersion != null )
+ {
+ buf.append( " <release>" ).append( releaseVersion ).append( "</release>\n" );
+ }
+
buf.append( " <versions>\n" );
for ( int i = 0; i < expectedVersions.length; i++ )
{