You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Stefan Seifert (JIRA)" <ji...@apache.org> on 2014/07/01 15:03:24 UTC

[jira] [Updated] (SLING-3718) Sling Models: Add self Injector for supporting chains of object injecting dependencies from a common source

     [ https://issues.apache.org/jira/browse/SLING-3718?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Stefan Seifert updated SLING-3718:
----------------------------------

    Attachment: 140701_SLING-3716_slingmodes_constructorinjection_SLING-3718_selfinjector.patch

attached is a combined patch for SLING-3716 and SLING-3718 (replacing the patch already attached to SLING-3716).

remarks:
* your comments are correct - adapting inside the injector is wrong. as recommended i introduced a @Self annotation (which was missing in the patch from SLING-3716) and tweaked the implementation of the SelfInjector a bit. not it works as expected re-using the adaption logic from ModelAdapterFactory.
* i added a component unit test and a counteraction for cyclic dependencies based on a threadlocal - please review
* the max. number of recursive invocations is currently hardcoded - perhaps it makes sense to expose it as OSGi property to make it configurable

i'm currenty not fully happy with the "@Self" annotation name. although its consistent with the other injectors and annotations the code reads not so intuitive if not the adaptable itself, but an object that can be adapted from the adaptable is injected:
{code:java}
@Model(adaptables=SlingHttpServletRequest.class)
public class MyController {
  @Self
  private MyBusiness myBusiness;
}
@Model(adaptables=SlingHttpServletRequest.class)
public class MyBusiness {
}
{code}
but perhaps one gets accustomed soon to this.

p.s. in the meantime a lot of inner classes have accumulated inside the ModelAdapterFactory class. perhaps it makes sense to extract them to separate files to make the class a bit more readable again.

> Sling Models: Add self Injector for supporting chains of object injecting dependencies from a common source
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: SLING-3718
>                 URL: https://issues.apache.org/jira/browse/SLING-3718
>             Project: Sling
>          Issue Type: Improvement
>          Components: Extensions
>            Reporter: Stefan Seifert
>            Priority: Minor
>         Attachments: 140701_SLING-3716_slingmodes_constructorinjection_SLING-3718_selfinjector.patch
>
>
> as discussed in my post on the sling mailing list
> http://mail-archives.apache.org/mod_mbox/sling-dev/201406.mbox/%3C580AFA6C6DAB8A4196956DA5EE17C8CA8F7619E558@feanor.pro-vision.de%3E
> it would be very helpful to have a "self-adapting" injector to support chains of objects (e.g. controller classes depending on business classes) that all can adapt themselves from a common source, e.g. a SlingHttpServletRequest or a ResourceResolver.
> souch an injector can be simple like this:
> {code:java}
> @Component
> @Service
> @Property(name = Constants.SERVICE_RANKING, intValue = 50)
> public class SelfAdaptInjector implements Injector {
>   public String getName() {
>     return "selfadapt";
>   }
>   public Object getValue(Object pAdaptable, String pName, Type pType, AnnotatedElement pElement,
> DisposalCallbackRegistry pCallbackRegistry) {
>     if (!(pType instanceof Class)) {
>       return null;
>     }
>     if (!(pAdaptable instanceof Adaptable)) {
>       return null;
>     }
>     return ((Adaptable)pAdaptable).adaptTo((Class<?>)pType);
>   }
> }
> {code}
> comment from Justin on this in the mailing list:
> {quote}
> The self injector is interesting. I held off on that initially because
> it seems too easy to create a circular injection. Any thoughts on how
> that can be avoided?
> \[...\]
> Regards,
> Justin
> {quote}



--
This message was sent by Atlassian JIRA
(v6.2#6252)