You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Shurmer, Jordan" <js...@scrippsnetworks.com> on 2016/08/18 20:06:54 UTC

Sling Model default Implementation Picker

Hello Sling Devs,

I've got a question about the sling models [FirstImplementationPicker][1]. This appears to essentially be a "default" implementation picker. It will be used in two situation: (1) my app doesn't provide any other ImplementationPickers; (2) all my app's ImplementationPickers returned null, declaring that they couldn't find an implementation.

The Second situation has caused me a bit of pain today, and I'm wondering FirstImplementationPicker was intended to act in that situation. Here's my thinking:

I have an interface which has multiple implementations. The correct implementation will be chosen based on a property of the Resource being adapted. If there is no Implementation found for one of the possible values of that property, I would like the resource to *not* be adapted (i.e. the adaptTo() returning null). So, I wrote a custom ImplementationPicker and returned null when there is no matching implementation, but then saw that one of my Impl's was chosen seemingly arbitrarily. 

I wonder if a "better" default implementation picker would return null if it is given multiple implementationTypes since it can't determine how one would want to choose one over the other, and only return the implementationType if there is only one to choose from.

WDYT? Do I just have bad expectations of this Interface?
 
[1]: https://github.com/apache/sling/blob/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/FirstImplementationPicker.java


Jordan Shurmer | Software Engineer | Scripps Lifestyle Studios

9721 Sherrill Blvd, Knoxville TN 37932
Office: 865-560-4887
jshurmer@scrippsnetworks.com

SCRIPPS NETWORKS INTERACTIVE | the Leader in Lifestyle Media | scrippsnetworksinteractive.com
HGTV | Food Network | Travel Channel | DIY Network | Cooking Channel | Great American Country | TVN | Fine Living | Asian Food Channel


RE: Sling Model default Implementation Picker

Posted by Stefan Seifert <ss...@pro-vision.de>.
>It would be awesome to see details about this default
>FirstImplementationPicker added to the Sling Models [documentation][1]. I'm
>not familiar with where that project lives, but if someone points me in the
>right direction I'd be glad to submit an update to the doc as well.
>
>[1]: https://sling.apache.org/documentation/bundles/models.html

the source of this doc is here:
https://svn.apache.org/repos/asf/sling/site/trunk/content/documentation/bundles/models.mdtext

if you can put together a patch file for this we can apply it easily.

stefan


RE: Sling Model default Implementation Picker

Posted by "Shurmer, Jordan" <js...@scrippsnetworks.com>.
Hi Stefan,

That makes sense. I was just surprised that Sling ended up picking the implementation for me. I'll try to put together something to make it configurable :)

It would be awesome to see details about this default FirstImplementationPicker added to the Sling Models [documentation][1]. I'm not familiar with where that project lives, but if someone points me in the right direction I'd be glad to submit an update to the doc as well.

[1]: https://sling.apache.org/documentation/bundles/models.html

Thanks,
Jordan Shurmer

-----Original Message-----
From: Stefan Seifert [mailto:sseifert@pro-vision.de] 
Sent: Thursday, August 18, 2016 5:30 PM
To: dev@sling.apache.org
Subject: RE: Sling Model default Implementation Picker

hello jordan.

you're right - the interface contract is defined here:
https://github.com/apache/sling/blob/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ImplementationPicker.java

returning null always means "i cannot pick an implementation", but not "pick no other implementation and return null for the adaption". so the default first implementation picker always steps in as last resort.

if you really need this behavior and cannot find another solution for your usecase please create an improvement ticket. we cannot change the existing behavior, but we could make it configurable (e.g. a switch to disable the first implementation picker by configuration), or provide other means to further control the implementation picking behavior.

stefan


>-----Original Message-----
>From: Shurmer, Jordan [mailto:jshurmer@scrippsnetworks.com]
>Sent: Thursday, August 18, 2016 10:07 PM
>To: dev@sling.apache.org
>Subject: Sling Model default Implementation Picker
>
>Hello Sling Devs,
>
>I've got a question about the sling models [FirstImplementationPicker][1].
>This appears to essentially be a "default" implementation picker. It 
>will be used in two situation: (1) my app doesn't provide any other 
>ImplementationPickers; (2) all my app's ImplementationPickers returned 
>null, declaring that they couldn't find an implementation.
>
>The Second situation has caused me a bit of pain today, and I'm 
>wondering FirstImplementationPicker was intended to act in that 
>situation. Here's my
>thinking:
>
>I have an interface which has multiple implementations. The correct 
>implementation will be chosen based on a property of the Resource being 
>adapted. If there is no Implementation found for one of the possible 
>values of that property, I would like the resource to *not* be adapted 
>(i.e. the
>adaptTo() returning null). So, I wrote a custom ImplementationPicker 
>and returned null when there is no matching implementation, but then 
>saw that one of my Impl's was chosen seemingly arbitrarily.
>
>I wonder if a "better" default implementation picker would return null 
>if it is given multiple implementationTypes since it can't determine 
>how one would want to choose one over the other, and only return the 
>implementationType if there is only one to choose from.
>
>WDYT? Do I just have bad expectations of this Interface?
>
>[1]:
>https://github.com/apache/sling/blob/trunk/bundles/extensions/models/im
>pl/s 
>rc/main/java/org/apache/sling/models/impl/FirstImplementationPicker.jav
>a
>
>
>Jordan Shurmer | Software Engineer | Scripps Lifestyle Studios
>
>9721 Sherrill Blvd, Knoxville TN 37932
>Office: 865-560-4887
>jshurmer@scrippsnetworks.com
>
>SCRIPPS NETWORKS INTERACTIVE | the Leader in Lifestyle Media | 
>scrippsnetworksinteractive.com HGTV | Food Network | Travel Channel | 
>DIY Network | Cooking Channel | Great American Country | TVN | Fine 
>Living | Asian Food Channel


RE: Sling Model default Implementation Picker

Posted by Stefan Seifert <ss...@pro-vision.de>.
hello jordan.

you're right - the interface contract is defined here:
https://github.com/apache/sling/blob/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/spi/ImplementationPicker.java

returning null always means "i cannot pick an implementation", but not "pick no other implementation and return null for the adaption". so the default first implementation picker always steps in as last resort.

if you really need this behavior and cannot find another solution for your usecase please create an improvement ticket. we cannot change the existing behavior, but we could make it configurable (e.g. a switch to disable the first implementation picker by configuration), or provide other means to further control the implementation picking behavior.

stefan


>-----Original Message-----
>From: Shurmer, Jordan [mailto:jshurmer@scrippsnetworks.com]
>Sent: Thursday, August 18, 2016 10:07 PM
>To: dev@sling.apache.org
>Subject: Sling Model default Implementation Picker
>
>Hello Sling Devs,
>
>I've got a question about the sling models [FirstImplementationPicker][1].
>This appears to essentially be a "default" implementation picker. It will
>be used in two situation: (1) my app doesn't provide any other
>ImplementationPickers; (2) all my app's ImplementationPickers returned
>null, declaring that they couldn't find an implementation.
>
>The Second situation has caused me a bit of pain today, and I'm wondering
>FirstImplementationPicker was intended to act in that situation. Here's my
>thinking:
>
>I have an interface which has multiple implementations. The correct
>implementation will be chosen based on a property of the Resource being
>adapted. If there is no Implementation found for one of the possible values
>of that property, I would like the resource to *not* be adapted (i.e. the
>adaptTo() returning null). So, I wrote a custom ImplementationPicker and
>returned null when there is no matching implementation, but then saw that
>one of my Impl's was chosen seemingly arbitrarily.
>
>I wonder if a "better" default implementation picker would return null if
>it is given multiple implementationTypes since it can't determine how one
>would want to choose one over the other, and only return the
>implementationType if there is only one to choose from.
>
>WDYT? Do I just have bad expectations of this Interface?
>
>[1]:
>https://github.com/apache/sling/blob/trunk/bundles/extensions/models/impl/s
>rc/main/java/org/apache/sling/models/impl/FirstImplementationPicker.java
>
>
>Jordan Shurmer | Software Engineer | Scripps Lifestyle Studios
>
>9721 Sherrill Blvd, Knoxville TN 37932
>Office: 865-560-4887
>jshurmer@scrippsnetworks.com
>
>SCRIPPS NETWORKS INTERACTIVE | the Leader in Lifestyle Media |
>scrippsnetworksinteractive.com
>HGTV | Food Network | Travel Channel | DIY Network | Cooking Channel |
>Great American Country | TVN | Fine Living | Asian Food Channel