[ 
https://issues.apache.org/jira/browse/SLING-4656?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14510554#comment-14510554
 ] 

Carsten Ziegeler commented on SLING-4656:
-----------------------------------------

I think we should fix compareTo - I think it's a copy of the 
ServiceReference.compareTo method

Adding these tests shows the problem
{noformat} 
    @Test public void testOrderingWithoutRanking() {
        final Map<String, Object> props1 = new HashMap<String, Object>();
        props1.put(Constants.SERVICE_ID, 1L);
        final ProviderHandler ph1 = new MyProviderHandler(props1);

        assertEquals(0, ph1.compareTo(ph1));

        final Map<String, Object> props2 = new HashMap<String, Object>();
        props2.put(Constants.SERVICE_ID, 2L);
        final ProviderHandler ph2 = new MyProviderHandler(props2);

        assertEquals(-1, ph1.compareTo(ph2));
        assertEquals(1, ph2.compareTo(ph1));
    }

    @Test public void testOrderingWithRanking() {
        final Map<String, Object> props1 = new HashMap<String, Object>();
        props1.put(Constants.SERVICE_ID, 1L);
        props1.put(Constants.SERVICE_RANKING, 50);
        final ProviderHandler ph1 = new MyProviderHandler(props1);

        final Map<String, Object> props2 = new HashMap<String, Object>();
        props2.put(Constants.SERVICE_ID, 2L);
        props2.put(Constants.SERVICE_RANKING, 150);
        final ProviderHandler ph2 = new MyProviderHandler(props2);

        assertEquals(1, ph1.compareTo(ph2));
        assertEquals(-1, ph2.compareTo(ph1));
    }
{noformat}

> ProviderHandler implements compareTo incorreclty
> ------------------------------------------------
>
>                 Key: SLING-4656
>                 URL: https://issues.apache.org/jira/browse/SLING-4656
>             Project: Sling
>          Issue Type: Bug
>          Components: ResourceResolver
>    Affects Versions: Resource Resolver 1.2.4
>            Reporter: Felix Meschberger
>             Fix For: Resource Resolver 1.2.6
>
>
> The implementation of the ResourceResolver's compareTo method is wrong as it 
> favors services registered later over services registered earlier, while 
> older services should actually be preferred (assuming there sevice.ranking 
> values are equal). In essence it implements natural ServiceReference ordering 
> instead of ranking order which is the inverse.
> Two options to fix:
> * Fix ProviderHandler.compareTo to implement service ranking and thus revers 
> of natural ordering
> * Fix ResourceProviderEntry.conditionalSort to use a reverse comparator
> While at it, the extraction of the service.ranking property is overly complex 
> since it first checks for null and then for the correct type. This can be 
> simplified by just checking for the correct type as a null value never 
> matches any type.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to