Author: bdelacretaz Date: Thu Jan 7 15:29:28 2010 New Revision: 896903 URL: http://svn.apache.org/viewvc?rev=896903&view=rev Log: SLING-1278 - make BundleVersionInfo abstract and Comparable
Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java - copied, changed from r896880, sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java Removed: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparator.java sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java?rev=896903&r1=896902&r2=896903&view=diff ============================================================================== --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java (original) +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleBundleVersionInfo.java Thu Jan 7 15:29:28 2010 @@ -23,7 +23,7 @@ import org.osgi.framework.Version; /** BundleVersionInfo based on a Bundle object */ -public class BundleBundleVersionInfo implements BundleVersionInfo<Bundle> { +public class BundleBundleVersionInfo extends BundleVersionInfo<Bundle> { private Bundle source; private final long lastModified; Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java?rev=896903&r1=896902&r2=896903&view=diff ============================================================================== --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java (original) +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionInfo.java Thu Jan 7 15:29:28 2010 @@ -23,36 +23,98 @@ /** Provides bundle version information, which can be * extracted from bundle files or Bundle objects. */ -public interface BundleVersionInfo<T> { +public abstract class BundleVersionInfo<T> implements Comparable<BundleVersionInfo<?>> { + + private static final int A_GREATER = 1; + private static final int B_GREATER = -1; + private static final int EQUAL = 0; + /** Marker used by Maven to identify snapshots */ - String SNAPSHOT_MARKER = "SNAPSHOT"; + public static final String SNAPSHOT_MARKER = "SNAPSHOT"; /** Name of the BND attribute that provides the bundle's last modified timestamp */ - String BND_LAST_MODIFIED = "Bnd-LastModified"; + public static final String BND_LAST_MODIFIED = "Bnd-LastModified"; /** Value for {...@link #getBundleLastModified} if corresponding header * is not present */ - long BND_LAST_MODIFIED_MISSING = -1L; + public static final long BND_LAST_MODIFIED_MISSING = -1L; /** Return the source of information: underlying File or Bundle */ - T getSource(); + public abstract T getSource(); /** True if the provided data is a valid bundle */ - boolean isBundle(); + public abstract boolean isBundle(); /** Return the bundle symbolic name, null if not available */ - String getBundleSymbolicName(); + public abstract String getBundleSymbolicName(); /** Return the bundle version, null if not available */ - Version getVersion(); + public abstract Version getVersion(); /** True if the bundle version indicates a snapshot */ - boolean isSnapshot(); + public abstract boolean isSnapshot(); /** Return the bundle last modification time, based on the BND_LAST_MODIFIED * manifest header, if available. This is *not* the Bundle.getLastModified() * value, which refers to actions in the OSGi framework. * @return BND_LAST_MODIFIED_MISSING if header not supplied */ - long getBundleLastModified(); + public abstract long getBundleLastModified(); + + + /** Compare based on bundle version info, and for snapshots + * based on {...@link #getBundleLastModified} + */ + public int compareTo(BundleVersionInfo<?> other) { + // Handle null values + if(other == null) { + throw new IllegalArgumentException("b is null, cannot compare"); + } + + // Handle non-bundles: we don't want them! + if(!isBundle()) { + throw new IllegalArgumentException("Not a bundle, cannot compare: " + this); + } + if(!other.isBundle()) { + throw new IllegalArgumentException("Not a bundle, cannot compare:" + other); + } + + // First compare symbolic names + int result = getBundleSymbolicName().compareTo(other.getBundleSymbolicName()); + + // Then compare versions + if(result == EQUAL) { + final Version va = getVersion(); + final Version vb = other.getVersion(); + if(va == null && vb == null) { + // result = EQUAL + } else if(vb == null) { + result = A_GREATER; + } else if(va == null) { + result = B_GREATER; + } else { + result = va.compareTo(vb); + } + + // more recent ones must come before older ones + result = -result; + } + + // Then, if snapshots, compare modification times, more recent comes first + if(result == EQUAL && isSnapshot()) { + final long ma = getBundleLastModified(); + final long mb = other.getBundleLastModified(); + if(ma > mb) { + result = A_GREATER; + } else if(mb > ma) { + result = B_GREATER; + } + + // more recent ones must come before older ones + result = -result; + } + + return result; + } + } Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java?rev=896903&r1=896902&r2=896903&view=diff ============================================================================== --- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java (original) +++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/bundleversion/FileBundleVersionInfo.java Thu Jan 7 15:29:28 2010 @@ -27,7 +27,7 @@ import org.osgi.framework.Version; /** BundleVersionInfo based on a bundle jar file */ -public class FileBundleVersionInfo implements BundleVersionInfo<File> { +public class FileBundleVersionInfo extends BundleVersionInfo<File> { private final String symbolicName; private final Version version; Copied: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java (from r896880, sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java) URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java?p2=sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java&p1=sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java&r1=896880&r2=896903&rev=896903&view=diff ============================================================================== --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparatorTest.java (original) +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/BundleVersionComparisonTest.java Thu Jan 7 15:29:28 2010 @@ -28,7 +28,7 @@ import org.junit.Test; -public class BundleVersionComparatorTest { +public class BundleVersionComparisonTest { @Test public void testSortBundles() { @@ -52,7 +52,7 @@ } final String firstBeforeSort = list.get(0).toString(); - Collections.sort(list, new BundleVersionComparator()); + Collections.sort(list); final String newFirstItem = list.get(0).toString(); assertFalse("First item (" + newFirstItem + ") must have changed during sort", firstBeforeSort.equals(newFirstItem)); @@ -67,28 +67,14 @@ public void testEqual() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); final MockBundleVersionInfo b = new MockBundleVersionInfo("a", "1.0", 1); - final BundleVersionComparator c = new BundleVersionComparator(); - assertEquals("Last-modified must not be relevant for non-snapshot bundles", 0, c.compare(a, b)); + assertEquals("Last-modified must not be relevant for non-snapshot bundles", 0, a.compareTo(b)); } public void testExceptionsOnNull() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); - final BundleVersionComparator c = new BundleVersionComparator(); try { - c.compare(a, null); - fail("Expected an IllegalArgumentException"); - } catch(IllegalArgumentException asExpected) { - } - - try { - c.compare(null, a); - fail("Expected an IllegalArgumentException"); - } catch(IllegalArgumentException asExpected) { - } - - try { - c.compare(null, null); + a.compareTo(null); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } @@ -97,16 +83,15 @@ public void testExceptionOnNonBundle() { final MockBundleVersionInfo a = new MockBundleVersionInfo("a", "1.0", 2); final MockBundleVersionInfo nonBundle = new MockBundleVersionInfo(); - final BundleVersionComparator c = new BundleVersionComparator(); try { - c.compare(a, nonBundle); + a.compareTo(nonBundle); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } try { - c.compare(nonBundle, a); + nonBundle.compareTo(a); fail("Expected an IllegalArgumentException"); } catch(IllegalArgumentException asExpected) { } Modified: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java?rev=896903&r1=896902&r2=896903&view=diff ============================================================================== --- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java (original) +++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/bundleversion/MockBundleVersionInfo.java Thu Jan 7 15:29:28 2010 @@ -20,7 +20,7 @@ import org.osgi.framework.Version; -class MockBundleVersionInfo implements BundleVersionInfo<String> { +class MockBundleVersionInfo extends BundleVersionInfo<String> { private final String source; private final String symbolicName;